Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Posizione fiscale non sostituisce le imposte durante l'importazione della fattura elettronica #4465

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1209,6 +1209,50 @@ def test_increased_decimal_precision(self):
],
)

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
80 changes: 46 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
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,51 @@
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]

Check warning on line 632 in l10n_it_fatturapa_in/wizard/wizard_import_fatturapa.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_fatturapa_in/wizard/wizard_import_fatturapa.py#L632

Added line #L632 was not covered by tests

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])]

Check warning on line 651 in l10n_it_fatturapa_in/wizard/wizard_import_fatturapa.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_fatturapa_in/wizard/wizard_import_fatturapa.py#L651

Added line #L651 was not covered by tests

# Apply fiscal position
if line_tax:
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 +1737,7 @@
):
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
Loading