tests: check import of cii

This commit is contained in:
Frederik Jaeckel 2025-01-23 11:43:38 +01:00
parent 48385f9250
commit f7769624cc

View file

@ -5,6 +5,7 @@
import os.path import os.path
import copy import copy
from lxml import etree
from decimal import Decimal from decimal import Decimal
from datetime import date from datetime import date
from trytond.tests.test_tryton import with_transaction 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 trytond.modules.account.tests import create_chart, get_fiscalyear
from .parsed_data import ( from .parsed_data import (
parsed_data_facturx_extended, parsed_data_facturx_basic, 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): def set_invoice_sequences(fiscalyear):
@ -552,4 +553,144 @@ class DocumentTestCase(object):
incoming._readxml_facturx_basic(xml_data) incoming._readxml_facturx_basic(xml_data)
self.assertEqual(incoming.parsed_data, parsed_data_facturx_basic) 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'<?xml version="1.0" encoding="UTF-8"?>' +
b'<notvalid></notvalid>')
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'<?xml version="1.0" encoding="UTF-8"?>\n' +
b'<rsm:CrossIndustryInvoice'))
# no supplier-party in db
self.assertEqual(
Party.search_count([('name', '=', 'Name of the Supplier')]),
0)
# no invoices
self.assertEqual(Invoice.search_count([]), 0)
config.create_supplier = True
config.accept_other_company = True
config.number_target = 'reference'
config.save()
# 'process' will queue the job to workers
IncDocument.process([document])
# run the usual call: process workers
self.prep_incomingdoc_run_worker()
# check imported invoice
invoice, = Invoice.search([])
self.assertEqual(invoice.type, 'in')
self.assertEqual(invoice.reference, 'RE2024.01234')
self.assertEqual(invoice.number, None)
self.assertEqual(invoice.invoice_date, date(2024, 6, 17))
self.assertEqual(invoice.currency.name, 'usd')
self.assertEqual(invoice.company.rec_name, 'm-ds')
self.assertEqual(invoice.description, 'Description of invoice')
self.assertEqual(
invoice.comment,
'Code=1, Some notes to the customer.\n' +
'Code=22, Subject=42, Goes to field comment.')
self.assertEqual(invoice.party.rec_name, 'Name of the Supplier')
self.assertEqual(invoice.account.rec_name, 'Main Payable')
self.assertEqual(document.result.__name__, 'account.invoice')
self.assertEqual(document.result.reference, 'RE2024.01234')
parsed_ori = copy.deepcopy(parsed_data_ci_invoice)
parsed_ori['seller_party']['party'] = invoice.party.id
self.assertEqual(
self.prep_sorted_dict(document.parsed_data),
self.prep_sorted_dict(parsed_ori))
attachment, = IrAttachment.search([
('resource.party', '=', invoice.party, 'account.invoice'),
('type', '=', 'data')])
self.assertEqual(attachment.data, document.data)
self.assertEqual(attachment.name, 'crossindustryinvoice-d22.xml')
# end DocumentTestCase # end DocumentTestCase