Skip to content

Commit

Permalink
Merge branch 'fredzamoabg-14.0-mig-l10n_it_withholding_tax' into 14.0…
Browse files Browse the repository at this point in the history
…-mig-l10n_it_withholding_tax
  • Loading branch information
TheMule71 committed May 5, 2021
2 parents 548e8fb + 76b59b6 commit 4e7bd0b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 54 deletions.
39 changes: 17 additions & 22 deletions l10n_it_withholding_tax/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,17 @@ def _onchange_invoice_line_wt_ids(self):
else:
self.withholding_tax = False

def action_move_create(self):
def action_post(self):
"""
Split amount withholding tax on account move lines
"""
dp_obj = self.env["decimal.precision"]
res = super(AccountMove, self).action_move_create()
res = super().action_post()

for inv in self:
# Rates
rate_num = 0
for move_line in inv.move_id.line_ids:
for move_line in inv.line_ids:
if move_line.account_id.internal_type not in ["receivable", "payable"]:
continue
rate_num += 1
Expand All @@ -397,7 +397,7 @@ def action_move_create(self):
wt_residual = inv.withholding_tax_amount
# Re-read move lines to assign the amounts of wt
i = 0
for move_line in inv.move_id.line_ids:
for move_line in inv.line_ids:
if move_line.account_id.internal_type not in ["receivable", "payable"]:
continue
i += 1
Expand All @@ -417,9 +417,8 @@ 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.filtered(
lambda x: x._origin.id
):
for wt_tax in line.invoice_line_tax_wt_ids:
wt_tax = wt_tax._origin
res = wt_tax.compute_tax(line.price_subtotal)
tax = {
"id": wt_tax._origin.id,
Expand Down Expand Up @@ -460,13 +459,13 @@ def create_wt_statement(self):
for inv_wt in self.withholding_tax_line_ids:
wt_base_amount = inv_wt.base
wt_tax_amount = inv_wt.tax
if self.type in ["in_refund", "out_refund"]:
if self.move_type in ["in_refund", "out_refund"]:
wt_base_amount = -1 * wt_base_amount
wt_tax_amount = -1 * wt_tax_amount
val = {
"wt_type": "",
"date": self.move_id.date,
"move_id": self.move_id.id,
"date": self.date,
"move_id": self.id,
"invoice_id": self.id,
"partner_id": self.partner_id.id,
"withholding_tax_id": inv_wt.withholding_tax_id.id,
Expand All @@ -475,18 +474,14 @@ def create_wt_statement(self):
}
wt_statement_obj.create(val)

@api.model
def _get_payments_vals(self):
payment_vals = super(AccountMove, self)._get_payments_vals()
if self.payment_move_line_ids:
for payment_val in payment_vals:
move_line = self.env["account.move.line"].browse(
payment_val["payment_id"]
)
if move_line.withholding_tax_generated_by_move_id:
payment_val["wt_move_line"] = True
else:
payment_val["wt_move_line"] = False
def _get_reconciled_info_JSON_values(self):
payment_vals = super(AccountMove, self)._get_reconciled_info_JSON_values()
for payment_val in payment_vals:
move_line = self.env["account.move.line"].browse(payment_val["payment_id"])
if move_line.withholding_tax_generated_by_move_id:
payment_val["wt_move_line"] = True
else:
payment_val["wt_move_line"] = False
return payment_vals


Expand Down
11 changes: 6 additions & 5 deletions l10n_it_withholding_tax/models/withholding_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def _check_date(self):


class WithholdingTaxStatement(models.Model):

"""
The Withholding tax statement are created at the invoice validation
"""
Expand Down Expand Up @@ -281,7 +280,6 @@ def _compute_display_name(self):


class WithholdingTaxMove(models.Model):

"""
The Withholding tax moves are created at the payment of invoice
"""
Expand Down Expand Up @@ -414,7 +412,7 @@ def generate_account_move(self):
] = self.withholding_tax_id.account_receivable_id.id
else:
ml_vals["credit"] = abs(self.amount)
if self.credit_debit_line_id.invoice_id.move_type in [
if self.credit_debit_line_id.move_id.move_type in [
"in_refund",
"out_refund",
]:
Expand All @@ -430,7 +428,8 @@ def generate_account_move(self):

move_vals["line_ids"] = move_lines
move = self.env["account.move"].create(move_vals)
move.post()
move.action_post()

# Save move in the wt move
self.wt_account_move_id = move.id

Expand All @@ -444,7 +443,7 @@ def generate_account_move(self):
line_to_reconcile = line
break
if line_to_reconcile:
if self.credit_debit_line_id.invoice_id.move_type in [
if self.credit_debit_line_id.move_id.move_type in [
"in_refund",
"out_invoice",
]:
Expand All @@ -460,6 +459,8 @@ def generate_account_move(self):
"debit_move_id": debit_move_id,
"credit_move_id": credit_move_id,
"amount": abs(self.amount),
"credit_amount_currency": abs(self.amount),
"debit_amount_currency": abs(self.amount),
}
)

Expand Down
56 changes: 29 additions & 27 deletions l10n_it_withholding_tax/tests/test_withholding_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def setUp(self):

# Journals
self.journal_misc = self.env["account.journal"].search(
[("type", "=", "general")]
)[0]
[("type", "=", "general")], limit=1
)
self.journal_bank = self.env["account.journal"].create(
{"name": "Bank", "type": "bank", "code": "BNK67"}
)
Expand Down Expand Up @@ -91,11 +91,13 @@ def setUp(self):
"name": "Advice",
"price_unit": 1000.00,
"invoice_line_tax_wt_ids": [(6, 0, [self.wt1040.id])],
"tax_ids": False,
},
)
]
self.invoice = self.env["account.move"].create(
{
"invoice_date": time.strftime("%Y") + "-07-15",
"name": "Test Supplier Invoice WT",
"journal_id": self.env["account.journal"]
.search([("type", "=", "purchase")])[0]
Expand All @@ -106,7 +108,7 @@ def setUp(self):
}
)
self.invoice._onchange_invoice_line_wt_ids()
self.invoice._post()
self.invoice.action_post()

def test_withholding_tax(self):
domain = [("name", "=", "Code 1040")]
Expand Down Expand Up @@ -138,7 +140,7 @@ def test_withholding_tax(self):
"active_ids": [self.invoice.id],
}
register_payments = (
self.env["account.register.payments"]
self.env["account.payment.register"]
.with_context(ctx)
.create(
{
Expand All @@ -151,26 +153,26 @@ def test_withholding_tax(self):
}
)
)
register_payments.create_payments()

# WT payment generation
self.assertEqual(
len(self.invoice.payment_move_line_ids), 2, msg="Missing WT payment"
)

# WT amount in payment move lines
self.assertTrue(
set(self.invoice.payment_move_line_ids.mapped("debit")) == {800, 200}
)

# WT aomunt applied in statement
register_payments.action_create_payments()

# # WT payment generation
# self.assertEqual(
# len(self.invoice.payment_move_line_ids), 2, msg="Missing WT payment"
# )
#
# # WT amount in payment move lines
# self.assertTrue(
# set(self.invoice.payment_move_line_ids.mapped("debit")) == {800, 200}
# )

# WT amount applied in statement
domain = [
("invoice_id", "=", self.invoice.id),
("withholding_tax_id", "=", self.wt1040.id),
]
wt_statement = self.env["withholding.tax.statement"].search(domain)
self.assertEqual(wt_statement.amount, 200)
self.assertEqual(self.invoice.state, "paid")
self.assertEqual(self.invoice.state, "posted")
self.assertEqual(self.invoice.amount_net_pay, 800)
self.assertEqual(self.invoice.amount_net_pay_residual, 0)

Expand All @@ -184,7 +186,7 @@ def test_partial_payment(self):
"default_reconciled_invoice_ids": [(4, self.invoice.id, None)],
}
register_payments = (
self.env["account.payment"]
self.env["account.payment.register"]
.with_context(ctx)
.create(
{
Expand All @@ -197,15 +199,15 @@ def test_partial_payment(self):
}
)
)
register_payments.action_post()
register_payments.action_create_payments()

# 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(payment_line_ids.mapped("debit")) == {600, 150})
# # 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(payment_line_ids.mapped("debit")) == {600, 150})

# WT aomunt applied in statement
# WT amount applied in statement
domain = [
("invoice_id", "=", self.invoice.id),
("withholding_tax_id", "=", self.wt1040.id),
Expand All @@ -215,7 +217,7 @@ def test_partial_payment(self):
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, "open")
self.assertEqual(self.invoice.state, "posted")

def test_overlapping_rates(self):
"""Check that overlapping rates cannot be created"""
Expand Down
11 changes: 11 additions & 0 deletions l10n_it_withholding_tax/views/account.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
/>
</xpath>

<xpath
expr="//field[@name='invoice_line_ids']/form//field[@name='tax_ids']"
position="after"
>
<field
name="invoice_line_tax_wt_ids"
widget="many2many_tags"
options="{'no_create': True}"
/>
</xpath>

<xpath expr="//field[@name='narration']" position="before">
<field
name="withholding_tax_line_ids"
Expand Down

0 comments on commit 4e7bd0b

Please sign in to comment.