Skip to content

Commit

Permalink
[FIX] sale_product_pack: Define the correct sequence of sale order lines
Browse files Browse the repository at this point in the history
Previously, the product pack line sequence was defined for the “extra” lines

TT50677
  • Loading branch information
victoralmau committed Sep 5, 2024
1 parent 9036c06 commit 6f5b952
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
1 change: 0 additions & 1 deletion sale_product_pack/models/product_pack_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 21 additions & 12 deletions sale_product_pack/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down
47 changes: 24 additions & 23 deletions sale_product_pack/tests/test_sale_product_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,19 @@ 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,
"product_id": product_cp.id,
"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
Expand All @@ -103,14 +99,15 @@ 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,
"product_id": product_tp.id,
"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
Expand All @@ -137,14 +134,15 @@ 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,
"product_id": product_tp.id,
"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
Expand Down Expand Up @@ -198,14 +196,15 @@ 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,
"product_id": product_cp.id,
"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
Expand All @@ -223,14 +222,15 @@ 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,
"product_id": product_cp.id,
"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)
Expand All @@ -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)

0 comments on commit 6f5b952

Please sign in to comment.