[MERGE] Merge with lp:openobject-addons
[odoo/odoo.git] / addons / crm / wizard / crm_phonecall_to_phonecall.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_phonecall2phonecall(osv.osv_memory):
26     """ Converts Phonecall to Phonecall"""
27
28     _name = 'crm.phonecall2phonecall'
29     _description = 'Phonecall To Phonecall'
30
31     def action_cancel(self, cr, uid, ids, context=None):
32         """
33         Closes Phonecall to Phonecall 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 Phonecall's IDs
38         @param context: A standard dictionary for contextual values
39         """
40         return {'type':'ir.actions.act_window_close'}
41
42     def action_apply(self, cr, uid, ids, context=None):
43         """
44         This converts Phonecall to Phonecall and opens Phonecall view
45         @param self: The object pointer
46         @param cr: the current row, from the database cursor,
47         @param uid: the current user’s ID for security checks,
48         @param ids: List of Phonecall to Phonecall IDs
49         @param context: A standard dictionary for contextual values
50
51         @return : Dictionary value for created Opportunity form
52         """
53         res = {}
54         record_id = context and context.get('active_id', False) or False
55         phonecall_obj = self.pool.get('crm.phonecall')
56
57         if record_id:
58             data_obj = self.pool.get('ir.model.data')
59
60             # Get Phonecall views
61             result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_phonecalls_filter')
62             res = data_obj.read(cr, uid, result, ['res_id'])
63             id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_phone_form_view')
64             id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_phone_tree_view')
65             if id2:
66                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
67             if id3:
68                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
69
70             phonecall = phonecall_obj.browse(cr, uid, record_id, context=context)
71
72             for this in self.browse(cr, uid, ids, context=context):
73                 values = {
74                         'name': this.name,
75                         'user_id': this.user_id and this.user_id.id,
76                         'categ_id': this.categ_id.id,
77                         'section_id': this.section_id.id or (phonecall.section_id and phonecall.section_id.id),
78                         'description': phonecall.description or '',
79                         'partner_id': phonecall.partner_id.id,
80                         'partner_address_id': phonecall.partner_address_id.id,
81                         'partner_mobile': phonecall.partner_mobile or False,
82                         'priority': phonecall.priority,
83                         'partner_phone': phonecall.partner_phone or False,
84                         'date': this.date
85                           }
86                 phonecall_id = phonecall_obj.create(cr, uid, values, context=context)
87             
88             res = {
89                 'name': _('Phone Call'),
90                 'view_type': 'form',
91                 'view_mode': 'form',
92                 'res_model': 'crm.phonecall',
93                 'view_id': False,
94                 'views': [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')],
95                 'type': 'ir.actions.act_window',
96                 'res_id': phonecall_id, 
97                 'domain': [('id', '=', phonecall_id)], 
98                 'search_view_id': res['res_id']
99                 }
100         return res
101
102     _columns = {
103                 'name' : fields.char('Call summary', size=64, required=True, select=1),
104                 'user_id' : fields.many2one('res.users',"Assign To"),
105                 'categ_id': fields.many2one('crm.case.categ', 'Category', required=True, \
106                         domain="['|',('section_id','=',False),('section_id','=',section_id),\
107                         ('object_id.model', '=', 'crm.phonecall')]"), 
108                 'date': fields.datetime('Date', required=True),
109                 'section_id':fields.many2one('crm.case.section','Sales Team'),
110                 }
111
112     def default_get(self, cr, uid, fields, context=None):
113         """
114         This function gets default values
115         @param self: The object pointer
116         @param cr: the current row, from the database cursor,
117         @param uid: the current user’s ID for security checks,
118         @param fields: List of fields for default value
119         @param context: A standard dictionary for contextual values
120
121         @return : default values of fields.
122         """
123         res = super(crm_phonecall2phonecall, self).default_get(cr, uid, fields, context=context)
124         record_id = context and context.get('active_id', False) or False
125         
126         if record_id:
127             phonecall = self.pool.get('crm.phonecall').browse(cr, uid, record_id, context=context)
128
129             categ_id = False
130             data_obj = self.pool.get('ir.model.data')
131             res_id = data_obj._get_id(cr, uid, 'crm', 'categ_phone2')
132             if res_id:
133                 categ_id = data_obj.browse(cr, uid, res_id, context=context).res_id
134
135             if 'name' in fields:
136                 res.update({'name': phonecall.name})
137             if 'user_id' in fields:
138                 res.update({'user_id': phonecall.user_id and phonecall.user_id.id or False})
139             if 'date' in fields:
140                 res.update({'date': phonecall.date})
141             if 'section_id' in fields:
142                 res.update({'section_id': phonecall.section_id and phonecall.section_id.id or False})
143             if 'categ_id' in fields:
144                 res.update({'categ_id': categ_id})
145         return res
146
147 crm_phonecall2phonecall()
148
149 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: