696 lines
28 KiB
Python
696 lines
28 KiB
Python
# -*- coding: utf-8 -*-
|
|
# This file is part of the document-incoming-invoice-xml-module
|
|
# from m-ds for Tryton. The COPYRIGHT file at the top level of
|
|
# this repository contains the full copyright notices and license terms.
|
|
|
|
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
|
|
from trytond.pool import Pool
|
|
from trytond.exceptions import UserError
|
|
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_ci_invoice)
|
|
|
|
|
|
def set_invoice_sequences(fiscalyear):
|
|
pool = Pool()
|
|
Sequence = pool.get('ir.sequence.strict')
|
|
SequenceType = pool.get('ir.sequence.type')
|
|
InvoiceSequence = pool.get('account.fiscalyear.invoice_sequence')
|
|
ModelData = pool.get('ir.model.data')
|
|
|
|
sequence = Sequence(
|
|
name=fiscalyear.name,
|
|
sequence_type=SequenceType(ModelData.get_id(
|
|
'account_invoice', 'sequence_type_account_invoice')),
|
|
company=fiscalyear.company,
|
|
)
|
|
sequence.save()
|
|
fiscalyear.invoice_sequences = []
|
|
invoice_sequence = InvoiceSequence()
|
|
invoice_sequence.fiscalyear = fiscalyear
|
|
invoice_sequence.in_invoice_sequence = sequence
|
|
invoice_sequence.in_credit_note_sequence = sequence
|
|
invoice_sequence.out_invoice_sequence = sequence
|
|
invoice_sequence.out_credit_note_sequence = sequence
|
|
invoice_sequence.save()
|
|
return fiscalyear
|
|
|
|
|
|
class DocumentTestCase(object):
|
|
""" check import of xml + pdf files
|
|
"""
|
|
def prep_sorted_dict(self, data):
|
|
""" sort dict by keys,
|
|
konvert tuple to list to revert changes by self.parsed_data
|
|
|
|
Args:
|
|
data (dict): dict-data
|
|
"""
|
|
return {
|
|
k: (
|
|
self.prep_sorted_dict(v) if isinstance(v, dict)
|
|
else list(v) if isinstance(v, tuple)
|
|
else v)
|
|
for k, v in sorted(data.items(), key=lambda item: item[0])}
|
|
|
|
def prep_incomingdoc_run_worker(self):
|
|
""" run tasks from queue
|
|
"""
|
|
Queue = Pool().get('ir.queue')
|
|
|
|
while True:
|
|
tasks = Queue.search([])
|
|
if not tasks:
|
|
break
|
|
for task in tasks:
|
|
task.run()
|
|
Queue.delete(tasks)
|
|
|
|
def prep_fiscalyear(self, company1):
|
|
""" prepare fiscal year, sequences...
|
|
"""
|
|
pool = Pool()
|
|
FiscalYear = pool.get('account.fiscalyear')
|
|
|
|
fisc_year = get_fiscalyear(company1, today=date(2025, 1, 15))
|
|
set_invoice_sequences(fisc_year)
|
|
fisc_year.save()
|
|
FiscalYear.create_period([fisc_year])
|
|
|
|
def prep_prodcat_category(self, company):
|
|
""" create product category
|
|
"""
|
|
pool = Pool()
|
|
ProdCat = pool.get('product.category')
|
|
Account = pool.get('account.account')
|
|
Tax = pool.get('account.tax')
|
|
|
|
# get accounts expense/revenue
|
|
acc_exp, acc_rev, acc_tax, = Account.search([
|
|
('name', 'in', ['Main Revenue', 'Main Expense', 'Main Tax'])
|
|
], order=[('name', 'ASC')])
|
|
self.assertEqual(acc_exp.name, 'Main Expense')
|
|
self.assertEqual(acc_rev.name, 'Main Revenue')
|
|
self.assertEqual(acc_tax.name, 'Main Tax')
|
|
|
|
# check tax
|
|
tax, = Tax.search([])
|
|
self.assertEqual(tax.name, '20% VAT')
|
|
self.assertEqual(tax.invoice_account.name, 'Main Tax')
|
|
self.assertEqual(tax.credit_note_account.name, 'Main Tax')
|
|
tax_19, = Tax.copy([tax], {
|
|
'name': '19% VAT', 'rate': Decimal('0.19')})
|
|
tax_7, = Tax.copy([tax], {
|
|
'name': '7% VAT', 'rate': Decimal('0.07')})
|
|
|
|
p_cat, p_cat_19, tax_7, = ProdCat.create([{
|
|
'name': 'Accounting',
|
|
'accounting': True,
|
|
'account_parent': False,
|
|
'account_expense': acc_exp.id,
|
|
'account_revenue': acc_rev.id,
|
|
'taxes_parent': False,
|
|
'customer_taxes': [('add', [tax.id])],
|
|
'supplier_taxes': [('add', [tax.id])],
|
|
}, {
|
|
'name': 'Accounting 19%',
|
|
'accounting': True,
|
|
'account_parent': False,
|
|
'account_expense': acc_exp.id,
|
|
'account_revenue': acc_rev.id,
|
|
'taxes_parent': False,
|
|
'customer_taxes': [('add', [tax_19.id])],
|
|
'supplier_taxes': [('add', [tax_19.id])],
|
|
}, {
|
|
'name': 'Accounting 7%',
|
|
'accounting': True,
|
|
'account_parent': False,
|
|
'account_expense': acc_exp.id,
|
|
'account_revenue': acc_rev.id,
|
|
'taxes_parent': False,
|
|
'customer_taxes': [('add', [tax_7.id])],
|
|
'supplier_taxes': [('add', [tax_7.id])],
|
|
}])
|
|
self.assertEqual(p_cat.name, 'Accounting')
|
|
self.assertEqual(p_cat.accounting, True)
|
|
self.assertEqual(p_cat.account_parent, False)
|
|
self.assertEqual(p_cat.taxes_parent, False)
|
|
self.assertEqual(p_cat.account_expense.name, 'Main Expense')
|
|
self.assertEqual(p_cat.account_revenue.name, 'Main Revenue')
|
|
self.assertEqual(p_cat.customer_taxes[0].name, '20% VAT')
|
|
self.assertEqual(p_cat.supplier_taxes[0].name, '20% VAT')
|
|
return [p_cat, p_cat_19, tax_7]
|
|
|
|
@with_transaction()
|
|
def test_xmldoc_check_xml_read_facturx_extended(self):
|
|
""" add incoming-dcument 'factur-x-extended' in memory,
|
|
read xml into 'parsed_data'
|
|
"""
|
|
pool = Pool()
|
|
IncDocument = pool.get('document.incoming')
|
|
|
|
with open(os.path.join(
|
|
os.path.split(__file__)[0],
|
|
'facturx-extended.xml'), 'rb') as fhdl:
|
|
xml_txt = fhdl.read()
|
|
|
|
incoming = IncDocument(data=xml_txt, name='facturx-extended.xml')
|
|
(xsdtype, funcname, xml_data) = incoming._facturx_detect_content()
|
|
self.assertEqual(xsdtype, 'Factur-X extended')
|
|
self.assertEqual(funcname, 'facturx_extended')
|
|
|
|
incoming._readxml_facturx_extended(xml_data)
|
|
self.assertEqual(
|
|
self.prep_sorted_dict(incoming.parsed_data),
|
|
parsed_data_facturx_extended)
|
|
|
|
@with_transaction()
|
|
def test_xmldoc_import_facturx_extended(self):
|
|
""" create incoming-document, load factur-x-extended xml, detect type
|
|
"""
|
|
pool = Pool()
|
|
IncDocument = pool.get('document.incoming')
|
|
Configuration = pool.get('document.incoming.configuration')
|
|
Party = pool.get('party.party')
|
|
IrAttachment = pool.get('ir.attachment')
|
|
|
|
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],
|
|
'facturx-extended.xml'), 'rb') as fhdl:
|
|
to_create.append({
|
|
'data': fhdl.read(),
|
|
'name': 'facturx-extended.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)
|
|
|
|
# check fail if missing supplier
|
|
config.create_supplier = False
|
|
config.save()
|
|
self.assertRaisesRegex(
|
|
UserError,
|
|
"Supplier party 'Name of the Supplier, 12345, " +
|
|
"Street of Supplier No 1, Berlin' not found. " +
|
|
"Auto-creation is currently turned off.",
|
|
document._process_supplier_invoice)
|
|
|
|
config.create_supplier = True
|
|
config.accept_other_company = False
|
|
config.save()
|
|
|
|
# check fail if invoice is not for our company
|
|
self.assertRaisesRegex(
|
|
UserError,
|
|
"The buyer party 'Our Company, 23456, Address Line 1; " +
|
|
"Address Line 2, Potsdam' differs from the corporate party.",
|
|
document._process_supplier_invoice)
|
|
|
|
config.create_supplier = True
|
|
config.accept_other_company = True
|
|
config.number_target = 'number'
|
|
config.save()
|
|
|
|
# check target of incoming invoice number
|
|
invoice = document._process_supplier_invoice()
|
|
self.assertEqual(invoice.number, 'RE2024.01234')
|
|
self.assertEqual(document.xsd_type, 'Factur-X extended')
|
|
self.assertEqual(invoice.reference, None)
|
|
|
|
config.number_target = 'reference'
|
|
config.save()
|
|
|
|
invoice = document._process_supplier_invoice()
|
|
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')
|
|
|
|
# now we have the supplier-party
|
|
self.assertEqual(
|
|
Party.search_count([('name', '=', 'Name of the Supplier')]),
|
|
1)
|
|
invoice.save()
|
|
|
|
# 'process' will queue the job to workers
|
|
IncDocument.process([document])
|
|
# run the usual call: process workers
|
|
self.prep_incomingdoc_run_worker()
|
|
|
|
self.assertEqual(document.result.__name__, 'account.invoice')
|
|
self.assertEqual(document.result.reference, 'RE2024.01234')
|
|
# seller-party was already created by 'invoice.save()'
|
|
# a few lines above,
|
|
# the party-id in 'seller_party' means: dont create this party
|
|
parsed_ori = copy.deepcopy(parsed_data_facturx_extended)
|
|
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, 'facturx-extended.xml')
|
|
|
|
@with_transaction()
|
|
def test_xmldoc_check_xml_read_facturx_basic(self):
|
|
""" add incoming-dcument 'facturx-basic' in memory,
|
|
read xml into 'parsed_data'
|
|
"""
|
|
pool = Pool()
|
|
IncDocument = pool.get('document.incoming')
|
|
|
|
with open(os.path.join(
|
|
os.path.split(__file__)[0],
|
|
'facturx-basic.xml'), 'rb') as fhdl:
|
|
xml_txt = fhdl.read()
|
|
|
|
incoming = IncDocument(data=xml_txt, name='facturx-basic.xml')
|
|
(xsdtype, funcname, xml_data) = incoming._facturx_detect_content()
|
|
self.assertEqual(xsdtype, 'Factur-X basic')
|
|
self.assertEqual(funcname, 'facturx_basic')
|
|
|
|
incoming._readxml_facturx_basic(xml_data)
|
|
self.assertEqual(incoming.parsed_data, parsed_data_facturx_basic)
|
|
|
|
@with_transaction()
|
|
def test_xmldoc_import_facturx_basic(self):
|
|
""" create incoming-document, load factur-x-basic xml, detect type
|
|
"""
|
|
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],
|
|
'facturx-basic.xml'), 'rb') as fhdl:
|
|
to_create.append({
|
|
'data': fhdl.read(),
|
|
'name': 'facturx-basic.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,
|
|
'Some notes to the customer.\n' +
|
|
'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_facturx_basic)
|
|
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, 'facturx-basic.xml')
|
|
|
|
@with_transaction()
|
|
def test_xmldoc_check_xml_read_facturx_en16931(self):
|
|
""" add incoming-dcument 'facturx-en16931' in memory,
|
|
read xml into 'parsed_data'
|
|
"""
|
|
pool = Pool()
|
|
IncDocument = pool.get('document.incoming')
|
|
|
|
with open(os.path.join(
|
|
os.path.split(__file__)[0],
|
|
'facturx-en16931.xml'), 'rb') as fhdl:
|
|
xml_txt = fhdl.read()
|
|
|
|
incoming = IncDocument(data=xml_txt, name='facturx-en16931.xml')
|
|
(xsdtype, funcname, xml_data) = incoming._facturx_detect_content()
|
|
self.assertEqual(xsdtype, 'Factur-X EN16931')
|
|
self.assertEqual(funcname, 'facturx_en16931')
|
|
|
|
incoming._readxml_facturx_en16931(xml_data)
|
|
self.assertEqual(
|
|
self.prep_sorted_dict(incoming.parsed_data),
|
|
parsed_data_facturx_en16931)
|
|
|
|
@with_transaction()
|
|
def test_xmldoc_import_facturx_en16931(self):
|
|
""" create incoming-document, load factur-x-en16931 xml, detect type
|
|
"""
|
|
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],
|
|
'facturx-en16931.xml'), 'rb') as fhdl:
|
|
to_create.append({
|
|
'data': fhdl.read(),
|
|
'name': 'facturx-en16931.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,
|
|
'Some notes to the customer.\n' +
|
|
'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_facturx_en16931)
|
|
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, 'facturx-en16931.xml')
|
|
|
|
@with_transaction()
|
|
def test_xmldoc_check_xml_read_pdf_facturx_basic(self):
|
|
""" add incoming-dcument 'PDF+facturx-basic' in memory,
|
|
read xml into 'parsed_data'
|
|
"""
|
|
pool = Pool()
|
|
IncDocument = pool.get('document.incoming')
|
|
|
|
with open(os.path.join(
|
|
os.path.split(__file__)[0],
|
|
'invoice-fx-basic.pdf'), 'rb') as fhdl:
|
|
pdf_data = fhdl.read()
|
|
|
|
incoming = IncDocument(data=pdf_data, name='invoice-fx-basic.pdf')
|
|
(xsdtype, funcname, xml_data) = incoming._facturx_detect_content()
|
|
self.assertEqual(incoming.xsd_type, 'PDF + Factur-X basic')
|
|
self.assertEqual(xsdtype, 'Factur-X basic')
|
|
self.assertEqual(funcname, 'facturx_basic')
|
|
|
|
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'<?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
|