Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] l10n_it_withholding_tax: reconcile payments after reset invoice to draft #3707

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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 @@
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

Check warning on line 194 in l10n_it_withholding_tax/models/account.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_withholding_tax/models/account.py#L193-L194

Added lines #L193 - L194 were not covered by tests
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 @@
# 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 @@
rec_move_ids.unlink()
# Delete wt move
for wt_move in wt_mls.mapped("move_id"):
wt_move.button_cancel()
wt_move.button_draft()

Check warning on line 622 in l10n_it_withholding_tax/models/account.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_withholding_tax/models/account.py#L622

Added line #L622 was not covered by tests
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")
Loading