Skip to content

Commit

Permalink
[FIX] sale_advance_payment: Use signed amount when computing residuals
Browse files Browse the repository at this point in the history
Otherwise, we might get incorrect residuals when we get refunds
  • Loading branch information
etobella committed Jan 3, 2025
1 parent 2dd2294 commit 900b1c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sale_advance_payment/models/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def _compute_advance_payment(self):
# Consider payments in related invoices.
invoice_paid_amount = 0.0
for inv in order.invoice_ids:
invoice_paid_amount += inv.amount_total - inv.amount_residual
invoice_paid_amount += (
inv.amount_total_signed - inv.amount_residual_signed
)
amount_residual = order.amount_total - advance_amount - invoice_paid_amount
payment_state = "not_paid"
if mls:
Expand Down
30 changes: 30 additions & 0 deletions sale_advance_payment/tests/test_sale_advance_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,33 @@ def test_03_residual_amount_big_pre_payment(self):
self.assertEqual(invoice.amount_residual, 0.0)
self.assertEqual(self.sale_order_1.amount_residual, 1600)
self.assertEqual(invoice.amount_residual, 0)

def test_04_refunds(self):
self.assertEqual(
self.sale_order_1.amount_residual,
3600,
)
self.sale_order_1.action_confirm()
invoice = self.sale_order_1._create_invoices()
invoice.action_post()
self.assertEqual(invoice.amount_total, 3600)
self.assertEqual(
self.sale_order_1.amount_residual,
3600,
)
self.env["account.move.reversal"].with_context(
active_model="account.move", active_ids=invoice.ids
).create({"journal_id": invoice.journal_id.id}).reverse_moves()
self.assertEqual(len(self.sale_order_1.invoice_ids), 2)
invoice = self.sale_order_1.invoice_ids.filtered(lambda x: x.state == "draft")
invoice.action_post()
self.assertEqual(
self.sale_order_1.amount_residual,
3600,
)
invoice = self.sale_order_1._create_invoices()
invoice.action_post()
self.assertEqual(
self.sale_order_1.amount_residual,
3600,
)

0 comments on commit 900b1c5

Please sign in to comment.