From 46a8cda75f110c9fa3bdccc5322c1a2aa9f3a466 Mon Sep 17 00:00:00 2001 From: Sven Fuehr Date: Thu, 4 Jan 2024 08:43:18 +0100 Subject: [PATCH] [FIX] l10n_es_edi_tbai: fix unit price, discount in multicurrency export MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently there are the following bugs when XML-exporting invoices that are not in company currency and use discounts: * The discount is computed and exported in document currency. But it should be exported in company currency. * The unit price value (excl. discount) is computed wrongly since it uses the wrong discount value. This commit corrects the discount and unit price computation / export. closes odoo/odoo#148566 X-original-commit: 1cc846e8a53fd7c4298b7324d9c4f272592f0497 Signed-off-by: William André (wan) Signed-off-by: Sven Führ (svfu) --- .../models/account_edi_format.py | 7 +- addons/l10n_es_edi_tbai/tests/test_edi_xml.py | 75 +++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/addons/l10n_es_edi_tbai/models/account_edi_format.py b/addons/l10n_es_edi_tbai/models/account_edi_format.py index 05a6c28790b..01f6ba964a9 100644 --- a/addons/l10n_es_edi_tbai/models/account_edi_format.py +++ b/addons/l10n_es_edi_tbai/models/account_edi_format.py @@ -330,10 +330,11 @@ class AccountEdiFormat(models.Model): invoice_lines = [] for line in invoice.invoice_line_ids.filtered(lambda line: line.display_type not in ('line_section', 'line_note')): if line.discount == 100.0: - gross_price_subtotal = line.price_unit * line.quantity + inverse_currency_rate = abs(line.move_id.amount_total_signed / line.move_id.amount_total) if line.move_id.amount_total else 1 + balance_before_discount = - line.price_unit * line.quantity * inverse_currency_rate else: - gross_price_subtotal = line.price_subtotal / (1 - line.discount / 100.0) - discount = (gross_price_subtotal - line.price_subtotal) * refund_sign + balance_before_discount = line.balance / (1 - line.discount / 100) + discount = (balance_before_discount - line.balance) if not any([t.l10n_es_type == 'sujeto_isp' for t in line.tax_ids]): total = line.price_total * abs(line.balance / line.amount_currency if line.amount_currency != 0 else 1) * -refund_sign diff --git a/addons/l10n_es_edi_tbai/tests/test_edi_xml.py b/addons/l10n_es_edi_tbai/tests/test_edi_xml.py index df7eb041806..d2128a64b76 100644 --- a/addons/l10n_es_edi_tbai/tests/test_edi_xml.py +++ b/addons/l10n_es_edi_tbai/tests/test_edi_xml.py @@ -43,6 +43,81 @@ class TestEdiTbaiXmls(TestEsEdiTbaiCommon): xml_expected = etree.fromstring(super().L10N_ES_TBAI_SAMPLE_XML_POST) self.assertXmlTreeEqual(xml_doc, xml_expected) + def test_xml_tree_post_multicurrency(self): + """Test of Customer Invoice XML. The invoice is not in company currency and has a line with a 100% discount""" + + currency_usd = self.env.ref('base.USD') + currency_usd.active = True + date = str(self.out_invoice.invoice_date) + self.env['res.currency.rate'].create({ + 'name': date, + 'company_id': self.company_data['company'].id, + 'currency_id': currency_usd.id, + 'rate': 0.5}) + invoice = self.env['account.move'].create({ + 'name': 'INV/01', + 'move_type': 'out_invoice', + 'invoice_date': date, + 'partner_id': self.partner_a.id, + 'currency_id': currency_usd.id, + 'invoice_line_ids': [ + (0, 0, { + 'product_id': self.product_a.id, + 'price_unit': 123.00, + 'quantity': 5, + 'discount': 20.0, + 'tax_ids': [(6, 0, self._get_tax_by_xml_id('s_iva21b').ids)], + }), + (0, 0, { + 'product_id': self.product_a.id, + 'price_unit': 123.00, + 'quantity': 5, + 'discount': 100.0, + 'tax_ids': [(6, 0, self._get_tax_by_xml_id('s_iva21b').ids)], + }), + ], + }) + + with freeze_time(self.frozen_today): + xml_doc = self.edi_format._get_l10n_es_tbai_invoice_xml(invoice, cancel=False)[invoice]['xml_file'] + xml_doc.remove(xml_doc.find("Signature", namespaces=NS_MAP)) + xml_expected_base = etree.fromstring(super().L10N_ES_TBAI_SAMPLE_XML_POST) + xpath = """ + + + + producta + 5.00 + 246.00 + 246.00 + 1190.64 + + + producta + 5.00 + 246.00 + 1230.00 + 0.00 + + + + + 1190.64 + + + + + 984.00 + 21.00 + 206.64 + N + + + + """ + xml_expected = self.with_applied_xpath(xml_expected_base, xpath) + self.assertXmlTreeEqual(xml_doc, xml_expected) + def test_xml_tree_in_post(self): """Test XML of vendor bill for LROE Batuz""" with freeze_time(self.frozen_today):