[IMP] account_edi_ubl_cii: add tax prediction if available

The aim of this commit is to leverage the power of the tax prediction
when possible to improve the quality of the tax retrieval of the edi
import.

Context:
The tax prediction happens when the invoice is given manually or with
the OCR but doesn't happen when it's imported by EDI because the tax is
already set by the edi.

Before this commit:
During EDI import, the tax retrieved could always be the same even if
the user make some modification to the previous bill.

After this commit:
The tax retrieved leverage the previous data to get better quality
result regarding tax retrieval.

closes odoo/odoo#145572

Task-id: 3626501
Ticket-id: 3531473
X-original-commit: 62bd397490cfcb21c2c92d67d761f68850054039
Related: odoo/enterprise#52430
Signed-off-by: William André (wan) <wan@odoo.com>
Signed-off-by: Brice Bartoletti (bib) <bib@odoo.com>
This commit is contained in:
Brice bib Bartoletti
2023-12-08 17:19:15 +00:00
parent 5fb51a7825
commit 1650252f22
3 changed files with 207 additions and 8 deletions
@@ -661,15 +661,24 @@ class AccountEdiCommon(models.AbstractModel):
('type_tax_use', '=', invoice_line.move_id.journal_id.type),
('amount', '=', amount),
]
tax_excl = self.env['account.tax'].search(domain + [('price_include', '=', False)], limit=1)
tax_incl = self.env['account.tax'].search(domain + [('price_include', '=', True)], limit=1)
if tax_excl:
inv_line_vals['taxes'].append(tax_excl.id)
elif tax_incl:
inv_line_vals['taxes'].append(tax_incl.id)
inv_line_vals['price_unit'] *= (1 + tax_incl.amount / 100)
else:
tax = False
if hasattr(invoice_line, '_predict_specific_tax'):
# company check is already done in the prediction query
predicted_tax_id = invoice_line\
._predict_specific_tax('percent', amount, invoice_line.move_id.journal_id.type)
tax = self.env['account.tax'].browse(predicted_tax_id)
if not tax:
tax = self.env['account.tax'].search(domain + [('price_include', '=', False)], limit=1)
if not tax:
tax = self.env['account.tax'].search(domain + [('price_include', '=', True)], limit=1)
if not tax:
logs.append(_("Could not retrieve the tax: %s %% for line '%s'.", amount, invoice_line.name))
else:
inv_line_vals['taxes'].append(tax.id)
if tax.price_include:
inv_line_vals['price_unit'] *= (1 + tax.amount / 100)
# Handle Fixed Taxes
for fixed_tax_vals in inv_line_vals['fixed_taxes_list']:
@@ -0,0 +1,136 @@
<?xml version='1.0' encoding='UTF-8'?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
<cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0</cbc:CustomizationID>
<cbc:ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
<cbc:ID>FAC/2023/00052</cbc:ID>
<cbc:IssueDate>2023-08-04</cbc:IssueDate>
<cbc:DueDate>2023-09-04</cbc:DueDate>
<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>EUR</cbc:DocumentCurrencyCode>
<cac:OrderReference>
<cbc:ID>FAC/2023/00052</cbc:ID>
<cbc:SalesOrderID>S00012</cbc:SalesOrderID>
</cac:OrderReference>
<cac:AdditionalDocumentReference>
<cbc:ID>FAC_2023_00052.pdf</cbc:ID>
<cac:Attachment>
</cac:Attachment>
</cac:AdditionalDocumentReference>
<cac:AccountingSupplierParty>
<cac:Party>
<cbc:EndpointID schemeID="9938">LU25587702</cbc:EndpointID>
<cac:PartyName>
<cbc:Name>ALD Automotive LU</cbc:Name>
</cac:PartyName>
<cac:PostalAddress>
<cbc:StreetName>270 rte d'Arlon</cbc:StreetName>
<cbc:CityName>Strassen</cbc:CityName>
<cbc:PostalZone>8010</cbc:PostalZone>
<cac:Country>
<cbc:IdentificationCode>LU</cbc:IdentificationCode>
</cac:Country>
</cac:PostalAddress>
<cac:PartyTaxScheme>
<cbc:CompanyID>LU12977109</cbc:CompanyID>
<cac:TaxScheme>
<cbc:ID>VAT</cbc:ID>
</cac:TaxScheme>
</cac:PartyTaxScheme>
<cac:PartyLegalEntity>
<cbc:RegistrationName>ALD Automotive LU</cbc:RegistrationName>
<cbc:CompanyID>LU12977109</cbc:CompanyID>
</cac:PartyLegalEntity>
<cac:Contact>
<cbc:Name>ALD Automotive LU</cbc:Name>
<cbc:ElectronicMail>adl@test.com</cbc:ElectronicMail>
</cac:Contact>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cac:Party>
<cbc:EndpointID schemeID="9938">LU25587702</cbc:EndpointID>
<cac:PartyName>
<cbc:Name>Odoo Lu</cbc:Name>
</cac:PartyName>
<cac:PostalAddress>
<cbc:StreetName>Rue de l'industrie 13</cbc:StreetName>
<cbc:CityName>Windhof</cbc:CityName>
<cac:Country>
<cbc:IdentificationCode>LU</cbc:IdentificationCode>
</cac:Country>
</cac:PostalAddress>
<cac:PartyTaxScheme>
<cbc:CompanyID>LU25587702</cbc:CompanyID>
<cac:TaxScheme>
<cbc:ID>VAT</cbc:ID>
</cac:TaxScheme>
</cac:PartyTaxScheme>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Odoo Lu</cbc:RegistrationName>
<cbc:CompanyID>LU25587702</cbc:CompanyID>
</cac:PartyLegalEntity>
<cac:Contact>
<cbc:Name>Odoo Lu</cbc:Name>
<cbc:ElectronicMail>odoo@test.com</cbc:ElectronicMail>
</cac:Contact>
</cac:Party>
</cac:AccountingCustomerParty>
<cac:Delivery>
<cac:DeliveryLocation>
<cac:Address>
<cbc:StreetName>Rue de l'industrie 13</cbc:StreetName>
<cbc:CityName>Windhof</cbc:CityName>
<cac:Country>
<cbc:IdentificationCode>LU</cbc:IdentificationCode>
</cac:Country>
</cac:Address>
</cac:DeliveryLocation>
</cac:Delivery>
<cac:PaymentMeans>
<cbc:PaymentMeansCode name="credit transfer">30</cbc:PaymentMeansCode>
<cbc:PaymentID>FAC/2023/00052</cbc:PaymentID>
<cac:PayeeFinancialAccount>
<cbc:ID>LU071241358706500000</cbc:ID>
</cac:PayeeFinancialAccount>
</cac:PaymentMeans>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="EUR">105.12</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxableAmount currencyID="EUR">657.00</cbc:TaxableAmount>
<cbc:TaxAmount currencyID="EUR">105.12</cbc:TaxAmount>
<cac:TaxCategory>
<cbc:ID>S</cbc:ID>
<cbc:Percent>16.0</cbc:Percent>
<cac:TaxScheme>
<cbc:ID>VAT</cbc:ID>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:LegalMonetaryTotal>
<cbc:LineExtensionAmount currencyID="EUR">657.00</cbc:LineExtensionAmount>
<cbc:TaxExclusiveAmount currencyID="EUR">657.00</cbc:TaxExclusiveAmount>
<cbc:TaxInclusiveAmount currencyID="EUR">762.12</cbc:TaxInclusiveAmount>
<cbc:PrepaidAmount currencyID="EUR">0.00</cbc:PrepaidAmount>
<cbc:PayableAmount currencyID="EUR">762.12</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
<cac:InvoiceLine>
<cbc:ID>1</cbc:ID>
<cbc:InvoicedQuantity unitCode="C62">1.0</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="EUR">657.00</cbc:LineExtensionAmount>
<cac:Item>
<cbc:Description>Locations et leasing opérationnel - Véhicule HG6542</cbc:Description>
<cbc:Name>Locations et leasing opérationnel</cbc:Name>
<cac:ClassifiedTaxCategory>
<cbc:ID>S</cbc:ID>
<cbc:Percent>16.0</cbc:Percent>
<cac:TaxScheme>
<cbc:ID>VAT</cbc:ID>
</cac:TaxScheme>
</cac:ClassifiedTaxCategory>
</cac:Item>
<cac:Price>
<cbc:PriceAmount currencyID="EUR">657.00</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
</Invoice>
@@ -5,6 +5,7 @@ from lxml import etree
from odoo import Command
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.tests import tagged
from odoo.tools import file_open
@tagged('post_install', '-at_install')
@@ -94,3 +95,56 @@ class TestAccountEdiUblCii(AccountTestInvoicingCommon):
new_invoice = invoice.journal_id._create_document_from_attachment(facturx_attachment.ids)
self.assertRecordValues(new_invoice.invoice_line_ids, line_vals)
def test_import_tax_prediction(self):
""" We are going to create 2 tax and import the e-invoice twice.
On the first attempt, as there isn't any data to leverage, the classic 'search' will be called and we expect
the first tax created to be the selected one as the retrieval order is `sequence, id`.
We will set the second tax on the bill and post it which make it the most probable one.
On the second attempt, we expect that second tax to be retrieved.
"""
if not hasattr(self.env["account.move.line"], '_predict_specific_tax'):
self.skipTest("The predictive bill module isn't install and thus prediction with edi can't be tested.")
# create 2 new taxes for the test seperatly to ensure the first gets the smaller id
new_tax_1 = self.env["account.tax"].create({
'name': 'tax with lower id could be retrieved first',
'amount_type': 'percent',
'type_tax_use': 'purchase',
'amount': 16.0,
})
new_tax_2 = self.env["account.tax"].create({
'name': 'tax with higher id could be retrieved second',
'amount_type': 'percent',
'type_tax_use': 'purchase',
'amount': 16.0,
})
file_path = "bis3_bill_example.xml"
file_path = f"{self.test_module}/tests/test_files/{file_path}"
with file_open(file_path, 'rb') as file:
xml_attachment = self.env['ir.attachment'].create({
'mimetype': 'application/xml',
'name': 'test_invoice.xml',
'raw': file.read(),
})
# Import the document for the first time
bill = self.env['account.journal']\
.with_context(default_journal_id=self.company_data["default_journal_purchase"].id)\
._create_document_from_attachment(xml_attachment.id)
# Ensure the first tax is retrieved as there isn't any prediction that could be leverage
self.assertEqual(bill.invoice_line_ids.tax_ids, new_tax_1)
# Set the second tax on the line to make it the most probable one
bill.invoice_line_ids.tax_ids = new_tax_2
bill.action_post()
# Import the bill again and ensure the prediction did his work
bill = self.env['account.journal']\
.with_context(default_journal_id=self.company_data["default_journal_purchase"].id)\
._create_document_from_attachment(xml_attachment.id)
self.assertEqual(bill.invoice_line_ids.tax_ids, new_tax_2)