From 214cbb086f0ed275249a214b625eaa33d2c46fdd Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Tue, 10 Jun 2025 13:45:07 +0200 Subject: [PATCH] get amount on line as net-value (even if invoice is gross-mode) --- mixin.py | 36 ++++++++++++++ template/Factur-X-1.07.2-extended/invoice.xml | 2 +- tests/test_edocument.py | 47 ++++++++++++++++++- tryton.cfg | 3 ++ 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/mixin.py b/mixin.py index c854b7d..f7cda1c 100644 --- a/mixin.py +++ b/mixin.py @@ -10,6 +10,7 @@ from trytond.exceptions import UserError from trytond.i18n import gettext from trytond.tools import cached_property from trytond.pool import Pool +from trytond.transaction import Transaction from trytond.modules.product import round_price @@ -160,6 +161,41 @@ class EdocumentMixin(object): taxname=tax.rec_name)) return tax.unece_code + def get_line_amount(self, line): + """ get amount of current invoice-line, + depends on modegross of invoice, set used-modegross to 'net' + + Args: + line (record): model account.invoice.line + """ + if line.modegross == 'net': + return line.amount + elif line.modegross == 'gross': + # get net-amount + # copy from account_invoice/invoice.py:2416-2434 + currency = ( + line.invoice.currency + if line.invoice else line.currency) + + amount = (Decimal(str(line.quantity or 0)) * ( + line.unit_price or Decimal(0))) + invoice_type = ( + line.invoice.type + if line.invoice else line.invoice_type) + + if (invoice_type == 'in' + and line.taxes_deductible_rate is not None + and line.taxes_deductible_rate != 1): + with Transaction().set_context(_deductible_rate=1): + tax_amount = sum( + t['amount'] for t in line._get_taxes().values()) + non_deductible_amount = ( + tax_amount * (1 - line.taxes_deductible_rate)) + amount += non_deductible_amount + if currency: + return currency.round(amount) + return amount + def get_tax_unece_code(self, tax): while tax: if tax.unece_code: diff --git a/template/Factur-X-1.07.2-extended/invoice.xml b/template/Factur-X-1.07.2-extended/invoice.xml index db85eb1..bad23a9 100644 --- a/template/Factur-X-1.07.2-extended/invoice.xml +++ b/template/Factur-X-1.07.2-extended/invoice.xml @@ -85,7 +85,7 @@ this repository contains the full copyright notices and license terms. --> ${TradeTax(this.invoice_line_tax(line))} - ${line.amount} + ${this.get_line_amount(line)} diff --git a/tests/test_edocument.py b/tests/test_edocument.py index 5ba67ac..02669f9 100644 --- a/tests/test_edocument.py +++ b/tests/test_edocument.py @@ -7,7 +7,8 @@ from lxml import etree import os from decimal import Decimal from datetime import date -from trytond.tests.test_tryton import ModuleTestCase, with_transaction +from trytond.tests.test_tryton import ( + ModuleTestCase, with_transaction, activate_module) from trytond.pool import Pool from trytond.modules.company.tests import create_company, set_company from trytond.modules.account.tests import create_chart, get_fiscalyear @@ -42,6 +43,14 @@ class EdocTestCase(ModuleTestCase): 'Test e-rechnung module' module = 'edocument_xrechnung' + @classmethod + def setUpClass(cls): + super().setUpClass() + activate_module([ + 'edocument_uncefact', 'party', 'bank', + 'account_invoice', 'sale_point_invoice', + 'product_grossprice'], 'en') + def prep_fiscalyear(self, company1): """ prepare fiscal year, sequences... """ @@ -84,7 +93,7 @@ class EdocTestCase(ModuleTestCase): 'number': 'DE02300209000106531065'}])]}]) return company - def prep_invoice(self, credit_note=False): + def prep_invoice(self, credit_note=False, modegross='net'): """ add invoice """ pool = Pool() @@ -130,6 +139,7 @@ class EdocTestCase(ModuleTestCase): to_create_invoice = [{ 'type': 'out', + 'modegross': modegross, 'description': 'description of invoice', 'comment': 'note line 1\nnote line 2', 'invoice_date': date(2024, 7, 1), @@ -149,6 +159,11 @@ class EdocTestCase(ModuleTestCase): 'currency': currency1.id, }])], }] + + if modegross == 'gross': + to_create_invoice[0]['lines'][0][1][0]['unit_gross_price'] = ( + Decimal('50.0') * Decimal('1.2')) + inv_lst, = Invoice.create(to_create_invoice) inv_lst.on_change_lines() inv_lst.save() @@ -233,6 +248,34 @@ class EdocTestCase(ModuleTestCase): {'identifiers': [('delete', [party.identifiers[0].id])]}] ) + @with_transaction() + def test_xrechn_export_facturx_gross(self): + """ run export - factur-x, modegross='gross' + """ + pool = Pool() + Template = pool.get('edocument.facturxext.invoice') + + company = self.prep_company() + with set_company(company): + create_chart(company=company, tax=True) + self.prep_fiscalyear(company) + invoice = self.prep_invoice(modegross='gross') + + template = Template(invoice) + + schema_file = os.path.join( + os.path.dirname(__file__), + 'Factur-X_1.07.2_EXTENDED', + 'Factur-X_1.07.2_EXTENDED.xsd') + + invoice_string = template.render('Factur-X-1.07.2-extended') + with open('gross_invoice_string.xml', 'wb') as fhdl: + fhdl.write(invoice_string) + + invoice_xml = etree.fromstring(invoice_string) + schema = etree.XMLSchema(etree.parse(schema_file)) + schema.assertValid(invoice_xml) + @with_transaction() def test_xrechn_export_facturx(self): """ run export - factur-x diff --git a/tryton.cfg b/tryton.cfg index 509c618..ecd8916 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -5,6 +5,9 @@ depends: party bank account_invoice +extras_depend: + sale_point_invoice + product_grossprice xml: message.xml configuration.xml