From ac4d4a7fbbf0942af6ba289c27316140c21bb718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 19 Mar 2023 19:57:30 +0100 Subject: [PATCH] Use multivalue API to access product multivalue fields Closes #7474 --- modules/account_dunning_fee/dunning.py | 7 +++++-- modules/account_stock_anglo_saxon/stock.py | 5 +++-- modules/account_stock_continental/stock.py | 4 +++- modules/product_cost_fifo/move.py | 8 +++++--- modules/production/production.py | 10 ++-------- modules/purchase/product.py | 9 ++++----- modules/purchase/purchase.py | 4 ++-- modules/sale/product.py | 18 ++++++++++-------- modules/stock/move.py | 6 ++++-- modules/stock_forecast/forecast.py | 9 ++------- modules/stock_shipment_cost/stock.py | 10 ++++++++-- 11 files changed, 48 insertions(+), 42 deletions(-) diff --git a/modules/account_dunning_fee/dunning.py b/modules/account_dunning_fee/dunning.py index 48261f9712..270be9d071 100644 --- a/modules/account_dunning_fee/dunning.py +++ b/modules/account_dunning_fee/dunning.py @@ -30,13 +30,16 @@ class Fee(DeactivableMixin, ModelSQL, ModelView): 'required': Eval('compute_method') == 'percentage', }) + def get_list_price(self, dunning, **pattern): + pattern.setdefault('company', dunning.company.id) + return self.product.get_multivalue('list_price', **pattern) + def get_amount(self, dunning): 'Return fee amount and currency' amount, currency = None, None if self.compute_method == 'list_price': - assert Transaction().context.get('company') == dunning.company.id currency = dunning.company.currency - amount = currency.round(self.product.list_price) + amount = currency.round(self.get_list_price(dunning)) elif self.compute_method == 'percentage': if dunning.second_currency: amount = dunning.amount_second_currency diff --git a/modules/account_stock_anglo_saxon/stock.py b/modules/account_stock_anglo_saxon/stock.py index a12b90a090..f599ed8665 100644 --- a/modules/account_stock_anglo_saxon/stock.py +++ b/modules/account_stock_anglo_saxon/stock.py @@ -51,8 +51,9 @@ class Move(metaclass=PoolMeta): AccountMoveLine = pool.get('account.move.line') Currency = pool.get('currency.currency') lines = super(Move, self)._get_account_stock_move_lines(type_) - if (type_.endswith('supplier') - and self.product.cost_price_method == 'fixed'): + cost_price_method = self.product.get_multivalue( + 'cost_price_method', **self._cost_price_pattern) + if type_.endswith('supplier') and cost_price_method == 'fixed': cost_price = Uom.compute_price(self.product.default_uom, self.cost_price, self.uom) with Transaction().set_context(date=self.effective_date): diff --git a/modules/account_stock_continental/stock.py b/modules/account_stock_continental/stock.py index fd62dde8f3..f8ddeb66b1 100644 --- a/modules/account_stock_continental/stock.py +++ b/modules/account_stock_continental/stock.py @@ -22,10 +22,12 @@ class Move(metaclass=PoolMeta): 'wrong type' move_line = AccountMoveLine() + cost_price_method = self.product.get_multivalue( + 'cost_price_method', **self._cost_price_pattern) if (( type_.endswith('supplier') or type_ in {'in_production', 'in_warehouse'}) - and self.product.cost_price_method != 'fixed'): + and cost_price_method != 'fixed'): unit_price = self.unit_price_company else: unit_price = self.cost_price diff --git a/modules/product_cost_fifo/move.py b/modules/product_cost_fifo/move.py index 13d49f5d6c..ba492e7b64 100644 --- a/modules/product_cost_fifo/move.py +++ b/modules/product_cost_fifo/move.py @@ -116,17 +116,19 @@ class Move(metaclass=PoolMeta): def _do(self): cost_price, to_save = super(Move, self)._do() + cost_price_method = self.product.get_multivalue( + 'cost_price_method', **self._cost_price_pattern) if (self.from_location.type != 'storage' and self.to_location.type == 'storage' - and self.product.cost_price_method == 'fifo'): + and cost_price_method == 'fifo'): cost_price = self._compute_product_cost_price('in') elif (self.to_location.type == 'supplier' and self.from_location.type == 'storage' - and self.product.cost_price_method == 'fifo'): + and cost_price_method == 'fifo'): cost_price = self._compute_product_cost_price('out') elif (self.from_location.type == 'storage' and self.to_location.type != 'storage' - and self.product.cost_price_method == 'fifo'): + and cost_price_method == 'fifo'): fifo_cost_price, cost_price, moves = ( self._update_fifo_out_product_cost_price()) if self.cost_price_required and self.cost_price is None: diff --git a/modules/production/production.py b/modules/production/production.py index 4cbe59cb92..2f991c51e5 100644 --- a/modules/production/production.py +++ b/modules/production/production.py @@ -456,10 +456,7 @@ class Production(ShipmentAssignMixin, Workflow, ModelSQL, ModelView): for input_ in self.inputs: if input_.state == 'cancelled': continue - if input_.cost_price is not None: - cost_price = input_.cost_price - else: - cost_price = input_.product.cost_price + cost_price = input_.get_cost_price() cost += (Decimal(str(input_.internal_quantity)) * cost_price) return round_price(cost) @@ -556,10 +553,7 @@ class Production(ShipmentAssignMixin, Workflow, ModelSQL, ModelView): for input_ in production.inputs: if input_.state == 'cancelled': continue - if input_.cost_price is not None: - cost_price = input_.cost_price - else: - cost_price = input_.product.cost_price + cost_price = input_.get_cost_price() input_quantities[input_.product] += ( Decimal(str(input_.internal_quantity))) input_costs[input_.product] += ( diff --git a/modules/purchase/product.py b/modules/purchase/product.py index 4c62848dce..f34c466cbf 100644 --- a/modules/purchase/product.py +++ b/modules/purchase/product.py @@ -158,7 +158,7 @@ class Product(metaclass=PoolMeta): ''' pool = Pool() Uom = pool.get('product.uom') - User = pool.get('res.user') + Company = pool.get('company.company') Currency = pool.get('currency.currency') Date = pool.get('ir.date') ProductSupplier = pool.get('purchase.product_supplier') @@ -177,14 +177,13 @@ class Product(metaclass=PoolMeta): currency = None if context.get('currency'): currency = Currency(context['currency']) - - user = User(Transaction().user) + elif context.get('company'): + currency = Company(context['company']).currency for product in products: unit_price = product._get_purchase_unit_price(quantity=quantity) default_uom = product.default_uom - default_currency = (user.company.currency if user.company - else None) + default_currency = currency if not uom or default_uom.category != uom.category: product_uom = default_uom else: diff --git a/modules/purchase/purchase.py b/modules/purchase/purchase.py index f8461017e6..25995f9f51 100644 --- a/modules/purchase/purchase.py +++ b/modules/purchase/purchase.py @@ -1377,8 +1377,8 @@ class Line(sequence_ordered(), ModelSQL, ModelView): if self.purchase.party: context['supplier'] = self.purchase.party.id context['purchase_date'] = self.purchase.purchase_date - if self.company: - context['company'] = self.company.id + if self.company: + context['company'] = self.company.id if self.unit: context['uom'] = self.unit.id elif self.product: diff --git a/modules/sale/product.py b/modules/sale/product.py index 7e3bdad481..283ca99615 100644 --- a/modules/sale/product.py +++ b/modules/sale/product.py @@ -170,24 +170,26 @@ class Product(metaclass=PoolMeta): ''' pool = Pool() Uom = pool.get('product.uom') - User = pool.get('res.user') + Company = pool.get('company.company') Currency = pool.get('currency.currency') Date = pool.get('ir.date') today = Date.today() + context = Transaction().context prices = {} assert len(products) == len(set(products)), "Duplicate products" uom = None - if Transaction().context.get('uom'): - uom = Uom(Transaction().context.get('uom')) + if context.get('uom'): + uom = Uom(context['uom']) currency = None if Transaction().context.get('currency'): currency = Currency(Transaction().context.get('currency')) - - user = User(Transaction().user) + company = None + if context.get('company'): + company = Company(context['company']) for product in products: unit_price = product._get_sale_unit_price(quantity=quantity) @@ -198,12 +200,12 @@ class Product(metaclass=PoolMeta): else: unit_price = Uom.compute_price( product.default_uom, unit_price, product.sale_uom) - if currency and user.company and unit_price is not None: - if user.company.currency != currency: + if currency and company and unit_price is not None: + if company.currency != currency: date = Transaction().context.get('sale_date') or today with Transaction().set_context(date=date): unit_price = Currency.compute( - user.company.currency, unit_price, + company.currency, unit_price, currency, round=False) prices[product.id] = unit_price return prices diff --git a/modules/stock/move.py b/modules/stock/move.py index c758115fd1..81facaafa8 100644 --- a/modules/stock/move.py +++ b/modules/stock/move.py @@ -783,13 +783,15 @@ class Move(Workflow, ModelSQL, ModelView): def _do(self): "Return cost_price and a list of moves to save" + cost_price_method = self.product.get_multivalue( + 'cost_price_method', **self._cost_price_pattern) if (self.from_location.type in ('supplier', 'production') and self.to_location.type == 'storage' - and self.product.cost_price_method == 'average'): + and cost_price_method == 'average'): return self._compute_product_cost_price('in'), [] elif (self.to_location.type == 'supplier' and self.from_location.type == 'storage' - and self.product.cost_price_method == 'average'): + and cost_price_method == 'average'): return self._compute_product_cost_price('out'), [] return None, [] diff --git a/modules/stock_forecast/forecast.py b/modules/stock_forecast/forecast.py index c95ddef009..d2f0f34add 100644 --- a/modules/stock_forecast/forecast.py +++ b/modules/stock_forecast/forecast.py @@ -440,7 +440,6 @@ class ForecastLine(ModelSQL, ModelView): 'Get stock moves for the forecast line' pool = Pool() Move = pool.get('stock.move') - Uom = pool.get('product.uom') Date = pool.get('ir.date') assert not self.moves @@ -460,11 +459,6 @@ class ForecastLine(ModelSQL, ModelView): nb_packet = ((self.quantity - self.quantity_executed) // self.minimal_quantity) distribution = self.distribute(delta, nb_packet) - unit_price = None - if self.forecast.destination.type == 'customer': - unit_price = self.product.list_price or 0 - unit_price = Uom.compute_price(self.product.default_uom, - unit_price, self.uom) moves = [] for day, qty in distribution.items(): @@ -479,7 +473,8 @@ class ForecastLine(ModelSQL, ModelView): move.planned_date = from_date + datetime.timedelta(day) move.company = self.forecast.company move.currency = self.forecast.company.currency - move.unit_price = unit_price + move.unit_price = ( + 0 if self.forecast.destination.type == 'customer' else None) moves.append(move) return moves diff --git a/modules/stock_shipment_cost/stock.py b/modules/stock_shipment_cost/stock.py index ae15cc8907..4dba4bb97f 100644 --- a/modules/stock_shipment_cost/stock.py +++ b/modules/stock_shipment_cost/stock.py @@ -30,9 +30,15 @@ class Move(metaclass=PoolMeta): class ShipmentCostMixin: __slots__ = () - carrier = fields.Many2One('carrier', 'Carrier', states={ + carrier = fields.Many2One( + 'carrier', "Carrier", + states={ 'readonly': Eval('shipment_cost_readonly', True), - }) + }, + context={ + 'company': Eval('company', -1), + }, + depends=['company']) cost_currency_used = fields.Function(fields.Many2One( 'currency.currency', "Cost Currency",