From 22d5e6ec9bbb5723fd924f1fed326d7dc864d196 Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Thu, 1 Aug 2013 11:45:53 +0200 Subject: [PATCH] [IMP] stock_account: started _get_value(location, product, datetime). This should be replaced by a SQL view. bzr revid: vmt@openerp.com-20130801094553-1vuorucdyne38ln3 --- addons/stock_account/product.py | 40 +++++------ addons/stock_account/standard_prices.py | 66 ++++++++++++++++++- addons/stock_account/tests/__init__.py | 20 ++++++ .../tests/test_standard_prices.py | 52 +++++++++++++++ 4 files changed, 151 insertions(+), 27 deletions(-) create mode 100644 addons/stock_account/tests/__init__.py create mode 100644 addons/stock_account/tests/test_standard_prices.py diff --git a/addons/stock_account/product.py b/addons/stock_account/product.py index 30284bbf091..0734befcc09 100644 --- a/addons/stock_account/product.py +++ b/addons/stock_account/product.py @@ -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) diff --git a/addons/stock_account/standard_prices.py b/addons/stock_account/standard_prices.py index 63f6a2ecae2..acaf3be74bd 100644 --- a/addons/stock_account/standard_prices.py +++ b/addons/stock_account/standard_prices.py @@ -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 diff --git a/addons/stock_account/tests/__init__.py b/addons/stock_account/tests/__init__.py new file mode 100644 index 00000000000..5030a6ba840 --- /dev/null +++ b/addons/stock_account/tests/__init__.py @@ -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: diff --git a/addons/stock_account/tests/test_standard_prices.py b/addons/stock_account/tests/test_standard_prices.py new file mode 100644 index 00000000000..22ec138f07b --- /dev/null +++ b/addons/stock_account/tests/test_standard_prices.py @@ -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: