From e4716b818823e3a8ffc13b7b61ce0a09b174e18f Mon Sep 17 00:00:00 2001 From: Borruso Date: Wed, 8 Nov 2023 17:11:15 +0100 Subject: [PATCH] [FIX] l10n_it_withholding_tax: reconcile payments after reset invoice to draft --- l10n_it_withholding_tax/models/account.py | 56 ++++++++++++++++--- .../tests/test_withholding_tax.py | 39 +++++++++++++ 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/l10n_it_withholding_tax/models/account.py b/l10n_it_withholding_tax/models/account.py index a70c488e40bb..44da0e5be2fc 100644 --- a/l10n_it_withholding_tax/models/account.py +++ b/l10n_it_withholding_tax/models/account.py @@ -85,11 +85,13 @@ def create(self, vals_list): ld = self.env["account.move.line"].browse(vals.get("debit_move_id")) lc = self.env["account.move.line"].browse(vals.get("credit_move_id")) - if ( - lc.withholding_tax_generated_by_move_id - or ld.withholding_tax_generated_by_move_id - ): + move_ids = ld.move_id | lc.move_id + lines = self.env["account.move.line"].search( + [("withholding_tax_generated_by_move_id", "in", move_ids.ids)] + ) + if lines: is_wt_move = True + reconcile.generate_wt_moves(is_wt_move, lines) else: is_wt_move = False # Wt moves creation @@ -99,7 +101,7 @@ def create(self, vals_list): and not is_wt_move ): # and not wt_existing_moves\ - reconcile.generate_wt_moves() + reconcile.generate_wt_moves(is_wt_move) ret |= reconcile return ret @@ -111,7 +113,7 @@ def _prepare_wt_move(self, vals): return vals @api.model - def generate_wt_moves(self): + def generate_wt_moves(self, is_wt_move, lines=None): wt_statement_obj = self.env["withholding.tax.statement"] # Reconcile lines line_payment_ids = [] @@ -169,9 +171,42 @@ def generate_wt_moves(self): wt_move = self.env["withholding.tax.move"].create(wt_move_vals) wt_moves.append(wt_move) # Generate account move - wt_move.generate_account_move() + if not is_wt_move: + wt_move.generate_account_move() + else: + self.reconcile_exist_account_move(lines, rec_line_statement, amount_wt) return wt_moves + @api.model + def reconcile_exist_account_move(self, lines, rec_line_statement, amount_wt): + line_to_reconcile = self.env["account.move.line"] + for line in lines: + if ( + line.account_id.account_type + in ["liability_payable", "asset_receivable"] + and line.partner_id + ): + line_to_reconcile = line + break + if line_to_reconcile: + if lines.move_id.move_type in ["in_refund", "out_invoice"]: + debit_move_id = rec_line_statement.id + credit_move_id = line_to_reconcile.id + else: + debit_move_id = line_to_reconcile.id + credit_move_id = rec_line_statement.id + self.env["account.partial.reconcile"].with_context( + no_generate_wt_move=True + ).create( + { + "debit_move_id": debit_move_id, + "credit_move_id": credit_move_id, + "amount": abs(amount_wt), + "credit_amount_currency": abs(amount_wt), + "debit_amount_currency": abs(amount_wt), + } + ) + def unlink(self): statements = [] for rec in self: @@ -441,7 +476,10 @@ def action_post(self): # update line move_line.write({"withholding_tax_amount": wt_amount}) # Create WT Statement - inv.create_wt_statement() + if not self.env["withholding.tax.statement"].search( + [("invoice_id", "=", inv.id)] + ): + inv.create_wt_statement() return res def get_wt_taxes_values(self): @@ -581,7 +619,7 @@ def remove_move_reconcile(self): rec_move_ids.unlink() # Delete wt move for wt_move in wt_mls.mapped("move_id"): - wt_move.button_cancel() + wt_move.button_draft() wt_move.unlink() return super(AccountMoveLine, self).remove_move_reconcile() diff --git a/l10n_it_withholding_tax/tests/test_withholding_tax.py b/l10n_it_withholding_tax/tests/test_withholding_tax.py index fe06fa5b1b9e..20b03c053326 100644 --- a/l10n_it_withholding_tax/tests/test_withholding_tax.py +++ b/l10n_it_withholding_tax/tests/test_withholding_tax.py @@ -281,3 +281,42 @@ def test_create_payments(self): payment_register.with_context( default_move_type="in_invoice" ).action_create_payments() + + def test_wt_after_repost(self): + wt_statement_ids = self.env["withholding.tax.statement"].search( + [ + ("invoice_id", "=", self.invoice.id), + ("withholding_tax_id", "=", self.wt1040.id), + ] + ) + self.assertEqual(len(wt_statement_ids), 1) + ctx = { + "active_model": "account.move", + "active_ids": [self.invoice.id], + "active_id": self.invoice.id, + "default_reconciled_invoice_ids": [(4, self.invoice.id, None)], + } + register_payment_form = Form( + self.payment_register_model.with_context(**ctx), view=self.register_view_id + ) + register_payment_form.amount = 600 + register_payment = register_payment_form.save() + register_payment.action_create_payments() + partials = self.invoice._get_reconciled_invoices_partials()[0] + self.assertTrue({p[1] for p in partials} == {600, 150}) + + self.invoice.button_draft() + self.invoice.action_post() + wt_statement_ids = self.env["withholding.tax.statement"].search( + [ + ("invoice_id", "=", self.invoice.id), + ("withholding_tax_id", "=", self.wt1040.id), + ] + ) + self.assertEqual(len(wt_statement_ids), 1) + debit_line_id = partials[0][2].move_id.line_ids.filtered(lambda l: l.debit) + self.invoice.js_assign_outstanding_line(debit_line_id.id) + self.assertEqual(self.invoice.amount_net_pay, 800) + self.assertEqual(self.invoice.amount_net_pay_residual, 200) + self.assertEqual(self.invoice.amount_residual, 250) + self.assertEqual(self.invoice.state, "posted")