diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 2392136f9cc..5478b151d2a 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -411,7 +411,12 @@ class stock_quant(osv.osv): self.move_quants_write(cr, uid, to_move_quants, move, location_to, dest_package_id, context=context) self.pool.get('stock.move').recalculate_move_state(cr, uid, to_recompute_move_ids, context=context) if location_to.usage == 'internal': - if self.search(cr, uid, [('product_id', '=', move.product_id.id), ('qty','<', 0)], limit=1, context=context): + # Do manual search for quant to avoid full table scan (order by id) + cr.execute(""" + SELECT 0 FROM stock_quant, stock_location WHERE product_id = %s AND stock_location.id = stock_quant.location_id AND + ((stock_location.parent_left >= %s AND stock_location.parent_left < %s) OR stock_location.id = %s) AND qty < 0.0 LIMIT 1 + """, (move.product_id.id, location_to.parent_left, location_to.parent_right, location_to.id)) + if cr.fetchone(): for quant in quants_reconcile: self._quant_reconcile_negative(cr, uid, quant, move, context=context)