|
|
"""Compute totals of multiple svl related values"""
self.value_svl = 0
self.quantity_svl = 0
self.avg_cost = 0
self.total_value = 0
self.company_currency_id = False
lots = self.filtered(lambda l: l.product_id.lot_valuated)
if not lots:
return
company_id = self.env.company
self.company_currency_id = company_id.currency_id
domain = [
*self.env['stock.valuation.layer']._check_company_domain(company_id),
('lot_id', 'in', lots.ids),
]
if self.env.context.get('to_date'):
to_date = fields.Datetime.to_datetime(self.env.context['to_date'])
domain.append(('create_date', '<=', to_date))
groups = self.env['stock.valuation.layer']._read_group(
domain,
groupby=['lot_id'],
aggregates=['value:sum', 'quantity:sum'],
)
group_mapping = {lot: aggregates for lot, *aggregates in groups}
for lot in lots:
value_sum, quantity_sum = group_mapping.get(lot._origin, (0, 0))
value_svl = self.company_currency_id.round(value_sum)
avg_cost = value_svl / quantity_sum if quantity_sum else 0
lot.value_svl = value_svl
lot.quantity_svl = quantity_sum
lot.avg_cost = avg_cost
lot.total_value = avg_cost * quantity_sum
|
|