merged with trunk
[odoo/odoo.git] / addons / hr_recruitment / wizard / hr_recruitment_phonecall.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 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 import time
23
24 from osv import osv, fields
25 from tools.translate import _
26
27 class job2phonecall(osv.osv_memory):
28     _name = 'hr.recruitment.job2phonecall'
29     _description = 'Schedule Phone Call'
30     _columns = {
31         'user_id': fields.many2one('res.users', 'Assign To'),
32         'deadline': fields.datetime('Planned Date'),
33         'note': fields.text('Goals'),
34         'category_id': fields.many2one('crm.case.categ', 'Category', required=True),
35                 }
36
37     def _date_user(self, cr, uid, context=None):
38         case_obj = self.pool.get('hr.applicant')
39         if context is None:
40             context = {}
41         case = case_obj.browse(cr, uid, context.get('active_id', False), context=context)
42         return case.user_id and case.user_id.id or False
43
44     def _date_category(self, cr, uid, context=None):
45         case_obj = self.pool.get('hr.applicant')
46         if context is None:
47             context = {}
48         case = case_obj.browse(cr, uid, context.get('active_id', False), context=context)
49         categ_id = self.pool.get('crm.case.categ').search(cr, uid, [('name','=','Outbound')], context=context)
50         return categ_id and categ_id[0] or case.categ_id and case.categ_id.id or False
51
52     def _get_note(self, cr, uid, context=None):
53         case_obj = self.pool.get('hr.applicant')
54         if context is None:
55             context = {}
56         case = case_obj.browse(cr, uid, context.get('active_id', False), context=context)
57         return case.description or ''
58
59     _defaults = {
60          'user_id': _date_user,
61          'category_id': _date_category,
62          'note': _get_note
63                  }
64
65     def make_phonecall(self, cr, uid, ids, context=None):
66         mod_obj = self.pool.get('ir.model.data')
67         job_case_obj = self.pool.get('hr.applicant')
68         data_obj = self.pool.get('ir.model.data')
69         phonecall_case_obj = self.pool.get('crm.phonecall')
70         if context is None:
71             context = {}
72         form = self.read(cr, uid, ids, [], context=context)[0]
73         result = mod_obj._get_id(cr, uid, 'crm', 'view_crm_case_phonecalls_filter')
74         res = mod_obj.read(cr, uid, result, ['res_id'], context=context)
75         # Select the view
76
77         id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_phone_tree_view')
78         id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_phone_form_view')
79         if id2:
80             id2 = data_obj.browse(cr, uid, id2, context=context).res_id
81         if id3:
82             id3 = data_obj.browse(cr, uid, id3, context=context).res_id
83
84         for job in job_case_obj.browse(cr, uid, context.get('active_ids', []), context=context):
85             #TODO: Take other info from job
86             new_phonecall_id = phonecall_case_obj.create(cr, uid, {
87                         'name': job.name,
88                         'user_id': form['user_id'],
89                         'categ_id': form['category_id'],
90                         'description': form['note'],
91                         'date': form['deadline'],
92                         'description': job.description,
93                         'partner_id': job.partner_id.id,
94                         'partner_address_id': job.partner_address_id.id,
95                         'partner_phone': job.partner_phone,
96                         'partner_mobile': job.partner_mobile,
97                         'description': job.description,
98                         'date':job.date,
99                     }, context=context)
100             new_phonecall = phonecall_case_obj.browse(cr, uid, new_phonecall_id, context=context)
101             vals = {}
102             job_case_obj.write(cr, uid, [job.id], vals, context=context)
103 #            job_case_obj.case_cancel(cr, uid, [job.id])
104             phonecall_case_obj.case_open(cr, uid, [new_phonecall_id])
105
106         return {
107             'name': _('Phone Call'),
108             'view_type': 'form',
109             'view_mode': 'tree,form',
110             'res_model': 'crm.phonecall',
111             'res_id': new_phonecall_id,
112             'views': [(id3,'form'), (id2,'tree'), (False,'calendar'), (False,'graph')],
113             'type': 'ir.actions.act_window',
114             'search_view_id': res['res_id']
115         }
116
117 job2phonecall()
118
119 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: