From 01f1d91006fcf8c1a3cb85e9a8e4754f07151e1b Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Wed, 18 Mar 2015 15:49:18 +0100 Subject: [PATCH] [FIX] stock: use owner for virtual stock and in/out Fixes odoo/odoo#5814. It will also calculate virtual stock when lot/owner/pack is specified, but will not filter the future stock on lot/pack. --- addons/stock/product.py | 29 +++++---- addons/stock/tests/__init__.py | 1 + addons/stock/tests/test_owner_available.py | 75 ++++++++++++++++++++++ 3 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 addons/stock/tests/test_owner_available.py diff --git a/addons/stock/product.py b/addons/stock/product.py index 5e0dca5adf8..9d5f71d733b 100644 --- a/addons/stock/product.py +++ b/addons/stock/product.py @@ -137,20 +137,21 @@ class product_product(osv.osv): domain_move_in += self._get_domain_dates(cr, uid, ids, context=context) + [('state', 'not in', ('done', 'cancel', 'draft'))] + domain_products domain_move_out += self._get_domain_dates(cr, uid, ids, context=context) + [('state', 'not in', ('done', 'cancel', 'draft'))] + domain_products domain_quant += domain_products - if context.get('lot_id') or context.get('owner_id') or context.get('package_id'): - if context.get('lot_id'): - domain_quant.append(('lot_id', '=', context['lot_id'])) - if context.get('owner_id'): - domain_quant.append(('owner_id', '=', context['owner_id'])) - if context.get('package_id'): - domain_quant.append(('package_id', '=', context['package_id'])) - moves_in = [] - moves_out = [] - else: - domain_move_in += domain_move_in_loc - domain_move_out += domain_move_out_loc - moves_in = self.pool.get('stock.move').read_group(cr, uid, domain_move_in, ['product_id', 'product_qty'], ['product_id'], context=context) - moves_out = self.pool.get('stock.move').read_group(cr, uid, domain_move_out, ['product_id', 'product_qty'], ['product_id'], context=context) + + if context.get('lot_id'): + domain_quant.append(('lot_id', '=', context['lot_id'])) + if context.get('owner_id'): + domain_quant.append(('owner_id', '=', context['owner_id'])) + owner_domain = ('restrict_partner_id', '=', context['owner_id']) + domain_move_in.append(owner_domain) + domain_move_out.append(owner_domain) + if context.get('package_id'): + domain_quant.append(('package_id', '=', context['package_id'])) + + domain_move_in += domain_move_in_loc + domain_move_out += domain_move_out_loc + moves_in = self.pool.get('stock.move').read_group(cr, uid, domain_move_in, ['product_id', 'product_qty'], ['product_id'], context=context) + moves_out = self.pool.get('stock.move').read_group(cr, uid, domain_move_out, ['product_id', 'product_qty'], ['product_id'], context=context) domain_quant += domain_quant_loc quants = self.pool.get('stock.quant').read_group(cr, uid, domain_quant, ['product_id', 'qty'], ['product_id'], context=context) diff --git a/addons/stock/tests/__init__.py b/addons/stock/tests/__init__.py index 9d14a332a28..4c20f7a769a 100644 --- a/addons/stock/tests/__init__.py +++ b/addons/stock/tests/__init__.py @@ -1,3 +1,4 @@ # -*- coding: utf-8 -*- from . import test_stock_flow +from . import test_owner_available diff --git a/addons/stock/tests/test_owner_available.py b/addons/stock/tests/test_owner_available.py new file mode 100644 index 00000000000..db69f2363f0 --- /dev/null +++ b/addons/stock/tests/test_owner_available.py @@ -0,0 +1,75 @@ +# Author: Leonardo Pistone +# Copyright 2015 Camptocamp SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +from openerp.addons.stock.tests.common import TestStockCommon + + +class TestVirtualAvailable(TestStockCommon): + + def setUp(self): + super(TestVirtualAvailable, self).setUp() + + self.env['stock.quant'].create({ + 'product_id': self.productA.id, + 'location_id': self.stock_location, + 'qty': 30.0, + }) + + self.env['stock.quant'].create({ + 'product_id': self.productA.id, + 'location_id': self.stock_location, + 'qty': 10.0, + 'owner_id': self.ref('base.res_partner_4'), + }) + + self.picking_out = self.env['stock.picking'].create({ + 'picking_type_id': self.ref('stock.picking_type_out')}) + self.env['stock.move'].create({ + 'name': 'a move', + 'product_id': self.productA.id, + 'product_uom_qty': 3.0, + 'product_uom': self.productA.uom_id.id, + 'picking_id': self.picking_out.id, + 'location_id': self.stock_location, + 'location_dest_id': self.customer_location}) + + self.picking_out_2 = self.env['stock.picking'].create({ + 'picking_type_id': self.ref('stock.picking_type_out')}) + self.env['stock.move'].create({ + 'restrict_partner_id': self.ref('base.res_partner_4'), + 'name': 'another move', + 'product_id': self.productA.id, + 'product_uom_qty': 5.0, + 'product_uom': self.productA.uom_id.id, + 'picking_id': self.picking_out_2.id, + 'location_id': self.stock_location, + 'location_dest_id': self.customer_location}) + + + def test_without_owner(self): + self.assertAlmostEqual(40.0, self.productA.virtual_available) + self.picking_out.action_assign() + self.picking_out_2.action_assign() + + self.assertAlmostEqual(32.0, self.productA.virtual_available) + + def test_with_owner(self): + prod_context = self.productA.with_context( + owner_id=self.ref('base.res_partner_4') + ) + self.assertAlmostEqual(10.0, prod_context.virtual_available) + self.picking_out.action_assign() + self.picking_out_2.action_assign() + self.assertAlmostEqual(5.0, prod_context.virtual_available)