Merge pull request #342 from Eficent/9.0-fix-stock_account_change_product_valuation

[9.0][FIX] stock_account_change_product_valuation: fix no posible to …
This commit is contained in:
Lois Rilo
2018-03-23 14:22:58 +01:00
committed by GitHub
3 changed files with 54 additions and 28 deletions

View File

@@ -3,3 +3,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import product
from . import product_category

View File

@@ -18,38 +18,45 @@ class ProductTemplate(models.Model):
'product_variant_ids')
if variants:
self.env['stock.quant'].search(
[('product_id', 'in', variants.ids)]).write(
[('product_id', 'in', variants.ids)]).sudo().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':
variants = self.filtered(lambda r: values.get(
'type', False) == 'product' or r.type == 'product').mapped(
product_type = values.get('type', False)
self.update_cost_method(new_method, product_type)
return super(ProductTemplate, self).write(values)
@api.multi
def update_cost_method(self, new_method, product_type):
if new_method == 'real':
variants = self.filtered(
lambda r: product_type == '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':
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')]):
for rec in variants:
quants = self.env['stock.quant'].search(
[('product_id', '=', rec.product_variant_ids.id),
('location_id.usage', '=', 'internal')])
quants.sudo().write(
{'cost': rec.standard_price})
elif new_method != 'real':
for variant in self.filtered(lambda r: r.cost_method == 'real'):
total_cost = 0.0
total_qty = 0.0
rounding = variant.uom_id.rounding
for variant in variant.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
variant.standard_price = \
float_round(avg_cost, precision_rounding=rounding)
return super(ProductTemplate, self).write(values)
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 True

View File

@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from openerp import api, models
class ProductCategory(models.Model):
_inherit = "product.category"
@api.multi
def write(self, values):
if values.get('property_cost_method', False):
products_to_upd = self.env['product.template'].search([
('categ_id', '=', self.ids)])
products_to_upd.write({
'cost_method': values.get('property_cost_method')})
return super(ProductCategory, self).write(values)