From abcc090b853c7cb9ffdd1da47e0e38755572565d Mon Sep 17 00:00:00 2001 From: Alejandro Ji Cheung Date: Thu, 28 Nov 2024 12:37:38 +0100 Subject: [PATCH] [IMP] procurement_auto_create_group: create a procurement.group for each stock.picking instead of each stock.move --- .../models/stock_rule.py | 6 +- .../tests/test_auto_create.py | 68 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/procurement_auto_create_group/models/stock_rule.py b/procurement_auto_create_group/models/stock_rule.py index 6123876468c4..019ca29096ee 100644 --- a/procurement_auto_create_group/models/stock_rule.py +++ b/procurement_auto_create_group/models/stock_rule.py @@ -23,7 +23,11 @@ def _get_auto_procurement_group(self, product): 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 = self._get_auto_procurement_group(move_to_copy.product_id) + # Get the same procurement group as the original move if it exists + # so that the new move is part of the same group as the original move + group = move_to_copy.picking_id.move_ids.mapped("move_dest_ids").group_id + if not group: + group = self._get_auto_procurement_group(move_to_copy.product_id) new_move_vals["group_id"] = group.id return new_move_vals diff --git a/procurement_auto_create_group/tests/test_auto_create.py b/procurement_auto_create_group/tests/test_auto_create.py index 090b9871adbe..38adcf453e80 100644 --- a/procurement_auto_create_group/tests/test_auto_create.py +++ b/procurement_auto_create_group/tests/test_auto_create.py @@ -108,6 +108,13 @@ def setUpClass(cls): "route_ids": [(6, 0, [push_route_auto.id])], } ) + cls.prod_auto_push_2 = cls.product_obj.create( + { + "name": "Test Product 5", + "type": "product", + "route_ids": [(6, 0, [push_route_auto.id])], + } + ) cls.prod_no_auto_push = cls.product_obj.create( { "name": "Test Product 4", @@ -168,6 +175,24 @@ def _push_trigger(cls, product): picking.move_ids.write({"quantity_done": 1.0}) picking.button_validate() + def _add_move_to_picking(self, picking, product): + move = self.move_obj.create( + { + "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_id": picking.id, + } + ) + picking.action_confirm() + move.write({"quantity_done": 1.0}) + picking._action_done() + def test_01_pull_push_no_auto_create_group(self): """Test auto creation of group.""" move = self.move_obj.search( @@ -245,3 +270,46 @@ def test_05_push_auto_create_group(self): ) self.assertTrue(move) self.assertTrue(move.group_id, "Procurement Group not assigned.") + + def test_06_auto_create_same_procurement_group_per_picking(self): + """Test auto creation of group for same procurement group per picking.""" + first_move = self.move_obj.search( + [ + ("product_id", "=", self.prod_auto_push.id), + ("location_dest_id", "=", self.loc_components.id), + ] + ) + self.assertFalse(first_move) + + picking = self.picking_obj.create( + { + "picking_type_id": self.env.ref("stock.picking_type_in").id, + "location_id": self.supplier_location.id, + "location_dest_id": self.location.id, + "move_ids": [], + } + ) + + self._add_move_to_picking(picking, self.prod_auto_push) + second_move = self.move_obj.search( + [ + ("product_id", "=", self.prod_auto_push.id), + ("location_dest_id", "=", self.loc_components.id), + ] + ) + self.assertTrue(second_move) + self.assertTrue(second_move.group_id, "Procurement Group not assigned.") + + self._add_move_to_picking(picking, self.prod_auto_push_2) + third_move = self.move_obj.search( + [ + ("product_id", "=", self.prod_auto_push_2.id), + ("location_dest_id", "=", self.loc_components.id), + ] + ) + self.assertTrue(third_move) + self.assertEqual( + third_move.move_dest_ids.group_id, + second_move.move_dest_ids.group_id, + "Procurement Group should be the same for the same picking.", + )