From bffa69da42d0737b2f7f7b1dd90a3fd2060b4e1d Mon Sep 17 00:00:00 2001 From: Bernat Puig Font Date: Thu, 4 Nov 2021 16:22:28 +0100 Subject: [PATCH] [IMP] procurement_auto_create_group: auto_create_group for push rules --- .../models/stock_rule.py | 8 + .../readme/CONFIGURE.rst | 9 +- .../readme/CONTRIBUTORS.rst | 1 + .../readme/USAGE.rst | 5 +- .../tests/test_auto_create.py | 158 +++++++++++++++--- .../views/procurement_view.xml | 6 + 6 files changed, 162 insertions(+), 25 deletions(-) diff --git a/procurement_auto_create_group/models/stock_rule.py b/procurement_auto_create_group/models/stock_rule.py index c699ccaae..0933f9c31 100644 --- a/procurement_auto_create_group/models/stock_rule.py +++ b/procurement_auto_create_group/models/stock_rule.py @@ -16,6 +16,14 @@ class StockRule(models.Model): if self.group_propagation_option != "propagate": self.auto_create_group = False + def _push_prepare_move_copy_values(self, move_to_copy, new_date): + new_move_vals = super()._push_prepare_move_copy_values(move_to_copy, new_date) + if self.auto_create_group: + group_data = self._prepare_auto_procurement_group_data() + group = self.env["procurement.group"].create(group_data) + new_move_vals["group_id"] = group.id + return new_move_vals + def _prepare_auto_procurement_group_data(self): name = self.env["ir.sequence"].next_by_code("procurement.group") or False if not name: diff --git a/procurement_auto_create_group/readme/CONFIGURE.rst b/procurement_auto_create_group/readme/CONFIGURE.rst index f4cfe39ea..d0a740ceb 100644 --- a/procurement_auto_create_group/readme/CONFIGURE.rst +++ b/procurement_auto_create_group/readme/CONFIGURE.rst @@ -2,7 +2,8 @@ 'Multi-Step Routes' and press the 'Save' button. #. Activate the developer mode. #. Go to *Inventory / Configuration / Warehouse Management / Routes* - and select the route you want to change. Select the pull rule you wish - to change and Select 'Propagation of Procurement Group': 'Propagate'. - The checkbox 'Auto-create Procurement Group' will then appear and you can - set it if you want to procurement group to be automatically created. + and select the route you want to change. Select the rule you wish + to change, and in case of a Pull rule or Push & Pull rule Select + 'Propagation of Procurement Group': 'Propagate'. The checkbox + 'Auto-create Procurement Group' will then appear and you can set + it if you want to procurement group to be automatically created. diff --git a/procurement_auto_create_group/readme/CONTRIBUTORS.rst b/procurement_auto_create_group/readme/CONTRIBUTORS.rst index 63707a39b..3866bf110 100644 --- a/procurement_auto_create_group/readme/CONTRIBUTORS.rst +++ b/procurement_auto_create_group/readme/CONTRIBUTORS.rst @@ -2,3 +2,4 @@ * Lois Rilo * Héctor Villarreal Ortega * Jacques-Etienne Baudoux (BCIM) +* Bernat Puig diff --git a/procurement_auto_create_group/readme/USAGE.rst b/procurement_auto_create_group/readme/USAGE.rst index e21466128..6ae370a3e 100644 --- a/procurement_auto_create_group/readme/USAGE.rst +++ b/procurement_auto_create_group/readme/USAGE.rst @@ -1,4 +1,5 @@ -#. Run a new procurement and make sure that it determines a pull rule +#. Run a new procurement and make sure that it determines a rule with the option 'Auto-create Procurement Group' set. #. When the procurement rule is executed, a procurement group with - format 'PG/000001' will be created. + format 'PG/000001' will be created. You can find this procurement + group in 'Additional info' tab of a transfer. diff --git a/procurement_auto_create_group/tests/test_auto_create.py b/procurement_auto_create_group/tests/test_auto_create.py index 861de80df..69457e3c8 100644 --- a/procurement_auto_create_group/tests/test_auto_create.py +++ b/procurement_auto_create_group/tests/test_auto_create.py @@ -11,57 +11,106 @@ class TestProcurementAutoCreateGroup(TransactionCase): self.rule_obj = self.env["stock.rule"] self.route_obj = self.env["stock.location.route"] self.move_obj = self.env["stock.move"] + self.picking_obj = self.env["stock.picking"] self.product_obj = self.env["product.product"] self.warehouse = self.env.ref("stock.warehouse0") self.location = self.env.ref("stock.stock_location_stock") - loc_components = self.env.ref("stock.stock_location_components") + self.company_id = self.env.ref("base.main_company") + self.supplier_location = self.env.ref("stock.stock_location_suppliers") + self.loc_components = self.env.ref("stock.stock_location_components") picking_type_id = self.env.ref("stock.picking_type_internal").id self.partner = self.env["res.partner"].create({"name": "Partner"}) # Create rules and routes: - route_auto = self.route_obj.create({"name": "Auto Create Group"}) + pull_push_route_auto = self.route_obj.create({"name": "Auto Create Group"}) self.rule_1 = self.rule_obj.create( { "name": "rule with autocreate", - "route_id": route_auto.id, + "route_id": pull_push_route_auto.id, "auto_create_group": True, "action": "pull_push", "warehouse_id": self.warehouse.id, "picking_type_id": picking_type_id, "location_id": self.location.id, - "location_src_id": loc_components.id, + "location_src_id": self.loc_components.id, "partner_address_id": self.partner.id, } ) - route_no_auto = self.route_obj.create({"name": "Not Auto Create Group"}) + pull_push_route_no_auto = self.route_obj.create( + {"name": "Not Auto Create Group"} + ) self.rule_obj.create( { "name": "rule with no autocreate", - "route_id": route_no_auto.id, + "route_id": pull_push_route_no_auto.id, "auto_create_group": False, "action": "pull_push", "warehouse_id": self.warehouse.id, "picking_type_id": picking_type_id, "location_id": self.location.id, - "location_src_id": loc_components.id, + "location_src_id": self.loc_components.id, + } + ) + push_route_auto = self.route_obj.create({"name": "Auto Create Group"}) + self.rule_1 = self.rule_obj.create( + { + "name": "route_auto", + "location_src_id": self.location.id, + "location_id": self.loc_components.id, + "route_id": push_route_auto.id, + "auto_create_group": True, + "auto": "manual", + "picking_type_id": picking_type_id, + "warehouse_id": self.warehouse.id, + "company_id": self.company_id.id, + "action": "push", + } + ) + push_route_no_auto = self.route_obj.create({"name": "Not Auto Create Group"}) + self.rule_obj.create( + { + "name": "route_no_auto", + "location_src_id": self.location.id, + "location_id": self.loc_components.id, + "route_id": push_route_no_auto.id, + "auto_create_group": False, + "auto": "manual", + "picking_type_id": picking_type_id, + "warehouse_id": self.warehouse.id, + "company_id": self.company_id.id, + "action": "push", } ) # Prepare products: - self.prod_auto = self.product_obj.create( + self.prod_auto_pull_push = self.product_obj.create( { "name": "Test Product 1", "type": "product", - "route_ids": [(6, 0, [route_auto.id])], + "route_ids": [(6, 0, [pull_push_route_auto.id])], } ) - self.prod_no_auto = self.product_obj.create( + self.prod_no_auto_pull_push = self.product_obj.create( { "name": "Test Product 2", "type": "product", - "route_ids": [(6, 0, [route_no_auto.id])], + "route_ids": [(6, 0, [pull_push_route_no_auto.id])], + } + ) + self.prod_auto_push = self.product_obj.create( + { + "name": "Test Product 3", + "type": "product", + "route_ids": [(6, 0, [push_route_auto.id])], + } + ) + self.prod_no_auto_push = self.product_obj.create( + { + "name": "Test Product 4", + "type": "product", + "route_ids": [(6, 0, [push_route_no_auto.id])], } ) @@ -83,22 +132,53 @@ class TestProcurementAutoCreateGroup(TransactionCase): ) return True - def test_01_no_auto_create_group(self): + def _push_trigger(self, product): + picking = self.picking_obj.create( + { + "picking_type_id": self.ref("stock.picking_type_in"), + "location_id": self.supplier_location.id, + "location_dest_id": self.location.id, + "move_lines": [ + ( + 0, + 0, + { + "name": "Test move", + "product_id": product.id, + "date_deadline": "2099-06-01 18:00:00", + "date": "2099-06-01 18:00:00", + "product_uom": product.uom_id.id, + "product_uom_qty": 1.0, + "location_id": self.supplier_location.id, + "location_dest_id": self.location.id, + }, + ) + ], + } + ) + picking.move_lines.write({"quantity_done": 1.0}) + picking.button_validate() + + def test_01_pull_push_no_auto_create_group(self): """Test auto creation of group.""" - move = self.move_obj.search([("product_id", "=", self.prod_no_auto.id)]) + move = self.move_obj.search( + [("product_id", "=", self.prod_no_auto_pull_push.id)] + ) self.assertFalse(move) - self._procure(self.prod_no_auto) - move = self.move_obj.search([("product_id", "=", self.prod_no_auto.id)]) + self._procure(self.prod_no_auto_pull_push) + move = self.move_obj.search( + [("product_id", "=", self.prod_no_auto_pull_push.id)] + ) self.assertTrue(move) self.assertFalse( move.group_id, "Procurement Group should not have been assigned." ) - def test_02_auto_create_group(self): - move = self.move_obj.search([("product_id", "=", self.prod_auto.id)]) + def test_02_pull_push_auto_create_group(self): + move = self.move_obj.search([("product_id", "=", self.prod_auto_pull_push.id)]) self.assertFalse(move) - self._procure(self.prod_auto) - move = self.move_obj.search([("product_id", "=", self.prod_auto.id)]) + self._procure(self.prod_auto_pull_push) + move = self.move_obj.search([("product_id", "=", self.prod_auto_pull_push.id)]) self.assertTrue(move) self.assertTrue(move.group_id, "Procurement Group not assigned.") self.assertEqual( @@ -114,3 +194,43 @@ class TestProcurementAutoCreateGroup(TransactionCase): proc_rule.write({"group_propagation_option": "none"}) proc_rule._onchange_group_propagation_option() self.assertFalse(proc_rule.auto_create_group) + + def test_04_push_no_auto_create_group(self): + """Test no auto creation of group.""" + move = self.move_obj.search( + [ + ("product_id", "=", self.prod_no_auto_push.id), + ("location_dest_id", "=", self.loc_components.id), + ] + ) + self.assertFalse(move) + self._push_trigger(self.prod_no_auto_push) + move = self.move_obj.search( + [ + ("product_id", "=", self.prod_no_auto_push.id), + ("location_dest_id", "=", self.loc_components.id), + ] + ) + self.assertTrue(move) + self.assertFalse( + move.group_id, "Procurement Group should not have been assigned." + ) + + def test_05_push_auto_create_group(self): + """Test auto creation of group.""" + move = self.move_obj.search( + [ + ("product_id", "=", self.prod_auto_push.id), + ("location_dest_id", "=", self.loc_components.id), + ] + ) + self.assertFalse(move) + self._push_trigger(self.prod_auto_push) + move = self.move_obj.search( + [ + ("product_id", "=", self.prod_auto_push.id), + ("location_dest_id", "=", self.loc_components.id), + ] + ) + self.assertTrue(move) + self.assertTrue(move.group_id, "Procurement Group not assigned.") diff --git a/procurement_auto_create_group/views/procurement_view.xml b/procurement_auto_create_group/views/procurement_view.xml index 8c383639c..5ae53071b 100644 --- a/procurement_auto_create_group/views/procurement_view.xml +++ b/procurement_auto_create_group/views/procurement_view.xml @@ -11,6 +11,12 @@ attrs="{'invisible':[('group_propagation_option','!=','propagate')]}" /> + + +