From d6a678c7d560b0757f55004cc49ad68de3a5be73 Mon Sep 17 00:00:00 2001 From: Rifakat Date: Thu, 4 Apr 2013 19:24:46 +0530 Subject: [PATCH] [FIX] stock: code optimization to restric checking product's availability in all the locations, if there are thousands of locations to be checked then should only check goods in the location where this product is ever come or went bzr revid: rha@tinyerp.com-20130404135446-ln4e3n7rdibajho3 --- addons/stock/stock.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/addons/stock/stock.py b/addons/stock/stock.py index f2258372907..2c2ac23dfa3 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -399,7 +399,16 @@ class stock_location(osv.osv): uom_rounding = self.pool.get('product.product').browse(cr, uid, product_id, context=context).uom_id.rounding if context.get('uom'): uom_rounding = uom_obj.browse(cr, uid, context.get('uom'), context=context).rounding - for id in self.search(cr, uid, [('location_id', 'child_of', ids)]): + locations_ids = self.search(cr, uid, [('location_id', 'child_of', ids)]) + #Fetch only those locations in which this product has ever processed(in or out) + cr.execute("""SELECT l.id FROM stock_location l WHERE l.id in %s AND + EXISTS (SELECT 1 FROM stock_move m WHERE m.product_id = %s + AND ((state = 'done' AND m.location_dest_id = l.id) + OR + (state in ('done','assigned') AND m.location_id = l.id))) + """, (tuple(locations_ids), product_id,)) + locations_ids = [i for i in cr.fetchall()] + for id in locations_ids: if lock: try: # Must lock with a separate select query because FOR UPDATE can't be used with