[FIX] stock: improvement search on product
- The search on qty_available is faster by looking only to the quants. - Less SQL queries
This commit is contained in:
committed by
Josse Colpaert
parent
d4152c2a40
commit
acbef5552d
+49
-39
@@ -185,17 +185,33 @@ class product_product(osv.osv):
|
||||
if operator == '=':
|
||||
operator = '=='
|
||||
|
||||
product_ids = self.search(cr, uid, [], context=context)
|
||||
ids = []
|
||||
if product_ids:
|
||||
#TODO: use a query instead of this browse record which is probably making the too much requests, but don't forget
|
||||
#the context that can be set with a location, an owner...
|
||||
for element in self.browse(cr, uid, product_ids, context=context):
|
||||
if eval(str(element[field]) + operator + str(value)):
|
||||
ids.append(element.id)
|
||||
res.append(('id', 'in', ids))
|
||||
if name == 'qty_available':
|
||||
res.append(('id', 'in', self._search_qty_available(cr, uid, operator, value, context)))
|
||||
else:
|
||||
product_ids = self.search(cr, uid, [], context=context)
|
||||
if product_ids:
|
||||
#TODO: use a query instead of this browse record which is probably making the too much requests, but don't forget
|
||||
#the context that can be set with a location, an owner...
|
||||
for element in self.browse(cr, uid, product_ids, context=context):
|
||||
if eval(str(element[field]) + operator + str(value)):
|
||||
ids.append(element.id)
|
||||
res.append(('id', 'in', ids))
|
||||
return res
|
||||
|
||||
def _search_qty_available(self, cr, uid, operator, value, context):
|
||||
domain_quant = self._get_domain_locations(cr, uid, [], context=context)[0]
|
||||
if context.get('lot_id'):
|
||||
domain_quant.append(('lot_id', '=', context['lot_id']))
|
||||
if context.get('owner_id'):
|
||||
domain_quant.append(('owner_id', '=', context['owner_id']))
|
||||
if context.get('package_id'):
|
||||
domain_quant.append(('package_id', '=', context['package_id']))
|
||||
quants = self.pool.get('stock.quant').read_group(cr, uid, domain_quant, ['product_id', 'qty'], ['product_id'], context=context)
|
||||
quants = dict(map(lambda x: (x['product_id'][0], x['qty']), quants))
|
||||
quants = dict((k, v) for k, v in quants.iteritems() if eval(str(v) + operator + str(value)))
|
||||
return(list(quants))
|
||||
|
||||
def _product_available_text(self, cr, uid, ids, field_names=None, arg=False, context=None):
|
||||
res = {}
|
||||
for product in self.browse(cr, uid, ids, context=context):
|
||||
@@ -316,41 +332,35 @@ class product_template(osv.osv):
|
||||
_inherit = 'product.template'
|
||||
|
||||
def _product_available(self, cr, uid, ids, name, arg, context=None):
|
||||
res = dict.fromkeys(ids, 0)
|
||||
for product in self.browse(cr, uid, ids, context=context):
|
||||
res[product.id] = {
|
||||
# "reception_count": sum([p.reception_count for p in product.product_variant_ids]),
|
||||
# "delivery_count": sum([p.delivery_count for p in product.product_variant_ids]),
|
||||
"qty_available": sum([p.qty_available for p in product.product_variant_ids]),
|
||||
"virtual_available": sum([p.virtual_available for p in product.product_variant_ids]),
|
||||
"incoming_qty": sum([p.incoming_qty for p in product.product_variant_ids]),
|
||||
"outgoing_qty": sum([p.outgoing_qty for p in product.product_variant_ids]),
|
||||
prod_available = dict.fromkeys(ids, 0)
|
||||
product_ids = self.browse(cr, uid, ids, context=context)
|
||||
var_ids = []
|
||||
for product in product_ids:
|
||||
var_ids += [p.id for p in product.product_variant_ids]
|
||||
variant_available= self.pool['product.product']._product_available(cr, uid, var_ids, context=context)
|
||||
|
||||
for product in product_ids:
|
||||
qty_available = 0
|
||||
virtual_available = 0
|
||||
incoming_qty = 0
|
||||
outgoing_qty = 0
|
||||
for p in product.product_variant_ids:
|
||||
qty_available += variant_available[p.id]["qty_available"]
|
||||
virtual_available += variant_available[p.id]["virtual_available"]
|
||||
incoming_qty += variant_available[p.id]["incoming_qty"]
|
||||
outgoing_qty += variant_available[p.id]["outgoing_qty"]
|
||||
prod_available[product.id] = {
|
||||
"qty_available":qty_available,
|
||||
"virtual_available":virtual_available,
|
||||
"incoming_qty":incoming_qty,
|
||||
"outgoing_qty":outgoing_qty,
|
||||
}
|
||||
return res
|
||||
return prod_available
|
||||
|
||||
def _search_product_quantity(self, cr, uid, obj, name, domain, context):
|
||||
prod = self.pool.get("product.product")
|
||||
res = []
|
||||
for field, operator, value in domain:
|
||||
#to prevent sql injections
|
||||
assert field in ('qty_available', 'virtual_available', 'incoming_qty', 'outgoing_qty'), 'Invalid domain left operand'
|
||||
assert operator in ('<', '>', '=', '!=', '<=', '>='), 'Invalid domain operator'
|
||||
assert isinstance(value, (float, int)), 'Invalid domain right operand'
|
||||
|
||||
if operator == '=':
|
||||
operator = '=='
|
||||
|
||||
product_ids = prod.search(cr, uid, [], context=context)
|
||||
ids = []
|
||||
if product_ids:
|
||||
#TODO: use a query instead of this browse record which is probably making the too much requests, but don't forget
|
||||
#the context that can be set with a location, an owner...
|
||||
for element in prod.browse(cr, uid, product_ids, context=context):
|
||||
if eval(str(element[field]) + operator + str(value)):
|
||||
ids.append(element.id)
|
||||
res.append(('product_variant_ids', 'in', ids))
|
||||
return res
|
||||
|
||||
product_variant_ids = prod.search(cr, uid, domain, context=context)
|
||||
return [('product_variant_ids', 'in', product_variant_ids)]
|
||||
|
||||
def _product_available_text(self, cr, uid, ids, field_names=None, arg=False, context=None):
|
||||
res = {}
|
||||
|
||||
Reference in New Issue
Block a user