From ed6a622c0237d62d4f691ddb3accfc2f43e84f26 Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Thu, 11 Jun 2015 15:05:23 +0200 Subject: [PATCH] [FIX] stock: choose quants depending on original move when scrapping When a product is scrapped, we set the reservation_id of the scrapped move on the quant corresponding to the original move (e.g., the move which brought a manufactured product from production to stock). By doing so, we will subtract the quantity to scrap from the appropriate quant. opw-641852 --- addons/stock/stock.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/addons/stock/stock.py b/addons/stock/stock.py index edb9ab10ab0..d16fc082b24 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -2467,6 +2467,7 @@ class stock_move(osv.osv): @param context: context arguments @return: Scraped lines """ + quant_obj = self.pool.get("stock.quant") #quantity should be given in MOVE UOM if quantity <= 0: raise osv.except_osv(_('Warning!'), _('Please provide a positive quantity to scrap.')) @@ -2501,6 +2502,18 @@ class stock_move(osv.osv): message = _("%s %s %s has been moved to scrap.") % (quantity, uom, product.name) move.picking_id.message_post(body=message) + # We "flag" the quant from which we want to scrap the products. To do so: + # - we select the quants related to the move we scrap from + # - we reserve the quants with the scrapped move + # See self.action_done, et particularly how is defined the "prefered_domain" for clarification + scrap_move = self.browse(cr, uid, new_move, context=context) + if move.state == 'done' and scrap_move.location_id.usage not in ('supplier', 'inventory', 'production'): + domain = [('qty', '>', 0), ('history_ids', 'in', [move.id])] + # We use scrap_move data since a reservation makes sense for a move not already done + quants = quant_obj.quants_get_prefered_domain(cr, uid, scrap_move.location_id, + scrap_move.product_id, quantity, domain=domain, prefered_domain_list=[], + restrict_lot_id=scrap_move.restrict_lot_id.id, restrict_partner_id=scrap_move.restrict_partner_id.id, context=context) + quant_obj.quants_reserve(cr, uid, quants, scrap_move, context=context) self.action_done(cr, uid, res, context=context) return res