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 ##############################################################################
23 from openerp.osv import fields, osv
24 from openerp.tools.translate import _
27 class crm_lead_forward_to_partner(osv.TransientModel):
28 """ Forward info history to partners. """
29 _name = 'crm.lead.forward.to.partner'
30 _inherit = "mail.compose.message"
32 def _get_composition_mode_selection(self, cr, uid, context=None):
33 composition_mode = super(crm_lead_forward_to_partner, self)._get_composition_mode_selection(cr, uid, context=context)
34 composition_mode.append(('forward', 'Forward'))
35 return composition_mode
38 'partner_ids': fields.many2many('res.partner',
39 'lead_forward_to_partner_res_partner_rel',
40 'wizard_id', 'partner_id', 'Additional contacts'),
41 'attachment_ids': fields.many2many('ir.attachment',
42 'lead_forward_to_partner_attachment_rel',
43 'wizard_id', 'attachment_id', 'Attachments'),
44 'history_mode': fields.selection([('info', 'Internal notes'),
45 ('latest', 'Latest email'), ('whole', 'Whole Story')],
46 'Send history', required=True),
50 'history_mode': 'info',
53 def default_get(self, cr, uid, fields, context=None):
56 # set as comment, perform overrided document-like action that calls get_record_data
57 old_mode = context.get('default_composition_mode', 'forward')
58 context['default_composition_mode'] = 'comment'
59 res = super(crm_lead_forward_to_partner, self).default_get(cr, uid, fields, context=context)
60 # back to forward mode
61 context['default_composition_mode'] = old_mode
62 res['composition_mode'] = context['default_composition_mode']
65 def get_record_data(self, cr, uid, model, res_id, context=None):
66 """ Override of mail.compose.message, to add default values coming
67 form the related lead.
71 res = super(crm_lead_forward_to_partner, self).get_record_data(cr, uid, model, res_id, context=context)
72 if model not in ('crm.lead') or not res_id:
74 template_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'crm_partner_assign', 'crm_partner_assign_email_template')[1]
75 context['history_mode'] = context.get('history_mode','whole')
76 mail_body_fields = ['partner_id', 'partner_name', 'title', 'function', 'street', 'street2', 'zip', 'city', 'country_id', 'state_id', 'email_from', 'phone', 'fax', 'mobile', 'description']
77 lead = self.pool.get('crm.lead').browse(cr, uid, res_id, context=context)
78 context['mail_body'] = self.pool.get('crm.lead')._mail_body(cr, uid, lead, mail_body_fields, context=context)
79 template = self.generate_email_for_composer(cr, uid, template_id, res_id, context)
80 res['subject'] = template['subject']
81 res['body'] = template['body']
84 def on_change_history_mode(self, cr, uid, ids, history_mode, model, res_id, context=None):
85 """ Update body when changing history_mode """
88 if model and model == 'crm.lead' and res_id:
89 lead = self.pool[model].browse(cr, uid, res_id, context=context)
90 context['history_mode'] = history_mode
91 body = self.get_record_data(cr, uid, 'crm.lead', res_id, context=context)['body']
92 return {'value': {'body': body}}
94 def create(self, cr, uid, values, context=None):
95 """ TDE-HACK: remove 'type' from context, because when viewing an
96 opportunity form view, a default_type is set and propagated
97 to the wizard, that has a not matching type field. """
98 default_type = context.pop('default_type', None)
99 new_id = super(crm_lead_forward_to_partner, self).create(cr, uid, values, context=context)
101 context['default_type'] = default_type
104 def action_forward(self, cr, uid, ids, context=None):
105 """ Forward the lead to a partner """
108 res = {'type': 'ir.actions.act_window_close'}
109 wizard = self.browse(cr, uid, ids[0], context=context)
110 if wizard.model not in ('crm.lead'):
113 lead = self.pool[wizard.model]
114 lead_ids = wizard.res_id and [wizard.res_id] or []
116 if wizard.composition_mode == 'mass_mail':
117 lead_ids = context and context.get('active_ids', []) or []
118 value = self.default_get(cr, uid, ['body', 'email_to', 'email_cc', 'subject', 'history_mode'], context=context)
119 self.write(cr, uid, ids, value, context=context)
121 return self.send_mail(cr, uid, ids, context=context)
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: