Files
tradon/modules/purchase_trade/configuration.py
2026-04-26 14:56:51 +02:00

142 lines
6.1 KiB
Python

from trytond.model import ModelSingleton, ModelSQL, ModelView, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.transaction import Transaction
class AccountConfiguration(metaclass=PoolMeta):
__name__ = 'account.configuration'
default_sale_padding_account = fields.MultiValue(fields.Many2One(
'account.account', "Default Sale Padding",
domain=[
('closed', '!=', True),
('type.revenue', '=', True),
('company', '=', Eval('context', {}).get('company', -1)),
]))
default_accrual_padding_account = fields.MultiValue(fields.Many2One(
'account.account', "Default Accrual Padding",
domain=[
('closed', '!=', True),
('type.statement', '=', 'balance'),
('company', '=', Eval('context', {}).get('company', -1)),
]))
@classmethod
def multivalue_model(cls, field):
pool = Pool()
if field in {
'default_sale_padding_account',
'default_accrual_padding_account',
}:
return pool.get('account.configuration.default_account')
return super().multivalue_model(field)
class AccountConfigurationDefaultAccount(metaclass=PoolMeta):
__name__ = 'account.configuration.default_account'
default_sale_padding_account = fields.Many2One(
'account.account', "Default Sale Padding",
domain=[
('closed', '!=', True),
('type.revenue', '=', True),
('company', '=', Eval('company', -1)),
])
default_accrual_padding_account = fields.Many2One(
'account.account', "Default Accrual Padding",
domain=[
('closed', '!=', True),
('type.statement', '=', 'balance'),
('company', '=', Eval('company', -1)),
])
class Configuration(ModelSingleton, ModelSQL, ModelView):
"Purchase Trade Configuration"
__name__ = 'purchase_trade.configuration'
_REPORT_LABELS = (
('sale_report_label', 'sale', 'report_sale', 'Proforma'),
('sale_bill_report_label', 'sale', 'report_bill', 'Draft'),
('invoice_report_label', 'account_invoice', 'report_invoice',
'Invoice'),
('invoice_cndn_report_label', 'account_invoice',
'report_invoice_ict_final', 'CN/DN'),
('invoice_prepayment_report_label', 'account_invoice',
'report_prepayment', 'Prepayment'),
('invoice_packing_list_report_label', 'purchase_trade',
'report_invoice_packing_list', 'Packing List'),
('invoice_payment_order_report_label', 'purchase_trade',
'report_payment_order', 'Payment Order'),
('purchase_report_label', 'purchase', 'report_purchase', 'Purchase'),
('shipment_shipping_report_label', 'stock',
'report_shipment_in_shipping', 'Shipping instructions'),
('shipment_insurance_report_label', 'purchase_trade',
'report_shipment_in_insurance', 'Insurance'),
('shipment_coo_report_label', 'purchase_trade',
'report_shipment_in_coo', 'COO'),
('shipment_packing_list_report_label', 'purchase_trade',
'report_shipment_in_packing_list', 'Packing List'),
)
pricing_rule = fields.Text("Pricing Rule")
sale_report_template = fields.Char("Sale Template")
sale_report_label = fields.Char("Sale Menu Label")
sale_bill_report_template = fields.Char("Sale Bill Template")
sale_bill_report_label = fields.Char("Sale Bill Menu Label")
sale_final_report_template = fields.Char("Sale Final Template")
invoice_report_template = fields.Char("Invoice Template")
invoice_report_label = fields.Char("Invoice Menu Label")
invoice_cndn_report_template = fields.Char("CN/DN Template")
invoice_cndn_report_label = fields.Char("CN/DN Menu Label")
invoice_prepayment_report_template = fields.Char("Prepayment Template")
invoice_prepayment_report_label = fields.Char("Prepayment Menu Label")
invoice_packing_list_report_template = fields.Char("Packing List Template")
invoice_packing_list_report_label = fields.Char("Packing List Menu Label")
invoice_payment_order_report_template = fields.Char("Payment Order Template")
invoice_payment_order_report_label = fields.Char(
"Payment Order Menu Label")
purchase_report_template = fields.Char("Purchase Template")
purchase_report_label = fields.Char("Purchase Menu Label")
shipment_shipping_report_template = fields.Char("Shipping Template")
shipment_shipping_report_label = fields.Char("Shipping Menu Label")
shipment_insurance_report_template = fields.Char("Insurance Template")
shipment_insurance_report_label = fields.Char("Insurance Menu Label")
shipment_coo_report_template = fields.Char("COO Template")
shipment_coo_report_label = fields.Char("COO Menu Label")
shipment_packing_list_report_template = fields.Char("Packing List Template")
shipment_packing_list_report_label = fields.Char(
"Packing List Menu Label")
@classmethod
def create(cls, vlist):
records = super().create(vlist)
cls._sync_report_labels(records)
return records
@classmethod
def write(cls, *args):
super().write(*args)
cls._sync_report_labels(sum(args[::2], []))
@classmethod
def _sync_report_labels(cls, records):
if not records:
return
pool = Pool()
ModelData = pool.get('ir.model.data')
ActionReport = pool.get('ir.action.report')
to_write = []
for record in records:
for field_name, module, xml_id, default_label in cls._REPORT_LABELS:
label = (getattr(record, field_name, '') or '').strip()
action_id = ModelData.get_id(module, xml_id)
action = ActionReport(action_id)
target_label = label or default_label
if getattr(action, 'name', '') != target_label:
to_write.extend(([action], {'name': target_label}))
if to_write:
with Transaction().set_user(0):
ActionReport.write(*to_write)