From 822b095cdff926d748239d2347cb8f700fc4ca37 Mon Sep 17 00:00:00 2001 From: Stepan Savelyev Date: Fri, 17 Jan 2020 17:13:45 +0500 Subject: [PATCH] [IMP] compute the custom name of document --- models/res_partner_contract.py | 3 +++ models/res_partner_contract_annex.py | 11 +++++++++++ wizard/res_partner_contract_wizard.py | 15 ++++++--------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/models/res_partner_contract.py b/models/res_partner_contract.py index 0bb7b97..cc83d34 100644 --- a/models/res_partner_contract.py +++ b/models/res_partner_contract.py @@ -105,6 +105,9 @@ class PartnerContract(models.Model, Extension): "context": {"self_id": self.id}, } + def get_name_by_document_template(self, document_template_id): + return self.name + def get_filename_by_document_template(self, document_template_id): return _("{type} {number} from {date}").format( type=_(dict(document_template_id._fields['document_type'].selection).get(document_template_id.document_type)), diff --git a/models/res_partner_contract_annex.py b/models/res_partner_contract_annex.py index 3cadda4..927e71b 100644 --- a/models/res_partner_contract_annex.py +++ b/models/res_partner_contract_annex.py @@ -85,6 +85,17 @@ class ContractOrderAnnex(models.Model, Extension): "context": {"self_id": self.id}, } + def get_name_by_document_template(self, document_template_id): + return { + "specification": "{number} {name}", + "approval_list": "{number}.1 {name}-1", + "act_at": "{number}.2 {name}-2", + "act_ad": "{number}.3 {name}-3", + }.get(document_template_id.document_type_name, "Unknown").format( + number=self.number, + name=self.name, + ) + def get_filename_by_document_template(self, document_template_id): return "{type} №{name}".format( type=_(dict(document_template_id._fields['document_type'].selection).get(document_template_id.document_type)), diff --git a/wizard/res_partner_contract_wizard.py b/wizard/res_partner_contract_wizard.py index 20c96ba..7c715ad 100644 --- a/wizard/res_partner_contract_wizard.py +++ b/wizard/res_partner_contract_wizard.py @@ -30,6 +30,7 @@ class ContractWizard(models.TransientModel): document_template = fields.Many2one( "res.partner.document.template", string="Document Template", ) + document_name = fields.Char(string="Document Name", compute='_compute_document_name') transient_field_ids = fields.One2many( "res.partner.contract.field.transient", "_contract_wizard_id", @@ -44,6 +45,10 @@ class ContractWizard(models.TransientModel): if not self.document_template: raise ValidationError("You did not set up the template...") + @api.depends('document_name', 'document_template', 'target') + def _compute_document_name(self): + self.document_name = self.target.get_name_by_document_template(self.document_template) + @api.onchange("document_template") def _onchange_document_template(self): """Creates transient fields for generate contract template @@ -158,15 +163,7 @@ class ContractWizard(models.TransientModel): if self.target._name == "res.partner.contract.annex": # TODO: bad fields.update({ - "annex_name": { - "specification": "{number} {name}", - "approval_list": "{number}.1 {name}-1", - "act_at": "{number}.2 {name}-2", - "act_ad": "{number}.3 {name}-3", - }.get(self.document_template.document_type_name).format( - number=self.target.number, - name=self.target.name, - ), + "annex_name": self.document_name, "specification_name": _("{name} from {date}").format( name="{}-{}".format(self.target.contract_id.name, self.target.order_id.name), date=self.target.contract_id.get_date().strftime("%d.%m.%Y"),