WHERE partner_longitude is not null
AND partner_latitude is not null
AND partner_weight > 0) AS d
- ORDER BY distance LIMIT 1""", (result[1],result[0]))
+ ORDER BY distance LIMIT 1""", (longitude, latitude))
res = cr.dictfetchone()
if res:
- part_ids.append(res['id'])
+ partner_ids.append(res['id'])
- total = 0
+ total_weight = 0
toassign = []
- for part2 in self.pool.get('res.partner').browse(cr, uid, part_ids, context=context):
- total += part2.partner_weight
- toassign.append( (part2.id, total) )
+ for partner in res_partner.browse(cr, uid, partner_ids, context=context):
+ total_weight += partner.partner_weight
+ toassign.append( (partner.id, total_weight) )
+
random.shuffle(toassign) # avoid always giving the leads to the first ones in db natural order!
- mypartner = random.randint(0,total)
- for t in toassign:
- if mypartner<=t[1]:
- vals = self.onchange_assign_id(cr,uid, ids, t[0], context=context)['value']
- vals.update({'partner_assigned_id': t[0], 'date_assign': time.strftime('%Y-%m-%d')})
- self.write(cr, uid, [part.id], vals, context=context)
+ nearest_weight = random.randint(0, total_weight)
+ for partner_id, weight in toassign:
+ if nearest_weight <= weight:
+ res_partner_ids[lead.id] = partner_id
break
- ok = True
- return ok
+ return res_partner_ids
crm_lead()
+
+ # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: