From 6f5b952a14f69ad681b15edfd73aef5f4d06a1c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Wed, 4 Sep 2024 16:59:39 +0200 Subject: [PATCH] [FIX] sale_product_pack: Define the correct sequence of sale order lines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the product pack line sequence was defined for the “extra” lines TT50677 --- sale_product_pack/models/product_pack_line.py | 1 - sale_product_pack/models/sale_order_line.py | 33 ++++++++----- .../tests/test_sale_product_pack.py | 47 ++++++++++--------- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/sale_product_pack/models/product_pack_line.py b/sale_product_pack/models/product_pack_line.py index 52272769..eaf7925c 100644 --- a/sale_product_pack/models/product_pack_line.py +++ b/sale_product_pack/models/product_pack_line.py @@ -16,7 +16,6 @@ def get_sale_order_line_vals(self, line, order): quantity = self.quantity * line.product_uom_qty line_vals = { "order_id": order.id, - "sequence": line.sequence, "product_id": self.product_id.id or False, "pack_parent_line_id": line.id, "pack_depth": line.pack_depth + 1, diff --git a/sale_product_pack/models/sale_order_line.py b/sale_product_pack/models/sale_order_line.py index 57e4a209..7aa63ce2 100644 --- a/sale_product_pack/models/sale_order_line.py +++ b/sale_product_pack/models/sale_order_line.py @@ -53,8 +53,11 @@ def expand_pack_line(self, write=False): # only want to update prices vals_list = [] if self.product_id.pack_ok and self.pack_type == "detailed": + new_sequence = self.sequence + 1 for subline in self.product_id.get_pack_lines(): vals = subline.get_sale_order_line_vals(self, self.order_id) + vals["sequence"] = new_sequence + new_sequence += 1 if write: existing_subline = first( self.pack_child_line_ids.filtered( @@ -72,21 +75,27 @@ def expand_pack_line(self, write=False): else: vals_list.append(vals) if vals_list: - self.create(vals_list) + return self.create(vals_list) @api.model_create_multi def create(self, vals_list): - new_vals = [] - res = self.browse() - for elem in vals_list: - product = self.env["product.product"].browse(elem.get("product_id")) - if product and product.pack_ok and product.pack_type != "non_detailed": - line = super().create([elem]) - line.expand_pack_line() - res |= line - else: - new_vals.append(elem) - res |= super().create(new_vals) + """We need to create everything and then add the extra lines as appropriate""" + res = super().create(vals_list) + if any( + x.product_id.pack_ok and x.product_id.pack_type != "non_detailed" + for x in res + ): + for item in res.filtered( + lambda x: x.product_id.pack_ok + and x.product_id.pack_type != "non_detailed" + ): + total_pack_lines = len(item.product_id.get_pack_lines()) + # Increase the sequence of the lines that are after to add the lines of + # the pack later + for after_line in res.filtered(lambda y: y.sequence > item.sequence): # noqa B023 + after_line.sequence += total_pack_lines + res += item.expand_pack_line() + res = res.sorted() return res def write(self, vals): diff --git a/sale_product_pack/tests/test_sale_product_pack.py b/sale_product_pack/tests/test_sale_product_pack.py index d8de1222..bbdbd3be 100644 --- a/sale_product_pack/tests/test_sale_product_pack.py +++ b/sale_product_pack/tests/test_sale_product_pack.py @@ -61,7 +61,7 @@ def _get_component_prices_sum(self, product_pack): def test_create_components_price_order_line(self): product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") - line = self.env["sale.order.line"].create( + so_lines = self.env["sale.order.line"].create( { "order_id": self.sale_order.id, "name": product_cp.name, @@ -69,15 +69,11 @@ def test_create_components_price_order_line(self): "product_uom_qty": 1, } ) + line = so_lines.filtered(lambda x: x.product_id == product_cp) # After create, there will be four lines self.assertEqual(len(self.sale_order.order_line), 4) - pack_line = self.sale_order.order_line.filtered( - lambda pline: pline.product_id == product_cp - ) - # Check if sequence is the same as pack product one - sequence = pack_line.sequence self.assertEqual( - [sequence, sequence, sequence, sequence], + [10, 11, 12, 13], self.sale_order.order_line.mapped("sequence"), ) # The products of those four lines are the main product pack and its @@ -103,7 +99,7 @@ def test_create_components_price_order_line(self): def test_create_ignored_price_order_line(self): product_tp = self.env.ref("product_pack.product_pack_cpu_detailed_ignored") - line = self.env["sale.order.line"].create( + so_lines = self.env["sale.order.line"].create( { "order_id": self.sale_order.id, "name": product_tp.name, @@ -111,6 +107,7 @@ def test_create_ignored_price_order_line(self): "product_uom_qty": 1, } ) + line = so_lines.filtered(lambda x: x.product_id == product_tp) # After create, there will be four lines self.assertEqual(len(self.sale_order.order_line), 4) # The products of those four lines are the main product pack and its @@ -137,7 +134,7 @@ def test_create_ignored_price_order_line(self): def test_create_totalized_price_order_line(self): product_tp = self.env.ref("product_pack.product_pack_cpu_detailed_totalized") - line = self.env["sale.order.line"].create( + so_lines = self.env["sale.order.line"].create( { "order_id": self.sale_order.id, "name": product_tp.name, @@ -145,6 +142,7 @@ def test_create_totalized_price_order_line(self): "product_uom_qty": 1, } ) + line = so_lines.filtered(lambda x: x.product_id == product_tp) # After create, there will be four lines self.assertEqual(len(self.sale_order.order_line), 4) # The products of those four lines are the main product pack and its @@ -198,7 +196,7 @@ def qty_in_order(): return sum(self.sale_order.order_line.mapped("product_uom_qty")) product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") - main_sol = self.env["sale.order.line"].create( + so_lines = self.env["sale.order.line"].create( { "order_id": self.sale_order.id, "name": product_cp.name, @@ -206,6 +204,7 @@ def qty_in_order(): "product_uom_qty": 1, } ) + main_sol = so_lines.filtered(lambda x: x.product_id == product_cp) total_qty_init = qty_in_order() # change qty of main sol main_sol.product_uom_qty = 2 * main_sol.product_uom_qty @@ -223,7 +222,7 @@ def qty_in_order(): def test_do_not_expand(self): product_cp = self.env.ref("product_pack.product_pack_cpu_detailed_components") - pack_line = self.env["sale.order.line"].create( + so_lines = self.env["sale.order.line"].create( { "order_id": self.sale_order.id, "name": product_cp.name, @@ -231,6 +230,7 @@ def test_do_not_expand(self): "product_uom_qty": 1, } ) + pack_line = so_lines.filtered(lambda x: x.product_id == product_cp) # After create, there will be four lines self.assertEqual(len(self.sale_order.order_line), 4) pack_line_update = pack_line.with_context(update_prices=True) @@ -250,26 +250,27 @@ def test_create_several_lines(self): "name": product_cp.name, "product_id": product_cp.id, "product_uom_qty": 1, + "sequence": 10, }, { "order_id": self.sale_order.id, "name": product_tp.name, "product_id": product_tp.id, "product_uom_qty": 1, + "sequence": 11, }, ] - self.env["sale.order.line"].create(vals) + lines = self.env["sale.order.line"].create(vals) # After create, there will be eight lines (4 + 4) self.assertEqual(len(self.sale_order.order_line), 8) # Check if lines are well ordered - self.assertEqual(self.sale_order.order_line[0].product_id, product_cp) - sequence_cp = self.sale_order.order_line[0].sequence - self.assertEqual(sequence_cp, self.sale_order.order_line[1].sequence) - self.assertEqual(sequence_cp, self.sale_order.order_line[2].sequence) - self.assertEqual(sequence_cp, self.sale_order.order_line[3].sequence) - - self.assertEqual(self.sale_order.order_line[4].product_id, product_tp) - sequence_tp = self.sale_order.order_line[4].sequence - self.assertEqual(sequence_tp, self.sale_order.order_line[5].sequence) - self.assertEqual(sequence_tp, self.sale_order.order_line[6].sequence) - self.assertEqual(sequence_tp, self.sale_order.order_line[7].sequence) + self.assertEqual( + [10, 11, 12, 13, 14, 15, 16, 17], + lines.mapped("sequence"), + ) + # La línea de product_cp tiene la secuencia 10 + cp_line = lines.filtered(lambda x: x.product_id == product_cp) + self.assertEqual(cp_line.sequence, 10) + # La línea de product_tp tiene la secuencia cambiada a 14 (1+4) + tp_line = lines.filtered(lambda x: x.product_id == product_tp) + self.assertEqual(tp_line.sequence, 14)