From 0ae679e433ee4c5ca32ae1302d87d7fb33e0c78d Mon Sep 17 00:00:00 2001 From: Simon Lejeune Date: Wed, 19 Jul 2017 20:09:19 +0200 Subject: [PATCH] [FIX] stock: scrap and sublocations Following the previous commit, it appears than the scrap mechanism needs some rigorousness when looking for quants characteristics (i.e. if we look for a quant without `lot_id`, it should not return one with one) but some flexibility with the location (i.e. we can look in sublocations to find one with the given characteristics). Before this commit, we centralized everything related to quant's search in stock_quant.py and the `_gather` method, which has a `strict` kw. As we exposed the `_get_quantity` method but is only used in stock_scrap, and that the `strict` flag does not fulfill the needs of stock_scrap, we inline the appropriate search, as it was done previously. --- addons/stock/models/stock_quant.py | 6 ------ addons/stock/models/stock_scrap.py | 11 +++++++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/addons/stock/models/stock_quant.py b/addons/stock/models/stock_quant.py index 24f7a847dbf..15084292a5e 100644 --- a/addons/stock/models/stock_quant.py +++ b/addons/stock/models/stock_quant.py @@ -102,12 +102,6 @@ class StockQuant(models.Model): return self.search(domain, order=removal_strategy_order) - @api.model - def _get_quantity(self, product_id, location_id, lot_id=None, package_id=None, owner_id=None, strict=False): - self = self.sudo() - quants = self._gather(product_id, location_id, lot_id=lot_id, package_id=package_id, owner_id=owner_id, strict=strict) - return sum(quants.mapped('quantity')) - @api.model def _get_available_quantity(self, product_id, location_id, lot_id=None, package_id=None, owner_id=None, strict=False): """ Return the available quantity, i.e. the sum of `quantity` minus the sum of diff --git a/addons/stock/models/stock_scrap.py b/addons/stock/models/stock_scrap.py index 839987cee2e..4eba509a8de 100644 --- a/addons/stock/models/stock_scrap.py +++ b/addons/stock/models/stock_scrap.py @@ -80,10 +80,13 @@ class StockScrap(models.Model): for scrap in self: move = self.env['stock.move'].create(scrap._prepare_move_values()) if self.product_id.type == 'product': - quantity_in_stock = self.env['stock.quant']._get_quantity( - self.product_id, self.location_id, lot_id=self.lot_id, - package_id=self.package_id, owner_id=self.owner_id, strict=True - ) + quantity_in_stock = sum(self.env['stock.quant'].search([ + ('product_id', '=', self.product_id.id), + ('lot_id', '=', self.lot_id and self.lot_id.id or False), + ('package_id', '=', self.package_id and self.package_id.id or False), + ('owner_id', '=', self.owner_id and self.owner_id.id or False), + ('location_id', 'child_of', self.location_id.id) + ]).mapped('quantity')) if quantity_in_stock < move.product_qty: # FIXME: float compare raise UserError(_('You cannot scrap a move without having available stock for %s. You can correct it with an inventory adjustment.') % move.product_id.name) move.action_done()