edocument_xrechnung/edocument.py

98 lines
3.1 KiB
Python
Raw Normal View History

2022-10-17 15:20:25 +00:00
# -*- coding: utf-8 -*-
2023-06-30 09:36:17 +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.
import genshi.template
2023-06-30 09:36:17 +00:00
import os
2022-10-18 15:33:17 +00:00
from decimal import Decimal
2024-12-09 12:24:39 +00:00
from trytond.modules.edocument_uncefact.edocument import Invoice
from .mixin import EdocumentMixin
2022-10-17 15:20:25 +00:00
2024-12-09 12:24:39 +00:00
class XRechnung(EdocumentMixin, Invoice):
2022-10-17 15:20:25 +00:00
'EDocument XRechnung'
__name__ = 'edocument.xrechnung.invoice'
2022-10-19 15:15:56 +00:00
def sales_order_nums(self):
""" get string of sale-numbers
"""
if getattr(self.invoice, 'sales', None) is not None:
return ', '.join([x.number for x in self.invoice.sales])
2023-06-30 13:29:51 +00:00
def negate_amount(self, amount):
""" amount * -1.0
"""
if amount is not None and amount:
if isinstance(amount, Decimal):
return amount.copy_negate()
elif isinstance(amount, float):
return -1.0 * amount
elif isinstance(amount, int):
return -1 * amount
2023-06-30 13:31:47 +00:00
else:
2023-06-30 13:29:51 +00:00
return amount
2022-10-18 15:33:17 +00:00
def prepaid_amount(self, invoice):
""" compute already paid amount
"""
return invoice.total_amount - invoice.amount_to_pay
2022-10-19 15:15:56 +00:00
def invoice_note(self):
""" get 'description' + 'comment'
"""
notes = []
if self.invoice.description:
notes.append(self.invoice.description)
if self.invoice.comment:
notes.extend(self.invoice.comment.split('\n'))
2024-11-21 13:43:35 +00:00
if notes:
2022-10-19 15:15:56 +00:00
return '; '.join(notes)
2022-10-17 15:20:25 +00:00
def _get_template(self, version):
""" load our own template if 'version' is ours
"""
2024-12-05 14:36:07 +00:00
loader = genshi.template.TemplateLoader(
os.path.join(os.path.dirname(__file__), 'template'),
auto_reload=True)
if version in ['XRechnung-2.2', 'XRechnung-2.3', 'XRechnung-3.0']:
2024-12-05 14:36:07 +00:00
file_name = {
'380': 'XRechnung_invoice.xml',
'389': 'XRechnung_invoice.xml',
'381': 'XRechnung_credit.xml',
'261': 'XRechnung_credit.xml',
}.get(self.type_code)
if file_name:
return loader.load(os.path.join(version, file_name))
2023-06-30 13:31:47 +00:00
else:
2023-06-30 13:29:51 +00:00
raise ValueError('invalid type-code "%s"' % self.type_code)
2023-06-30 09:36:17 +00:00
else:
return super()._get_template(version)
2024-12-09 12:24:39 +00:00
# end XRechnung
class FacturX(EdocumentMixin, Invoice):
'Factur-X'
__name__ = 'edocument.facturxext.invoice'
def _get_template(self, version):
""" load our own template if 'version' is ours
"""
loader = genshi.template.TemplateLoader(
os.path.join(os.path.dirname(__file__), 'template'),
auto_reload=True)
if version == 'Factur-X-1.07.2-extended':
if self.type_code in ['380', '389', '381', '261']:
return loader.load(os.path.join(version, 'invoice.xml'))
else:
raise ValueError('invalid type-code "%s"' % self.type_code)
else:
return super()._get_template(version)
2022-10-17 15:20:25 +00:00
2024-12-09 12:24:39 +00:00
# end FacturX