Skip to content

Commit

Permalink
[FIX] l10n_it_reverse_charge: Find only one mapped tax
Browse files Browse the repository at this point in the history
  • Loading branch information
SirTakobi authored and eLBati committed Jul 17, 2022
1 parent f0a77a8 commit a00977c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 22 deletions.
36 changes: 16 additions & 20 deletions l10n_it_reverse_charge/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2017 Alex Comba - Agile Business Group
# Copyright 2017 Lorenzo Battistini - Agile Business Group
# Copyright 2017 Marco Calcagni - Dinamiche Aziendali srl
# Copyright 2022 Simone Rubino - TAKOBI

from odoo import api, fields, models
from odoo.exceptions import Warning as UserError
Expand Down Expand Up @@ -348,17 +349,14 @@ def generate_self_invoice(self):
raise UserError(_(
"Invoice %s, line\n%s\nis RC but has not tax"
) % ((self.reference or self.partner_id.display_name), line.name))
tax_ids = list()
for tax_mapping in rc_type.tax_ids:
for line_tax_id in line_tax_ids:
if tax_mapping.purchase_tax_id == line_tax_id:
tax_ids.append(tax_mapping.sale_tax_id.id)
if not tax_ids:
raise UserError(_("Tax code used is not a RC tax.\nCan't "
"find tax mapping"))
if line_tax_ids:
mapped_taxes = rc_type.map_tax(
line_tax_ids,
'purchase_tax_id',
'sale_tax_id',
)
if line_tax_ids and mapped_taxes:
rc_invoice_line['invoice_line_tax_ids'] = [
(6, False, tax_ids)]
(6, False, mapped_taxes.ids)]
rc_invoice_line[
'account_id'] = rc_type.transitory_account_id.id
rc_invoice_lines.append([0, False, rc_invoice_line])
Expand Down Expand Up @@ -408,17 +406,15 @@ def generate_supplier_self_invoice(self):
supplier_invoice.journal_id = rc_type.supplier_journal_id.id
for inv_line in supplier_invoice.invoice_line_ids:
line_tax_ids = inv_line.invoice_line_tax_ids
tax_ids = list()
for tax_mapping in rc_type.tax_ids:
for line_tax_id in line_tax_ids:
if tax_mapping.original_purchase_tax_id == line_tax_id:
tax_ids.append(tax_mapping.purchase_tax_id.id)
if not tax_ids:
raise UserError(_("Tax code used is not a RC tax.\nCan't "
"find tax mapping"))
if line_tax_ids:
mapped_taxes = rc_type.map_tax(
line_tax_ids,
'original_purchase_tax_id',
'purchase_tax_id',
)
if line_tax_ids and mapped_taxes:
inv_line.invoice_line_tax_ids = [
(6, False, tax_ids)]
(6, False, mapped_taxes.ids),
]

inv_line.account_id = rc_type.transitory_account_id.id
self.rc_self_purchase_invoice_id = supplier_invoice.id
Expand Down
34 changes: 33 additions & 1 deletion l10n_it_reverse_charge/models/account_rc_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright 2022 Simone Rubino - TAKOBI

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError, ValidationError


class AccountRCTypeTax(models.Model):
Expand Down Expand Up @@ -124,3 +124,35 @@ def _constrain_with_supplier_self_invoice(self):
if with_supplier_types:
with_supplier_types.mapped('tax_ids') \
._constrain_supplier_self_invoice_mapping()

def map_tax(self, taxes, key_tax_field, value_tax_field):
"""
Map each tax in `taxes`, based on the mapping defined by `self.tax_ids`.
Raise an exception if a mapping is not found for some of `taxes`.
:param key_tax_field: Field of the mapping lines
to be used as key for searching the tax
:param value_tax_field: Field of the mapping lines
to be used as value for mapping the tax
:param taxes: Taxes to be mapped
"""
self.ensure_one()
mapped_taxes = self.env['account.tax'].browse()
for tax in taxes:
for tax_mapping in self.tax_ids:
if tax_mapping[key_tax_field] == tax:
mapped_taxes |= tax_mapping[value_tax_field]
break
else:
# Tax not found in mapping
raise UserError(
_("Can't find tax mapping for {tax_name} "
"in Reverse Charge Type {rc_type_name}, "
"please check the configuration.")
.format(
tax_name=tax.display_name,
rc_type_name=self.display_name,
)
)
return mapped_taxes
47 changes: 46 additions & 1 deletion l10n_it_reverse_charge/tests/test_account_rc_type.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2022 Simone Rubino - TAKOBI
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.exceptions import ValidationError
from odoo.exceptions import UserError, ValidationError
from odoo.fields import first

from .rc_common import ReverseChargeCommon
Expand All @@ -26,3 +26,48 @@ def test_with_supplier_self_invoice(self):

self.assertIn("Original Purchase Tax is required", exc_message)
self.assertIn(rc_type.display_name, exc_message)

def test_tax_map_find(self):
"""
Check that Reverse Charge Types can map
Original Purchase Tax to Purchase Tax.
"""
rc_type = self.rc_type_eeu
rc_type_mapping = first(rc_type.tax_ids)
key_tax = rc_type_mapping.original_purchase_tax_id

mapped_tax = rc_type.map_tax(
key_tax,
'original_purchase_tax_id',
'purchase_tax_id',
)

value_tax = rc_type_mapping.purchase_tax_id
self.assertEqual(value_tax, mapped_tax)

def test_tax_map_not_found(self):
"""
Check that Reverse Charge Types
raise an Error when can't map a tax.
"""
rc_type = self.rc_type_eeu
key_taxes = rc_type.tax_ids.mapped('original_purchase_tax_id')
other_tax = self.env['account.tax'].search(
[
('id', 'not in', key_taxes.ids),
],
limit=1,
)
self.assertTrue(other_tax)

with self.assertRaises(UserError) as ue:
rc_type.map_tax(
other_tax,
'original_purchase_tax_id',
'purchase_tax_id',
)
exc_message = ue.exception.args[0]

self.assertIn("Can't find tax mapping", exc_message)
self.assertIn(rc_type.display_name, exc_message)
self.assertIn(other_tax.display_name, exc_message)

0 comments on commit a00977c

Please sign in to comment.