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 ##############################################################################
26 from osv import osv, fields
28 from tools.translate import _
30 class crm_lead_forward_to_partner(osv.osv_memory):
31 """Forwards lead history"""
32 _name = 'crm.lead.forward.to.partner'
33 _inherit = "crm.send.mail"
36 'name': fields.selection([('user', 'User'), ('partner', 'Partner'), \
37 ('email', 'Email Address')], 'Send to', required=True),
38 'user_id': fields.many2one('res.users', "User"),
39 'partner_id' : fields.many2one('res.partner', 'Partner'),
40 'address_id' : fields.many2one('res.partner.address', 'Address'),
41 'history': fields.selection([('info', 'Case Information'), ('latest', 'Latest email'), ('whole', 'Whole Story')], 'Send history', required=True),
47 'email_from': lambda self, cr, uid, *a: self.pool.get('res.users')._get_email_from(cr, uid, uid)[uid]
50 def get_whole_history(self, cr, uid, ids, context=None):
51 """This function gets whole communication history and returns as top posting style
52 @param self: The object pointer
53 @param cr: the current row, from the database cursor,
54 @param uid: the current user’s ID for security checks,
55 @param ids: List of history IDs
56 @param context: A standard dictionary for contextual values
60 whole.append(self.get_latest_history(cr, uid, hist_id, context=context))
61 whole = '\n\n'.join(whole)
64 def get_latest_history(self, cr, uid, hist_id, context=None):
65 """This function gets latest communication and returns as top posting style
66 @param self: The object pointer
67 @param cr: the current row, from the database cursor,
68 @param uid: the current user’s ID for security checks,
69 @param hist_id: Id of latest history
70 @param context: A standard dictionary for contextual values
72 log_pool = self.pool.get('mailgate.message')
73 hist = log_pool.browse(cr, uid, hist_id, context=context)
74 header = '-------- Original Message --------'
75 sender = 'From: %s' %(hist.email_from or '')
76 to = 'To: %s' % (hist.email_to or '')
77 sentdate = 'Date: %s' % (hist.date or '')
78 desc = '\n%s'%(hist.description)
79 original = [header, sender, to, sentdate, desc]
80 original = '\n'.join(original)
83 def on_change_email(self, cr, uid, ids, user):
84 """This function fills email information based on user selected
85 @param self: The object pointer
86 @param cr: the current row, from the database cursor,
87 @param uid: the current user’s ID for security checks,
88 @param ids: List of Mail’s IDs
89 @param user: Changed User id
90 @param partner: Changed Partner id
93 return {'value': {'email_to': False}}
94 email = self.pool.get('res.users')._get_email_from(cr, uid, [user])[user]
95 return {'value': {'email_to': email}}
97 def on_change_history(self, cr, uid, ids, history_type, context=None):
98 """Gives message body according to type of history selected
99 * info: Forward the case information
100 * whole: Send the whole history
101 * latest: Send the latest histoy
102 @param self: The object pointer
103 @param cr: the current row, from the database cursor,
104 @param uid: the current user’s ID for security checks,
105 @param ids: List of history IDs
106 @param context: A standard dictionary for contextual values
108 #TODO: ids and context are not comming
110 res_id = context.get('active_id')
111 msg_val = self._get_case_history(cr, uid, history_type, res_id, context=context)
113 res = {'value': {'body' : '\n\n' + msg_val}}
116 def _get_case_history(self, cr, uid, history_type, res_id, context=None):
121 case_info = self.get_lead_details(cr, uid, res_id, context=context)
122 model_pool = self.pool.get('crm.lead')
124 if history_type == 'info':
127 elif history_type == 'whole':
128 log_ids = model_pool.browse(cr, uid, res_id, context=context).message_ids
129 log_ids = map(lambda x: x.id, filter(lambda x: x.history, log_ids))
130 msg_val = case_info + '\n\n' + self.get_whole_history(cr, uid, log_ids, context=context)
132 elif history_type == 'latest':
133 log_ids = model_pool.browse(cr, uid, res_id, context=context).message_ids
134 log_ids = filter(lambda x: x.history and x.id, log_ids)
138 msg_val = case_info + '\n\n' + self.get_latest_history(cr, uid, log_ids[0].id, context=context)
142 def on_change_partner(self, cr, uid, ids, partner_id):
143 """This function fills address information based on partner/user selected
144 @param self: The object pointer
145 @param cr: the current row, from the database cursor,
146 @param uid: the current user’s ID for security checks,
147 @param ids: List of Mail’s IDs
148 @param user: Changed User id
149 @param partner: Changed Partner id
152 return {'value' : {'email_to' : False, 'address_id': False}}
154 addr = self.pool.get('res.partner').address_get(cr, uid, [partner_id], ['contact'])
155 data = {'address_id': addr['contact']}
156 data.update(self.on_change_address(cr, uid, ids, addr['contact'])['value'])
159 'domain' : {'address_id' : partner_id and "[('partner_id', '=', partner_id)]" or "[]"}
162 def on_change_address(self, cr, uid, ids, address_id):
165 email = self.pool.get('res.partner.address').browse(cr, uid, address_id).email
166 return {'value': {'email_to' : email}}
168 def action_forward(self, cr, uid, ids, context=None):
170 Forward the lead to a partner
174 this = self.browse(cr, uid, ids[0], context=context)
175 case_pool = self.pool.get(context.get('active_model'))
176 res_id = context and context.get('active_id', False) or False
177 case = case_pool.browse(cr, uid, res_id, context=context)
179 context.update({'mail': 'forward'})
180 super(crm_lead_forward_to_partner, self).action_send(cr, uid, ids, context=context)
182 to_write = {'date_assign': time.strftime('%Y-%m-%d')}
183 if (this.name == 'partner' and this.partner_id):
184 to_write['partner_assigned_id'] = this.partner_id.id
186 if this.name == 'user':
187 to_write.update({'user_id' : this.user_id.id})
188 email_re = r'([^ ,<@]+@[^> ,]+)'
189 email_cc = re.findall(email_re, case.email_cc or '')
192 new_cc.append(case.email_cc)
193 for to in this.email_to.split(','):
194 email_to = re.findall(email_re, to)
195 email_to = email_to and email_to[0] or ''
196 if email_to not in email_cc:
198 to_write.update({'email_cc' : ', '.join(new_cc) })
199 case_pool.write(cr, uid, case.id, to_write, context=context)
201 return {'type': 'ir.actions.act_window_close'}
203 def get_lead_details(self, cr, uid, lead_id, context=None):
205 lead_proxy = self.pool.get('crm.lead')
206 lead = lead_proxy.browse(cr, uid, lead_id, context=context)
207 if not lead.type or lead.type == 'lead' or not lead.partner_address_id:
209 'partner_name', 'title', 'function', 'street', 'street2',
210 'zip', 'city', 'country_id', 'state_id', 'email_from',
211 'phone', 'fax', 'mobile', 'categ_id', 'description',
214 for field_name in field_names:
215 field_definition = lead_proxy._columns[field_name]
218 if field_definition._type == 'selection':
219 if hasattr(field_definition.selection, '__call__'):
220 key = field_definition.selection(lead_proxy, cr, uid, context=context)
222 key = field_definition.selection
223 value = dict(key).get(lead[field_name], lead[field_name])
224 elif field_definition._type == 'many2one':
226 value = lead[field_name].name_get()[0][1]
228 value = lead[field_name]
230 body.append("%s: %s" % (field_definition.string, value or ''))
231 elif lead.type == 'opportunity':
232 pa = lead.partner_address_id
234 "Partner: %s" % (lead.partner_id and lead.partner_id.name_get()[0][1]),
235 "Contact: %s" % (pa.name or ''),
236 "Title: %s" % (pa.title or ''),
237 "Function: %s" % (pa.function or ''),
238 "Street: %s" % (pa.street or ''),
239 "Street2: %s" % (pa.street2 or ''),
240 "Zip: %s" % (pa.zip or ''),
241 "City: %s" % (pa.city or ''),
242 "Country: %s" % (pa.country_id and pa.country_id.name_get()[0][1] or ''),
243 "State: %s" % (pa.state_id and pa.state_id.name_get()[0][1] or ''),
244 "Email: %s" % (pa.email or ''),
245 "Phone: %s" % (pa.phone or ''),
246 "Fax: %s" % (pa.fax or ''),
247 "Mobile: %s" % (pa.mobile or ''),
248 "Lead Category: %s" % (lead.categ_id and lead.categ_id.name or ''),
249 "Details: %s" % (lead.description or ''),
251 return "\n".join(body + ['---'])
253 def default_get(self, cr, uid, fields, context=None):
255 This function gets default values
260 defaults = super(crm_lead_forward_to_partner, self).default_get(cr, uid, fields, context=context)
262 active_id = context.get('active_id')
266 lead_proxy = self.pool.get('crm.lead')
267 lead = lead_proxy.browse(cr, uid, active_id, context=context)
269 body = self._get_case_history(cr, uid, defaults.get('history', 'latest'), lead.id, context=context)
271 'subject' : '%s: %s' % (_('Fwd'), lead.name),
277 crm_lead_forward_to_partner()
279 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: