edocument_xrechnung/tests/test_edocument.py

161 lines
5.6 KiB
Python
Raw Normal View History

2022-10-17 15:20:25 +00:00
# -*- coding: utf-8 -*-
2023-06-30 13:58:48 +00:00
# This file is part of the edocument-module for Tryton from m-ds.de.
2022-10-17 15:20:25 +00:00
# The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms.
from lxml import etree
import os
from unittest.mock import Mock
from decimal import Decimal
from datetime import date
2022-10-17 15:20:25 +00:00
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
from trytond.pool import Pool
from trytond.modules.edocument_uncefact.tests.test_edocument_uncefact import get_invoice
2024-12-05 09:47:05 +00:00
from trytond.exceptions import UserError
2023-06-30 13:58:48 +00:00
from unittest.mock import Mock
2022-10-19 15:15:56 +00:00
from decimal import Decimal
2022-10-17 15:20:25 +00:00
class EdocTestCase(ModuleTestCase):
'Test e-rechnung module'
module = 'edocument_xrechnung'
2024-12-05 09:47:05 +00:00
@with_transaction()
def test_xrechn_check_validator(self):
""" check validation of optional route-id
"""
Party = Pool().get('party.party')
party, = Party.create([{'name': 'P1'}])
self.assertEqual(party.xrechnung_routeid, False)
Party.write(*[
[party],
{
'xrechnung_routeid': True,
'identifiers': [('create', [{
'type': 'edoc_route_id',
'code': '1234'}])]}])
self.assertEqual(party.xrechnung_routeid, True)
self.assertEqual(party.get_xrechnung_route_id(), '1234')
self.assertRaisesRegex(
UserError,
"No XRechnung routing ID is stored for the party 'P1'.",
Party.write,
*[
[party],
{'identifiers': [('delete', [party.identifiers[0].id])]}]
)
2022-10-17 15:20:25 +00:00
@with_transaction()
2023-06-30 13:29:51 +00:00
def test_xrechn_export_xml_invoice(self):
""" run export - invoice
2022-10-17 15:20:25 +00:00
"""
pool = Pool()
Template = pool.get('edocument.xrechnung.invoice')
Identifier = pool.get('party.identifier')
2022-10-19 15:15:56 +00:00
Party = pool.get('party.party')
Bank = pool.get('bank')
BankAccount = pool.get('bank.account')
BankNumber = pool.get('bank.account.number')
2022-10-17 15:20:25 +00:00
invoice = get_invoice()
invoice.payment_term_date = date.today()
2023-06-30 13:58:48 +00:00
invoice.party.get_xrechnung_route_id = Mock(
return_value='xrechn-route-id-123')
2022-10-19 15:15:56 +00:00
invoice.company.party.bank_accounts = [
2023-06-30 13:58:48 +00:00
Mock(
spec=BankAccount,
2022-10-19 15:15:56 +00:00
currency=invoice.currency,
bank=Mock(spec=Bank, party=Mock(spec=Party, name='Bank')),
2023-06-30 13:58:48 +00:00
owners=[invoice.company.party],
numbers=[Mock(spec=BankNumber, type='other', number='123456')],
2022-10-19 15:15:56 +00:00
)]
invoice.description = 'description of invoice'
invoice.comment = 'note line 1\nnote line 2'
invoice.taxes[0].tax.rate = Decimal('0.1')
2022-10-17 15:20:25 +00:00
invoice.identifiers = [
2023-06-30 13:58:48 +00:00
Mock(
spec=Identifier,
2022-10-17 15:20:25 +00:00
type='edoc_route_id',
code='xrechn-route-id-123')
]
template = Template(invoice)
schema_file = os.path.join(
os.path.dirname(__file__), 'os-UBL-2.1',
'xsd', 'maindoc', 'UBL-Invoice-2.1.xsd')
for x in ['XRechnung-2.2', 'XRechnung-2.3', 'XRechnung-3.0']:
invoice_string = template.render(x)
invoice_xml = etree.fromstring(invoice_string)
schema = etree.XMLSchema(etree.parse(schema_file))
schema.assertValid(invoice_xml)
2023-06-30 13:29:51 +00:00
@with_transaction()
def test_xrechn_export_xml_creditnote(self):
""" run export - creditnote
"""
pool = Pool()
Template = pool.get('edocument.xrechnung.invoice')
Identifier = pool.get('party.identifier')
Party = pool.get('party.party')
Bank = pool.get('bank')
BankAccount = pool.get('bank.account')
BankNumber = pool.get('bank.account.number')
invoice = get_invoice()
# credit note
invoice.lines[0].quantity = -1
invoice.lines[0].amount = Decimal('-100.0')
invoice.taxes[0].base = Decimal('-100.0')
invoice.taxes[0].amount = Decimal('-10.0')
invoice.untaxed_amount = Decimal('-100.0')
invoice.tax_amount = Decimal('-10.0')
invoice.total_amount = Decimal('-110.0')
invoice.lines_to_pay[0].debit = Decimal('-110.0')
invoice.party.get_xrechnung_route_id = Mock(
return_value='xrechn-route-id-123')
invoice.company.party.bank_accounts = [
Mock(
spec=BankAccount,
currency=invoice.currency,
bank=Mock(spec=Bank, party=Mock(spec=Party, name='Bank')),
owners=[invoice.company.party],
numbers=[Mock(spec=BankNumber, type='other', number='123456')],
)]
invoice.description = 'description of invoice'
invoice.comment = 'note line 1\nnote line 2'
invoice.taxes[0].tax.rate = Decimal('0.1')
invoice.identifiers = [
Mock(
spec=Identifier,
type='edoc_route_id',
code='xrechn-route-id-123')
]
template = Template(invoice)
schema_file = os.path.join(
os.path.dirname(__file__), 'os-UBL-2.1',
'xsd', 'maindoc', 'UBL-CreditNote-2.1.xsd')
for x in ['XRechnung-2.2', 'XRechnung-2.3', 'XRechnung-3.0']:
invoice_string = template.render(x)
invoice_xml = etree.fromstring(invoice_string)
schema = etree.XMLSchema(etree.parse(schema_file))
schema.assertValid(invoice_xml)
# invoice_string = template.render('XRechnung-2.2')
# with open('xrechnung-test-creditnote.xml', 'wt') as fhdl:
# fhdl.write(invoice_string.decode('utf8'))
2022-10-17 15:20:25 +00:00
# end EdocTestCase
2023-06-30 13:58:48 +00:00
del ModuleTestCase