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

@@ -12,7 +12,7 @@ product's costing method or the type of product requires proper readjustments:
Changing the type of product. Changing the type of product.
* From consumable to stockable: the resulting inventory value will be reset * From consumable to stockable: the resulting inventory value will be reset
to 0, since everything that was received as a stockable was expensed out to 0, since everything that was received as a stockable was expensed out
at the time of entering the supplier invoice. at the time of entering the supplier invoice.
Changing the costing method: Changing the costing method:

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')
total_cost = 0.0 for variant in variants:
total_qty = 0.0 total_cost = 0.0
rounding = rec.uom_id.rounding total_qty = 0.0
for variant in rec.product_variant_ids: rounding = variant.uom_id.rounding
quants = self.env['stock.quant'].search( for quant in self.env['stock.quant'].search(
[('product_id', '=', variant.id), [('product_id', '=', variant.id),
('location_id.usage', '=', 'internal')]) ('location_id.usage', '=', 'internal')]):
total_cost += quant.cost * quant.qty
for quant in quants: total_qty += quant.qty
total_cost += quant.cost * quant.qty if total_qty:
total_qty += quant.qty avg_cost = total_cost / total_qty
if total_qty: else:
avg_cost = total_cost / total_qty avg_cost = 0.0
else: variant.standard_price = \
avg_cost = 0.0 float_round(avg_cost, precision_rounding=rounding)
rec.standard_price = float_round(
avg_cost, precision_rounding=rounding)
return super(ProductTemplate, self).write(values) return super(ProductTemplate, self).write(values)