[IMP] stock_account: added view (maybe the subselect is wrong though) for the quant history.

bzr revid: vmt@openerp.com-20130801151803-6nxx70rlifdw330p
This commit is contained in:
Vo Minh Thu
2013-08-01 17:18:03 +02:00
parent 22d5e6ec9b
commit 98c886a454
3 changed files with 54 additions and 44 deletions
+12
View File
@@ -131,6 +131,18 @@ class stock_location(osv.osv):
# Quants
#----------------------------------------------------------
# Useful model for the stock_quant_move_rel many2many relation table.
# In particular provide a ID column used in reporting (see the quant_history
# model).
class quant_history_rel(osv.osv):
_name = 'quant.history.rel'
_table = 'stock_quant_move_rel'
_columns = {
'move_id': fields.many2one('stock.move'),
'quant_id': fields.many2one('stock.quant'),
}
class stock_quant(osv.osv):
"""
Quants are the smallest unit of stock physical instances
+37 -42
View File
@@ -10,20 +10,20 @@ wuants creation.
import openerp
from openerp.osv import expression
class standard_prices(openerp.osv.orm.Model):
class price_history(openerp.osv.orm.Model):
_name = 'standard.prices'
_name = 'price.history'
_columns = {
'company_id': openerp.osv.fields.many2one('res.company',
required=True),
'quant_id': openerp.osv.fields.many2one('stock.quant'),
'product_id': openerp.osv.fields.many2one('product.product'),
'product_id': openerp.osv.fields.many2one('product.product'), # required = True
'datetime': openerp.osv.fields.datetime(),
'cost': openerp.osv.fields.float(), # called standard_price on
# product.product
'datetime': openerp.osv.fields.datetime(),
'reason': openerp.osv.fields.char(),
# TODO 'origin': openerp.osv.fields.reference(),
'quant_id': openerp.osv.fields.many2one('stock.quant'),
}
def _get_default_company(self, cr, uid, context=None):
@@ -40,45 +40,40 @@ class standard_prices(openerp.osv.orm.Model):
'company_id': _get_default_company,
}
class stock_value(openerp.osv.orm.Model):
class quant_history(openerp.osv.orm.Model):
_name = 'stock.value'
_name = 'quant.history'
_auto = False
_columns = {
'id': openerp.osv.fields.integer(),
'move_id': openerp.osv.fields.many2one('stock.move'),
'quant_id': openerp.osv.fields.many2one('stock.quant'),
'location_id': openerp.osv.fields.many2one('stock.location'),
'product_id': openerp.osv.fields.many2one('product.product'),
'quantity': openerp.osv.fields.integer(),
'date': openerp.osv.fields.datetime(),
'cost': openerp.osv.fields.float(),
}
def _get_value(self, cr, uid, location_id, product_id, moment, context=None):
# stock_location = self.pool['stock.location']
stock_move = self.pool['stock.move']
product_product = self.pool['product.product']
# Fetch stock moves completed before the requested date.
domain = expression.AND([
[('product_id', '=', product_id)],
[('state', '=', 'done')],
[('date', '<=', moment)],
])
outgoing_domain = [('location_id', '=', location_id)]
incoming_domain = [('location_dest_id', '=', location_id)]
move_ids = stock_move.search(cr, uid,
expression.AND([
expression.OR([incoming_domain, outgoing_domain]),
domain]),
context=context)
product = product_product.browse(cr, uid, [product_id],
context=context)[0]
if product.cost_method == 'standard':
quantity = 0
for move in stock_move.browse(cr, uid, move_ids, context=context):
if move.location_id == move.location_dest_id:
pass
elif move.location_id == location_id:
# outgoing move
quantity -= move.product_uom_qty
elif move.location_dest_id == location_id:
quantity += move.product_uom_qty
# incoming move
return quantity * product.standard_price # TODO from standard.prices
return 123456
def init(self, cr):
openerp.tools.drop_view_if_exists(cr, 'stock_valuation')
cr.execute("""
create or replace view quant_history as (
select
history.id as id,
history.move_id as move_id,
history.quant_id as quant_id,
stock_quant.location_id as location_id,
stock_quant.product_id as product_id,
stock_quant.qty as quantity,
stock_move.date as date,
(select price_history.cost from price_history where price_history.datetime < stock_move.date order by price_history.datetime asc limit 1) as cost
from
stock_quant_move_rel as history
left join
stock_move on stock_move.id = history.move_id
left join
stock_quant on stock_quant.id = history.quant_id
)""")
@@ -7,7 +7,7 @@ from openerp.tests import common
class test_transaction_case(common.TransactionCase):
def test_00(self):
def _test_00(self):
cr, uid = self.cr, self.uid
location_id = self.registry('stock.location').create(cr, uid,
{'name': 'Test Location A'})
@@ -20,7 +20,7 @@ class test_transaction_case(common.TransactionCase):
location_id, product_id, openerp.osv.fields.datetime.now())
self.assertEqual(value, 0)
def test_01(self):
def _test_01(self):
cr, uid = self.cr, self.uid
stock_location_company = self.registry('ir.model.data').get_object(cr, uid, 'stock', 'stock_location_company')
location_id = self.registry('stock.location').create(cr, uid,
@@ -45,6 +45,9 @@ class test_transaction_case(common.TransactionCase):
self.assertEqual(value, 0)
self.registry('stock.move').action_done(cr, uid, [move_id])
value = self.registry('stock.value')._get_value(cr, uid,
location_id, product_id, openerp.osv.fields.datetime.now())
self.assertEqual(value, 10)
if __name__ == '__main__':
unittest2.main()