From f7769624ccb065719ac6cd411619a11985cc09b2 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Thu, 23 Jan 2025 11:43:38 +0100 Subject: [PATCH] tests: check import of cii --- tests/document.py | 143 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 142 insertions(+), 1 deletion(-) diff --git a/tests/document.py b/tests/document.py index 186b112..54c5f78 100644 --- a/tests/document.py +++ b/tests/document.py @@ -5,6 +5,7 @@ import os.path import copy +from lxml import etree from decimal import Decimal from datetime import date from trytond.tests.test_tryton import with_transaction @@ -14,7 +15,7 @@ from trytond.modules.company.tests import create_company, set_company from trytond.modules.account.tests import create_chart, get_fiscalyear from .parsed_data import ( parsed_data_facturx_extended, parsed_data_facturx_basic, - parsed_data_facturx_en16931) + parsed_data_facturx_en16931, parsed_data_ci_invoice) def set_invoice_sequences(fiscalyear): @@ -552,4 +553,144 @@ class DocumentTestCase(object): incoming._readxml_facturx_basic(xml_data) self.assertEqual(incoming.parsed_data, parsed_data_facturx_basic) + @with_transaction() + def test_xmldoc_check_facturx_get_level(self): + """ detect factur-x and 'CrossIndustryInvoice D22' and + invalid files + """ + pool = Pool() + IncDocument = pool.get('document.incoming') + + # factur-x + with open(os.path.join( + os.path.split(__file__)[0], + 'facturx-en16931.xml'), 'rb') as fhdl: + xml_text = fhdl.read() + + xml_data = etree.fromstring(xml_text) + incoming = IncDocument(data=xml_text, name='facturx-en16931.xml') + (fx_flavour, fx_level) = incoming._facturx_get_level(xml_data) + self.assertEqual(fx_flavour, 'factur-x') + self.assertEqual(fx_level, 'en16931') + + # CrossIndustryInvoice D22 + with open(os.path.join( + os.path.split(__file__)[0], + 'crossindustryinvoice-d22.xml'), 'rb') as fhdl: + xml_text = fhdl.read() + + xml_data = etree.fromstring(xml_text) + incoming = IncDocument( + data=xml_text, + name='crossindustryinvoice-d22.xml') + (fx_flavour, fx_level) = incoming._facturx_get_level(xml_data) + self.assertEqual(fx_flavour, None) + self.assertEqual(fx_level, None) + + # invalid xml-data + xml_text = (b'' + + b'') + xml_data = etree.fromstring(xml_text) + incoming = IncDocument(data=xml_text, name='invalid-file.xml') + self.assertRaisesRegex( + Exception, + 'Could not detect if the document is a Factur-X, ' + + 'ZUGFeRD 1.0 or Order-X document.', + incoming._facturx_get_level, + xml_data) + + @with_transaction() + def test_xmldoc_import_crossindustryinvouce_d22(self): + """ create incoming-document, import CII-D22 + """ + pool = Pool() + IncDocument = pool.get('document.incoming') + Configuration = pool.get('document.incoming.configuration') + Party = pool.get('party.party') + IrAttachment = pool.get('ir.attachment') + Invoice = pool.get('account.invoice') + + company = create_company('m-ds') + with set_company(company): + + create_chart(company=company, tax=True) + self.prep_fiscalyear(company) + + product_categories = self.prep_prodcat_category(company) + config = Configuration( + product_category=product_categories) + config.save() + + self.assertEqual(config.create_supplier, True) + self.assertEqual(config.accept_other_company, False) + self.assertEqual(config.number_target, 'reference') + self.assertEqual(len(config.product_category), 3) + self.assertEqual(config.product_category[0].name, 'Accounting') + self.assertEqual(config.product_category[1].name, 'Accounting 19%') + self.assertEqual(config.product_category[2].name, 'Accounting 7%') + + to_create = [] + with open(os.path.join( + os.path.split(__file__)[0], + 'crossindustryinvoice-d22.xml'), 'rb') as fhdl: + to_create.append({ + 'data': fhdl.read(), + 'name': 'crossindustryinvoice-d22.xml', + 'type': 'supplier_invoice'}) + + document, = IncDocument.create(to_create) + self.assertEqual(document.mime_type, 'application/xml') + self.assertEqual(document.company.id, company.id) + self.assertTrue(document.data.startswith( + b'\n' + + b'