From 2f69aeb428f85da1b2bf07ad3109006c17f0aff6 Mon Sep 17 00:00:00 2001 From: eLBati Date: Tue, 2 Apr 2024 16:53:00 +0200 Subject: [PATCH] [FIX] l10n_it_withholding_tax: Do not generate WT moves and amount during refund --- l10n_it_withholding_tax/models/account.py | 11 ++++- .../tests/test_withholding_tax.py | 46 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/l10n_it_withholding_tax/models/account.py b/l10n_it_withholding_tax/models/account.py index d48be9af5d65..a0c053c4e4bb 100644 --- a/l10n_it_withholding_tax/models/account.py +++ b/l10n_it_withholding_tax/models/account.py @@ -58,10 +58,19 @@ def create(self, vals_list): paying_invoice = move_lines.filtered(lambda x: x.exists()).move_id.filtered( lambda x: x.is_invoice() ) + + # If we are reconciling a vendor bill and its refund, + # we do not need to generate Withholding Tax Moves + # or change the reconciliation amount + in_refunding = len(paying_invoice) == 2 and set( + paying_invoice.mapped("move_type") + ) == {"in_invoice", "in_refund"} # XXX # the following code mimics 12.0 behaviour; probably it's not correct - if paying_invoice: + if not in_refunding: paying_invoice = first(paying_invoice) + else: + paying_invoice = self.env["account.move"].browse() # Limit value of reconciliation if ( diff --git a/l10n_it_withholding_tax/tests/test_withholding_tax.py b/l10n_it_withholding_tax/tests/test_withholding_tax.py index 2cfc8361fb74..815b94e2e1e4 100644 --- a/l10n_it_withholding_tax/tests/test_withholding_tax.py +++ b/l10n_it_withholding_tax/tests/test_withholding_tax.py @@ -367,3 +367,49 @@ def test_refund_wt_propagation(self): # Assert: The refund has the Withholding Tax flag enabled self.assertTrue(refund.withholding_tax) + + def test_refund_reconciliation_amount(self): + """ + When a refund is created, the amount reconciled + is the whole amount of the vendor bill. + """ + # Arrange: Create a bill + bill = self._create_bill() + bill_amount = bill.amount_total + + # Act: Create a refund + refund = self._get_refund(bill) + + # Assert: The reconciliation is for the whole bill + reconciliation = self.env["account.partial.reconcile"].search( + [ + ("debit_move_id", "in", refund.move_id.line_ids.ids), + ("credit_move_id", "in", bill.move_id.line_ids.ids), + ] + ) + self.assertEqual(reconciliation.amount, bill_amount) + + def test_refund_wt_moves(self): + """ + When a refund is created, + no Withholding Tax Moves are created. + """ + # Arrange: Create a bill + bill = self._create_bill() + + # Act: Create a refund + refund = self._get_refund(bill) + + # Assert: There are no Withholding Tax Moves + reconciliation = self.env["account.partial.reconcile"].search( + [ + ("debit_move_id", "in", refund.move_id.line_ids.ids), + ("credit_move_id", "in", bill.move_id.line_ids.ids), + ] + ) + withholding_tax_moves = self.env["withholding.tax.move"].search( + [ + ("reconcile_partial_id", "=", reconciliation.id), + ] + ) + self.assertFalse(withholding_tax_moves)