Skip to content

Commit

Permalink
[FIX] sale_fixed_discount: convert _onchange_discount_fixed() to comp…
Browse files Browse the repository at this point in the history
…uted method
  • Loading branch information
astirpe committed Jun 23, 2024
1 parent 2aa9055 commit c73fbd4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
9 changes: 6 additions & 3 deletions sale_fixed_discount/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ def _convert_to_tax_base_line_dict(self, **kwargs):

return super()._convert_to_tax_base_line_dict()

@api.onchange("discount_fixed", "price_unit")
def _onchange_discount_fixed(self):
self.discount = self._get_discount_from_fixed_discount()
@api.depends("discount_fixed", "price_unit")
def _compute_discount(self):
lines_with_discount_fixed = self.filtered(lambda sol: sol.discount_fixed)
for line in lines_with_discount_fixed:
line.discount = line._get_discount_from_fixed_discount()
return super(SaleOrderLine, self - lines_with_discount_fixed)

def _get_discount_from_fixed_discount(self):
"""Calculate the discount percentage from the fixed discount amount."""
Expand Down
23 changes: 22 additions & 1 deletion sale_fixed_discount/tests/test_sale_fixed_discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def test_02_fixed_discount_mismatch(self):
line.discount = 5.0

def test_03_fixed_discount_invoice(self):
"""Test discount_fixed value propagation to account.move"""
"""Test discount_fixed value propagation to account.move.
Case of editing order line by using UI.
"""
with Form(self.sale) as sale_order:
with sale_order.order_line.edit(0) as line:
line.discount_fixed = 20.0
Expand All @@ -102,6 +104,10 @@ def test_03_fixed_discount_invoice(self):
self.sale._create_invoices()

self.assertEqual(self.sale.invoice_ids.invoice_line_ids.discount_fixed, 20.0)
self.assertEqual(self.sale.invoice_ids.invoice_line_ids.discount, 10.0)

self.assertEqual(self.sale.invoice_ids.tax_totals["amount_untaxed"], 180.0)
self.assertEqual(self.sale.invoice_ids.tax_totals["amount_total"], 207.0)

def test_04_fixed_discount_without_price(self):
with Form(self.sale) as sale_order:
Expand All @@ -112,3 +118,18 @@ def test_04_fixed_discount_without_price(self):
self.assertEqual(line.discount, 0.0)
self.assertEqual(line.price_subtotal, 0.0)
self.assertEqual(self.sale.amount_total, 0.0)

def test_05_fixed_discount_invoice(self):
"""Test discount_fixed value propagation to account.move.
Case of editing order line without using UI (onchange would be not triggered).
"""
self.sale.order_line.discount_fixed = 20.0

self.sale.action_confirm()
self.sale._create_invoices()

self.assertEqual(self.sale.invoice_ids.invoice_line_ids.discount_fixed, 20.0)
self.assertEqual(self.sale.invoice_ids.invoice_line_ids.discount, 10.0)

self.assertEqual(self.sale.invoice_ids.tax_totals["amount_untaxed"], 180.0)
self.assertEqual(self.sale.invoice_ids.tax_totals["amount_total"], 207.0)

0 comments on commit c73fbd4

Please sign in to comment.