From bed2036f55536fc739d1c3c23dc18761682fbf8f Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Tue, 5 Jan 2021 15:18:59 +0100 Subject: [PATCH] [MIG/REF] stock_generate_putaway_from_inventory: Migration to 14.0 --- .../__manifest__.py | 3 +- .../models/__init__.py | 1 + .../models/stock_inventory.py | 83 +++++++++---------- .../models/stock_location.py | 18 ++++ .../tests/test_generate_putaway.py | 44 ++++------ .../views/stock_inventory.xml | 8 +- 6 files changed, 82 insertions(+), 75 deletions(-) create mode 100644 stock_generate_putaway_from_inventory/models/stock_location.py diff --git a/stock_generate_putaway_from_inventory/__manifest__.py b/stock_generate_putaway_from_inventory/__manifest__.py index e3ac83e8d..e3f050f05 100644 --- a/stock_generate_putaway_from_inventory/__manifest__.py +++ b/stock_generate_putaway_from_inventory/__manifest__.py @@ -4,8 +4,9 @@ { "name": "Stock Generate Putaway from Inventory", "summary": "Generate Putaway locations per Product deduced from Inventory", - "version": "12.0.1.0.0", + "version": "14.0.1.0.0", "author": "Akretion, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/stock-logistics-warehouse", "category": "Warehouse", "depends": ["stock"], "license": "AGPL-3", diff --git a/stock_generate_putaway_from_inventory/models/__init__.py b/stock_generate_putaway_from_inventory/models/__init__.py index 35536816e..ea35990c3 100644 --- a/stock_generate_putaway_from_inventory/models/__init__.py +++ b/stock_generate_putaway_from_inventory/models/__init__.py @@ -1 +1,2 @@ from . import stock_inventory +from . import stock_location diff --git a/stock_generate_putaway_from_inventory/models/stock_inventory.py b/stock_generate_putaway_from_inventory/models/stock_inventory.py index 965081024..533998e13 100644 --- a/stock_generate_putaway_from_inventory/models/stock_inventory.py +++ b/stock_generate_putaway_from_inventory/models/stock_inventory.py @@ -8,52 +8,51 @@ from odoo.exceptions import ValidationError class StockInventory(models.Model): _inherit = "stock.inventory" - def action_generate_putaway_strategy(self): - for inventory in self: - inventory._generate_putaway_strategy() + def action_generate_putaway_rules(self): + self._generate_putaway_rules() - def _get_putaway_strategy(self, loc): - if loc.putaway_strategy_id: - return loc.putaway_strategy_id - elif loc.location_id: - return self._get_putaway_strategy(loc.location_id) + def _generate_putaway_rules(self): + for record in self: + if record.state != "done": + raise ValidationError( + _( + "Please validate the inventory before generating " + "the putaway strategy." + ) + ) + for location in record.location_ids: + record.line_ids._generate_putaway_rules(location) - def _update_product_putaway_strategy(self, inventory_line, strategy): - putaway_line_obj = self.env["stock.fixed.putaway.strat"] - putaway_line = putaway_line_obj.search( + +class StockInventoryLine(models.Model): + _inherit = "stock.inventory.line" + + def _generate_putaway_rules(self, inventory_location): + # Eliminate lines for other IN locations + # and eliminate lines where quantity is null + self.filtered( + lambda x: x.location_id._is_child(inventory_location) and x.product_qty > 0 + )._update_product_putaway_rule(inventory_location) + + def _update_product_putaway_rule(self, location_in): + putaway_rule_obj = self.env["stock.putaway.rule"] + putaway_rules = putaway_rule_obj.search( [ - ("product_id", "=", inventory_line.product_id.id), - ("putaway_id", "=", strategy.id), + ("product_id", "in", self.mapped("product_id").ids), + ("location_in_id", "=", location_in.id), ] ) - if putaway_line: - putaway_line.write({"fixed_location_id": inventory_line.location_id.id}) - else: - putaway_line_obj.create( - { - "product_id": inventory_line.product_id.id, - "fixed_location_id": inventory_line.location_id.id, - "putaway_id": strategy.id, - } + for record in self: + putaway_rule = putaway_rules.filtered( + lambda x: x.product_id == record.product_id ) - - def _generate_putaway_strategy(self): - if self.state != "done": - raise ValidationError( - _( - "Please validate the inventory before generating " - "the putaway strategy." + if putaway_rule: + putaway_rule.location_out_id = record.location_id + else: + putaway_rule_obj.create( + { + "product_id": record.product_id.id, + "location_in_id": location_in.id, + "location_out_id": record.location_id.id, + } ) - ) - putaway = self._get_putaway_strategy(self.location_id) - if not putaway: - raise ValidationError( - _( - "Inventory location doesn't have a putaway strategy. " - "Please set a putaway strategy on the inventory's " - "location." - ) - ) - for line in self.line_ids: - if line.product_qty > 0: - self._update_product_putaway_strategy(line, putaway) diff --git a/stock_generate_putaway_from_inventory/models/stock_location.py b/stock_generate_putaway_from_inventory/models/stock_location.py new file mode 100644 index 000000000..80e5e5efb --- /dev/null +++ b/stock_generate_putaway_from_inventory/models/stock_location.py @@ -0,0 +1,18 @@ +# Copyright 2021 Akretion (https://www.akretion.com). +# @author Pierrick Brun +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class StockLocation(models.Model): + _inherit = "stock.location" + + def _is_child(self, location): + self.ensure_one() + if self == location: + return True + elif self.location_id: + return self.location_id._is_child(location) + else: + return False diff --git a/stock_generate_putaway_from_inventory/tests/test_generate_putaway.py b/stock_generate_putaway_from_inventory/tests/test_generate_putaway.py index bc392f96f..7449ea89d 100644 --- a/stock_generate_putaway_from_inventory/tests/test_generate_putaway.py +++ b/stock_generate_putaway_from_inventory/tests/test_generate_putaway.py @@ -14,57 +14,47 @@ class TestGeneratePutaway(TransactionCase): self.inventory = self.env["stock.inventory"].create( { "name": "example inventory", - "location_id": self.inventory_location.id, + "location_ids": [(6, 0, [self.inventory_location.id])], } ) self.inventory_line_1_product = ref("product.product_product_24") - self.inventory_line_1_location = ref("stock.stock_location_14") + self.inventory_line_1_location = ref("stock.stock_location_components") self.inventory_line_1 = self.env["stock.inventory.line"].create( { "product_id": self.inventory_line_1_product.id, "location_id": self.inventory_line_1_location.id, + "inventory_id": self.inventory.id, + "product_qty": 1, } ) self.irrelevant_location = ref("stock.stock_location_customers") def test_error_not_validated(self): - putaway = self.env["product.putaway"].create({"name": "Putaway example"}) - self.inventory.putaway_strategy_id = putaway self.inventory.action_cancel_draft() with self.assertRaises(ValidationError): - self.inventory.action_generate_putaway_strategy() - - def test_error_location_has_no_putaway_strategy(self): - self.inventory_location.putaway_strategy_id = self.env["product.putaway"] - self.inventory.action_start() - self.inventory.action_validate() - with self.assertRaises(ValidationError): - self.inventory.action_generate_putaway_strategy() + self.inventory.action_generate_putaway_rules() def test_putaway_line_location_update(self): - putaway = self.env["product.putaway"].create({"name": "Putaway example"}) - putaway_line_1 = self.env["stock.fixed.putaway.strat"].create( + putaway = self.env["stock.putaway.rule"].create( { - "putaway_id": putaway.id, - "fixed_location_id": self.irrelevant_location.id, + "location_out_id": self.irrelevant_location.id, + "location_in_id": self.inventory_location.id, "product_id": self.inventory_line_1_product.id, } ) - self.inventory_location.putaway_strategy_id = putaway self.inventory.action_start() self.inventory.action_validate() - self.inventory.action_generate_putaway_strategy() - self.assertEqual( - putaway_line_1.fixed_location_id, self.inventory_line_1_location - ) + self.inventory.action_generate_putaway_rules() + self.assertEqual(putaway.location_out_id, self.inventory_line_1_location) def test_putaway_line_location_create(self): - putaway = self.env["product.putaway"].create({"name": "Putaway example"}) - self.inventory_location.putaway_strategy_id = putaway self.inventory.action_start() self.inventory.action_validate() - self.inventory.action_generate_putaway_strategy() - putaway_line = putaway.product_location_ids.filtered( - lambda r: r.product_id == self.inventory_line_1_product + self.inventory.action_generate_putaway_rules() + putaway = self.env["stock.putaway.rule"].search( + [ + ("product_id", "=", self.inventory_line_1_product.id), + ("location_in_id", "=", self.inventory_location.id), + ] ) - self.assertEqual(putaway_line.fixed_location_id, self.inventory_line_1_location) + self.assertEqual(putaway.location_out_id, self.inventory_line_1_location) diff --git a/stock_generate_putaway_from_inventory/views/stock_inventory.xml b/stock_generate_putaway_from_inventory/views/stock_inventory.xml index d2497fc75..5301bcb89 100644 --- a/stock_generate_putaway_from_inventory/views/stock_inventory.xml +++ b/stock_generate_putaway_from_inventory/views/stock_inventory.xml @@ -1,14 +1,12 @@ - - + Generate putaway strategies code -records.action_generate_putaway_strategy() +records.action_generate_putaway_rules() - - +