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.
* 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.
Changing the costing method:

View File

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