1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
25 from osv import osv, fields
26 from tools.translate import _
27 from mail.mail_message import to_email
29 class crm_lead_forward_to_partner(osv.osv_memory):
30 """Forwards lead history"""
31 _name = 'crm.lead.forward.to.partner'
32 _inherit = "mail.compose.message"
35 'send_to': fields.selection([('user', 'User'), ('partner', 'Partner'), \
36 ('email', 'Email Address')], 'Send to', required=True),
37 'user_id': fields.many2one('res.users', "User"),
38 'attachment_ids': fields.many2many('ir.attachment','lead_forward_to_partner_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'),
39 'partner_id' : fields.many2one('res.partner', 'Partner'),
40 'history': fields.selection([('info', 'Case Information'), ('latest', 'Latest email'), ('whole', 'Whole Story')], 'Send history', required=True),
46 'email_from': lambda self, cr, uid, *a: self.pool.get('res.users')._get_email_from(cr, uid, uid)[uid],
51 def on_change_email(self, cr, uid, ids, user):
53 return {'value': {'email_to': False}}
54 email = self.pool.get('res.users')._get_email_from(cr, uid, [user])[user]
55 return {'value': {'email_to': email}}
57 def on_change_history(self, cr, uid, ids, history_type, context=None):
58 """Gives message body according to type of history selected
59 * info: Forward the case information
60 * whole: Send the whole history
61 * latest: Send the latest histoy
63 #TODO: ids and context are not comming
65 res_id = context.get('active_id')
66 model = context.get('active_model')
67 lead = self.pool.get(model).browse(cr, uid, res_id, context)
68 body_text = self._get_message_body_text(cr, uid, lead, history_type, context=context)
70 res = {'value': {'body_text' : body_text}}
73 def on_change_partner(self, cr, uid, ids, partner_id):
74 """This function fills address information based on partner/user selected
77 return {'value' : {'email_to' : False}}
78 partner_obj = self.pool.get('res.partner')
80 partner = partner_obj.browse(cr, uid, [partner_id])
81 user_id = partner and partner[0].user_id or False
82 data.update({'email_from': partner and partner[0].email or "",
83 'email_cc' : user_id and user_id.user_email or '',
84 'user_id': user_id and user_id.id or False})
85 return {'value' : data}
87 def action_forward(self, cr, uid, ids, context=None):
89 Forward the lead to a partner
93 res = {'type': 'ir.actions.act_window_close'}
94 model = context.get('active_model')
95 if model not in ('crm.lead'):
98 this = self.browse(cr, uid, ids[0], context=context)
99 lead = self.pool.get(model)
100 lead_id = context and context.get('active_id', False) or False
101 lead_ids = lead_id and [lead_id] or []
102 mode = context.get('mail.compose.message.mode')
103 if mode == 'mass_mail':
104 lead_ids = context and context.get('active_ids', []) or []
105 value = self.default_get(cr, uid, ['body_text', 'email_to', 'email_cc', 'subject', 'history'], context=context)
106 self.write(cr, uid, ids, value, context=context)
107 context['mail.compose.message.mode'] = mode
109 self.send_mail(cr, uid, ids, context=context)
110 for case in lead.browse(cr, uid, lead_ids, context=context):
111 if (this.send_to == 'partner' and this.partner_id):
112 lead.assign_partner(cr, uid, [case.id], this.partner_id.id, context=context)
114 if this.send_to == 'user':
115 lead.allocate_salesman(cr, uid, [case.id], [this.user_id.id], context=context)
117 email_cc = to_email(case.email_cc)
118 email_cc = email_cc and email_cc[0] or ''
121 new_cc.append(email_cc)
122 for to in this.email_to.split(','):
123 email_to = to_email(to)
124 email_to = email_to and email_to[0] or ''
125 if email_to not in new_cc:
127 update_vals = {'email_cc' : ', '.join(new_cc) }
128 lead.write(cr, uid, [case.id], update_vals, context=context)
131 def _get_info_body_text(self, cr, uid, lead, context=None):
133 proxy = self.pool.get(lead._name)
134 if lead.type == 'opportunity':
135 field_names += ['partner_id']
137 'partner_name' , 'title', 'function', 'street', 'street2',
138 'zip', 'city', 'country_id', 'state_id', 'email_from',
139 'phone', 'fax', 'mobile', 'categ_id', 'description',
141 return proxy._mail_body_text(cr, uid, lead, field_names, context=context)
143 def _get_message_body_text(self, cr, uid, lead, mode='whole', context=None):
144 """This function gets whole communication history and returns as top posting style
146 mail_message = self.pool.get('mail.message')
148 body = self._get_info_body_text(cr, uid, lead, context=context)
149 if mode in ('whole', 'latest'):
150 message_ids = lead.message_ids
151 message_ids = map(lambda x: x.id, filter(lambda x: x.email_from, message_ids))
152 if mode == 'latest' and len(message_ids):
153 message_ids = [message_ids[0]]
154 for message in mail_message.browse(cr, uid, message_ids, context=context):
155 header = '-------- Original Message --------'
156 sender = 'From: %s' %(message.email_from or '')
157 to = 'To: %s' % (message.email_to or '')
158 sentdate = 'Date: %s' % (message.date or '')
159 desc = '\n%s'%(message.body_text)
160 original = [header, sender, to, sentdate, desc, '\n']
161 original = '\n'.join(original)
165 def get_value(self, cr, uid, model, res_id, context=None):
168 res = super(crm_lead_forward_to_partner, self).get_value(cr, uid, model, res_id, context=context)
169 if model not in ("crm.lead"):
171 proxy = self.pool.get(model)
172 partner = self.pool.get('res.partner')
173 lead = proxy.browse(cr, uid, res_id, context=context)
174 mode = context.get('mail.compose.message.mode')
175 if mode == "forward":
176 body_type = context.get('mail.compose.message.body')
177 email_cc = res.get('email_cc', "")
178 email = res.get('email_to', "")
179 subject = '%s: %s - %s' % (_('Fwd'), 'Lead forward', lead.name)
180 body = self._get_message_body_text(cr, uid, lead, body_type, context=context)
181 partner_assigned_id = lead.partner_assigned_id and lead.partner_assigned_id.id or False
183 if not partner_assigned_id:
184 partner_assigned_id = proxy.search_geo_partner(cr, uid, [lead.id], context=None).get(lead.id, False)
185 if partner_assigned_id:
186 assigned_partner = partner.browse(cr, uid, partner_assigned_id, context=context)
187 user_id = assigned_partner.user_id and assigned_partner.user_id.id or False
188 email_cc = assigned_partner.user_id and assigned_partner.user_id.user_email or ''
189 email = assigned_partner.email
194 'email_cc' : email_cc,
196 'partner_assigned_id': partner_assigned_id,
202 def default_get(self, cr, uid, fields, context=None):
205 context['mail.compose.message.mode'] = 'forward'
206 context['mail.compose.message.body'] = 'info'
207 return super(crm_lead_forward_to_partner, self).default_get(cr, uid, fields, context=context)
208 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: