[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.
This commit is contained in:
Simon Lejeune
2017-07-19 20:09:19 +02:00
parent 52c21bd53e
commit 0ae679e433
2 changed files with 7 additions and 10 deletions
-6
View File
@@ -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
+7 -4
View File
@@ -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()