[MERGE] mail/chatter complete review/refactoring
[odoo/odoo.git] / addons / crm_partner_assign / wizard / crm_forward_to_partner.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 import re
24 import time
25 import tools
26
27 from osv import osv, fields
28 from tools.translate import _
29
30 class crm_lead_forward_to_partner(osv.osv_memory):
31     """ Forward info history to partners. """
32     _name = 'crm.lead.forward.to.partner'
33     _inherit = "mail.compose.message"
34
35     def default_get(self, cr, uid, fields, context=None):
36         if context is None:
37             context = {}
38         # set as comment, perform overrided document-like action that calls get_record_data
39         old_mode = context.get('default_composition_mode', 'forward')
40         context['default_composition_mode'] = 'comment'
41         res = super(crm_lead_forward_to_partner, self).default_get(cr, uid, fields, context=context)
42         # back to forward mode
43         context['default_composition_mode'] = old_mode
44         res['composition_mode'] = context['default_composition_mode']
45         return res
46
47     def _get_composition_mode_selection(self, cr, uid, context=None):
48         composition_mode = super(crm_lead_forward_to_partner, self)._get_composition_mode_selection(cr, uid, context=context)
49         composition_mode.append(('forward', 'Forward'))
50         return composition_mode
51
52     _columns = {
53         'partner_ids': fields.many2many('res.partner',
54             'lead_forward_to_partner_res_partner_rel',
55             'wizard_id', 'partner_id', 'Additional contacts'),
56         'attachment_ids': fields.many2many('ir.attachment',
57             'lead_forward_to_partner_attachment_rel',
58             'wizard_id', 'attachment_id', 'Attachments'),
59         'history_mode': fields.selection([('info', 'Case Information'),
60             ('latest', 'Latest email'), ('whole', 'Whole Story')],
61             'Send history', required=True),
62     }
63
64     _defaults = {
65         'history_mode': 'latest',
66         'content_subtype': lambda self,cr, uid, context={}: 'html',
67     }
68
69     def get_record_data(self, cr, uid, model, res_id, context=None):
70         """ Override of mail.compose.message, to add default values coming
71             form the related lead.
72         """
73         res = super(crm_lead_forward_to_partner, self).get_record_data(cr, uid, model, res_id, context=context)
74         if model not in ('crm.lead') or not res_id:
75             return res
76         lead_obj = self.pool.get(model)
77         lead = lead_obj.browse(cr, uid, res_id, context=context)
78         subject = '%s: %s - %s' % (_('Fwd'), _('Lead forward'), lead.name)
79         body = self._get_message_body(cr, uid, lead, 'info', context=context)
80         res.update({
81             'subject': subject,
82             'body': body,
83             })
84         return res
85
86     def on_change_history_mode(self, cr, uid, ids, history_mode, model, res_id, context=None):
87         """ Update body when changing history_mode """
88         if model and model == 'crm.lead' and res_id:
89             lead = self.pool.get(model).browse(cr, uid, res_id, context=context)
90             body = self._get_message_body(cr, uid, lead, history_mode, context=context)
91             return {'value': {'body': body}}
92
93     def create(self, cr, uid, values, context=None):
94         """ TDE-HACK: remove 'type' from context, because when viewing an
95             opportunity form view, a default_type is set and propagated
96             to the wizard, that has a not matching type field. """
97         default_type = context.pop('default_type', None)
98         new_id = super(crm_lead_forward_to_partner, self).create(cr, uid, values, context=context)
99         if default_type:
100             context['default_type'] = default_type
101         return new_id
102
103     def action_forward(self, cr, uid, ids, context=None):
104         """ Forward the lead to a partner """
105         if context is None:
106             context = {}
107         res = {'type': 'ir.actions.act_window_close'}
108         wizard = self.browse(cr, uid, ids[0], context=context)
109         if wizard.model not in ('crm.lead'):
110             return res
111
112         lead = self.pool.get(wizard.model)
113         lead_ids = wizard.res_id and [wizard.res_id] or []
114
115         if wizard.composition_mode == 'mass_mail':
116             lead_ids = context and context.get('active_ids', []) or []
117             value = self.default_get(cr, uid, ['body', 'email_to', 'email_cc', 'subject', 'history_mode'], context=context)
118             self.write(cr, uid, ids, value, context=context)
119
120         self.send_mail(cr, uid, ids, context=context)
121         # for case in lead.browse(cr, uid, lead_ids, context=context):
122             # TODO: WHAT TO DO WITH THAT ?
123             # if (this.send_to == 'partner' and this.partner_id):
124             #     lead.assign_partner(cr, uid, [case.id], this.partner_id.id, context=context)
125
126             # if this.send_to == 'user':
127             #     lead.allocate_salesman(cr, uid, [case.id], [this.user_id.id], context=context)
128         return res
129
130     def _get_info_body(self, cr, uid, lead, context=None):
131         field_names = []
132         proxy = self.pool.get(lead._name)
133         if lead.type == 'opportunity':
134             field_names += ['partner_id']
135         field_names += [
136             'partner_name' , 'title', 'function', 'street', 'street2',
137             'zip', 'city', 'country_id', 'state_id', 'email_from',
138             'phone', 'fax', 'mobile', 'categ_id', 'description',
139         ]
140         return proxy._mail_body(cr, uid, lead, field_names, context=context)
141
142     def _get_message_body(self, cr, uid, lead, history_mode='whole', context=None):
143         """ This function gets whole communication history and returns as top
144             posting style
145             #1: form a body, based on the lead
146             #2: append to the body the communication history, based on the
147                 history_mode parameter
148
149             - info: Forward the case information
150             - latest: Send the latest history
151             - whole: Send the whole history
152
153             :param lead: browse_record on crm.lead
154             :param history_mode: 'whole' or 'latest'
155         """
156         mail_message = self.pool.get('mail.message')
157         body = self._get_info_body(cr, uid, lead, context=context)
158         if history_mode not in ('whole', 'latest'):
159             return body or ''
160         for message in lead.message_ids:
161             header = '-------- Original Message --------'
162             sentdate = 'Date: %s' % (message.date or '')
163             desc = '\n%s'%(message.body)
164             original = [header, sentdate, desc, '\n']
165             original = '\n'.join(original)
166             body += original
167             if history_mode == 'latest':
168                 break
169         return body or ''
170
171
172 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: