From 5d3ea94d6b59298e069cd13b00e09dc8e2d660b6 Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Tue, 29 Oct 2013 16:09:59 +0100 Subject: [PATCH] [IMP] stock_account: standard price historization did it in my own way bzr revid: qdp-launchpad@openerp.com-20131029150959-rbv9lyzhcx6pi4vc --- addons/stock/stock.py | 8 +++++- addons/stock_account/product.py | 36 +++++++++++++++---------- addons/stock_account/product_view.xml | 28 +++++++++++++++++++ addons/stock_account/standard_prices.py | 19 ++++++------- 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/addons/stock/stock.py b/addons/stock/stock.py index cab7c43d318..1eb57f7d8db 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -1196,6 +1196,12 @@ class stock_move(osv.osv): res.append((line.id, name)) return res + def create(self, cr, uid, vals, context=None): + if vals.get('product_id') and not vals.get('price_unit'): + prod_obj = self.pool.get('product.product') + vals['price_unit'] = prod_obj.browse(cr, uid, vals['product_id'], context=context).standard_price + return super(stock_move, self).create(cr, uid, vals, context=context) + # FP Note: put this on quants, with the auto creation algo # def _check_tracking(self, cr, uid, ids, context=None): # """ Checks if serial number is assigned to stock move or not. @@ -1332,7 +1338,7 @@ class stock_move(osv.osv): "* Available: When products are reserved, it is set to \'Available\'.\n"\ "* Done: When the shipment is processed, the state is \'Done\'."), - 'price_unit': fields.float('Unit Price', help="Technical field used to record the product cost set by the user during a picking confirmation (when average price costing method is used). Value given in company currency and in product uom."), # as it's a technical field, we intentionally don't provide the digits attribute + 'price_unit': fields.float('Unit Price', help="Technical field used to record the product cost set by the user during a picking confirmation (when costing method used is 'average price' or 'real'). Value given in company currency and in product uom."), # as it's a technical field, we intentionally don't provide the digits attribute 'company_id': fields.many2one('res.company', 'Company', required=True, select=True), 'backorder_id': fields.related('picking_id','backorder_id',type='many2one', relation="stock.picking", string="Back Order of", select=True), diff --git a/addons/stock_account/product.py b/addons/stock_account/product.py index 0debb4edd6c..9883ee9ee13 100644 --- a/addons/stock_account/product.py +++ b/addons/stock_account/product.py @@ -166,27 +166,35 @@ class product_product(osv.osv): return move_ids + def create(self, cr, uid, vals, context=None): + product_id = super(product_product, self).create(cr, uid, vals, context=context) + price_history_obj = self.pool['prices.history'] + if vals.get('cost_method') in ('standard', 'average'): + price_history_obj.create(cr, uid, { + 'product_id': product_id, + 'cost': vals.get('standard_price', 0.0), + 'reason': 'standard_price is set', + }, context=context) + def write(self, cr, uid, ids, values, context=None): if 'standard_price' in values: - standard_prices = self.pool['standard.prices'] + price_history_obj = self.pool['prices.history'] for product in self.browse(cr, uid, ids, context=context): - data = { - 'product_id': product.id, - 'cost': values['standard_price'], - 'reason': 'standard_price is changed.', - # Provided by self._defaults: - # 'company_id': - # 'quant_id': - # 'datetime': - } if product.cost_method in ('standard', 'average'): - standard_prices.create(cr, uid, data, context=context) + price_history_obj.create(cr, uid, { + 'product_id': product.id, + 'cost': values['standard_price'], + 'reason': 'standard_price is changed.', + # Provided by self._defaults: + # 'company_id': + # 'quant_id': + # 'datetime': + }, context=context) elif product.cost_method in ('real',): # For a 'real' cost_method, entries in standard_prices are - # created when quants are created. + # created when quants are created. #TODO not true pass - return super(product_product, self).write(cr, uid, ids, values, - context=context) + return super(product_product, self).write(cr, uid, ids, values, context=context) _columns = { 'valuation':fields.property(type='selection', selection=[('manual_periodic', 'Periodical (manual)'), diff --git a/addons/stock_account/product_view.xml b/addons/stock_account/product_view.xml index 2aa7f6d8622..7b310dfacef 100644 --- a/addons/stock_account/product_view.xml +++ b/addons/stock_account/product_view.xml @@ -91,6 +91,34 @@ + + + stock.history.tree + stock.history + + + + + + + + + + + + + + Stock History + stock.history + form + tree,graph,form + + + + diff --git a/addons/stock_account/standard_prices.py b/addons/stock_account/standard_prices.py index f1b134f6187..0738e2562b9 100644 --- a/addons/stock_account/standard_prices.py +++ b/addons/stock_account/standard_prices.py @@ -1,18 +1,13 @@ -""" -Keep track of the ``product.product`` standard prices as they are changed. - -The ``standard.prices`` model records each ``standard`` or ``average`` -``cost_method`` change. For the ``real`` ``cost_method`` it records every -wuants creation. - -""" from openerp import tools from openerp.osv import fields, osv -class price_history(osv.osv): +class prices_history(osv.osv): + """ + Keep track of the ``product.product`` standard prices as they are changed. + """ - _name = 'price.history' + _name = 'prices.history' _rec_name = 'datetime' _columns = { @@ -52,6 +47,7 @@ class stock_history(osv.osv): 'quantity': fields.integer('Quantity'), 'date': fields.datetime('Date'), 'cost': fields.float('Value'), + 'cost_method': fields.char('Cost Method'), } def init(self, cr): @@ -65,9 +61,10 @@ class stock_history(osv.osv): stock_move.product_id AS product_id, stock_move.product_qty AS quantity, stock_move.date AS date, + ir_property.value_text AS cost_method, CASE WHEN ir_property.value_text <> 'real' - THEN (SELECT price_history.cost FROM price_history WHERE price_history.datetime <= stock_move.date AND price_history.product_id = stock_move.product_id ORDER BY price_history.datetime ASC limit 1) + THEN (SELECT prices_history.cost FROM prices_history WHERE prices_history.datetime <= stock_move.date AND prices_history.product_id = stock_move.product_id ORDER BY prices_history.datetime ASC limit 1) ELSE stock_move.price_unit END AS cost FROM