diff --git a/stock_inventory_exclude_sublocation/README.rst b/stock_inventory_exclude_sublocation/README.rst index 7d1865718..38d75ff58 100644 --- a/stock_inventory_exclude_sublocation/README.rst +++ b/stock_inventory_exclude_sublocation/README.rst @@ -28,7 +28,7 @@ To use this module, you simply need to: .. 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/9.0 + :target: https://runbot.odoo-community.org/runbot/153/10.0 Bug Tracker diff --git a/stock_inventory_exclude_sublocation/__openerp__.py b/stock_inventory_exclude_sublocation/__manifest__.py similarity index 96% rename from stock_inventory_exclude_sublocation/__openerp__.py rename to stock_inventory_exclude_sublocation/__manifest__.py index 3774ab76d..1d7ee88d3 100644 --- a/stock_inventory_exclude_sublocation/__openerp__.py +++ b/stock_inventory_exclude_sublocation/__manifest__.py @@ -6,7 +6,7 @@ "name": "Stock Inventory Exclude Sublocation", "summary": "Allow to perform inventories of a location without including " "its child locations.", - "version": "9.0.1.0.1", + "version": "10.0.1.0.0", "author": "Eficent," "Odoo Community Association (OCA)", "website": "https://github.com/OCA/stock-logistics-warehouse", diff --git a/stock_inventory_exclude_sublocation/models/stock_inventory.py b/stock_inventory_exclude_sublocation/models/stock_inventory.py index 99e15b2e4..fdc647ad6 100644 --- a/stock_inventory_exclude_sublocation/models/stock_inventory.py +++ b/stock_inventory_exclude_sublocation/models/stock_inventory.py @@ -3,10 +3,10 @@ # (http://www.eficent.com) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from openerp import api, fields, models +from odoo import api, fields, models -class StockInventory(models.Model): +class Inventory(models.Model): _inherit = 'stock.inventory' exclude_sublocation = fields.Boolean( @@ -14,45 +14,65 @@ class StockInventory(models.Model): track_visibility='onchange', readonly=True, states={'draft': [('readonly', False)]}) - @api.model - def _get_inventory_lines(self, inventory): - if inventory.exclude_sublocation: - product_obj = self.env['product.product'] + @api.multi + def _get_inventory_lines_values(self): + if self.exclude_sublocation: domain = ' location_id = %s' - args = (tuple(inventory.location_id.ids),) - if inventory.company_id.id: - domain += ' and company_id = %s' - args += (inventory.company_id.id,) - if inventory.partner_id: - domain += ' and owner_id = %s' - args += (inventory.partner_id.id,) - if inventory.lot_id: - domain += ' and lot_id = %s' - args += (inventory.lot_id.id,) - if inventory.product_id: - domain += ' and product_id = %s' - args += (inventory.product_id.id,) - if inventory.package_id: - domain += ' and package_id = %s' - args += (inventory.package_id.id,) + args = (tuple(self.location_id.ids),) - 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''' + domain + ''' - GROUP BY product_id, location_id, lot_id, package_id, partner_id - ''', args) vals = [] - for product_line in self.env.cr.dictfetchall(): - for key, value in product_line.items(): - if not value: - product_line[key] = False - product_line['inventory_id'] = inventory.id - product_line['theoretical_qty'] = product_line['product_qty'] - if product_line['product_id']: - product = product_obj.browse(product_line['product_id']) - product_line['product_uom_id'] = product.uom_id.id - vals.append(product_line) + product_obj = self.env['product.product'] + # Empty recordset of products available in stock_quants + quant_products = self.env['product.product'] + # Empty recordset of products to filter + products_to_filter = self.env['product.product'] + + if self.company_id.id: + domain += ' and company_id = %s' + args += (self.company_id.id,) + if self.partner_id: + domain += ' and owner_id = %s' + args += (self.partner_id.id,) + if self.lot_id: + domain += ' and lot_id = %s' + args += (self.lot_id.id,) + if self.product_id: + domain += ' and product_id = %s' + args += (self.product_id.id,) + products_to_filter |= self.product_id + if self.package_id: + domain += ' and package_id = %s' + args += (self.package_id.id,) + if self.category_id: + categ_products = product_obj.search( + [('categ_id', '=', self.category_id.id)]) + domain += ' AND product_id = ANY (%s)' + args += (categ_products.ids,) + products_to_filter |= 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 %s + GROUP BY product_id, location_id, lot_id, package_id, + partner_id """ % domain, args) + + for product_data in self.env.cr.dictfetchall(): + 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_obj.browse( + product_data['product_id']).uom_id.id + quant_products |= product_obj.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 else: - return super(StockInventory, self)._get_inventory_lines(inventory) + return super(Inventory, self)._get_inventory_lines_values() diff --git a/stock_inventory_exclude_sublocation/tests/test_exclude_sublocation.py b/stock_inventory_exclude_sublocation/tests/test_exclude_sublocation.py index bc9bbfc60..0fe5afbc2 100644 --- a/stock_inventory_exclude_sublocation/tests/test_exclude_sublocation.py +++ b/stock_inventory_exclude_sublocation/tests/test_exclude_sublocation.py @@ -3,7 +3,7 @@ # (http://www.eficent.com) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from openerp.tests.common import TransactionCase +from odoo.tests.common import TransactionCase class TestStockInventoryExcludeSublocation(TransactionCase):