[FIX]crm: several calls to _get_id for external id 'categ_phone2' which were not...
[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 openerp.osv import fields, osv
23 from openerp.tools.translate import _
24
25 import time
26
27 class crm_phonecall2phonecall(osv.osv_memory):
28     _name = 'crm.phonecall2phonecall'
29     _description = 'Phonecall To Phonecall'
30
31     _columns = {
32         'name' : fields.char('Call summary', size=64, required=True, select=1),
33         'user_id' : fields.many2one('res.users',"Assign To"),
34         'contact_name':fields.char('Contact', size=64),
35         'phone':fields.char('Phone', size=64),
36         'categ_id': fields.many2one('crm.case.categ', 'Category', \
37                 domain="['|',('section_id','=',False),('section_id','=',section_id),\
38                 ('object_id.model', '=', 'crm.phonecall')]"), 
39         'date': fields.datetime('Date'),
40         'section_id':fields.many2one('crm.case.section','Sales Team'),
41         'action': fields.selection([('schedule','Schedule a call'), ('log','Log a call')], 'Action', required=True),
42         'partner_id' : fields.many2one('res.partner', "Partner"),
43         'note':fields.text('Note')
44     }
45
46
47     def action_cancel(self, cr, uid, ids, context=None):
48         """
49         Closes Phonecall to Phonecall form
50         """
51         return {'type':'ir.actions.act_window_close'}
52
53     def action_schedule(self, cr, uid, ids, context=None):
54         value = {}
55         if context is None:
56             context = {}
57         phonecall = self.pool.get('crm.phonecall')
58         phonecall_ids = context and context.get('active_ids') or []
59         for this in self.browse(cr, uid, ids, context=context):
60             phocall_ids = phonecall.schedule_another_phonecall(cr, uid, phonecall_ids, this.date, this.name, \
61                     this.user_id and this.user_id.id or False, \
62                     this.section_id and this.section_id.id or False, \
63                     this.categ_id and this.categ_id.id or False, \
64                     action=this.action, context=context)
65
66         return phonecall.redirect_phonecall_view(cr, uid, phocall_ids[phonecall_ids[0]], context=context)
67     
68     def default_get(self, cr, uid, fields, context=None):
69         """
70         This function gets default values
71         
72         """
73         res = super(crm_phonecall2phonecall, self).default_get(cr, uid, fields, context=context)
74         record_id = context and context.get('active_id', False) or False
75         res.update({'action': 'schedule', 'date': time.strftime('%Y-%m-%d %H:%M:%S')})
76         if record_id:
77             phonecall = self.pool.get('crm.phonecall').browse(cr, uid, record_id, context=context)
78
79             categ_id = False
80             data_obj = self.pool.get('ir.model.data')
81             try:
82                 res_id = data_obj._get_id(cr, uid, 'crm', 'categ_phone2')
83                 categ_id = data_obj.browse(cr, uid, res_id, context=context).res_id
84             except ValueError:
85                 pass
86
87             if 'name' in fields:
88                 res.update({'name': phonecall.name})
89             if 'user_id' in fields:
90                 res.update({'user_id': phonecall.user_id and phonecall.user_id.id or False})
91             if 'date' in fields:
92                 res.update({'date': False})
93             if 'section_id' in fields:
94                 res.update({'section_id': phonecall.section_id and phonecall.section_id.id or False})
95             if 'categ_id' in fields:
96                 res.update({'categ_id': categ_id})
97             if 'partner_id' in fields:
98                 res.update({'partner_id': phonecall.partner_id and phonecall.partner_id.id or False})
99         return res
100
101 crm_phonecall2phonecall()
102
103 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: