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-06-10 12:50:57 +00:00
from urllib import urlencode
from urlparse import urljoin
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-10 12:50:57 +00:00
def get_partner ( self , cr , uid , ids , context ) :
lead_obj = self . pool . get ( ' crm.lead ' )
partner_id = lead_obj . search_geo_partner ( cr , uid , ids , context )
if partner_id :
partner_id = partner_id [ ids [ 0 ] ]
else :
partner_id = False
return partner_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 ' )
email_template_obj = self . pool . get ( ' email.template ' )
base_url = self . pool . get ( ' ir.config_parameter ' ) . get_param ( cr , uid , ' web.base.url ' )
template_id = email_template_obj . search ( cr , uid , [ ( ' name ' , ' = ' , ' Lead Mass Mail ' ) , ( ' model_id.name ' , ' = ' , ' crm.lead.forward.to.partner ' ) ] )
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 :
res [ ' body ' ] = email_template_obj . get_email_template ( cr , uid , template_id [ 0 ] ) . body_html
if active_ids :
leads = lead_obj . browse ( cr , uid , active_ids , context = context )
for lead in leads :
if ( not lead . partner_assigned_id ) and default_composition_mode == ' mass_mail ' :
partner_id = self . get_partner ( cr , uid , [ lead . id ] , context )
res [ ' assignation_lines ' ] . append ( { ' lead_id ' : lead . id ,
' subject ' : lead . name ,
' city ' : lead . city ,
' country_id ' : lead . country_id . id ,
' partner_assigned_id ' : partner_id ,
' lead_link ' : " %s /?db= %s #id= %s &model=crm.lead " % ( base_url , cr . dbname , lead . id )
} )
elif default_composition_mode == ' forward ' :
res [ ' assignation_lines ' ] . append ( { ' lead_id ' : lead . id ,
' subject ' : lead . name ,
' city ' : lead . city ,
' country_id ' : lead . country_id . id ,
' partner_assigned_id ' : lead . partner_assigned_id . id ,
' lead_link ' : " %s /?db= %s #id= %s &model=crm.lead " % ( base_url , cr . dbname , lead . id )
} )
res [ ' partner_id ' ] = lead . partner_assigned_id . id
2010-06-25 17:41:41 +00:00
return res
2013-06-10 12:50:57 +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 ' )
record = self . browse ( cr , uid , ids , context = context )
email_template_obj = self . pool . get ( ' email.template ' )
tamplate_id = email_template_obj . search ( cr , uid , [ ( ' name ' , ' = ' , ' Lead Mass Mail ' ) , ( ' model_id.name ' , ' = ' , ' crm.lead.forward.to.partner ' ) ] )
if record [ 0 ] . forward_type == " single " :
email_template_obj . send_mail ( cr , uid , tamplate_id [ 0 ] , ids [ 0 ] )
active_ids = context . get ( ' active_ids ' )
if active_ids :
lead_obj . write ( cr , uid , active_ids , { ' partner_assigned_id ' : record [ 0 ] . partner_id . id , ' user_id ' : record [ 0 ] . partner_id . user_id . id } )
else :
for lead in record [ 0 ] . assignation_lines :
self . write ( cr , uid , ids , { ' partner_id ' : lead . partner_assigned_id . id ,
' lead_single_link ' : lead . lead_link ,
' lead_single_id ' : lead . lead_id . id
} )
email_template_obj . send_mail ( cr , uid , tamplate_id [ 0 ] , ids [ 0 ] )
lead_obj . write ( cr , uid , [ lead . lead_id . id ] , { ' partner_assigned_id ' : lead . partner_assigned_id . id , ' user_id ' : lead . partner_assigned_id . user_id . id } )
return True
_columns = {
' 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 ' ) ,
' assignation_lines ' : fields . one2many ( ' crm.lead.assignation ' , ' forward_id ' , ' Partner Assignation ' ) ,
' show_mail ' : fields . boolean ( ' Show the email will be sent ' ) ,
' body ' : fields . html ( ' Contents ' , help = ' Automatically sanitized HTML contents ' ) ,
' lead_single_id ' : fields . many2one ( ' crm.lead ' , ' Lead ' ) ,
' lead_single_link ' : fields . char ( ' Lead Single Links ' , size = 128 ) ,
}
_defaults = {
' forward_type ' : ' single ' ,
}
class crm_lead_assignation ( osv . TransientModel ) :
_name = ' crm.lead.assignation '
_columns = {
' forward_id ' : fields . many2one ( ' crm.lead.forward.to.partner ' , ' Partner Assignation ' ) ,
' lead_id ' : fields . many2one ( ' crm.lead ' , ' Lead ' ) ,
' subject ' : fields . char ( ' Subject ' , size = 64 ) ,
' city ' : fields . char ( ' City ' , size = 128 ) ,
' country_id ' : fields . many2one ( ' res.country ' , ' Country ' ) ,
' partner_assigned_id ' : fields . many2one ( ' res.partner ' , ' Assigned Partner ' ) ,
' lead_link ' : fields . char ( ' Lead Single Links ' , size = 128 ) ,
}
# # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: