[IMP] stock_account: started _get_value(location, product, datetime).
This should be replaced by a SQL view. bzr revid: vmt@openerp.com-20130801094553-1vuorucdyne38ln3
This commit is contained in:
@@ -167,32 +167,24 @@ class product_product(osv.osv):
|
||||
return move_ids
|
||||
|
||||
def write(self, cr, uid, ids, values, context=None):
|
||||
standard_prices = self.pool['standard.prices']
|
||||
for record in self.browse(cr, uid, ids, context=context):
|
||||
if record.cost_method == 'standard':
|
||||
if 'standard_price' in values:
|
||||
standard_prices = self.pool['standard.prices']
|
||||
for product in self.browse(cr, uid, ids, context=context):
|
||||
data = {
|
||||
'company_id': False,
|
||||
'quant_id': False,
|
||||
'product_id': False,
|
||||
'cost': False,
|
||||
'date': False,
|
||||
'reason': False,
|
||||
'product_id': product.id,
|
||||
'cost': values['standard_price'],
|
||||
'reason': 'standard_price is changed.',
|
||||
# Provided by self._defaults:
|
||||
# 'company_id':
|
||||
# 'quant_id':
|
||||
# 'datetime':
|
||||
}
|
||||
standard_prices.create(cr, uid, data, context=context)
|
||||
elif record.cost_method == 'average':
|
||||
data = {
|
||||
'company_id': False,
|
||||
'quant_id': False,
|
||||
'product_id': False,
|
||||
'cost': False,
|
||||
'date': False,
|
||||
'reason': False,
|
||||
}
|
||||
standard_prices.create(cr, uid, data, context=context)
|
||||
elif record.cost_method == 'real':
|
||||
# For a 'real' cost_method, entries in standard_prices are
|
||||
# created when quants are created.
|
||||
pass
|
||||
if product.cost_method in ('standard', 'average'):
|
||||
standard_prices.create(cr, uid, data, context=context)
|
||||
elif product.cost_method in ('real',):
|
||||
# For a 'real' cost_method, entries in standard_prices are
|
||||
# created when quants are created.
|
||||
pass
|
||||
return super(product_product, self).write(cr, uid, ids, values,
|
||||
context=context)
|
||||
|
||||
|
||||
@@ -8,17 +8,77 @@ wuants creation.
|
||||
"""
|
||||
|
||||
import openerp
|
||||
from openerp.osv import expression
|
||||
|
||||
class standard_prices(openerp.osv.orm.Model):
|
||||
|
||||
_name = 'standard.prices'
|
||||
|
||||
_columns = {
|
||||
'company_id': openerp.osv.fields.many2one('res.company', required=True),
|
||||
'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'),
|
||||
'cost': openerp.osv.fields.float(),
|
||||
'date': openerp.osv.fields.date(),
|
||||
'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(),
|
||||
}
|
||||
|
||||
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': openerp.osv.fields.datetime.now,
|
||||
'company_id': _get_default_company,
|
||||
}
|
||||
|
||||
class stock_value(openerp.osv.orm.Model):
|
||||
|
||||
_name = 'stock.value'
|
||||
|
||||
_columns = {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Tests for the stock_account module.
|
||||
|
||||
This module groups a few sub-modules containing unittest2 test cases.
|
||||
|
||||
Tests can be explicitely added to the `fast_suite` or `checks` lists or not.
|
||||
See the :ref:`test-framework` section in the :ref:`features` list.
|
||||
"""
|
||||
|
||||
import test_standard_prices
|
||||
|
||||
fast_suite = [
|
||||
]
|
||||
|
||||
checks = [
|
||||
test_standard_prices,
|
||||
]
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
@@ -0,0 +1,52 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import unittest2
|
||||
|
||||
import openerp
|
||||
from openerp.tests import common
|
||||
|
||||
|
||||
class test_transaction_case(common.TransactionCase):
|
||||
|
||||
def test_00(self):
|
||||
cr, uid = self.cr, self.uid
|
||||
location_id = self.registry('stock.location').create(cr, uid,
|
||||
{'name': 'Test Location A'})
|
||||
product_id = self.registry('product.product').create(cr, uid,
|
||||
{
|
||||
'name': 'Test Product A',
|
||||
'standard_price': 1,
|
||||
})
|
||||
value = self.registry('stock.value')._get_value(cr, uid,
|
||||
location_id, product_id, openerp.osv.fields.datetime.now())
|
||||
self.assertEqual(value, 0)
|
||||
|
||||
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,
|
||||
{'name': 'Test Location A'})
|
||||
product_id = self.registry('product.product').create(cr, uid,
|
||||
{
|
||||
'name': 'Test Product A',
|
||||
'standard_price': 1,
|
||||
})
|
||||
product = self.registry('product.product').browse(cr, uid, product_id)
|
||||
move_id = self.registry('stock.move').create(cr, uid,
|
||||
{
|
||||
'name': 'Test Stock Move 1',
|
||||
'product_id': product_id,
|
||||
'location_id': stock_location_company.id, # arbitrary source location
|
||||
'location_dest_id': location_id,
|
||||
'product_uom_qty': 10,
|
||||
'product_uom': product.uom_id.id,
|
||||
})
|
||||
value = self.registry('stock.value')._get_value(cr, uid,
|
||||
location_id, product_id, openerp.osv.fields.datetime.now())
|
||||
self.assertEqual(value, 0)
|
||||
|
||||
self.registry('stock.move').action_done(cr, uid, [move_id])
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest2.main()
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
Reference in New Issue
Block a user