From a28ba8dbf3c4ffab72b47a3b72215c6215fec1e3 Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Mon, 8 Nov 2021 10:47:12 +0100 Subject: [PATCH] [IMP] product: harmonize price_compute behavior * consistent choice of company for currency, currency rates and prices * support currency conversion date through params --- addons/product/models/product.py | 10 +++++++--- addons/product/models/product_template.py | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/addons/product/models/product.py b/addons/product/models/product.py index aa5c82b374d..62d5827f2c6 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -663,7 +663,7 @@ class ProductProduct(models.Model): res |= seller return res.sorted('price')[:1] - def price_compute(self, price_type, uom=False, currency=False, company=None): + def price_compute(self, price_type, uom=False, currency=False, company=None, date=False): # TDE FIXME: delegate to template or not ? fields are reencoded here ... # compatibility about context keys used a bit everywhere in the code if not uom and self._context.get('uom'): @@ -676,7 +676,11 @@ class ProductProduct(models.Model): # standard_price field can only be seen by users in base.group_user # Thus, in order to compute the sale price from the cost for users not in this group # We fetch the standard price as the superuser - products = self.with_company(company or self.env.company).sudo() + products = self.with_company(company).sudo() + if not company: + company = self.env.company + + date = date or fields.Date.today() prices = dict.fromkeys(self.ids, 0.0) for product in products: @@ -696,7 +700,7 @@ class ProductProduct(models.Model): # This is right cause a field cannot be in more than one currency if currency: prices[product.id] = product.currency_id._convert( - prices[product.id], currency, product.company_id, fields.Date.today()) + prices[product.id], currency, company, date) return prices diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index ea4c690e9b5..93c3792e65b 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -609,7 +609,7 @@ class ProductTemplate(models.Model): }, } - def price_compute(self, price_type, uom=False, currency=False, company=None): + def price_compute(self, price_type, uom=False, currency=False, company=None, date=False): # TDE FIXME: delegate to template or not ? fields are reencoded here ... # compatibility about context keys used a bit everywhere in the code if not uom and self._context.get('uom'): @@ -625,7 +625,7 @@ class ProductTemplate(models.Model): templates = self.with_company(company).sudo() if not company: company = self.env.company - date = self.env.context.get('date') or fields.Date.today() + date = date or fields.Date.today() prices = dict.fromkeys(self.ids, 0.0) for template in templates: