[IMP] Do manual query in search for negative quants to avoid ORM ordering by id and as such always using the wrong index

[IMP] Simplify code

[IMP] Negative quant search should include child locations also

[IMP] Comment

[IMP] Could be child_of also
This commit is contained in:
Josse Colpaert 2015-02-09 16:51:57 +01:00
parent 33a8989d77
commit d42af010ce
1 changed files with 6 additions and 1 deletions

View File

@ -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)