diff --git a/stock_inventory_pos_category/README.rst b/stock_inventory_pos_category/README.rst new file mode 100644 index 000000000000..bcbfbc90fa5f --- /dev/null +++ b/stock_inventory_pos_category/README.rst @@ -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 +`_. 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 + +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. diff --git a/stock_inventory_pos_category/__init__.py b/stock_inventory_pos_category/__init__.py new file mode 100644 index 000000000000..cde864bae21a --- /dev/null +++ b/stock_inventory_pos_category/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import models diff --git a/stock_inventory_pos_category/__manifest__.py b/stock_inventory_pos_category/__manifest__.py new file mode 100644 index 000000000000..c54dac1624e3 --- /dev/null +++ b/stock_inventory_pos_category/__manifest__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Akretion (Alexis de Lattre ) +# 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, +} diff --git a/stock_inventory_pos_category/models/__init__.py b/stock_inventory_pos_category/models/__init__.py new file mode 100644 index 000000000000..9525632ba4e7 --- /dev/null +++ b/stock_inventory_pos_category/models/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import stock_inventory diff --git a/stock_inventory_pos_category/models/stock_inventory.py b/stock_inventory_pos_category/models/stock_inventory.py new file mode 100644 index 000000000000..123f33bdc8e9 --- /dev/null +++ b/stock_inventory_pos_category/models/stock_inventory.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Akretion (Alexis de Lattre ) +# 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 diff --git a/stock_inventory_pos_category/views/stock_inventory.xml b/stock_inventory_pos_category/views/stock_inventory.xml new file mode 100644 index 000000000000..f736b3fcf223 --- /dev/null +++ b/stock_inventory_pos_category/views/stock_inventory.xml @@ -0,0 +1,24 @@ + + + + + + + + select POS categories on inventory form + stock.inventory + + + + + + + + + +