if not result:
return None
return float(result.group(2)),float(result.group(1))
-
+
+def geo_query_address(street=None, zip=None, city=None, state=None, country=None):
+ if country and ',' in country and (country.endswith(' of') or country.endswith(' of the')):
+ # put country qualifier in front, otherwise GMap gives wrong results,
+ # e.g. 'Congo, Democratic Republic of the' => 'Democratic Republic of the Congo'
+ country = '{1} {0}'.format(*country.split(',',1))
+ return tools.ustr(', '.join(filter(None, [street,
+ ("%s %s" % (zip or '', city or '')).strip(),
+ state,
+ country])))
class res_partner_grade(osv.osv):
_order = 'sequence'
}
res_partner_grade()
+class res_partner_activation(osv.osv):
+ _name = 'res.partner.activation'
+ _order = 'sequence'
+
+ _columns = {
+ 'sequence' : fields.integer('Sequence'),
+ 'name' : fields.char('Name', size=32, required=True),
+ }
+
+res_partner_activation()
class res_partner(osv.osv):
_inherit = "res.partner"
help="Gives the probability to assign a lead to this partner. (0 means no assignation.)"),
'opportunity_assigned_ids': fields.one2many('crm.lead', 'partner_assigned_id',\
'Assigned Opportunities'),
- 'grade_id': fields.many2one('res.partner.grade', 'Partner Grade')
+ 'grade_id': fields.many2one('res.partner.grade', 'Partner Grade'),
+ 'activation' : fields.many2one('res.partner.activation', 'Activation', select=1),
+ 'date_partnership' : fields.date('Partnership Date'),
+ 'date_review' : fields.date('Latest Partner Review'),
+ 'date_review_next' : fields.date('Next Partner Review'),
}
_defaults = {
'partner_weight': lambda *args: 0
}
def geo_localize(self, cr, uid, ids, context=None):
- for partner in self.browse(cr, uid, ids, context=context):
- if not partner.address:
+ # Don't pass context to browse()! We need country names in english below
+ for partner in self.browse(cr, uid, ids):
+ if not partner:
continue
- contact = partner.address[0] #TOFIX: should be get latitude and longitude for default contact?
- addr = ', '.join(filter(None, [
- contact.street,
- "%s %s" % (contact.zip , contact.city),
- contact.state_id and contact.state_id.name,
- contact.country_id and contact.country_id.name]))
- result = geo_find(tools.ustr(addr))
+ result = geo_find(geo_query_address(street=partner.street,
+ zip=partner.zip,
+ city=partner.city,
+ state=partner.state_id.name,
+ country=partner.country_id.name))
if result:
self.write(cr, uid, [partner.id], {
'partner_latitude': result[0],
'partner_longitude': result[1],
- 'date_localization': time.strftime('%Y-%m-%d')
+ 'date_localization': fields.date.context_today(self,cr,uid,context=context)
}, context=context)
return True
res_partner()
partners = self.pool.get('res.partner').browse(cr, uid, [partner_assigned_id], context=context)
user_id = partners[0] and partners[0].user_id.id or False
return {'value':
- {'date_assign': time.strftime('%Y-%m-%d'),
+ {'date_assign': fields.date.context_today(self,cr,uid,context=context),
'user_id' : user_id}
}
- def assign_partner(self, cr, uid, ids, partner_id=None, context=None):
+ def action_assign_partner(self, cr, uid, ids, context=None):
+ return self.assign_partner(cr, uid, ids, partner_id=False, context=context)
+
+ def assign_partner(self, cr, uid, ids, partner_id=False, context=None):
partner_ids = {}
res = False
- if partner_id is None:
+ res_partner = self.pool.get('res.partner')
+ if not partner_id:
partner_ids = self.search_geo_partner(cr, uid, ids, context=context)
for lead in self.browse(cr, uid, ids, context=context):
if not partner_id:
partner_id = partner_ids.get(lead.id, False)
- res = super(crm_lead, self).assign_partner(cr, uid, [lead.id], partner_id, context=context)
- self.write(cr, uid, [lead.id], {'date_assign': time.strftime('%Y-%m-%d'), 'partner_assigned_id': partner_id}, context=context)
+ if not partner_id:
+ continue
+ self.assign_geo_localize(cr, uid, [lead.id], lead.partner_latitude, lead.partner_longitude, context=context)
+ partner = res_partner.browse(cr, uid, partner_id, context=context)
+ if partner.user_id:
+ for lead_id in ids:
+ self.allocate_salesman(cr, uid, [lead_id], [partner.user_id.id], context=context)
+ self.write(cr, uid, [lead.id], {'date_assign': fields.date.context_today(self,cr,uid,context=context), 'partner_assigned_id': partner_id}, context=context)
return res
-
def assign_geo_localize(self, cr, uid, ids, latitude=False, longitude=False, context=None):
- for lead in self.browse(cr, uid, ids, context=context):
+ # Don't pass context to browse()! We need country name in english below
+ for lead in self.browse(cr, uid, ids):
if not lead.country_id:
continue
- addr = ', '.join(filter(None, [
- lead.street,
- "%s %s" % (lead.zip, lead.city),
- lead.state_id and lead.state_id.name or '',
- lead.country_id and lead.country_id.name or ''
- ]))
- result = geo_find(tools.ustr(addr))
+ result = geo_find(geo_query_address(street=lead.street,
+ zip=lead.zip,
+ city=lead.city,
+ state=lead.state_id.name,
+ country=lead.country_id.name))
if not latitude and result:
latitude = result[0]
if not longitude and result:
'partner_longitude': longitude
}, context=context)
return True
-
+
def search_geo_partner(self, cr, uid, ids, context=None):
res_partner = self.pool.get('res.partner')
res_partner_ids = {}
('partner_weight', '>', 0),
('partner_latitude', '>', latitude - 2), ('partner_latitude', '<', latitude + 2),
('partner_longitude', '>', longitude - 1.5), ('partner_longitude', '<', longitude + 1.5),
- ('country', '=', lead.country_id.id),
+ ('country_id', '=', lead.country_id.id),
], context=context)
# 2. second way: in the same country, big area
('partner_weight', '>', 0),
('partner_latitude', '>', latitude - 4), ('partner_latitude', '<', latitude + 4),
('partner_longitude', '>', longitude - 3), ('partner_longitude', '<' , longitude + 3),
- ('country', '=', lead.country_id.id),
+ ('country_id', '=', lead.country_id.id),
], context=context)
+ # 3. third way: in the same country, extra large area
+ if not partner_ids:
+ partner_ids = res_partner.search(cr, uid, [
+ ('partner_weight','>', 0),
+ ('partner_latitude','>', latitude - 8), ('partner_latitude','<', latitude + 8),
+ ('partner_longitude','>', longitude - 8), ('partner_longitude','<', longitude + 8),
+ ('country', '=', lead.country_id.id),
+ ], context=context)
# 5. fifth way: anywhere in same country
if not partner_ids:
# still haven't found any, let's take all partners in the country!
- partner_ids = partner.search(cr, uid, [
+ partner_ids = res_partner.search(cr, uid, [
('partner_weight', '>', 0),
- ('country', '=', lead.country_id.id),
+ ('country_id', '=', lead.country_id.id),
], context=context)
# 6. sixth way: closest partner whatsoever, just to have at least one result
return res_partner_ids
crm_lead()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: