diff --git a/stock_inventory_lockdown/README.rst b/stock_inventory_lockdown/README.rst index a8dd6b5cd..eb9dc5bfe 100644 --- a/stock_inventory_lockdown/README.rst +++ b/stock_inventory_lockdown/README.rst @@ -10,20 +10,17 @@ This module lets you lock down the locations during an inventory. Usage ===== -.. image:: images/location_locked.png - :alt: Error message - .. image:: images/move_error.png :alt: Error message While an inventory is in the state "In progress", no stock moves can be recorded in/out of the inventory's location: users will get an error message. -Creating or modifying a locations is also forbidden. +Creating or modifying locations is also forbidden. .. 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/8.0 + :target: https://runbot.odoo-community.org/runbot/153/9.0 Bug Tracker =========== @@ -31,11 +28,7 @@ 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 -`_. +help us smashing it by providing a detailed and welcomed feedback. Credits ======= diff --git a/stock_inventory_lockdown/__openerp__.py b/stock_inventory_lockdown/__openerp__.py index 09bca4a4b..abccecde3 100644 --- a/stock_inventory_lockdown/__openerp__.py +++ b/stock_inventory_lockdown/__openerp__.py @@ -3,11 +3,11 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { - "name": "Inventory lock down", + "name": "Inventory Lock Down", "summary": "Lock down stock locations during inventories.", - "version": "8.0.1.0.0", + "version": "9.0.1.0.0", "depends": ["stock"], - "author": u"Numérigraphe,Odoo Community Association (OCA)", + "author": "Numérigraphe,Odoo Community Association (OCA)", "category": "Warehouse Management", "images": [ "images/move_error.png", diff --git a/stock_inventory_lockdown/images/location_locked.png b/stock_inventory_lockdown/images/location_locked.png deleted file mode 100644 index fd4448561..000000000 Binary files a/stock_inventory_lockdown/images/location_locked.png and /dev/null differ diff --git a/stock_inventory_lockdown/images/move_error.png b/stock_inventory_lockdown/images/move_error.png index 9d420f9b2..813c94ef2 100644 Binary files a/stock_inventory_lockdown/images/move_error.png and b/stock_inventory_lockdown/images/move_error.png differ diff --git a/stock_inventory_lockdown/models/stock_inventory.py b/stock_inventory_lockdown/models/stock_inventory.py index 4886be466..71c001f6c 100644 --- a/stock_inventory_lockdown/models/stock_inventory.py +++ b/stock_inventory_lockdown/models/stock_inventory.py @@ -9,18 +9,24 @@ class StockInventory(models.Model): _inherit = 'stock.inventory' @api.model - def _get_locations_open_inventories(self): - """IDs of location in open exhaustive inventories, with children""" - inventories = self.search([('state', '=', 'confirm')]) + def _get_locations_open_inventories(self, locations_ids=None): + """IDs of locations in open exhaustive inventories, with children""" + inventory_domain = [('state', '=', 'confirm')] + if locations_ids: + inventory_domain.append(('location_id', 'child_of', locations_ids)) + inventories = self.search(inventory_domain) if not inventories: # Early exit if no match found return [] location_ids = inventories.mapped('location_id') # Extend to the children Locations - return self.env['stock.location'].search( - [('location_id', 'child_of', location_ids.ids), - ('usage', 'in', ['internal', 'transit'])]) + location_domain = [ + ('location_id', 'child_of', location_ids.ids), + ('usage', 'in', ['internal', 'transit'])] + if locations_ids: + location_domain.append(('location_id', 'child_of', locations_ids)) + return self.env['stock.location'].search(location_domain) @api.multi def action_done(self): diff --git a/stock_inventory_lockdown/models/stock_location.py b/stock_inventory_lockdown/models/stock_location.py index a15b8210f..574ac2866 100644 --- a/stock_inventory_lockdown/models/stock_location.py +++ b/stock_inventory_lockdown/models/stock_location.py @@ -14,13 +14,12 @@ class StockLocation(models.Model): @api.multi def _check_inventory(self): """Error if an inventory is being conducted here""" - location_inventory_open_ids = self.env['stock.inventory'].sudo( - )._get_locations_open_inventories() - for location in self: - if location in location_inventory_open_ids: - raise ValidationError( - _('An inventory is being conducted at this ' - 'location')) + location_inventory_open_ids = self.env['stock.inventory'].sudo().\ + _get_locations_open_inventories(self.ids) + if location_inventory_open_ids: + raise ValidationError( + _('An inventory is being conducted at this ' + 'location')) @api.multi def write(self, vals): diff --git a/stock_inventory_lockdown/models/stock_quant.py b/stock_inventory_lockdown/models/stock_quant.py index ed3dbb3ad..5cec98a4b 100644 --- a/stock_inventory_lockdown/models/stock_quant.py +++ b/stock_inventory_lockdown/models/stock_quant.py @@ -20,26 +20,19 @@ class StockQuant(models.Model): """ if not self.env.context.get('bypass_lockdown', False): # Find the locked locations - locked_location_ids = self.env[ - 'stock.inventory']._get_locations_open_inventories() - if locked_location_ids and 'location_id' in vals.keys(): - messages = set() - # Find the destination locations - location_dest_id = self.env['stock.location'].browse( - vals['location_id']) - for quant in self: - # Source locations - location_id = quant.location_id - # Moving to a location locked down - if location_dest_id in locked_location_ids: - messages.add(location_dest_id.name) - # Moving from a location locked down - if location_id in locked_location_ids: - messages.add(location_id.name) - if len(messages): - raise ValidationError( - _('An inventory is being conducted at the following ' - 'location(s):\n%s') % "\n - ".join(messages)) + locked_location_ids = [] + if 'location_id' in vals.keys(): + locked_location_ids = self.env[ + 'stock.inventory']._get_locations_open_inventories( + self.env['stock.location'].browse( + vals['location_id']).ids + self.mapped( + 'location_id').ids + ) + if locked_location_ids: + location_names = locked_location_ids.mapped('name') + raise ValidationError( + _('An inventory is being conducted at the following ' + 'location(s):\n%s') % "\n - ".join(location_names)) return super(StockQuant, self).write(vals) @api.model @@ -49,9 +42,9 @@ class StockQuant(models.Model): """ quant = super(StockQuant, self).create(vals) if not self.env.context.get('bypass_lockdown', False): - locked_location_ids = self.env[ - 'stock.inventory']._get_locations_open_inventories() - if quant.location_id in locked_location_ids: + locked_location_ids = self.env['stock.inventory'].\ + _get_locations_open_inventories(quant.location_id.ids) + if locked_location_ids: raise ValidationError( _('An inventory is being conducted at the following ' 'location(s):\n%s') % " - " + quant.location_id.name) diff --git a/stock_inventory_lockdown/tests/test_stock_inventory_lockdown.py b/stock_inventory_lockdown/tests/test_stock_inventory_lockdown.py index 905aad629..cfcb7dfeb 100644 --- a/stock_inventory_lockdown/tests/test_stock_inventory_lockdown.py +++ b/stock_inventory_lockdown/tests/test_stock_inventory_lockdown.py @@ -4,7 +4,6 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openerp.exceptions import ValidationError - from openerp.addons.stock.tests.common import TestStockCommon