Skip to content

Commit

Permalink
Add module stock_inventory_pos_category
Browse files Browse the repository at this point in the history
  • Loading branch information
alexis-via committed Mar 16, 2018
1 parent 6565cc7 commit e7084f5
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 0 deletions.
51 changes: 51 additions & 0 deletions stock_inventory_pos_category/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

============================
Stock Inventory POS category
============================

When you create an inventory, you can choose to select a particular product category. With this module, you can also choose to select one or more point of sale categories.

Usage
=====

Go to the menu *Inventory > Inventory Control > Inventory Adjustments*, create a new inventory and select *Point of Sale Categories* in the list, then choose the point of sale categories that you want to inventory.

Note that, if you select a parent point of sale category, Odoo will get the products attached to the children categories.

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/153/10.0

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/stock-logistics-warehouse/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.

Credits
=======

Contributors
------------

* Alexis de Lattre <[email protected]>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
3 changes: 3 additions & 0 deletions stock_inventory_pos_category/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from . import models
18 changes: 18 additions & 0 deletions stock_inventory_pos_category/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Akretion (Alexis de Lattre <[email protected]>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'Stock Inventory POS Category',
'version': '10.0.1.0.0',
'category': 'Inventory, Logistics, Warehousing',
'license': 'AGPL-3',
'summary': 'Create an inventory for one or several POS categories',
'author': 'Akretion,Odoo Community Association (OCA)',
'website': 'http://www.akretion.com',
'depends': ['stock', 'point_of_sale'],
'data': [
'views/stock_inventory.xml',
],
'installable': True,
}
3 changes: 3 additions & 0 deletions stock_inventory_pos_category/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from . import stock_inventory
81 changes: 81 additions & 0 deletions stock_inventory_pos_category/models/stock_inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Akretion (Alexis de Lattre <[email protected]>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class StockInventory(models.Model):
_inherit = 'stock.inventory'

pos_categ_ids = fields.Many2many(
'pos.category', string='Point of Sale Categories',
readonly=True, states={'draft': [('readonly', False)]},
help="Specify one or several point of sale categories to focus "
"your inventory on specific point of sale categories")

@api.model
def _selection_filter(self):
res_filter = super(StockInventory, self)._selection_filter()
res_filter.append(('pos_categories', _('Point of Sale Categories')))
return res_filter

@api.onchange('filter')
def onchange_filter(self):
super(StockInventory, self).onchange_filter()
if self.filter != 'pos_categories':
self.pos_categ_ids = False

@api.constrains('filter', 'pos_categ_ids')
def _check_filter_pos_categories(self):
for inv in self:
if inv.filter != 'pos_categories' and inv.pos_categ_ids:
raise ValidationError(_(
'The selected inventory options are not coherent.'))

@api.multi
def _get_inventory_lines_values(self):
vals = super(StockInventory, self)._get_inventory_lines_values()
if self.pos_categ_ids:
locations = self.env['stock.location'].search([
('id', 'child_of', [self.location_id.id])])
vals = []
Product = self.env['product.product']
quant_products = self.env['product.product']
products_to_filter = self.env['product.product']
pos_categ_products = Product.search([
('pos_categ_id', 'child_of', self.pos_categ_ids.ids)])
products_to_filter |= pos_categ_products

self.env.cr.execute("""
SELECT product_id, sum(qty) as product_qty, location_id,
lot_id as prod_lot_id, package_id, owner_id as partner_id
FROM stock_quant
WHERE location_id in %s AND company_id = %s AND
product_id = ANY (%s)
GROUP BY product_id, location_id, lot_id,
package_id, partner_id
""", (tuple(locations.ids), self.company_id.id,
pos_categ_products.ids))
# copy-pasted from odoo/addons/stock/models/stock_inventory.py
# So it is copyright Odoo S.A.
for product_data in self.env.cr.dictfetchall():
# replace the None the dictionary by False,
# because falsy values are tested later on
for void_field in [
item[0] for item in product_data.items()
if item[1] is None]:
product_data[void_field] = False
product_data['theoretical_qty'] = product_data['product_qty']
if product_data['product_id']:
product_data['product_uom_id'] = Product.browse(
product_data['product_id']).uom_id.id
quant_products |= Product.browse(
product_data['product_id'])
vals.append(product_data)
if self.exhausted:
exhausted_vals = self._get_exhausted_inventory_line(
products_to_filter, quant_products)
vals.extend(exhausted_vals)
return vals
24 changes: 24 additions & 0 deletions stock_inventory_pos_category/views/stock_inventory.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2018 Akretion (Alexis de Lattre <[email protected]>)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->

<odoo>


<record id="view_inventory_form" model="ir.ui.view">
<field name="name">select POS categories on inventory form</field>
<field name="model">stock.inventory</field>
<field name="inherit_id" ref="stock.view_inventory_form" />
<field name="arch" type="xml">
<field name="category_id" position="after">
<field name="pos_categ_ids"
widget="many2many_tags" options="{'no_create': True}"
attrs="{'invisible': [('filter', '!=', 'pos_categories')], 'required': [('filter', '=', 'pos_categories')]}" />
</field>
</field>
</record>


</odoo>

0 comments on commit e7084f5

Please sign in to comment.