Add template management

This commit is contained in:
2026-04-06 15:17:17 +02:00
parent 1f62ae91dd
commit 888b880bd6
7 changed files with 166 additions and 29 deletions

View File

@@ -5,6 +5,9 @@ from trytond.modules.purchase_trade.numbers_to_words import amount_to_currency_w
from trytond.exceptions import UserError
from trytond.modules.account_invoice.invoice import (
InvoiceReport as BaseInvoiceReport)
from trytond.modules.sale.sale import SaleReport as BaseSaleReport
from trytond.modules.purchase.purchase import (
PurchaseReport as BasePurchaseReport)
class Invoice(metaclass=PoolMeta):
@@ -670,9 +673,7 @@ class InvoiceLine(metaclass=PoolMeta):
return round(Decimal(net) * Decimal('2204.62'),2)
class InvoiceReport(BaseInvoiceReport):
__name__ = 'account.invoice'
class ReportTemplateMixin:
@classmethod
def _get_purchase_trade_configuration(cls):
Configuration = Pool().get('purchase_trade.configuration')
@@ -686,31 +687,20 @@ class InvoiceReport(BaseInvoiceReport):
return getattr(action, 'name', '') or ''
@classmethod
def _resolve_configured_report_path(cls, action):
def _get_action_report_path(cls, action):
if isinstance(action, dict):
return action.get('report') or ''
return getattr(action, 'report', '') or ''
@classmethod
def _resolve_template_path(cls, action, field_name, default_prefix):
config = cls._get_purchase_trade_configuration()
report_path = cls._get_action_report_path(action) or ''
action_name = cls._get_action_name(action)
if (report_path.endswith('/prepayment.fodt')
or action_name == 'Prepayment'):
template = (
getattr(config, 'invoice_prepayment_report_template', '')
if config else '')
elif (report_path.endswith('/invoice_ict_final.fodt')
or action_name == 'CN/DN'):
template = (
getattr(config, 'invoice_cndn_report_template', '')
if config else '')
else:
template = (
getattr(config, 'invoice_report_template', '')
if config else '')
template = getattr(config, field_name, '') if config else ''
template = (template or '').strip()
if not template:
raise UserError('No template found')
if '/' not in template:
return f'account_invoice/{template}'
return f'{default_prefix}/{template}'
return template
@classmethod
@@ -727,3 +717,47 @@ class InvoiceReport(BaseInvoiceReport):
def _execute(cls, records, header, data, action):
resolved_action = cls._get_resolved_action(action)
return super()._execute(records, header, data, resolved_action)
class InvoiceReport(ReportTemplateMixin, BaseInvoiceReport):
__name__ = 'account.invoice'
@classmethod
def _resolve_configured_report_path(cls, action):
report_path = cls._get_action_report_path(action) or ''
action_name = cls._get_action_name(action)
if (report_path.endswith('/prepayment.fodt')
or action_name == 'Prepayment'):
field_name = 'invoice_prepayment_report_template'
elif (report_path.endswith('/invoice_ict_final.fodt')
or action_name == 'CN/DN'):
field_name = 'invoice_cndn_report_template'
else:
field_name = 'invoice_report_template'
return cls._resolve_template_path(action, field_name, 'account_invoice')
class SaleReport(ReportTemplateMixin, BaseSaleReport):
__name__ = 'sale.sale'
@classmethod
def _resolve_configured_report_path(cls, action):
report_path = cls._get_action_report_path(action)
action_name = cls._get_action_name(action)
if report_path.endswith('/bill.fodt') or action_name == 'Bill':
field_name = 'sale_bill_report_template'
elif report_path.endswith('/sale_final.fodt') or action_name == 'Sale (final)':
field_name = 'sale_final_report_template'
else:
field_name = 'sale_report_template'
return cls._resolve_template_path(action, field_name, 'sale')
class PurchaseReport(ReportTemplateMixin, BasePurchaseReport):
__name__ = 'purchase.purchase'
@classmethod
def _resolve_configured_report_path(cls, action):
return cls._resolve_template_path(
action, 'purchase_report_template', 'purchase')