Skip to content

Commit

Permalink
[FIX] l10n_it_fatturapa_in: Map taxes based on fiscal position
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAionTech committed Nov 28, 2024
1 parent a149fa6 commit a79f614
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 35 deletions.
46 changes: 45 additions & 1 deletion l10n_it_fatturapa_in/tests/test_import_fatturapa_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from psycopg2 import IntegrityError

from odoo import fields
from odoo import Command, fields
from odoo.exceptions import UserError, ValidationError
from odoo.modules import get_module_resource
from odoo.tests import Form
Expand Down Expand Up @@ -1150,6 +1150,50 @@ def test_ignore_global_discount(self):
self.assertEqual(invoice.amount_tax, 5.12)
self.assertEqual(invoice.amount_total, 28.39)

def test_fiscal_position_tax_mapping(self):
"""Taxes must be mapped
according to the fiscal position of the imported bill."""
# Arrange
file_name = "IT01234567890_FPR03.xml"
res = self.run_wizard(
"test_fiscal_position_tax_mapping",
file_name,
)
bill = self.env[res["res_model"]].search(res["domain"])
tax = bill.invoice_line_ids.tax_ids
other_tax = tax.copy(
default={
"sequence": tax.sequence + 1,
},
)
fiscal_position = self.env["account.fiscal.position"].create(
{
"name": "Test fiscal position",
"tax_ids": [
Command.create(
{
"tax_src_id": tax.id,
"tax_dest_id": other_tax.id,
}
),
],
}
)
bill.partner_id.property_account_position_id = fiscal_position
# pre-condition
self.assertEqual(fiscal_position.map_tax(tax), other_tax)

# Act
res = self.run_wizard(
"test_fiscal_position_tax_mapped",
file_name,
)

# Assert
mapped_bill = self.env[res["res_model"]].search(res["domain"])
self.assertEqual(mapped_bill.fiscal_position_id, fiscal_position)
self.assertEqual(mapped_bill.invoice_line_ids.tax_ids, other_tax)


class TestFatturaPAEnasarco(FatturapaCommon):
def setUp(self):
Expand Down
79 changes: 45 additions & 34 deletions l10n_it_fatturapa_in/wizard/wizard_import_fatturapa.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from odoo import api, fields, models, registry
from odoo.exceptions import UserError
from odoo.fields import first
from odoo.fields import Command, first
from odoo.osv import expression
from odoo.tools import float_is_zero, frozendict
from odoo.tools.translate import _
Expand Down Expand Up @@ -614,39 +614,50 @@ def get_line_product(self, line, partner):
return product

def adjust_accounting_data(self, product, line_vals):
account = self.get_credit_account(product)
line_vals["account_id"] = account.id

new_tax = None
if len(product.product_tmpl_id.supplier_taxes_id) == 1:
new_tax = product.product_tmpl_id.supplier_taxes_id[0]
elif len(account.tax_ids) == 1:
new_tax = account.tax_ids[0]
line_tax = self.env["account.tax"]
if (
line_vals.get("tax_ids")
and line_vals["tax_ids"][0][0] == fields.Command.SET
):
line_tax_id = line_vals["tax_ids"][0][2][0]
line_tax = self.env["account.tax"].browse(line_tax_id)
if new_tax and line_tax and new_tax != line_tax:
if new_tax._get_tax_amount() != line_tax._get_tax_amount():
self.log_inconsistency(
_(
"XML contains tax %(line_tax)s. "
"Product %(product)s has tax %(new_tax)s. Using "
"the XML one"
line_tax_commands = line_vals.get("tax_ids")
if line_tax_commands and line_tax_commands[0][0] == fields.Command.SET:
line_tax_id = line_tax_commands[0][2][0]
else:
line_tax_id = False
line_tax = self.env["account.tax"].browse(line_tax_id)

if product:
account = self.get_credit_account(product)
line_vals["account_id"] = account.id

new_tax = None
if len(product.product_tmpl_id.supplier_taxes_id) == 1:
new_tax = product.product_tmpl_id.supplier_taxes_id[0]
elif len(account.tax_ids) == 1:
new_tax = account.tax_ids[0]

if new_tax and line_tax and new_tax != line_tax:
if new_tax._get_tax_amount() != line_tax._get_tax_amount():
self.log_inconsistency(
_(
"XML contains tax %(line_tax)s. "
"Product %(product)s has tax %(new_tax)s. Using "
"the XML one"
)
% {
"line_tax": line_tax.name,
"product": product.name,
"new_tax": new_tax.name,
}
)
% {
"line_tax": line_tax.name,
"product": product.name,
"new_tax": new_tax.name,
}
)
else:
# If product has the same amount of the one in XML,
# I use it. Typical case: 22% det 50%
line_vals["tax_ids"] = [(6, 0, [new_tax.id])]
else:
# If product has the same amount of the one in XML,
# I use it. Typical case: 22% det 50%
line_vals["tax_ids"] = [(6, 0, [new_tax.id])]

# Apply fiscal position
bill = self.env["account.move"].browse(line_vals["move_id"])
fiscal_position = bill.fiscal_position_id
if fiscal_position:
line_tax = fiscal_position.map_tax(line_tax)
line_vals["tax_ids"] = [
Command.set(line_tax.ids),
] + line_tax_commands[1:]

# move_line.tax_ids
# move_line.name
Expand Down Expand Up @@ -1725,7 +1736,7 @@ def _set_invoice_lines(
):
if product:
invoice_line_data["product_id"] = product.id
self.adjust_accounting_data(product, invoice_line_data)
self.adjust_accounting_data(product, invoice_line_data)
self.create_and_get_line_id(
invoice_lines, invoice_line_model, invoice_line_data
)
Expand Down

0 comments on commit a79f614

Please sign in to comment.