From ae0276999c1d8fbbf88658ced80136cc003fe0c0 Mon Sep 17 00:00:00 2001 From: lreficent Date: Mon, 2 Oct 2017 14:53:32 +0200 Subject: [PATCH] [9.0][FIX] stock_account_change_product_valuation: fix no posible to use logic from UI and security issues. --- .../models/__init__.py | 1 + .../models/product.py | 63 ++++++++++--------- .../models/product_category.py | 18 ++++++ 3 files changed, 54 insertions(+), 28 deletions(-) create mode 100644 stock_account_change_product_valuation/models/product_category.py diff --git a/stock_account_change_product_valuation/models/__init__.py b/stock_account_change_product_valuation/models/__init__.py index 7f738af89..50639b626 100644 --- a/stock_account_change_product_valuation/models/__init__.py +++ b/stock_account_change_product_valuation/models/__init__.py @@ -3,3 +3,4 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from . import product +from . import product_category diff --git a/stock_account_change_product_valuation/models/product.py b/stock_account_change_product_valuation/models/product.py index 53e6589fd..34252d83e 100644 --- a/stock_account_change_product_valuation/models/product.py +++ b/stock_account_change_product_valuation/models/product.py @@ -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 diff --git a/stock_account_change_product_valuation/models/product_category.py b/stock_account_change_product_valuation/models/product_category.py new file mode 100644 index 000000000..74c91de65 --- /dev/null +++ b/stock_account_change_product_valuation/models/product_category.py @@ -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)