2010-04-28 11:52:22 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $Id$
#
# This program is free software: you can redistribute it and/or modify
2010-10-28 06:54:18 +00:00
# it under the terms of the GNU Affero General Public License as published by
2010-04-28 11:52:22 +00:00
# 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
2010-10-28 06:54:18 +00:00
# GNU Affero General Public License for more details.
2010-04-28 11:52:22 +00:00
#
2010-10-28 06:54:18 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-04-28 11:52:22 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv
from openerp . tools . translate import _
2013-02-22 12:11:55 +00:00
2013-06-13 14:36:01 +00:00
2013-02-22 12:11:55 +00:00
class crm_lead_forward_to_partner ( osv . TransientModel ) :
2012-08-24 14:20:00 +00:00
""" Forward info history to partners. """
2010-04-28 11:52:22 +00:00
_name = ' crm.lead.forward.to.partner '
2013-06-11 06:49:10 +00:00
2013-06-20 15:17:17 +00:00
def _convert_to_assignation_line ( self , cr , uid , lead , partner ) :
base_url = self . pool . get ( ' ir.config_parameter ' ) . get_param ( cr , uid , ' web.base.url ' )
lead_location = [ ]
partner_location = [ ]
if lead . country_id :
lead_location . append ( lead . country_id . name )
if lead . city :
lead_location . append ( lead . city )
if partner :
if partner . country_id :
partner_location . append ( partner . country_id . name )
if partner . city :
partner_location . append ( partner . city )
return { ' lead_id ' : lead . id ,
' lead_location ' : " , " . join ( lead_location ) ,
' partner_assigned_id ' : partner and partner . id or False ,
' partner_location ' : " , " . join ( partner_location ) ,
' lead_link ' : " %s /?db= %s #id= %s &model=crm.lead " % ( base_url , cr . dbname , lead . id )
}
2013-03-12 00:29:34 +00:00
def default_get ( self , cr , uid , fields , context = None ) :
if context is None :
context = { }
2013-06-10 12:50:57 +00:00
lead_obj = self . pool . get ( ' crm.lead ' )
2013-06-11 06:49:10 +00:00
email_template_obj = self . pool . get ( ' email.template ' )
2013-06-13 14:36:01 +00:00
try :
2013-06-20 15:07:25 +00:00
template_id = self . pool . get ( ' ir.model.data ' ) . get_object_reference ( cr , uid , ' crm_channel ' , ' email_template_lead_forward_mail ' ) [ 1 ]
2013-06-13 14:36:01 +00:00
except ValueError :
template_id = False
2013-03-12 00:29:34 +00:00
res = super ( crm_lead_forward_to_partner , self ) . default_get ( cr , uid , fields , context = context )
2013-06-10 12:50:57 +00:00
active_ids = context . get ( ' active_ids ' )
default_composition_mode = context . get ( ' default_composition_mode ' )
res [ ' assignation_lines ' ] = [ ]
if template_id :
2013-06-11 06:49:10 +00:00
res [ ' body ' ] = email_template_obj . get_email_template ( cr , uid , template_id ) . body_html
2013-06-10 12:50:57 +00:00
if active_ids :
2013-06-13 16:38:13 +00:00
lead_ids = lead_obj . browse ( cr , uid , active_ids , context = context )
if default_composition_mode == ' mass_mail ' :
partner_assigned_ids = lead_obj . search_geo_partner ( cr , uid , active_ids , context = context )
else :
partner_assigned_ids = dict ( ( lead . id , lead . partner_assigned_id and lead . partner_assigned_id . id or False ) for lead in lead_ids )
res [ ' partner_id ' ] = lead_ids [ 0 ] . partner_assigned_id . id
for lead in lead_ids :
2013-06-20 15:16:48 +00:00
partner_id = partner_assigned_ids . get ( lead . id ) or False
2013-06-20 15:17:17 +00:00
partner = False
2013-06-20 15:16:48 +00:00
if partner_id :
2013-06-20 15:17:17 +00:00
partner = self . pool . get ( ' res.partner ' ) . browse ( cr , uid , partner_id , context = context )
res [ ' assignation_lines ' ] . append ( self . _convert_to_assignation_line ( cr , uid , lead , partner ) )
2010-06-25 17:41:41 +00:00
return res
2013-06-13 14:36:01 +00:00
2010-04-28 11:52:22 +00:00
def action_forward ( self , cr , uid , ids , context = None ) :
2013-06-10 12:50:57 +00:00
lead_obj = self . pool . get ( ' crm.lead ' )
2013-06-13 16:38:13 +00:00
record = self . browse ( cr , uid , ids [ 0 ] , context = context )
2013-06-11 06:49:10 +00:00
email_template_obj = self . pool . get ( ' email.template ' )
2013-06-13 16:38:13 +00:00
try :
2013-06-20 15:07:25 +00:00
template_id = self . pool . get ( ' ir.model.data ' ) . get_object_reference ( cr , uid , ' crm_channel ' , ' email_template_lead_forward_mail ' ) [ 1 ]
2013-06-13 16:38:13 +00:00
except ValueError :
raise osv . except_osv ( _ ( ' Email Template Error ' ) ,
_ ( ' The Forward Email Template is not in the database ' ) )
local_context = context . copy ( )
if not ( record . forward_type == ' single ' ) :
2013-06-20 15:10:39 +00:00
no_email = [ ]
2013-06-13 16:38:13 +00:00
for lead in record . assignation_lines :
2013-06-20 15:18:42 +00:00
if lead . partner_assigned_id and not lead . partner_assigned_id . email :
2013-06-20 15:10:39 +00:00
no_email . append ( lead . partner_assigned_id . name )
if no_email :
raise osv . except_osv ( _ ( ' Email Error ' ) ,
( ' Set an email address for the partner(s): %s ' % " , " . join ( no_email ) ) )
if record . forward_type == ' single ' and not record . partner_id . email :
raise osv . except_osv ( _ ( ' Email Error ' ) ,
( ' Set an email address for the partner %s ' % record . partner_id . name ) )
2013-06-13 16:38:13 +00:00
partners_leads = { }
for lead in record . assignation_lines :
2013-06-20 15:18:42 +00:00
partner = record . forward_type == ' single ' and record . partner_id or lead . partner_assigned_id
2013-06-13 16:38:13 +00:00
lead_details = {
' lead_link ' : lead . lead_link ,
' lead_id ' : lead . lead_id ,
}
2013-06-20 15:18:42 +00:00
if partner :
partner_leads = partners_leads . get ( partner . id )
if partner_leads :
partner_leads [ ' leads ' ] . append ( lead_details )
else :
partners_leads [ partner . id ] = { ' partner ' : partner , ' leads ' : [ lead_details ] }
2013-06-13 16:38:13 +00:00
for partner_id , partner_leads in partners_leads . items ( ) :
local_context [ ' partner_id ' ] = partner_leads [ ' partner ' ]
local_context [ ' partner_leads ' ] = partner_leads [ ' leads ' ]
email_template_obj . send_mail ( cr , uid , template_id , ids [ 0 ] , context = local_context )
lead_ids = [ lead [ ' lead_id ' ] . id for lead in partner_leads [ ' leads ' ] ]
lead_obj . write ( cr , uid , lead_ids , { ' partner_assigned_id ' : partner_id , ' user_id ' : partner_leads [ ' partner ' ] . user_id . id } )
self . pool . get ( ' crm.lead ' ) . message_subscribe ( cr , uid , lead_ids , [ partner_id ] , context = context )
2013-06-10 12:50:57 +00:00
return True
2013-06-13 14:36:01 +00:00
2013-06-20 15:18:42 +00:00
# def action_clean_lines(self, cr, uid, ids, context=None):
# record = self.browse(cr, uid, ids[0], context=context)
# invalid_lines = []
# for line in record.assignation_lines:
# if not line.partner_assigned_id:
# invalid_lines.append((2, line.id))
# self.write(cr, uid, ids[0], {'assignation_lines': invalid_lines}, context=context)
# model, action_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'crm_channel', 'action_crm_send_mass_forward')
# action = self.pool[model].read(cr, uid, action_id, context=context)
# action['res_id'] = ids[0]
# return action
2013-06-20 15:17:17 +00:00
2013-06-12 07:35:19 +00:00
def get_portal_url ( self , cr , uid , ids , context = None ) :
portal_link = " %s /?db= %s " % ( self . pool . get ( ' ir.config_parameter ' ) . get_param ( cr , uid , ' web.base.url ' ) , cr . dbname )
return portal_link
2013-06-13 14:36:01 +00:00
2013-06-10 12:50:57 +00:00
_columns = {
2013-06-13 14:36:01 +00:00
' forward_type ' : fields . selection ( [ ( ' single ' , ' a single partner: manual selection of partner ' ) , ( ' assigned ' , " several partners: automatic assignation, using GPS coordinates and partner ' s grades " ) , ] , ' Forward selected leads to ' ) ,
' partner_id ' : fields . many2one ( ' res.partner ' , ' Forward Leads To ' ) ,
2013-06-10 12:50:57 +00:00
' assignation_lines ' : fields . one2many ( ' crm.lead.assignation ' , ' forward_id ' , ' Partner Assignation ' ) ,
' body ' : fields . html ( ' Contents ' , help = ' Automatically sanitized HTML contents ' ) ,
}
2013-06-13 14:36:01 +00:00
2013-06-10 12:50:57 +00:00
_defaults = {
2013-06-20 15:17:17 +00:00
' forward_type ' : lambda self , cr , uid , c : c . get ( ' forward_type ' ) or ' single ' ,
2013-06-10 12:50:57 +00:00
}
2013-06-13 14:36:01 +00:00
2013-06-10 12:50:57 +00:00
class crm_lead_assignation ( osv . TransientModel ) :
_name = ' crm.lead.assignation '
_columns = {
2013-06-13 14:36:01 +00:00
' forward_id ' : fields . many2one ( ' crm.lead.forward.to.partner ' , ' Partner Assignation ' ) ,
' lead_id ' : fields . many2one ( ' crm.lead ' , ' Lead ' ) ,
' lead_location ' : fields . char ( ' Lead Location ' , size = 128 ) ,
2013-06-10 12:50:57 +00:00
' partner_assigned_id ' : fields . many2one ( ' res.partner ' , ' Assigned Partner ' ) ,
2013-06-13 14:36:01 +00:00
' partner_location ' : fields . char ( ' Partner Location ' , size = 128 ) ,
2013-06-11 06:49:10 +00:00
' lead_link ' : fields . char ( ' Lead Single Links ' , size = 128 ) ,
2013-06-10 12:50:57 +00:00
}
2013-06-13 14:36:01 +00:00
def on_change_lead_id ( self , cr , uid , ids , lead_id , context = None ) :
if not context :
context = { }
if not lead_id :
return { ' value ' : { ' lead_location ' : False } }
lead = self . pool . get ( ' crm.lead ' ) . browse ( cr , uid , lead_id , context = context )
lead_location = [ ]
if lead . country_id :
lead_location . append ( lead . country_id . name )
if lead . city :
lead_location . append ( lead . city )
return { ' value ' : { ' lead_location ' : " , " . join ( lead_location ) } }
def on_change_partner_assigned_id ( self , cr , uid , ids , partner_assigned_id , context = None ) :
if not context :
context = { }
if not partner_assigned_id :
return { ' value ' : { ' lead_location ' : False } }
partner = self . pool . get ( ' res.partner ' ) . browse ( cr , uid , partner_assigned_id , context = context )
partner_location = [ ]
if partner . country_id :
partner_location . append ( partner . country_id . name )
if partner . city :
partner_location . append ( partner . city )
return { ' value ' : { ' partner_location ' : " , " . join ( partner_location ) } }
2013-06-10 12:50:57 +00:00
# # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: