account_invoice_xrechnung/tests/test_invoice.py

257 lines
9.7 KiB
Python
Raw Normal View History

2022-10-19 15:47:47 +00:00
# -*- coding: utf-8 -*-
2023-06-30 09:21:48 +00:00
# This file is part of the account-invoice-xrechnung-module
# from m-ds for Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2022-10-19 15:47:47 +00:00
2024-12-05 16:32:56 +00:00
from decimal import Decimal
from datetime import date
from facturx import get_facturx_xml_from_pdf
2022-10-19 15:47:47 +00:00
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
2024-12-05 16:32:56 +00:00
from trytond.pool import Pool
from trytond.transaction import Transaction
from trytond.modules.company.tests import create_company, set_company
from trytond.modules.account.tests import create_chart, get_fiscalyear
from .xml_data import xml_from_pdf
2024-12-05 16:32:56 +00:00
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
2022-10-19 15:47:47 +00:00
class InvoiceTestCase(ModuleTestCase):
'Test invoice module'
module = 'account_invoice_xrechnung'
2024-12-05 16:32:56 +00:00
def prep_fiscalyear(self, company1):
""" prepare fiscal year, sequences...
"""
pool = Pool()
FiscalYear = pool.get('account.fiscalyear')
fisc_year = get_fiscalyear(company1, today=date(2024, 1, 15))
set_invoice_sequences(fisc_year)
self.assertEqual(len(fisc_year.invoice_sequences), 1)
FiscalYear.create_period([fisc_year])
def prep_invoice(self, party_customer):
""" add invoice
"""
pool = Pool()
Invoice = pool.get('account.invoice')
Taxes = pool.get('account.tax')
Account = pool.get('account.account')
Journal = pool.get('account.journal')
Currency = pool.get('currency.currency')
Uom = pool.get('product.uom')
currency1, = Currency.search([('code', '=', 'usd')])
tax_lst = Taxes.search([('name', '=', '20% VAT')])
self.assertEqual(len(tax_lst), 1)
account_lst = Account.search([
('name', 'in', ['Main Revenue', 'Main Receivable'])
], order=[('name', 'ASC')])
self.assertEqual(len(account_lst), 2)
self.assertEqual(account_lst[0].name, 'Main Receivable')
journ_lst = Journal.search([('name', '=', 'Revenue')])
self.assertEqual(len(journ_lst), 1)
to_create_invoice = [{
'type': 'out',
'description': 'Parts',
'invoice_date': date(2024, 7, 1),
'party': party_customer.id,
'invoice_address': party_customer.addresses[0].id,
'account': account_lst[0].id,
'journal': journ_lst[0].id,
'currency': currency1.id,
'lines': [('create', [{
'type': 'line',
'quantity': 2.0,
'description': 'Product 1',
2024-12-05 16:32:56 +00:00
'unit': Uom.search([('symbol', '=', 'u')])[0].id,
'unit_price': Decimal('50.0'),
'taxes': [('add', [tax_lst[0].id])],
'account': account_lst[1].id,
'currency': currency1.id,
}])],
}]
inv_lst, = Invoice.create(to_create_invoice)
inv_lst.on_change_lines()
inv_lst.save()
Invoice.validate_invoice([inv_lst])
Invoice.post([inv_lst])
self.assertEqual(inv_lst.currency.code, 'usd')
self.assertEqual(len(inv_lst.move.lines), 3)
return inv_lst
2022-10-19 15:47:47 +00:00
@with_transaction()
2024-12-05 16:32:56 +00:00
def test_xrechnung_configuration(self):
""" test configuration
2022-10-19 15:47:47 +00:00
"""
2024-12-05 16:32:56 +00:00
pool = Pool()
Configuration = pool.get('account.configuration')
Party = pool.get('party.party')
Country = pool.get('country.country')
2024-12-05 16:32:56 +00:00
ActionReport = pool.get('ir.action.report')
ExportWiz = pool.get('account_invoice_xrechnung.runrep', type='wizard')
Tax = pool.get('account.tax')
2024-12-10 12:56:40 +00:00
BankAccount = pool.get('bank.account')
Bank = pool.get('bank')
2024-12-05 16:32:56 +00:00
country_de, = Country.create([{
'name': 'Germany',
'code': 'DE',
'code3': 'DEU'}])
2024-12-05 16:32:56 +00:00
pty1, = Party.create([{
'name': 'Payee',
'addresses': [('create', [{
'invoice': True,
'street': 'Applicant Street 1',
'postal_code': '12345',
'city': 'Usertown',
'country': country_de.id,
2024-12-05 16:32:56 +00:00
}])],
}])
company1 = create_company('m-ds')
Party.write(*[
[company1.party],
{'addresses': [(
'write',
[company1.party.addresses[0]],
{'country': country_de.id})]}])
2024-12-05 16:32:56 +00:00
with set_company(company1):
with Transaction().set_context({'company': company1.id}):
2024-12-10 12:56:40 +00:00
bank_party, = Party.create([{
'name': 'Bank 123',
'addresses': [('create', [{}])]}])
bank, = Bank.create([{'party': bank_party.id}])
acc_company, = BankAccount.create([
{
'bank': bank.id,
'owners': [('add', [company1.party.id])],
'numbers': [('create', [{
'type': 'iban',
'number': 'DE02300209000106531065'}])]}])
2024-12-05 16:32:56 +00:00
# update report to 'pdf'
inv_report, = ActionReport.search([
('model', '=', 'account.invoice'),
('report_name', '=', 'account.invoice')])
self.assertEqual(inv_report.extension, '')
ActionReport.write(*[
[inv_report], {'extension': 'pdf'}])
2024-12-10 12:56:40 +00:00
cfg1 = Configuration(
xrechn_zugferd_report=inv_report,
edocument_bank=list(acc_company.numbers))
2024-12-05 16:32:56 +00:00
cfg1.save()
self.assertEqual(cfg1.xrechn_zugferd_report.name, 'Invoice')
create_chart(company=company1, tax=True)
self.prep_fiscalyear(company1)
tax, = Tax.search([('name', '=', '20% VAT')])
Tax.write(*[
[tax],
{'unece_code': 'GST', 'unece_category_code': 'S'}])
2024-12-05 16:32:56 +00:00
invoice = self.prep_invoice(pty1)
# start wizard with two selected records
with Transaction().set_context({
'active_ids': [invoice.id],
'active_id': invoice.id,
'active_model': 'account.invoice'}):
(sess_id, start_state, end_state) = ExportWiz.create()
w_obj = ExportWiz(sess_id)
self.assertEqual(start_state, 'start')
self.assertEqual(end_state, 'end')
result = ExportWiz.execute(sess_id, {}, start_state)
self.assertEqual(
list(result['view']['defaults'].keys()), [
'as_zip', 'edocument', 'invoice', 'state',
'invoice.'])
data = {}
for x in result['view']['defaults'].keys():
if '.' in x:
continue
data[x] = result['view']['defaults'][x]
setattr(w_obj.start, x, data[x])
self.assertEqual(
w_obj.start.edocument,
'edocument.facturxext.invoice-ferd')
self.assertEqual(w_obj.start.invoice, invoice)
self.assertEqual(w_obj.start.as_zip, True)
w_obj.start.as_zip = False
data['as_zip'] = False
# (action, data)
result = ExportWiz.execute(
sess_id, {'start': data}, 'export')
self.assertEqual(len(result['actions']), 1)
(action, data) = result['actions'][0]
self.assertEqual(
action['report_name'],
'account_invoice_xrechnung.export')
self.assertEqual(action['type'], 'ir.action.report')
self.assertEqual(action['records'], 'selected')
# 2nd step, wizard told us which report we must execute
ReportExport = pool.get(
'account_invoice_xrechnung.export',
type='report')
data2 = {}
data2.update(data)
data2['action_id'] = action['id']
data2['model'] = 'account.invoice'
(ext, pdfdata, dprint, fname) = ReportExport.execute(
[data['invoice']], data2)
# extract xml
(xml_fname, xml_frompdf) = get_facturx_xml_from_pdf(
pdfdata)
self.assertEqual(xml_fname, 'factur-x.xml')
self.assertEqual(
xml_frompdf.decode('utf8'),
xml_from_pdf % {
'datetoday': date.today().strftime('%Y%m%d')})
ExportWiz.delete(sess_id)
2022-10-19 15:47:47 +00:00
# end InvoiceTestCase
2023-04-04 11:08:09 +00:00
2023-06-30 09:21:48 +00:00
2023-04-04 11:08:09 +00:00
del ModuleTestCase