Skip to content

Commit

Permalink
[FIX] l10n_it_withholding_tax: reconcile payments after reset invoice…
Browse files Browse the repository at this point in the history
… to draft
  • Loading branch information
Borruso committed Nov 8, 2023
1 parent b7d12ca commit 244d444
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
56 changes: 47 additions & 9 deletions l10n_it_withholding_tax/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = []
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
39 changes: 39 additions & 0 deletions l10n_it_withholding_tax/tests/test_withholding_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 244d444

Please sign in to comment.