Use multivalue API to access product multivalue fields

Closes #7474
This commit is contained in:
Cédric Krier committed 2023-03-19 19:57:30 +01:00
1 parent 5c7648a6e1
commit ac4d4a7fbb
11 files changed
+48 -42

No files matched your search

+5 -2
View File
@@ -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
+3 -2
View File
@@ -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):
+3 -1
View File
@@ -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
+5 -3
View File
@@ -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:
+2 -8
View File
@@ -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] += (
+4 -5
View File
@@ -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:
+2 -2
View File
@@ -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:
+10 -8
View File
@@ -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
+4 -2
View File
@@ -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, []
+2 -7
View File
@@ -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
+8 -2
View File
@@ -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",