Skip to content

Commit 6a7b2ec

Browse files
committed
[FIX] l10n_it_fatturapa_in: Map taxes based on fiscal position
1 parent 9f6c1ad commit 6a7b2ec

File tree

2 files changed

+91
-35
lines changed

2 files changed

+91
-35
lines changed

l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from psycopg2 import IntegrityError
77

8-
from odoo import fields
8+
from odoo import Command, fields
99
from odoo.exceptions import UserError, ValidationError
1010
from odoo.modules import get_module_resource
1111
from odoo.tests import Form
@@ -1209,6 +1209,50 @@ def test_increased_decimal_precision(self):
12091209
],
12101210
)
12111211

1212+
def test_fiscal_position_tax_mapping(self):
1213+
"""Taxes must be mapped
1214+
according to the fiscal position of the imported bill."""
1215+
# Arrange
1216+
file_name = "IT01234567890_FPR03.xml"
1217+
res = self.run_wizard(
1218+
"test_fiscal_position_tax_mapping",
1219+
file_name,
1220+
)
1221+
bill = self.env[res["res_model"]].search(res["domain"])
1222+
tax = bill.invoice_line_ids.tax_ids
1223+
other_tax = tax.copy(
1224+
default={
1225+
"sequence": tax.sequence + 1,
1226+
},
1227+
)
1228+
fiscal_position = self.env["account.fiscal.position"].create(
1229+
{
1230+
"name": "Test fiscal position",
1231+
"tax_ids": [
1232+
Command.create(
1233+
{
1234+
"tax_src_id": tax.id,
1235+
"tax_dest_id": other_tax.id,
1236+
}
1237+
),
1238+
],
1239+
}
1240+
)
1241+
bill.partner_id.property_account_position_id = fiscal_position
1242+
# pre-condition
1243+
self.assertEqual(fiscal_position.map_tax(tax), other_tax)
1244+
1245+
# Act
1246+
res = self.run_wizard(
1247+
"test_fiscal_position_tax_mapped",
1248+
file_name,
1249+
)
1250+
1251+
# Assert
1252+
mapped_bill = self.env[res["res_model"]].search(res["domain"])
1253+
self.assertEqual(mapped_bill.fiscal_position_id, fiscal_position)
1254+
self.assertEqual(mapped_bill.invoice_line_ids.tax_ids, other_tax)
1255+
12121256

12131257
class TestFatturaPAEnasarco(FatturapaCommon):
12141258
def setUp(self):

l10n_it_fatturapa_in/wizard/wizard_import_fatturapa.py

+46-34
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from odoo import api, fields, models
1010
from odoo.exceptions import UserError
11-
from odoo.fields import first
11+
from odoo.fields import Command, first
1212
from odoo.osv import expression
1313
from odoo.tools import float_is_zero, frozendict
1414
from odoo.tools.translate import _
@@ -614,39 +614,51 @@ def get_line_product(self, line, partner):
614614
return product
615615

616616
def adjust_accounting_data(self, product, line_vals):
617-
account = self.get_credit_account(product)
618-
line_vals["account_id"] = account.id
619-
620-
new_tax = None
621-
if len(product.product_tmpl_id.supplier_taxes_id) == 1:
622-
new_tax = product.product_tmpl_id.supplier_taxes_id[0]
623-
elif len(account.tax_ids) == 1:
624-
new_tax = account.tax_ids[0]
625-
line_tax = self.env["account.tax"]
626-
if (
627-
line_vals.get("tax_ids")
628-
and line_vals["tax_ids"][0][0] == fields.Command.SET
629-
):
630-
line_tax_id = line_vals["tax_ids"][0][2][0]
631-
line_tax = self.env["account.tax"].browse(line_tax_id)
632-
if new_tax and line_tax and new_tax != line_tax:
633-
if new_tax._get_tax_amount() != line_tax._get_tax_amount():
634-
self.log_inconsistency(
635-
_(
636-
"XML contains tax %(line_tax)s. "
637-
"Product %(product)s has tax %(new_tax)s. Using "
638-
"the XML one"
617+
line_tax_commands = line_vals.get("tax_ids")
618+
if line_tax_commands and line_tax_commands[0][0] == fields.Command.SET:
619+
line_tax_id = line_tax_commands[0][2][0]
620+
else:
621+
line_tax_id = False
622+
line_tax = self.env["account.tax"].browse(line_tax_id)
623+
624+
if product:
625+
account = self.get_credit_account(product)
626+
line_vals["account_id"] = account.id
627+
628+
new_tax = None
629+
if len(product.product_tmpl_id.supplier_taxes_id) == 1:
630+
new_tax = product.product_tmpl_id.supplier_taxes_id[0]
631+
elif len(account.tax_ids) == 1:
632+
new_tax = account.tax_ids[0]
633+
634+
if new_tax and line_tax and new_tax != line_tax:
635+
if new_tax._get_tax_amount() != line_tax._get_tax_amount():
636+
self.log_inconsistency(
637+
_(
638+
"XML contains tax %(line_tax)s. "
639+
"Product %(product)s has tax %(new_tax)s. Using "
640+
"the XML one"
641+
)
642+
% {
643+
"line_tax": line_tax.name,
644+
"product": product.name,
645+
"new_tax": new_tax.name,
646+
}
639647
)
640-
% {
641-
"line_tax": line_tax.name,
642-
"product": product.name,
643-
"new_tax": new_tax.name,
644-
}
645-
)
646-
else:
647-
# If product has the same amount of the one in XML,
648-
# I use it. Typical case: 22% det 50%
649-
line_vals["tax_ids"] = [(6, 0, [new_tax.id])]
648+
else:
649+
# If product has the same amount of the one in XML,
650+
# I use it. Typical case: 22% det 50%
651+
line_vals["tax_ids"] = [(6, 0, [new_tax.id])]
652+
653+
# Apply fiscal position
654+
if line_tax:
655+
bill = self.env["account.move"].browse(line_vals["move_id"])
656+
fiscal_position = bill.fiscal_position_id
657+
if fiscal_position:
658+
line_tax = fiscal_position.map_tax(line_tax)
659+
line_vals["tax_ids"] = [
660+
Command.set(line_tax.ids),
661+
] + line_tax_commands[1:]
650662

651663
# move_line.tax_ids
652664
# move_line.name
@@ -1725,7 +1737,7 @@ def _set_invoice_lines(
17251737
):
17261738
if product:
17271739
invoice_line_data["product_id"] = product.id
1728-
self.adjust_accounting_data(product, invoice_line_data)
1740+
self.adjust_accounting_data(product, invoice_line_data)
17291741
self.create_and_get_line_id(
17301742
invoice_lines, invoice_line_model, invoice_line_data
17311743
)

0 commit comments

Comments
 (0)