diff --git a/addons/product/product.py b/addons/product/product.py index 6797d9a31ea..4b63100dbee 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 @@ -294,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 @@ -365,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 = { @@ -419,9 +471,20 @@ class product_product(osv.osv): 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 = {} + 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_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 %s for the given date" % (product.name))) + def _product_price(self, cr, uid, ids, name, arg, context=None): res = {} if context is None: diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 984f3fc4b12..d67f98d8f6b 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', @@ -2532,7 +2532,6 @@ class stock_warehouse(osv.osv): output_loc = warehouse.lot_stock_id picking_type_obj.write(cr, uid, warehouse.in_type_id.id, {'default_location_dest_id': input_loc.id}, context=context) picking_type_obj.write(cr, uid, warehouse.out_type_id.id, {'default_location_src_id': output_loc.id}, context=context) - picking_type_obj.write(cr, uid, warehouse.int_type_id.id, {'active': new_reception_step != 'one_step'}, context=context) picking_type_obj.write(cr, uid, warehouse.pick_type_id.id, {'active': new_delivery_step != 'ship_only'}, context=context) picking_type_obj.write(cr, uid, warehouse.pack_type_id.id, {'active': new_delivery_step == 'pick_pack_ship'}, context=context) @@ -2666,7 +2665,7 @@ class stock_warehouse(osv.osv): 'sequence_id': int_seq_id, 'default_location_src_id': wh_stock_loc.id, 'default_location_dest_id': wh_stock_loc.id, - 'active': reception_steps != 'one_step', + 'active': True, 'pack': False, 'color': color}, context=context) pack_type_id = picking_type_obj.create(cr, uid, vals={ 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', diff --git a/addons/stock_account/wizard/stock_valuation_history.py b/addons/stock_account/wizard/stock_valuation_history.py index 1bbd70ac204..a56726bb1c6 100644 --- a/addons/stock_account/wizard/stock_valuation_history.py +++ b/addons/stock_account/wizard/stock_valuation_history.py @@ -21,7 +21,8 @@ class wizard_valuation_history(osv.osv_memory): data = self.read(cr, uid, ids, context=context)[0] ctx = context.copy() ctx['history_date'] = data['date'] - ctx['group_by'] = ['product_id'] + ctx['search_default_group_by_product'] = True + ctx['search_default_group_by_location'] = True return { 'domain': "[('date', '<=', '" + data['date'] + "')]", 'name': _('Stock Value At Date'), @@ -36,6 +37,7 @@ class wizard_valuation_history(osv.osv_memory): class stock_history(osv.osv): _name = 'stock.history' _auto = False + _order = 'date asc' def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False): res = super(stock_history, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby) @@ -54,7 +56,7 @@ class stock_history(osv.osv): res = {} for line in self.browse(cr, uid, ids, context=context): if line.product_id.cost_method == 'real': - res[line.id] = line.quantity * line.price_unit_on_move + res[line.id] = line.quantity * line.price_unit_on_quant else: res[line.id] = line.quantity * product_obj.get_history_price(cr, uid, line.product_id.id, line.company_id.id, context=context) return res @@ -68,7 +70,7 @@ class stock_history(osv.osv): 'product_categ_id': fields.many2one('product.category', 'Product Category', required=True), 'quantity': fields.integer('Quantity'), 'date': fields.datetime('Operation Date'), - 'price_unit_on_move': fields.float('Value'), + 'price_unit_on_quant': fields.float('Value'), 'cost_method': fields.char('Cost Method'), 'inventory_value': fields.function(_get_inventory_value, string="Inventory Value", type='float', readonly=True), } @@ -77,23 +79,52 @@ class stock_history(osv.osv): tools.drop_view_if_exists(cr, 'stock_history') cr.execute(""" CREATE OR REPLACE VIEW stock_history AS ( - SELECT - stock_move.id AS id, + (SELECT + stock_move.id::text || '-' || quant.id::text AS id, stock_move.id AS move_id, stock_move.location_dest_id AS location_id, stock_move.product_id AS product_id, product_template.categ_id AS product_categ_id, - stock_move.product_qty AS quantity, + quant.qty AS quantity, stock_move.date AS date, ir_property.value_text AS cost_method, - stock_move.price_unit as price_unit_on_move + quant.cost as price_unit_on_quant FROM stock_move + LEFT JOIN + stock_quant quant ON quant.id IN (SELECT quant_id FROM stock_quant_move_rel WHERE move_id = stock_move.id) + LEFT JOIN + stock_location location ON stock_move.location_dest_id = location.id LEFT JOIN product_product ON product_product.id = stock_move.product_id LEFT JOIN product_template ON product_template.id = product_product.product_tmpl_id LEFT JOIN ir_property ON (ir_property.name = 'cost_method' and ir_property.res_id = 'product.template,' || product_template.id::text) - WHERE stock_move.state = 'done' + WHERE stock_move.state = 'done' AND location.usage = 'internal' + ) UNION + (SELECT + '-' || stock_move.id::text || '-' || quant.id::text AS id, + stock_move.id AS move_id, + stock_move.location_id AS location_id, + stock_move.product_id AS product_id, + product_template.categ_id AS product_categ_id, + - quant.qty AS quantity, + stock_move.date AS date, + ir_property.value_text AS cost_method, + quant.cost as price_unit_on_quant + FROM + stock_move + LEFT JOIN + stock_quant quant ON quant.id IN (SELECT quant_id FROM stock_quant_move_rel WHERE move_id = stock_move.id) + LEFT JOIN + stock_location location ON stock_move.location_id = location.id + LEFT JOIN + product_product ON product_product.id = stock_move.product_id + LEFT JOIN + product_template ON product_template.id = product_product.product_tmpl_id + LEFT JOIN + ir_property ON (ir_property.name = 'cost_method' and ir_property.res_id = 'product.template,' || product_template.id::text) + WHERE stock_move.state = 'done' AND location.usage = 'internal' + ) )""") diff --git a/addons/stock_account/wizard/stock_valuation_history_view.xml b/addons/stock_account/wizard/stock_valuation_history_view.xml index e00404e757b..273e77aebff 100644 --- a/addons/stock_account/wizard/stock_valuation_history_view.xml +++ b/addons/stock_account/wizard/stock_valuation_history_view.xml @@ -38,7 +38,7 @@ Stock Value At Date stock.history - + @@ -60,7 +60,8 @@ - + +