[IMP] Fasten up stock valuation report

bzr revid: jco@openerp.com-20140313174244-c7dg2thz6r7d5fek
This commit is contained in:
Josse Colpaert 2014-03-13 18:42:44 +01:00
parent fd5f6e7ccd
commit 13566b866e
1 changed files with 35 additions and 38 deletions

View File

@ -41,27 +41,32 @@ class stock_history(osv.osv):
_auto = False _auto = False
_order = 'date asc' _order = 'date asc'
# def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False): 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) res = super(stock_history, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby)
# if 'inventory_value' in fields: import time
# for line in res: if 'inventory_value' in fields:
# if '__domain' in line: for line in res:
# lines = self.search(cr, uid, line['__domain'], context=context) if '__domain' in line:
# inv_value = 0.0 lines = self.search(cr, uid, line['__domain'], context=context)
# for line2 in self.browse(cr, uid, lines, context=context): inv_value = 0.0
# inv_value += line2.inventory_value product_obj = self.pool.get("product.product")
# line['inventory_value'] = inv_value print time.time()
# return res for line_rec in self.browse(cr, uid, lines, context=context):
# inv_value += line_rec.quantity * product_obj.get_history_price(cr, uid, line_rec.product_id.id, line_rec.company_id.id, context=context)
# def _get_inventory_value(self, cr, uid, ids, name, attr, context=None): line['inventory_value'] = inv_value
# product_obj = self.pool.get("product.product") print "end", time.time()
# res = {} return res
# for line in self.browse(cr, uid, ids, context=context):
# if line.product_id.cost_method == 'real': def _get_inventory_value(self, cr, uid, ids, name, attr, context=None):
# res[line.id] = line.quantity * line.price_unit_on_quant print "pass"
# else: product_obj = self.pool.get("product.product")
# res[line.id] = line.quantity * product_obj.get_history_price(cr, uid, line.product_id.id, line.company_id.id, context=context) res = {}
# return 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_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
_columns = { _columns = {
'move_id': fields.many2one('stock.move', 'Stock Move', required=True), 'move_id': fields.many2one('stock.move', 'Stock Move', required=True),
@ -74,8 +79,7 @@ class stock_history(osv.osv):
'date': fields.datetime('Operation Date'), 'date': fields.datetime('Operation Date'),
'price_unit_on_quant': fields.float('Value'), 'price_unit_on_quant': fields.float('Value'),
'cost_method': fields.char('Cost Method'), 'cost_method': fields.char('Cost Method'),
'inventory_value': fields.float('Inventory val'), 'inventory_value': fields.function(_get_inventory_value, string="Inventory Value", type='float', readonly=True),
#'inventory_value': fields.function(_get_inventory_value, string="Inventory Value", type='float', readonly=True),
} }
def init(self, cr): def init(self, cr):
@ -91,12 +95,9 @@ class stock_history(osv.osv):
quant.qty AS quantity, quant.qty AS quantity,
stock_move.date AS date, stock_move.date AS date,
ir_property.value_text AS cost_method, ir_property.value_text AS cost_method,
quant.cost as price_unit_on_quant, quant.cost as price_unit_on_quant
rel.cost as inventory_value
FROM FROM
stock_move stock_quant as quant, stock_quant_move_rel, 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 LEFT JOIN
stock_location location ON stock_move.location_dest_id = location.id stock_location location ON stock_move.location_dest_id = location.id
LEFT JOIN LEFT JOIN
@ -105,10 +106,8 @@ class stock_history(osv.osv):
product_template ON product_template.id = product_product.product_tmpl_id product_template ON product_template.id = product_product.product_tmpl_id
LEFT JOIN LEFT JOIN
ir_property ON (ir_property.name = 'cost_method' and ir_property.res_id = 'product.template,' || product_template.id::text) ir_property ON (ir_property.name = 'cost_method' and ir_property.res_id = 'product.template,' || product_template.id::text)
LEFT JOIN WHERE stock_move.state = 'done' AND location.usage = 'internal' AND stock_quant_move_rel.quant_id = quant.id
(select p.cost as cost, p.product_template_id as tmpl from prices_history as p, (select max(datetime) as datetime, product_template_id from prices_history group by company_id, product_template_id, datetime) as b where b.product_template_id = p.product_template_id and p.datetime = b.datetime) rel AND stock_quant_move_rel.move_id = stock_move.id
ON rel.tmpl = product_template.id
WHERE stock_move.state = 'done' AND location.usage = 'internal'
) UNION ) UNION
(SELECT (SELECT
'-' || stock_move.id::text || '-' || quant.id::text AS id, '-' || stock_move.id::text || '-' || quant.id::text AS id,
@ -119,12 +118,9 @@ class stock_history(osv.osv):
- quant.qty AS quantity, - quant.qty AS quantity,
stock_move.date AS date, stock_move.date AS date,
ir_property.value_text AS cost_method, ir_property.value_text AS cost_method,
quant.cost as price_unit_on_quant, quant.cost as price_unit_on_quant
0.0 as inventory_value
FROM FROM
stock_move stock_quant as quant, stock_quant_move_rel, 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 LEFT JOIN
stock_location location ON stock_move.location_id = location.id stock_location location ON stock_move.location_id = location.id
LEFT JOIN LEFT JOIN
@ -133,6 +129,7 @@ class stock_history(osv.osv):
product_template ON product_template.id = product_product.product_tmpl_id product_template ON product_template.id = product_product.product_tmpl_id
LEFT JOIN LEFT JOIN
ir_property ON (ir_property.name = 'cost_method' and ir_property.res_id = 'product.template,' || product_template.id::text) 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' WHERE stock_move.state = 'done' AND location.usage = 'internal' AND stock_quant_move_rel.quant_id = quant.id
AND stock_quant_move_rel.move_id = stock_move.id
) )
)""") )""")