[IMP]: crm: Improvement in yaml for crm lead and crm phonecall form and minor improve...
[odoo/odoo.git] / addons / crm / wizard / crm_phonecall_to_opportunity.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import osv, fields
23 from tools.translate import _
24
25 class crm_phonecall2opportunity(osv.osv_memory):
26     """ Converts Phonecall to Opportunity"""
27
28     _name = 'crm.phonecall2opportunity'
29     _description = 'Phonecall To Opportunity'
30
31     def action_cancel(self, cr, uid, ids, context=None):
32         """
33         Closes Phonecall to Opportunity form
34         @param self: The object pointer
35         @param cr: the current row, from the database cursor,
36         @param uid: the current user’s ID for security checks,
37         @param ids: List of Phonecall to Opportunity's IDs
38         @param context: A standard dictionary for contextual values
39         """
40
41         return {'type':'ir.actions.act_window_close'}
42
43     def view_init(self, cr, uid, fields, context=None):
44         """
45         This function checks for precondition before wizard executes
46         @param self: The object pointer
47         @param cr: the current row, from the database cursor,
48         @param uid: the current user’s ID for security checks,
49         @param fields: List of fields for default value
50         @param context: A standard dictionary for contextual values
51
52         """
53         phonecall_obj = self.pool.get('crm.phonecall')
54         record_id = context and context.get('active_id', False) or False
55         case = phonecall_obj.browse(cr, uid, record_id, context=context)
56         if case.state in ['done', 'cancel']:
57                 raise osv.except_osv(_("Warning"), _("Closed/Cancelled Phone \
58 Call Could not convert into Opportunity"))
59
60
61     def action_apply(self, cr, uid, ids, context=None):
62         """
63         This converts Phonecall to Opportunity and opens Phonecall view
64         @param self: The object pointer
65         @param cr: the current row, from the database cursor,
66         @param uid: the current user’s ID for security checks,
67         @param ids: List of Phonecall to Opportunity IDs
68         @param context: A standard dictionary for contextual values
69
70         @return : Dictionary value for created Opportunity form
71         """
72         
73         record_id = context and context.get('active_id', False) or False
74         print "ids", ids 
75         if record_id:
76             opp_obj = self.pool.get('crm.opportunity')
77             phonecall_obj = self.pool.get('crm.phonecall')
78             case = phonecall_obj.browse(cr, uid, record_id, context=context)
79             data_obj = self.pool.get('ir.model.data')
80             result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_opportunities_filter')
81             res = data_obj.read(cr, uid, result, ['res_id'])
82             id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_oppor')
83             id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_oppor')
84             if id2:
85                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
86             if id3:
87                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
88             for this in self.browse(cr, uid, ids, context=context):
89                 new_opportunity_id = opp_obj.create(cr, uid, {
90                                 'name': this.name,
91                                 'planned_revenue': this.planned_revenue,
92                                 'probability': this.probability,
93                                 'partner_id': this.partner_id and this.partner_id.id or False,
94                                 'section_id': case.section_id and case.section_id.id or False,
95                                 'description': case.description or False,
96                                 'phonecall_id': case.id,
97                                 'priority': case.priority,
98                                 'phone': case.partner_phone or False,
99                             })
100                 new_opportunity = opp_obj.browse(cr, uid, new_opportunity_id)
101                 vals = {
102                             'partner_id': this.partner_id.id,
103                             'opportunity_id' : new_opportunity_id,
104                             }
105                 phonecall_obj.write(cr, uid, [case.id], vals)
106                 phonecall_obj.case_close(cr, uid, [case.id])
107                 opp_obj.case_open(cr, uid, [new_opportunity_id])
108
109             value = {
110                 'name': _('Opportunity'),
111                 'view_type': 'form',
112                 'view_mode': 'form,tree',
113                 'res_model': 'crm.opportunity',
114                 'res_id': int(new_opportunity_id),
115                 'view_id': False,
116                 'views': [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')],
117                 'type': 'ir.actions.act_window',
118                 'search_view_id': res['res_id']
119             }
120             return value
121
122     _columns = {
123         'name' : fields.char('Opportunity Summary', size=64, required=True, select=1),
124         'probability': fields.float('Success Probability'),
125         'planned_revenue': fields.float('Expected Revenue'),
126         'partner_id': fields.many2one('res.partner', 'Partner'),
127     }
128
129     def default_get(self, cr, uid, fields, context=None):
130         """
131         This function gets default values
132         @param self: The object pointer
133         @param cr: the current row, from the database cursor,
134         @param uid: the current user’s ID for security checks,
135         @param fields: List of fields for default value
136         @param context: A standard dictionary for contextual values
137
138         @return : default values of fields.
139         """
140         record_id = context and context.get('active_id', False) or False
141         res = super(crm_phonecall2opportunity, self).default_get(cr, uid, fields, context=context)
142
143         if record_id:
144             phonecall = self.pool.get('crm.phonecall').browse(cr, uid, record_id, context=context)
145             if 'name' in fields:
146                 res.update({'name': phonecall.name})
147             if 'partner_id' in fields:
148                 res.update({'partner_id': phonecall.partner_id and phonecall.partner_id.id or False})
149         return res
150
151 crm_phonecall2opportunity()