[IMP] Partners Geo Assignation - new module

bzr revid: fp@tinyerp.com-20100611220209-kerfxsckm6us8yh7
This commit is contained in:
Fabien Pinckaers 2010-06-12 00:02:09 +02:00
parent caca420c82
commit 71be09ecc6
7 changed files with 288 additions and 45 deletions

View File

@ -83,7 +83,8 @@
<field name="date_action"/>
<field name="title_action"/>
<field name="priority" string="Priority"/>
<field name="type" readonly="1"/>
<newline/>
<field name="type" invisible="1"/>
</group>
<notebook colspan="4">
<page string="Opportunity">
@ -134,6 +135,27 @@
icon="gtk-convert" />
</group>
</page>
<page string="Lead">
<group colspan="2" col="4">
<separator string="Contact" colspan="4" col="4"/>
<field name="partner_name" string="Partner Name" colspan="4"/>
<newline/>
<field domain="[('domain', '=', 'contact')]" name="title"/>
<field name="function" />
<field name="street" colspan="4"/>
<field name="street2" colspan="4"/>
<field name="zip"/>
<field name="city"/>
<field name="country_id"/>
<field name="state_id"/>
</group>
<group colspan="2" col="3">
<separator string="Communication" colspan="4" col="3"/>
<field name="fax"/>
<newline/>
<field name="mobile"/>
</group>
</page>
<page string="Emails" groups="base.group_extended">
<group colspan="4">

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import partner_geo_assign

View File

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
'name': 'Partner Geo-Localisation',
'version': '1.0',
'category': 'Generic Modules/Production',
'description': """
This is the module used by OpenERP SA to redirect customers to his partners,
based on geolocalisation.
""",
'author': 'OpenERP SA',
'depends': ['crm'],
'update_xml': ['res_partner_view.xml'],
'demo_xml': [],
'installable': True,
'active': False,
'certificate': False,
}

View File

@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv
from osv import fields
import urllib,re
import random, time
def geo_find(addr):
import urllib,re
regex = '<coordinates>([+-]?[0-9\.]+),([+-]?[0-9\.]+),([+-]?[0-9\.]+)</coordinates>'
url = 'http://maps.google.com/maps/geo?q=' + urllib.quote(addr) + '&output=xml&oe=utf8&sensor=false'
xml = urllib.urlopen(url).read()
if '<error>' in xml:
print 'Error'
return None
result = re.search(regex, xml, re.M|re.I)
if not result:
print 'No Regex', xml
return None
return float(result.group(1)),float(result.group(2))
class res_partner(osv.osv):
_inherit = "res.partner"
_columns = {
'partner_latitude': fields.float('Geo Latitude', digits=(16,2)),
'partner_longitude': fields.float('Geo Longitude', digits=(16,2)),
'partner_weight': fields.integer('Weight',
help="Gives the probability to assign a lead to this partner. (0 means no assignation.)"),
}
_defaults = {
'partner_weight': lambda *args: 0
}
def geo_localize(self, cr, uid, ids, context=None):
regex = '<coordinates>([+-]?[0-9\.]+),([+-]?[0-9\.]+),([+-]?[0-9\.]+)</coordinates>'
for partner in self.browse(cr, uid, ids, context=context):
part = partner.address[0]
addr = ', '.join(filter(None, [part.street, part.street2, (part.zip or '')+' '+(part.city or ''), part.state_id and part.state_id.name, part.country_id and part.country_id.name]))
result = geo_find(addr)
if result:
print 'Write', {
'partner_latitude': result[0],
'partner_longitude': result[1]
}
self.write(cr, uid, [partner.id], {
'partner_latitude': result[0],
'partner_longitude': result[1]
}, context=context)
return True
res_partner()
class crm_lead(osv.osv):
_inherit = "crm.lead"
_columns = {
'partner_latitude': fields.float('Geo Latitude', digits=(16,2)),
'partner_longitude': fields.float('Geo Longitude', digits=(16,2)),
'partner_assigned_id': fields.many2one('res.partner','Assigned Partner')
'date_assign': fields.date('Assignation Date')
}
def assign_partner(self, cr, uid, ids, context=None):
for part in self.browse(cr, uid, ids, context=context):
if not part.country_id:
continue
addr = ', '.join(filter(None, [part.street, part.street2, (part.zip or '')+' '+(part.city or ''), part.state_id and part.state_id.name, part.country_id and part.country_id.name]))
result = geo_find(addr)
if result:
self.write(cr, uid, [part.id], {
'partner_latitude': result[0],
'partner_longitude': result[1]
}, context=context)
part_ids = self.pool.get('res.partner').search(cr, uid, [
('partner_weight','>',0),
('partner_latitude','>',result[0]-2), ('partner_latitude','<',result[0]+2),
('partner_longitude','>',result[1]-1.5), ('partner_longitude','<',result[1]+1.5)
], context=context)
if not part_ids:
part_ids = self.pool.get('res.partner').search(cr, uid, [
('partner_weight','>',0),
('partner_latitude','>',result[0]-4), ('partner_latitude','<',result[0]+4),
('partner_longitude','>',result[1]-3), ('partner_longitude','<',result[1]+3)
], context=context)
total = 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) )
mypartner = random.randint(0,total)
for t in toassign:
if mypartner<=t[1]:
self.write(cr, uid, [part.id], {'partner_assigned_id': t[0], 'date_assign': time.strftime('%Y-%m-%d')}, context=context)
break
return True
crm_lead()

View File

@ -0,0 +1,45 @@
<?xml version="1.0"?>
<openerp>
<data>
<record id="view_crm_opportunity_geo_assign_form" model="ir.ui.view">
<field name="name">crm.lead.geo_assign.inherit</field>
<field name="model">crm.lead</field>
<field name="type">form</field>
<field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
<field name="arch" type="xml">
<xpath expr="//notebook[last()]" position="inside">
<page string="Partner Assignation">
<field name="partner_assigned_id"/>
<label string="" colspan="1"/>
<button string="Geo Assign" name="assign_partner" type="object"/>
<newline/>
<field name="partner_latitude"/>
<field name="partner_longitude"/>
</page>
</xpath>
</field>
</record>
<record id="view_crm_partner_geo_form" model="ir.ui.view">
<field name="name">res.partner.geo.inherit</field>
<field name="model">res.partner</field>
<field name="type">form</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//notebook[last()]" position="inside">
<page string="Partner Association">
<field name="partner_weight"/>
<label string="" colspan="1"/>
<button string="Geo Localize" name="geo_localize" icon="gtk-apply" type="object"/>
<newline/>
<field name="partner_latitude"/>
<field name="partner_longitude"/>
<field name="date_assign"/>
</page>
</xpath>
</field>
</record>
</data>
</openerp>

View File

@ -233,7 +233,13 @@
<field name="date_planned" widget="date"/>
<field name="price_unit"/>
<field name="company_id" groups="base.group_multi_company" widget="selection"/>
<field name="account_analytic_id" colspan="4" groups="base.group_extended"/>
<field name="price_subtotal" readonly="1"/>
<group colspan="4" col="4" groups="base.group_extended">
<separator colspan="4" string="Taxes"/>
<field colspan="4" nolabel="1" name="taxes_id"
domain="[('parent_id','=',False),('type_tax_use','!=','sale')]"/>
</group>
</page>
<page string="Notes">
<field colspan="4" name="notes" nolabel="1"/>
@ -243,9 +249,6 @@
<field colspan="4" name="move_ids" nolabel="1" widget="many2many"/>
</page>
<page string="Invoicing" groups="base.group_extended">
<field name="account_analytic_id" colspan="4"/>
<separator colspan="4" string="Taxes"/>
<field colspan="4" nolabel="1" name="taxes_id" domain="[('parent_id','=',False),('type_tax_use','!=','sale')]"/>
<separator colspan="4" string="Manual Invoices"/>
<field name="invoiced"/>
<newline/>