Skip to content

Commit d625a76

Browse files
committed
[FIX] sale_product_pack: adjust discount formula for detailed packs
The discount formula for detailed packs has been updated to address inconsistencies when pricelists explicitly display discounts. The new formula ensures accurate representation of the combined discount by correctly factoring in both the parent pack and component discounts. Example: Parent pack discount: 5% Component A discount: 10% Component B discount: 20% The issue occurred when pricelists explicitly displayed discounts. For example, when the pricelist showed the discount percentage but did not properly calculate the combined effect of the pack discount and the component discounts, the displayed total was inconsistent. With the new formula: Component A effective discount: 100.0 - ((100.0 - 5.0) * (100.0 - 10.0) / 100.0) = 14.5% Component B effective discount: 100.0 - ((100.0 - 5.0) * (100.0 - 20.0) / 100.0) = 24.0% This ensures that when pricelists explicitly show discounts, the displayed percentage matches the calculated prices.
1 parent edc63a6 commit d625a76

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Diff for: sale_product_pack/models/sale_order_line.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ def _get_pack_line_discount(self):
153153
if self.pack_parent_line_id.pack_component_price == "detailed":
154154
for pack_line in self.pack_parent_line_id.product_id.pack_line_ids:
155155
if pack_line.product_id == self.product_id:
156-
discount = pack_line.sale_discount
156+
discount = 100.0 - (
157+
(100.0 - self.discount) * (100.0 - pack_line.sale_discount) / 100.0
158+
)
157159
break
158160
return discount
159161

Diff for: sale_product_pack/tests/test_sale_product_pack.py

+34
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,37 @@ def test_create_several_lines_03(self):
335335
self.assertEqual(sequence_tp, self.sale_order.order_line[2].sequence)
336336
self.assertEqual(sequence_tp, self.sale_order.order_line[3].sequence)
337337
self.assertEqual(self.sale_order.order_line[4].product_id, product)
338+
339+
def test_compute_discount_for_detailed_packs(self):
340+
group_discount = self.env.ref("sale.group_discount_per_so_line")
341+
self.env.user.write({"groups_id": [(4, group_discount.id)]})
342+
product_pack = self.env.ref("product_pack.product_pack_cpu_detailed_components")
343+
sale_order = self.env["sale.order"].create(
344+
{
345+
"partner_id": self.env.ref("base.res_partner_12").id,
346+
"pricelist_id": self.discount_pricelist.id,
347+
"order_line": [
348+
(
349+
0,
350+
0,
351+
{
352+
"product_id": product_pack.id,
353+
"product_uom_qty": 1,
354+
"pack_component_price": "detailed",
355+
},
356+
)
357+
],
358+
}
359+
)
360+
sale_order.action_update_prices()
361+
pack_lines = sale_order.order_line.filtered(
362+
lambda line: line.pack_parent_line_id
363+
)
364+
self.assertEqual(
365+
len(pack_lines), 3, "Expected 3 lines for the components of the pack."
366+
)
367+
self.assertEqual(
368+
pack_lines.mapped("discount"),
369+
[19, 19, 10],
370+
"Discounts for the pack lines are not calculated correctly.",
371+
)

0 commit comments

Comments
 (0)