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/>.
|
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
|
|
|
|
|
2010-06-25 17:41:41 +00:00
|
|
|
|
import base64
|
2010-07-14 15:17:13 +00:00
|
|
|
|
import time
|
2010-07-15 07:33:13 +00:00
|
|
|
|
import re
|
2010-06-25 17:41:41 +00:00
|
|
|
|
from osv import osv, fields
|
2010-04-28 11:52:22 +00:00
|
|
|
|
import tools
|
2010-06-25 17:41:41 +00:00
|
|
|
|
from tools.translate import _
|
2010-04-28 11:52:22 +00:00
|
|
|
|
|
|
|
|
|
class crm_lead_forward_to_partner(osv.osv_memory):
|
2010-05-12 13:16:01 +00:00
|
|
|
|
"""Forwards lead history"""
|
2010-04-28 11:52:22 +00:00
|
|
|
|
_name = 'crm.lead.forward.to.partner'
|
2010-07-15 07:33:13 +00:00
|
|
|
|
_inherit = "crm.send.mail"
|
2010-04-28 11:52:22 +00:00
|
|
|
|
|
|
|
|
|
_columns = {
|
2010-05-10 12:03:37 +00:00
|
|
|
|
'name': fields.selection([('user', 'User'), ('partner', 'Partner'), \
|
2010-06-25 17:41:41 +00:00
|
|
|
|
('email', 'Email Address')], 'Send to', required=True),
|
|
|
|
|
'user_id': fields.many2one('res.users', "User"),
|
|
|
|
|
'partner_id' : fields.many2one('res.partner', 'Partner'),
|
|
|
|
|
'address_id' : fields.many2one('res.partner.address', 'Address'),
|
2010-07-15 07:33:13 +00:00
|
|
|
|
'history': fields.selection([('info', 'Case Information'), ('latest', 'Latest email'), ('whole', 'Whole Story')], 'Send history', required=True),
|
2010-04-28 11:52:22 +00:00
|
|
|
|
}
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
|
|
|
|
_defaults = {
|
2010-06-25 18:30:58 +00:00
|
|
|
|
'name' : 'email',
|
2010-07-15 07:33:13 +00:00
|
|
|
|
'history': 'latest',
|
2010-06-25 17:41:41 +00:00
|
|
|
|
'email_from': lambda self, cr, uid, *a: self.pool.get('res.users')._get_email_from(cr, uid, uid)[uid]
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-10 12:03:37 +00:00
|
|
|
|
def get_whole_history(self, cr, uid, ids, context=None):
|
2010-06-25 17:41:41 +00:00
|
|
|
|
"""This function gets whole communication history and returns as top posting style
|
2010-05-12 13:16:01 +00:00
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of history IDs
|
|
|
|
|
@param context: A standard dictionary for contextual values
|
|
|
|
|
"""
|
2010-05-10 12:03:37 +00:00
|
|
|
|
whole = []
|
|
|
|
|
for hist_id in ids:
|
|
|
|
|
whole.append(self.get_latest_history(cr, uid, hist_id, context=context))
|
|
|
|
|
whole = '\n\n'.join(whole)
|
|
|
|
|
return whole or ''
|
2010-05-12 06:54:33 +00:00
|
|
|
|
|
|
|
|
|
def get_latest_history(self, cr, uid, hist_id, context=None):
|
2010-05-12 13:16:01 +00:00
|
|
|
|
"""This function gets latest communication and returns as top posting style
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param hist_id: Id of latest history
|
|
|
|
|
@param context: A standard dictionary for contextual values
|
|
|
|
|
"""
|
2010-05-10 12:03:37 +00:00
|
|
|
|
log_pool = self.pool.get('mailgate.message')
|
2010-05-12 06:54:33 +00:00
|
|
|
|
hist = log_pool.browse(cr, uid, hist_id, context=context)
|
2010-05-10 12:03:37 +00:00
|
|
|
|
header = '-------- Original Message --------'
|
|
|
|
|
sender = 'From: %s' %(hist.email_from or '')
|
|
|
|
|
to = 'To: %s' % (hist.email_to or '')
|
2010-05-12 06:54:33 +00:00
|
|
|
|
sentdate = 'Date: %s' % (hist.date or '')
|
2010-05-10 12:03:37 +00:00
|
|
|
|
desc = '\n%s'%(hist.description)
|
|
|
|
|
original = [header, sender, to, sentdate, desc]
|
|
|
|
|
original = '\n'.join(original)
|
|
|
|
|
return original
|
2010-05-12 06:54:33 +00:00
|
|
|
|
|
2010-05-12 13:16:01 +00:00
|
|
|
|
def on_change_email(self, cr, uid, ids, user):
|
|
|
|
|
"""This function fills email information based on user selected
|
2010-05-10 12:03:37 +00:00
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of Mail’s IDs
|
2010-05-12 13:16:01 +00:00
|
|
|
|
@param user: Changed User id
|
|
|
|
|
@param partner: Changed Partner id
|
2010-05-10 12:03:37 +00:00
|
|
|
|
"""
|
2010-05-12 13:16:01 +00:00
|
|
|
|
if not user:
|
2010-05-10 12:03:37 +00:00
|
|
|
|
return {'value': {'email_to': False}}
|
2010-06-25 17:41:41 +00:00
|
|
|
|
email = self.pool.get('res.users')._get_email_from(cr, uid, [user])[user]
|
2010-05-10 12:03:37 +00:00
|
|
|
|
return {'value': {'email_to': email}}
|
|
|
|
|
|
2010-06-25 17:41:41 +00:00
|
|
|
|
def on_change_history(self, cr, uid, ids, history_type, context=None):
|
2010-05-12 13:16:01 +00:00
|
|
|
|
"""Gives message body according to type of history selected
|
|
|
|
|
* info: Forward the case information
|
|
|
|
|
* whole: Send the whole history
|
|
|
|
|
* latest: Send the latest histoy
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of history IDs
|
|
|
|
|
@param context: A standard dictionary for contextual values
|
|
|
|
|
"""
|
2010-05-10 12:03:37 +00:00
|
|
|
|
#TODO: ids and context are not comming
|
|
|
|
|
res = False
|
2010-06-03 11:40:23 +00:00
|
|
|
|
res_id = context.get('active_id')
|
2010-06-25 17:41:41 +00:00
|
|
|
|
msg_val = self._get_case_history(cr, uid, history_type, res_id, context=context)
|
|
|
|
|
if msg_val:
|
2010-07-15 07:33:13 +00:00
|
|
|
|
res = {'value': {'body' : '\n\n' + msg_val}}
|
2010-06-25 17:41:41 +00:00
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
def _get_case_history(self, cr, uid, history_type, res_id, context=None):
|
|
|
|
|
if not res_id:
|
|
|
|
|
return
|
|
|
|
|
|
2010-06-25 18:38:55 +00:00
|
|
|
|
msg_val = ''
|
2010-07-15 07:33:13 +00:00
|
|
|
|
case_info = self.get_lead_details(cr, uid, res_id, context=context)
|
2010-06-25 17:41:41 +00:00
|
|
|
|
model_pool = self.pool.get('crm.lead')
|
2010-07-15 07:33:13 +00:00
|
|
|
|
|
2010-06-25 17:41:41 +00:00
|
|
|
|
if history_type == 'info':
|
2010-07-15 07:33:13 +00:00
|
|
|
|
msg_val = case_info
|
2010-05-12 06:54:33 +00:00
|
|
|
|
|
2010-06-25 17:41:41 +00:00
|
|
|
|
elif history_type == 'whole':
|
2010-05-12 06:54:33 +00:00
|
|
|
|
log_ids = model_pool.browse(cr, uid, res_id, context=context).message_ids
|
2010-07-16 10:41:21 +00:00
|
|
|
|
log_ids = map(lambda x: x.id, filter(lambda x: x.history, log_ids))
|
2010-07-15 07:33:13 +00:00
|
|
|
|
msg_val = case_info + '\n\n' + self.get_whole_history(cr, uid, log_ids, context=context)
|
2010-05-12 06:54:33 +00:00
|
|
|
|
|
2010-06-25 17:41:41 +00:00
|
|
|
|
elif history_type == 'latest':
|
2010-05-12 06:54:33 +00:00
|
|
|
|
log_ids = model_pool.browse(cr, uid, res_id, context=context).message_ids
|
2010-07-16 10:41:21 +00:00
|
|
|
|
log_ids = filter(lambda x: x.history and x.id, log_ids)
|
2010-05-12 06:54:33 +00:00
|
|
|
|
if not log_ids:
|
2010-07-15 07:33:13 +00:00
|
|
|
|
msg_val = case_info
|
|
|
|
|
else:
|
|
|
|
|
msg_val = case_info + '\n\n' + self.get_latest_history(cr, uid, log_ids[0].id, context=context)
|
2010-05-12 06:54:33 +00:00
|
|
|
|
|
2010-06-25 17:41:41 +00:00
|
|
|
|
return msg_val
|
|
|
|
|
|
2010-04-28 11:52:22 +00:00
|
|
|
|
def on_change_partner(self, cr, uid, ids, partner_id):
|
2010-05-12 13:16:01 +00:00
|
|
|
|
"""This function fills address information based on partner/user selected
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of Mail’s IDs
|
|
|
|
|
@param user: Changed User id
|
|
|
|
|
@param partner: Changed Partner id
|
|
|
|
|
"""
|
2010-05-03 06:37:00 +00:00
|
|
|
|
if not partner_id:
|
|
|
|
|
return {'value' : {'email_to' : False, 'address_id': False}}
|
|
|
|
|
|
|
|
|
|
addr = self.pool.get('res.partner').address_get(cr, uid, [partner_id], ['contact'])
|
|
|
|
|
data = {'address_id': addr['contact']}
|
|
|
|
|
data.update(self.on_change_address(cr, uid, ids, addr['contact'])['value'])
|
2010-04-28 11:52:22 +00:00
|
|
|
|
return {
|
2010-05-10 12:03:37 +00:00
|
|
|
|
'value' : data,
|
2010-05-03 06:37:00 +00:00
|
|
|
|
'domain' : {'address_id' : partner_id and "[('partner_id', '=', partner_id)]" or "[]"}
|
2010-04-28 11:52:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def on_change_address(self, cr, uid, ids, address_id):
|
|
|
|
|
email = ''
|
|
|
|
|
if address_id:
|
|
|
|
|
email = self.pool.get('res.partner.address').browse(cr, uid, address_id).email
|
2010-05-03 06:37:00 +00:00
|
|
|
|
return {'value': {'email_to' : email}}
|
2010-04-28 11:52:22 +00:00
|
|
|
|
|
|
|
|
|
def action_forward(self, cr, uid, ids, context=None):
|
|
|
|
|
"""
|
|
|
|
|
Forward the lead to a partner
|
|
|
|
|
"""
|
2010-11-23 07:05:05 +00:00
|
|
|
|
if context is None:
|
2010-11-19 13:48:01 +00:00
|
|
|
|
context = {}
|
2010-04-28 11:52:22 +00:00
|
|
|
|
this = self.browse(cr, uid, ids[0], context=context)
|
2010-07-15 11:24:25 +00:00
|
|
|
|
case_pool = self.pool.get(context.get('active_model'))
|
|
|
|
|
res_id = context and context.get('active_id', False) or False
|
2010-04-28 11:52:22 +00:00
|
|
|
|
case = case_pool.browse(cr, uid, res_id, context=context)
|
|
|
|
|
|
2010-07-15 11:24:25 +00:00
|
|
|
|
context.update({'mail': 'forward'})
|
|
|
|
|
super(crm_lead_forward_to_partner, self).action_send(cr, uid, ids, context=context)
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
2010-07-15 11:24:25 +00:00
|
|
|
|
to_write = {'date_assign': time.strftime('%Y-%m-%d')}
|
|
|
|
|
if (this.name == 'partner' and this.partner_id):
|
|
|
|
|
to_write['partner_assigned_id'] = this.partner_id.id
|
|
|
|
|
|
2010-07-15 12:14:46 +00:00
|
|
|
|
if this.name == 'user':
|
|
|
|
|
to_write.update({'user_id' : this.user_id.id})
|
2010-07-15 11:24:25 +00:00
|
|
|
|
email_re = r'([^ ,<@]+@[^> ,]+)'
|
2010-07-15 07:33:13 +00:00
|
|
|
|
email_cc = re.findall(email_re, case.email_cc or '')
|
2010-07-19 14:02:15 +00:00
|
|
|
|
new_cc = []
|
2010-07-20 06:11:47 +00:00
|
|
|
|
if case.email_cc:
|
|
|
|
|
new_cc.append(case.email_cc)
|
2010-07-15 07:33:13 +00:00
|
|
|
|
for to in this.email_to.split(','):
|
|
|
|
|
email_to = re.findall(email_re, to)
|
|
|
|
|
email_to = email_to and email_to[0] or ''
|
|
|
|
|
if email_to not in email_cc:
|
2010-07-19 14:02:15 +00:00
|
|
|
|
new_cc.append(to)
|
2010-07-20 06:11:47 +00:00
|
|
|
|
to_write.update({'email_cc' : ', '.join(new_cc) })
|
2010-07-15 12:14:46 +00:00
|
|
|
|
case_pool.write(cr, uid, case.id, to_write, context=context)
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
2010-12-28 10:44:45 +00:00
|
|
|
|
return {'type': 'ir.actions.act_window_close'}
|
2010-04-28 11:52:22 +00:00
|
|
|
|
|
2010-05-12 06:54:33 +00:00
|
|
|
|
def get_lead_details(self, cr, uid, lead_id, context=None):
|
2010-07-15 07:33:13 +00:00
|
|
|
|
body = []
|
2010-05-10 12:03:37 +00:00
|
|
|
|
lead_proxy = self.pool.get('crm.lead')
|
2010-05-12 06:54:33 +00:00
|
|
|
|
lead = lead_proxy.browse(cr, uid, lead_id, context=context)
|
2010-07-28 17:20:37 +00:00
|
|
|
|
if not lead.type or lead.type == 'lead' or not lead.partner_address_id:
|
2010-05-03 06:37:00 +00:00
|
|
|
|
field_names = [
|
2010-07-28 17:20:37 +00:00
|
|
|
|
'partner_name', 'title', 'function', 'street', 'street2',
|
|
|
|
|
'zip', 'city', 'country_id', 'state_id', 'email_from',
|
|
|
|
|
'phone', 'fax', 'mobile', 'categ_id', 'description',
|
2010-05-03 06:37:00 +00:00
|
|
|
|
]
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
2010-05-03 06:37:00 +00:00
|
|
|
|
for field_name in field_names:
|
|
|
|
|
field_definition = lead_proxy._columns[field_name]
|
|
|
|
|
value = None
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
2010-05-03 06:37:00 +00:00
|
|
|
|
if field_definition._type == 'selection':
|
|
|
|
|
if hasattr(field_definition.selection, '__call__'):
|
|
|
|
|
key = field_definition.selection(lead_proxy, cr, uid, context=context)
|
|
|
|
|
else:
|
2010-07-28 17:20:37 +00:00
|
|
|
|
key = field_definition.selection
|
2010-05-03 06:37:00 +00:00
|
|
|
|
value = dict(key).get(lead[field_name], lead[field_name])
|
|
|
|
|
elif field_definition._type == 'many2one':
|
|
|
|
|
if lead[field_name]:
|
|
|
|
|
value = lead[field_name].name_get()[0][1]
|
|
|
|
|
else:
|
|
|
|
|
value = lead[field_name]
|
2010-06-25 17:41:41 +00:00
|
|
|
|
|
2010-07-15 07:33:13 +00:00
|
|
|
|
body.append("%s: %s" % (field_definition.string, value or ''))
|
2010-05-03 06:37:00 +00:00
|
|
|
|
elif lead.type == 'opportunity':
|
|
|
|
|
pa = lead.partner_address_id
|
2010-07-15 07:33:13 +00:00
|
|
|
|
body = [
|
2010-07-28 17:20:37 +00:00
|
|
|
|
"Partner: %s" % (lead.partner_id and lead.partner_id.name_get()[0][1]),
|
|
|
|
|
"Contact: %s" % (pa.name or ''),
|
|
|
|
|
"Title: %s" % (pa.title or ''),
|
|
|
|
|
"Function: %s" % (pa.function or ''),
|
|
|
|
|
"Street: %s" % (pa.street or ''),
|
|
|
|
|
"Street2: %s" % (pa.street2 or ''),
|
|
|
|
|
"Zip: %s" % (pa.zip or ''),
|
|
|
|
|
"City: %s" % (pa.city or ''),
|
|
|
|
|
"Country: %s" % (pa.country_id and pa.country_id.name_get()[0][1] or ''),
|
|
|
|
|
"State: %s" % (pa.state_id and pa.state_id.name_get()[0][1] or ''),
|
|
|
|
|
"Email: %s" % (pa.email or ''),
|
|
|
|
|
"Phone: %s" % (pa.phone or ''),
|
|
|
|
|
"Fax: %s" % (pa.fax or ''),
|
|
|
|
|
"Mobile: %s" % (pa.mobile or ''),
|
|
|
|
|
"Lead Category: %s" % (lead.categ_id and lead.categ_id.name or ''),
|
|
|
|
|
"Details: %s" % (lead.description or ''),
|
2010-05-03 06:37:00 +00:00
|
|
|
|
]
|
2010-07-15 07:33:13 +00:00
|
|
|
|
return "\n".join(body + ['---'])
|
2010-06-25 12:42:02 +00:00
|
|
|
|
|
2010-05-10 12:03:37 +00:00
|
|
|
|
def default_get(self, cr, uid, fields, context=None):
|
|
|
|
|
"""
|
|
|
|
|
This function gets default values
|
|
|
|
|
"""
|
|
|
|
|
if context is None:
|
|
|
|
|
context = {}
|
|
|
|
|
|
2010-06-25 17:41:41 +00:00
|
|
|
|
defaults = super(crm_lead_forward_to_partner, self).default_get(cr, uid, fields, context=context)
|
|
|
|
|
|
2010-06-25 18:22:06 +00:00
|
|
|
|
active_id = context.get('active_id')
|
|
|
|
|
if not active_id:
|
2010-06-25 17:41:41 +00:00
|
|
|
|
return defaults
|
2010-05-10 12:03:37 +00:00
|
|
|
|
|
|
|
|
|
lead_proxy = self.pool.get('crm.lead')
|
2010-06-25 18:22:06 +00:00
|
|
|
|
lead = lead_proxy.browse(cr, uid, active_id, context=context)
|
2010-06-25 12:42:02 +00:00
|
|
|
|
|
2010-07-15 07:33:13 +00:00
|
|
|
|
body = self._get_case_history(cr, uid, defaults.get('history', 'latest'), lead.id, context=context)
|
2010-06-25 17:41:41 +00:00
|
|
|
|
defaults.update({
|
|
|
|
|
'subject' : '%s: %s' % (_('Fwd'), lead.name),
|
2010-07-15 07:33:13 +00:00
|
|
|
|
'body' : body,
|
2011-02-07 12:31:53 +00:00
|
|
|
|
'email_cc' : ''
|
2010-06-25 17:41:41 +00:00
|
|
|
|
})
|
|
|
|
|
return defaults
|
2010-04-28 13:51:36 +00:00
|
|
|
|
|
2010-05-03 06:37:00 +00:00
|
|
|
|
crm_lead_forward_to_partner()
|
|
|
|
|
|
2010-04-28 11:52:22 +00:00
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|