account_invoice_xrechnung/wizard_runreport.py

145 lines
4.9 KiB
Python
Raw Permalink Normal View History

2022-10-18 15:33:31 +00:00
# -*- coding: utf-8 -*-
2023-06-30 09:29:53 +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-18 15:33:31 +00:00
from trytond.model import ModelView, fields
2022-10-20 12:43:58 +00:00
from trytond.wizard import Wizard, StateView, StateReport, Button
2022-10-18 15:33:31 +00:00
from trytond.pool import Pool
from trytond.exceptions import UserError
from trytond.i18n import gettext
2022-10-18 15:33:31 +00:00
from trytond.transaction import Transaction
2022-10-19 15:47:47 +00:00
sel_edocument = [
2024-12-05 10:28:44 +00:00
('edocument.xrechnung.invoice-2.2', 'XRechnung UBL Invoice 2.2'),
('edocument.xrechnung.invoice-2.3', 'XRechnung UBL Invoice 2.3'),
('edocument.xrechnung.invoice-3.0', 'XRechnung UBL Invoice 3.0'),
2024-12-05 16:32:40 +00:00
('edocument.facturxext.invoice', 'Factur-X Extended'),
('edocument.facturxext.invoice-ferd', 'ZUGFeRD 2.3.2'),
2022-10-19 15:47:47 +00:00
('edocument.uncefact.invoice', 'CII CrossIndustryInvoice D16B'),
]
2022-10-20 12:43:58 +00:00
edoc_versions = {
2024-12-05 10:28:44 +00:00
'edocument.xrechnung.invoice-2.2': 'XRechnung-2.2',
'edocument.xrechnung.invoice-2.3': 'XRechnung-2.3',
'edocument.xrechnung.invoice-3.0': 'XRechnung-3.0',
2024-12-05 16:32:40 +00:00
'edocument.facturxext.invoice': 'Factur-X-1.07.2-extended',
'edocument.facturxext.invoice-ferd': 'Factur-X-1.07.2-extended',
2024-11-21 13:34:17 +00:00
'edocument.uncefact.invoice': '16B-CII'}
2022-10-20 12:43:58 +00:00
2022-10-19 15:47:47 +00:00
class RunXRechnungReportStart(ModelView):
2022-10-20 12:43:58 +00:00
'eDocument Export'
2022-10-19 15:47:47 +00:00
__name__ = 'account_invoice_xrechnung.runrep.start'
2023-06-30 09:21:48 +00:00
invoice = fields.Many2One(
string='Invoice', readonly=True,
2022-10-19 15:47:47 +00:00
model_name='account.invoice', required=True)
2023-06-30 09:21:48 +00:00
state = fields.Char(
string='State', readonly=True, states={'invisible': True})
edocument = fields.Selection(
string='Type', required=True, selection=sel_edocument)
2022-10-20 12:43:58 +00:00
as_zip = fields.Boolean(string='ZIP-File')
@classmethod
def default_as_zip(cls):
""" default: True
"""
return True
2022-10-19 15:47:47 +00:00
@classmethod
def default_edocument(cls):
""" default xrechnung
"""
2024-12-05 10:28:44 +00:00
return 'edocument.xrechnung.invoice-3.0'
2022-10-19 15:47:47 +00:00
# end RunXRechnungReportStart
2022-10-18 15:33:31 +00:00
class RunXRechnungReport(Wizard):
2022-10-20 12:43:58 +00:00
'eDocument Export'
2022-10-18 15:33:31 +00:00
__name__ = 'account_invoice_xrechnung.runrep'
2022-10-20 12:43:58 +00:00
start_state = 'start'
2022-10-19 15:47:47 +00:00
export = StateReport('account_invoice_xrechnung.export')
2022-10-20 12:43:58 +00:00
start = StateView(
2022-10-19 15:47:47 +00:00
model_name='account_invoice_xrechnung.runrep.start',
view='account_invoice_xrechnung.wiz_start_form',
buttons=[
2022-10-20 12:43:58 +00:00
Button(string='Cancel', state='end', icon='tryton-cancel'),
2022-10-19 15:47:47 +00:00
Button(string='Export', state='export', icon='tryton-export'),
2024-11-21 13:34:17 +00:00
])
2022-10-19 15:47:47 +00:00
2022-10-20 12:43:58 +00:00
def default_start(self, fields):
2022-10-19 15:47:47 +00:00
""" set defaults
"""
context = Transaction().context
2024-12-05 10:28:44 +00:00
pool = Pool()
Invoice = pool.get('account.invoice')
WizRepStart = pool.get('account_invoice_xrechnung.runrep.start')
2024-12-06 13:04:35 +00:00
Configuration = pool.get('account.configuration')
2022-10-19 15:47:47 +00:00
2024-12-06 13:04:35 +00:00
cfg1 = Configuration.get_singleton()
invoice = Invoice.browse([context.get('active_id', -1)])
2022-10-19 15:47:47 +00:00
result = {
2024-12-05 10:28:44 +00:00
'edocument': WizRepStart.default_edocument(),
2022-10-19 15:47:47 +00:00
'invoice': context.get('active_id', -1),
2024-11-21 13:34:17 +00:00
'state': invoice[0].state if invoice else ''}
2024-12-06 13:04:35 +00:00
if cfg1 and cfg1.xrechn_default:
result['edocument'] = cfg1.xrechn_default
2022-10-19 15:47:47 +00:00
return result
2022-10-18 15:33:31 +00:00
2024-12-06 13:04:35 +00:00
def generate_invoice_reports(self, data):
""" generate missing reports and store to db
Args:
data (dict): report-data
"""
pool = Pool()
Invoice = pool.get('account.invoice')
XReport = pool.get('account_invoice_xrechnung.export', type='report')
invoices = Invoice.search([
('id', '=', data['invoice']),
('type', '=', 'out')])
2024-12-11 15:50:02 +00:00
# check if stored report is pdf
to_delete_report = [
x
for x in invoices
if x.invoice_report_cache and (x.invoice_report_format != 'pdf')]
# delete reports
if to_delete_report:
Invoice.write(*[
to_delete_report, {
'invoice_report_cache': None,
'invoice_report_format': None}])
to_generate = [x for x in invoices if not x.invoice_report_cache]
2024-12-06 13:04:35 +00:00
if to_generate:
# the pdf should be stored in db...
XReport.generate_pdf_data(to_generate)
2024-12-06 13:04:35 +00:00
2022-10-19 15:47:47 +00:00
def do_export(self, action):
2022-10-20 12:43:58 +00:00
""" run export
2022-10-18 15:33:31 +00:00
"""
2025-02-10 11:21:01 +00:00
if self.start.state not in {'paid', 'posted'}:
raise UserError(gettext(
'account_invoice_xrechnung.msg_invoice_must_posted',
2024-11-21 13:34:17 +00:00
invname=self.start.invoice.rec_name))
2024-12-06 13:04:35 +00:00
data = {
2022-10-20 12:43:58 +00:00
'invoice': self.start.invoice.id,
'edocument': self.start.edocument,
2024-11-21 13:34:17 +00:00
'as_zip': self.start.as_zip}
2022-10-18 15:33:31 +00:00
2024-12-06 13:04:35 +00:00
# if zugferd - generate missing report-cache-items
if data['edocument'] == 'edocument.facturxext.invoice-ferd':
# pdf is stored to db
self.generate_invoice_reports(data)
return action, data
2022-10-18 15:33:31 +00:00
# end RunXRechnungReport