diff --git a/addons/product/product.py b/addons/product/product.py index a076417ebff..4b63100dbee 100644 --- a/addons/product/product.py +++ b/addons/product/product.py @@ -295,6 +295,37 @@ class product_category(osv.osv): ] +class prices_history(osv.osv): + """ + Keep track of the ``product.template`` 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_template_id': fields.many2one('product.template', 'Product Template', 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, + } + + #---------------------------------------------------------- # Products @@ -366,13 +397,33 @@ class product_template(osv.osv): return {'value': {'uom_po_id': uom_id}} return {} + 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 template for a given date''' + product_template_id = super(product_template, self).create(cr, uid, vals, context=context) + price_history_obj = self.pool['prices.history'] + price_history_obj.create(cr, uid, { + 'product_template_id': product_template_id, + 'cost': vals.get('standard_price', 0.0), + 'reason': _('Product template created and standard price set'), + }, context=context) + return product_template_id + def write(self, cr, uid, ids, vals, context=None): + ''' Store the standard price change in order to be able to retrieve the cost of a product template for a given date''' if 'uom_po_id' in vals: new_uom = self.pool.get('product.uom').browse(cr, uid, vals['uom_po_id'], context=context) for product in self.browse(cr, uid, ids, context=context): old_uom = product.uom_po_id if old_uom.category_id.id != new_uom.category_id.id: raise osv.except_osv(_('Unit of Measure categories Mismatch!'), _("New Unit of Measure '%s' must belong to same Unit of Measure category '%s' as of old Unit of Measure '%s'. If you need to change the unit of measure, you may deactivate this product from the 'Procurements' tab and create a new one.") % (new_uom.name, old_uom.category_id.name, old_uom.name,)) + if 'standard_price' in vals: + price_history_obj = self.pool['prices.history'] + for prod_template_id in ids: + price_history_obj.create(cr, uid, { + 'product_template_id': prod_template_id, + 'cost': vals['standard_price'], + 'reason': _('standard price is changed.'), + }, context=context) return super(product_template, self).write(cr, uid, ids, vals, context=context) _defaults = { @@ -414,37 +465,6 @@ 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: @@ -457,12 +477,13 @@ class product_product(osv.osv): def get_history_price(self, cr, uid, product_id, company_id, context=None): if context is None: context = {} + product = self.browse(cr, uid, product_id, context=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) + history_ids = prices_history_obj.search(cr, uid, [('company_id', '=', company_id), ('product_template_id', '=', product.product_tmpl_id.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))) + raise osv.except_osv(_('Error!'), _("No standard price associated for product %s for the given date" % (product.name))) def _product_price(self, cr, uid, ids, name, arg, context=None): res = {} @@ -821,29 +842,6 @@ 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"