From f2ef7dc29b779a37c75ad22062767185588e054c Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Fri, 6 Dec 2013 11:31:50 +0100 Subject: [PATCH] [REF] stock_account, product: moved the standard price historization in 'product' module, as it doesn't depend of stock (or account) at all and it was causing problems for products created before stock_account was installed, like all demo data products (no entry for them in prices_history table) bzr revid: qdp-launchpad@openerp.com-20131206103150-2mnvsipy9uai3xlk --- addons/product/product.py | 67 +++++++++++++++++++++++++++++++- addons/stock/stock.py | 2 +- addons/stock_account/product.py | 68 --------------------------------- 3 files changed, 67 insertions(+), 70 deletions(-) diff --git a/addons/product/product.py b/addons/product/product.py index 6797d9a31ea..a076417ebff 100644 --- a/addons/product/product.py +++ b/addons/product/product.py @@ -21,6 +21,7 @@ import math import re +import time from _common import rounding @@ -413,15 +414,56 @@ class product_template(osv.osv): return super(product_template, self).name_get(cr, user, ids, context) +class prices_history(osv.osv): + """ + Keep track of the ``product.product`` standard prices as they are changed. + """ + + _name = 'prices.history' + _rec_name = 'datetime' + _order = 'datetime desc' + + _columns = { + 'company_id': fields.many2one('res.company', required=True), + 'product_id': fields.many2one('product.product', 'Product', required=True), + 'datetime': fields.datetime('Historization Time'), + 'cost': fields.float('Historized Cost'), + 'reason': fields.char('Reason'), + } + + def _get_default_company(self, cr, uid, context=None): + if 'force_company' in context: + return context['force_company'] + else: + company = self.pool['res.users'].browse(cr, uid, uid, + context=context).company_id + return company.id if company else False + + _defaults = { + 'datetime': fields.datetime.now, + 'company_id': _get_default_company, + } + + class product_product(osv.osv): def view_header_get(self, cr, uid, view_id, view_type, context=None): if context is None: context = {} res = super(product_product, self).view_header_get(cr, uid, view_id, view_type, context) if (context.get('categ_id', False)): - return _('Products: ')+self.pool.get('product.category').browse(cr, uid, context['categ_id'], context=context).name + return _('Products: ') + self.pool.get('product.category').browse(cr, uid, context['categ_id'], context=context).name return res + def get_history_price(self, cr, uid, product_id, company_id, context=None): + if context is None: + context = {} + date = context.get('history_date', time.strftime('%Y-%m-%d %H:%M:%s')) + prices_history_obj = self.pool.get('prices.history') + history_ids = prices_history_obj.search(cr, uid, [('company_id', '=', company_id), ('product_id', '=', product_id), ('datetime', '<=', date)], limit=1) + if history_ids: + return prices_history_obj.read(cr, uid, history_ids[0], ['cost'], context=context)['cost'] + raise osv.except_osv(_('Error!'), _("No standard price associated for product with ID %d for the given date" % (product_id))) + def _product_price(self, cr, uid, ids, name, arg, context=None): res = {} if context is None: @@ -779,6 +821,29 @@ class product_product(osv.osv): args.append((('categ_id', 'child_of', context['search_default_categ_id']))) return super(product_product, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count) + def create(self, cr, uid, vals, context=None): + ''' Store the initial standard price in order to be able to retrieve the cost of a product for a given date''' + product_id = super(product_product, self).create(cr, uid, vals, context=context) + price_history_obj = self.pool['prices.history'] + price_history_obj.create(cr, uid, { + 'product_id': product_id, + 'cost': vals.get('standard_price', 0.0), + 'reason': _('Product created and standard price set'), + }, context=context) + return product_id + + def write(self, cr, uid, ids, values, context=None): + ''' Store the standard price change in order to be able to retrieve the cost of a product for a given date''' + if 'standard_price' in values: + price_history_obj = self.pool['prices.history'] + for product in self.browse(cr, uid, ids, context=context): + price_history_obj.create(cr, uid, { + 'product_id': product.id, + 'cost': values['standard_price'], + 'reason': _('standard price is changed.'), + }, context=context) + return super(product_product, self).write(cr, uid, ids, values, context=context) + class product_packaging(osv.osv): _name = "product.packaging" diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 984f3fc4b12..93fd62f20ca 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -1254,7 +1254,7 @@ class stock_move(osv.osv): 'priority': fields.selection([('0', 'Not urgent'), ('1', 'Urgent')], 'Priority'), 'create_date': fields.datetime('Creation Date', readonly=True, select=True), 'date': fields.datetime('Date', required=True, select=True, help="Move date: scheduled date until move is done, then date of actual move processing", states={'done': [('readonly', True)]}), - 'date_expected': fields.datetime('Scheduled Date', states={'done': [('readonly', True)]}, required=True, select=True, help="Scheduled date for the processing of this move"), + 'date_expected': fields.datetime('Expected Date', states={'done': [('readonly', True)]}, required=True, select=True, help="Scheduled date for the processing of this move"), 'product_id': fields.many2one('product.product', 'Product', required=True, select=True, domain=[('type', '<>', 'service')], states={'done': [('readonly', True)]}), # TODO: improve store to add dependency on product UoM 'product_qty': fields.function(_quantity_normalize, type='float', store=True, string='Quantity', diff --git a/addons/stock_account/product.py b/addons/stock_account/product.py index 54c86f7cf11..9681084a6c8 100644 --- a/addons/stock_account/product.py +++ b/addons/stock_account/product.py @@ -21,55 +21,10 @@ from openerp.osv import fields, osv from openerp.tools.translate import _ -import time - -class prices_history(osv.osv): - """ - Keep track of the ``product.product`` standard prices as they are changed. - """ - - _name = 'prices.history' - _rec_name = 'datetime' - _order = 'datetime desc' - - _columns = { - 'company_id': fields.many2one('res.company', required=True), - 'product_id': fields.many2one('product.product', 'Product', required=True), - 'datetime': fields.datetime('Historization Time'), - 'cost': fields.float('Historized Cost'), - 'reason': fields.char('Reason'), - # TODO 'origin': openerp.osv.fields.reference(), - #'quant_id': openerp.osv.fields.many2one('stock.quant'), - } - - def _get_default_company(self, cr, uid, context=None): - if 'force_company' in context: - return context['force_company'] - else: - company = self.pool['res.users'].browse(cr, uid, uid, - context=context).company_id - return company.id if company else False - - _defaults = { - #'quant_id': False, - 'datetime': fields.datetime.now, - 'company_id': _get_default_company, - } - class product_product(osv.osv): _inherit = "product.product" - def get_history_price(self, cr, uid, product_id, company_id, context=None): - if context is None: - context = {} - date = context.get('history_date', time.strftime('%Y-%m-%d %H:%M:%s')) - prices_history_obj = self.pool.get('prices.history') - history_ids = prices_history_obj.search(cr, uid, [('company_id', '=', company_id), ('product_id', '=', product_id), ('datetime', '<=', date)], limit=1) - if history_ids: - return prices_history_obj.read(cr, uid, history_ids[0], ['cost'], context=context)['cost'] - raise osv.except_osv(_('Error!'), _("No standard price associated for product with ID %d for the given date" % (product_id))) - def get_product_accounts(self, cr, uid, product_id, context=None): """ To get the stock input account, stock output account and stock journal related to product. @param product_id: product id @@ -210,29 +165,6 @@ class product_product(osv.osv): return move_ids - def create(self, cr, uid, vals, context=None): - ''' Store the initial standard price in order to be able to retrieve the cost of a product for a given date (to make stock valuation at date)''' - product_id = super(product_product, self).create(cr, uid, vals, context=context) - price_history_obj = self.pool['prices.history'] - price_history_obj.create(cr, uid, { - 'product_id': product_id, - 'cost': vals.get('standard_price', 0.0), - 'reason': _('Product created and standard price set'), - }, context=context) - return product_id - - def write(self, cr, uid, ids, values, context=None): - ''' Store the standard price change in order to be able to retrieve the cost of a product for a given date (to make stock valuation at date)''' - if 'standard_price' in values: - price_history_obj = self.pool['prices.history'] - for product in self.browse(cr, uid, ids, context=context): - price_history_obj.create(cr, uid, { - 'product_id': product.id, - 'cost': values['standard_price'], - 'reason': _('standard price is changed.'), - }, 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)'), ('real_time', 'Real Time (automated)')], string='Inventory Valuation',