avoiding loops

This commit is contained in:
mreficent
2017-07-13 11:14:34 +02:00
committed by Jordi Ballester Alomar
parent 6bff8f043a
commit def59aab8b
2 changed files with 34 additions and 36 deletions

View File

@@ -14,44 +14,42 @@ class ProductTemplate(models.Model):
if values.get('type', False): if values.get('type', False):
new_type = values.get('type') new_type = values.get('type')
if new_type == 'product': if new_type == 'product':
for rec in self: variants = self.filtered(lambda r: r.type == 'consu').mapped(
if rec.type == 'consu': 'product_variant_ids')
for variant in rec.product_variant_ids: if variants:
self.env['stock.quant'].search( self.env['stock.quant'].search(
[('product_id', '=', variant.id)]).write( [('product_id', 'in', variants.ids)]).write(
{'cost': 0.0}) {'cost': 0.0})
rec.standard_price = 0.0 variants.standard_price = 0.0
if values.get('cost_method', False): if values.get('cost_method', False):
new_method = values.get('cost_method') new_method = values.get('cost_method')
if new_method == 'real': if new_method == 'real':
for rec in self: variants = self.filtered(lambda r: values.get(
rec_type = values.get('type', False) or rec.type 'type', False) == 'product' or r.type == 'product').mapped(
if rec_type == 'product': 'product_variant_ids')
for variant in rec.product_variant_ids: for variant in variants:
quants = self.env['stock.quant'].search( self.env['stock.quant'].search(
[('product_id', '=', variant.id), [('product_id', 'in', variants.ids),
('location_id.usage', '=', 'internal')]) ('location_id.usage', '=', 'internal')]).write(
quants.write( {'cost': variant.standard_price})
{'cost': rec.standard_price})
elif new_method != 'real': elif new_method != 'real':
for rec in self: variants = self.filtered(lambda r: r.cost_method == 'real').\
if rec.cost_method == 'real': mapped('product_variant_ids')
for variant in variants:
total_cost = 0.0 total_cost = 0.0
total_qty = 0.0 total_qty = 0.0
rounding = rec.uom_id.rounding rounding = variant.uom_id.rounding
for variant in rec.product_variant_ids: for quant in self.env['stock.quant'].search(
quants = self.env['stock.quant'].search(
[('product_id', '=', variant.id), [('product_id', '=', variant.id),
('location_id.usage', '=', 'internal')]) ('location_id.usage', '=', 'internal')]):
for quant in quants:
total_cost += quant.cost * quant.qty total_cost += quant.cost * quant.qty
total_qty += quant.qty total_qty += quant.qty
if total_qty: if total_qty:
avg_cost = total_cost / total_qty avg_cost = total_cost / total_qty
else: else:
avg_cost = 0.0 avg_cost = 0.0
rec.standard_price = float_round( variant.standard_price = \
avg_cost, precision_rounding=rounding) float_round(avg_cost, precision_rounding=rounding)
return super(ProductTemplate, self).write(values) return super(ProductTemplate, self).write(values)