[IMP] account_invoice: now have its _get_formview_action method. _get_formview_action...
[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 from openerp.osv import fields, osv
24 from openerp.tools.translate import _
25
26
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"
31
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
36
37     _columns = {
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),
47     }
48
49     _defaults = {
50         'history_mode': 'info',
51     }
52
53     def default_get(self, cr, uid, fields, context=None):
54         if context is None:
55             context = {}
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']
63         return res
64
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.
68         """
69         if context is None:
70             context = {}
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:
73             return res
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']
82         return res
83
84     def on_change_history_mode(self, cr, uid, ids, history_mode, model, res_id, context=None):
85         """ Update body when changing history_mode """
86         if context is None:
87             context = {}
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}}
93
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)
100         if default_type:
101             context['default_type'] = default_type
102         return new_id
103
104     def action_forward(self, cr, uid, ids, context=None):
105         """ Forward the lead to a partner """
106         if context is None:
107             context = {}
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'):
111             return res
112
113         lead = self.pool[wizard.model]
114         lead_ids = wizard.res_id and [wizard.res_id] or []
115
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)
120
121         return self.send_mail(cr, uid, ids, context=context)
122
123
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: