From a12f0497f9b75561451aaf3d67061a26062851b8 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Tue, 2 Jun 2020 09:30:22 +0200 Subject: [PATCH] [FIX] contract: Currency is not editable + pricelist from partner Previous related field was not accurated nor editable. Now the field is got properly from a computed field. Reviewing this, as the currency was taken (and it continues being taken) from the partner pricelist if no pricelist is explicitly set, automatic price should use the same logic for using partner pricelist. --- contract/models/abstract_contract_line.py | 8 ++++++- contract/models/contract.py | 28 +++++++++++++++++------ contract/views/contract.xml | 2 -- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/contract/models/abstract_contract_line.py b/contract/models/abstract_contract_line.py index a7c603367..5d2598a65 100644 --- a/contract/models/abstract_contract_line.py +++ b/contract/models/abstract_contract_line.py @@ -191,12 +191,18 @@ class ContractAbstractContractLine(models.AbstractModel): """ for line in self: if line.automatic_price: + pricelist = ( + line.contract_id.pricelist_id or + line.contract_id.partner_id.with_context( + force_company=line.contract_id.company_id.id, + ).property_product_pricelist + ) product = line.product_id.with_context( quantity=line.env.context.get( 'contract_line_qty', line.quantity, ), - pricelist=line.contract_id.pricelist_id.id, + pricelist=pricelist.id, partner=line.contract_id.partner_id.id, date=line.env.context.get( 'old_date', fields.Date.context_today(line) diff --git a/contract/models/contract.py b/contract/models/contract.py index a9199ba83..12ef80668 100644 --- a/contract/models/contract.py +++ b/contract/models/contract.py @@ -33,7 +33,8 @@ class ContractContract(models.Model): ondelete='restrict', ) currency_id = fields.Many2one( - related="company_id.currency_id", + compute="_compute_currency_id", + comodel_name="res.currency", string="Currency", readonly=True, ) @@ -147,6 +148,24 @@ class ContractContract(models.Model): ) return invoices + @api.depends("pricelist_id", "partner_id", "journal_id", "company_id") + def _compute_currency_id(self): + for rec in self: + currency = self.env['res.currency'] + if any(rec.contract_line_ids.mapped('automatic_price')): + # Use pricelist currency + currency = ( + rec.pricelist_id.currency_id or + rec.partner_id.with_context( + force_company=rec.company_id.id, + ).property_product_pricelist.currency_id + ) + rec.currency_id = ( + currency.id or + rec.journal_id.currency_id.id or + rec.company_id.currency_id.id + ) + @api.multi def _compute_invoice_count(self): for rec in self: @@ -299,11 +318,6 @@ class ContractContract(models.Model): _("Please define a %s journal for the company '%s'.") % (self.contract_type, self.company_id.name or '') ) - currency = ( - self.pricelist_id.currency_id - or self.partner_id.property_product_pricelist.currency_id - or self.company_id.currency_id - ) invoice_type = 'out_invoice' if self.contract_type == 'purchase': invoice_type = 'in_invoice' @@ -318,7 +332,7 @@ class ContractContract(models.Model): invoice_vals = vinvoice._convert_to_write(vinvoice._cache) invoice_vals.update({ 'name': self.code, - 'currency_id': currency.id, + 'currency_id': self.currency_id.id, 'date_invoice': date_invoice, 'journal_id': journal.id, 'origin': self.name, diff --git a/contract/views/contract.xml b/contract/views/contract.xml index f90da91db..c29928019 100644 --- a/contract/views/contract.xml +++ b/contract/views/contract.xml @@ -178,8 +178,6 @@ options="{'no_create': True}" groups="base.group_multi_company"/>