diff --git a/addons/stock/models/product.py b/addons/stock/models/product.py index 693f5c0119e..391542c829f 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -76,7 +76,7 @@ class Product(models.Model): reordering_min_qty = fields.Float(compute='_compute_nbr_reordering_rules') reordering_max_qty = fields.Float(compute='_compute_nbr_reordering_rules') - @api.depends('stock_quant_ids', 'stock_move_ids') + @api.depends('stock_move_ids.product_qty', 'stock_move_ids.state') def _compute_quantities(self): res = self._compute_quantities_dict(self._context.get('lot_id'), self._context.get('owner_id'), self._context.get('package_id'), self._context.get('from_date'), self._context.get('to_date')) for product in self: @@ -154,9 +154,15 @@ class Product(models.Model): It will return all stock locations when no parameters are given Possible parameters are shop, warehouse, location, force_company, compute_child ''' - # TDE FIXME: clean that brol, context seems overused Warehouse = self.env['stock.warehouse'] + if self.env.context.get('company_owned', False): + company_id = self.env.user.company_id.id + return ( + [('location_id.company_id', '=', company_id)], + [('location_id.company_id', '=', False), ('location_dest_id.company_id', '=', company_id)], + [('location_id.company_id', '=', company_id), ('location_dest_id.company_id', '=', False), + ]) location_ids = [] if self.env.context.get('location', False): if isinstance(self.env.context['location'], pycompat.integer_types): diff --git a/addons/stock/tests/test_move.py b/addons/stock/tests/test_move.py index 22ae95448e4..2fc3d884045 100644 --- a/addons/stock/tests/test_move.py +++ b/addons/stock/tests/test_move.py @@ -12,6 +12,10 @@ class StockMove(TransactionCase): self.customer_location = self.env.ref('stock.stock_location_customers') self.supplier_location = self.env.ref('stock.stock_location_suppliers') self.pack_location = self.env.ref('stock.location_pack_zone') + self.transit_location = self.env['stock.location'].search([ + ('company_id', '=', self.env.user.company_id.id), + ('usage', '=', 'transit'), + ], limit=1) self.uom_unit = self.env.ref('product.product_uom_unit') self.product1 = self.env['product.product'].create({ 'name': 'Product A', @@ -2632,3 +2636,39 @@ class StockMove(TransactionCase): self.assertEqual(quant.in_date, Datetime.to_string(initial_in_date_lot1)) elif quant.lot_id == lot2: self.assertEqual(quant.in_date, initial_in_date_lot2) + + def test_transit_1(self): + """ Receive some products, send some to transit, check the product's `available_qty` + computed field with or without the "company_owned" key in the context. + """ + move1 = self.env['stock.move'].create({ + 'name': 'test_transit_1', + 'location_id': self.supplier_location.id, + 'location_dest_id': self.stock_location.id, + 'product_id': self.product1.id, + 'product_uom': self.uom_unit.id, + 'product_uom_qty': 10.0, + 'picking_type_id': self.env.ref('stock.picking_type_in').id, + }) + move1.action_confirm() + move1.action_assign() + move1.move_line_ids.qty_done = 10 + move1.action_done() + + self.assertEqual(self.product1.qty_available, 10.0) + + move2 = self.env['stock.move'].create({ + 'name': 'test_transit_1', + 'location_id': self.stock_location.id, + 'location_dest_id': self.transit_location.id, + 'product_id': self.product1.id, + 'product_uom': self.uom_unit.id, + 'product_uom_qty': 5.0, + }) + move2.action_confirm() + move2.action_assign() + move2.move_line_ids.qty_done = 5 + move2.action_done() + + self.assertEqual(self.product1.qty_available, 5.0) + self.assertEqual(self.product1.with_context(company_owned=True).qty_available, 10.0)