Skip to content

Commit

Permalink
[FIX] l10n_it_withholding_tax: porting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMule71 committed Mar 24, 2021
1 parent 5ad1d1a commit f289f9f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 78 deletions.
46 changes: 16 additions & 30 deletions l10n_it_withholding_tax/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.float_utils import float_round


class AccountFullReconcile(models.Model):
Expand Down Expand Up @@ -50,11 +51,8 @@ def create(self, vals):
if vals.get("credit_move_id"):
ml_ids.append(vals.get("credit_move_id"))
move_lines = self.env["account.move.line"].browse(ml_ids)
for ml in move_lines:
domain = [("move_id", "=", ml.move_id.id)]
invoice = self.env["account.move"].search(domain)
if invoice:
break
invoice = move_lines.filtered(lambda x: x.exists()).move_id
# invoice.ensure_one() XXX - should we do this?
# Limit value of reconciliation
if invoice and invoice.withholding_tax and invoice.amount_net_pay:
# We must consider amount in foreign currency, if present
Expand Down Expand Up @@ -185,9 +183,8 @@ def default_get(self, fields):
Compute amount to pay proportionally to amount total - wt
"""
rec = super(AccountAbstractPayment, self).default_get(fields)
invoice_defaults = self.resolve_2many_commands(
"invoice_ids", rec.get("invoice_ids")
)
invoice_defaults = self.new({"reconciled_invoice_ids": rec.get("reconciled_invoice_ids")}).reconciled_invoice_ids

if invoice_defaults and len(invoice_defaults) == 1:
invoice = invoice_defaults[0]
if (
Expand Down Expand Up @@ -300,26 +297,29 @@ def _prepare_wt_values(self):
@api.depends(
"invoice_line_ids.price_subtotal",
"withholding_tax_line_ids.tax",
"currency_id",
"company_id",
"invoice_date",
"amount_total",
# "payment_move_line_ids",
)
def _compute_amount_withholding_tax(self):
dp_obj = self.env["decimal.precision"]
for invoice in self:
withholding_tax_amount = 0.0
for wt_line in invoice.withholding_tax_line_ids:
withholding_tax_amount += round(
withholding_tax_amount += float_round(
wt_line.tax, dp_obj.precision_get("Account")
)
invoice.amount_net_pay = invoice.amount_total - withholding_tax_amount
amount_net_pay_residual = invoice.amount_net_pay
invoice.withholding_tax_amount = withholding_tax_amount
for line in invoice.line_ids._reconciled_lines():

reconciled_lines = invoice.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable'))
reconciled_amls = reconciled_lines.mapped('matched_debit_ids.debit_move_id') + \
reconciled_lines.mapped('matched_credit_ids.credit_move_id')

for line in reconciled_amls:
if not line.withholding_tax_generated_by_move_id:
amount_net_pay_residual -= line.debit or line.credit
invoice.amount_net_pay_residual = amount_net_pay_residual
invoice.amount_net_pay_residual = float_round(amount_net_pay_residual, dp_obj.precision_get("Account"))

withholding_tax = fields.Boolean("Withholding Tax")
withholding_tax_line_ids = fields.One2many(
Expand Down Expand Up @@ -352,21 +352,7 @@ def _compute_amount_withholding_tax(self):
readonly=True,
)

@api.model
def create(self, vals):
invoice = super(AccountMove, self.with_context(mail_create_nolog=True)).create(
vals
)

if (
any(line.invoice_line_tax_wt_ids for line in invoice.invoice_line_ids)
and not invoice.withholding_tax_line_ids
):
invoice.compute_taxes()

return invoice

@api.onchange("invoice_line_ids")
@api.onchange("line_ids")
def _onchange_invoice_line_wt_ids(self):
self.ensure_one()
wt_taxes_grouped = self.get_wt_taxes_values()
Expand Down Expand Up @@ -421,7 +407,7 @@ def get_wt_taxes_values(self):
for invoice in self:
for line in invoice.invoice_line_ids:
taxes = []
for wt_tax in line.invoice_line_tax_wt_ids:
for wt_tax in line.invoice_line_tax_wt_ids.filtered(lambda x: x.id):
res = wt_tax.compute_tax(line.price_subtotal)
tax = {
"id": wt_tax.id,
Expand Down
57 changes: 29 additions & 28 deletions l10n_it_withholding_tax/models/withholding_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class WithholdingTax(models.Model):
)
def _compute_get_rate(self):
for wt in self:
wt.tax = 0
wt.base = 1
if not wt.id:
continue
self.env.cr.execute(
"""
SELECT tax, base FROM withholding_tax_rate
Expand All @@ -29,9 +33,6 @@ def _compute_get_rate(self):
if rate:
wt.tax = rate[0]
wt.base = rate[1]
else:
wt.tax = 0
wt.base = 1

def _default_wt_journal(self):
misc_journal = self.env["account.journal"].search([("code", "=", _("MISC"))])
Expand Down Expand Up @@ -143,32 +144,32 @@ class WithholdingTaxRate(models.Model):

@api.constrains("date_start", "date_stop")
def _check_date(self):
self.ensure_one()
if self.withholding_tax_id.active:
domain = [
("withholding_tax_id", "=", self.withholding_tax_id.id),
("id", "!=", self.id),
]
if self.date_start:
domain.extend(
[
"|",
("date_stop", ">=", self.date_start),
("date_stop", "=", False),
]
)
if self.date_stop:
domain.extend(
[
"|",
("date_start", "<=", self.date_stop),
("date_start", "=", False),
]
)
for rate in self:
if rate.withholding_tax_id.active:
domain = [
("withholding_tax_id", "=", rate.withholding_tax_id.id),
("id", "!=", rate.id),
]
if rate.date_start:
domain.extend(
[
"|",
("date_stop", ">=", rate.date_start),
("date_stop", "=", False),
]
)
if rate.date_stop:
domain.extend(
[
"|",
("date_start", "<=", rate.date_stop),
("date_start", "=", False),
]
)

overlapping_rate = self.env["withholding.tax.rate"].search(domain, limit=1)
if overlapping_rate:
raise ValidationError(_("Error! You cannot have 2 rates that overlap!"))
overlapping_rate = rate.env["withholding.tax.rate"].search(domain, limit=1)
if overlapping_rate:
raise ValidationError(_("Error! You cannot have 2 rates that overlap!"))

withholding_tax_id = fields.Many2one(
"withholding.tax", string="Withholding Tax", ondelete="cascade", readonly=True
Expand Down
28 changes: 8 additions & 20 deletions l10n_it_withholding_tax/tests/test_withholding_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,12 @@ def setUp(self):
.search([("type", "=", "purchase")])[0]
.id,
"partner_id": self.env.ref("base.res_partner_12").id,
"account_id": self.env["account.account"]
.search(
[
(
"user_type_id",
"=",
self.env.ref("account.data_account_type_receivable").id,
)
],
limit=1,
order="id",
)
.id,
"invoice_line_ids": invoice_line_vals,
"type": "in_invoice",
"move_type": "in_invoice",
}
)
self.invoice._onchange_invoice_line_wt_ids()
self.invoice.action_invoice_open()
self.invoice._post()

def test_withholding_tax(self):
domain = [("name", "=", "Code 1040")]
Expand Down Expand Up @@ -155,7 +142,7 @@ def test_withholding_tax(self):
.with_context(ctx)
.create(
{
"payment_date": time.strftime("%Y") + "-07-15",
"date": time.strftime("%Y") + "-07-15",
"amount": 800,
"journal_id": self.journal_bank.id,
"payment_method_id": self.env.ref(
Expand Down Expand Up @@ -194,14 +181,14 @@ def test_partial_payment(self):
"active_model": "account.move",
"active_ids": [self.invoice.id],
"active_id": self.invoice.id,
"default_invoice_ids": [(4, self.invoice.id, None)],
"default_reconciled_invoice_ids": [(4, self.invoice.id, None)],
}
register_payments = (
self.env["account.payment"]
.with_context(ctx)
.create(
{
"payment_date": time.strftime("%Y") + "-07-15",
"date": time.strftime("%Y") + "-07-15",
"amount": 600,
"journal_id": self.journal_bank.id,
"payment_method_id": self.env.ref(
Expand All @@ -210,11 +197,12 @@ def test_partial_payment(self):
}
)
)
register_payments.action_validate_invoice_payment()
register_payments.action_post()

# WT amount in payment move lines
payment_line_ids = self.invoice.line_ids.filtered(lambda l: l.account_id.internal_type in ['receivable', 'payable'])
self.assertTrue(
set(self.invoice.payment_move_line_ids.mapped("debit")) == {600, 150}
set(payment_line_ids.mapped("debit")) == {600, 150}
)

# WT aomunt applied in statement
Expand Down

0 comments on commit f289f9f

Please sign in to comment.