[MERGE] fix HR
[odoo/odoo.git] / addons / hr_recruitment / wizard / hr_recruitment_create_partner_job.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 from osv import osv, fields
23 from tools.translate import _
24
25 class hr_recruitment_partner_create(osv.osv_memory):
26     _name = 'hr.recruitment.partner.create'
27     _description = 'Create Partner from job application'
28     _columns = {
29         'close': fields.boolean('Close job request'),
30                 }
31
32     def view_init(self, cr , uid , fields_list, context=None):
33         case_obj = self.pool.get('hr.applicant')
34         if context is None:
35             context = {}
36         for case in case_obj.browse(cr, uid, context['active_ids'], context=context):
37             if case.partner_id:
38                 raise osv.except_osv(_('Error!'),
39                     _('A contact is already defined on this job request.'))
40         pass
41
42     def make_order(self, cr, uid, ids, context=None):
43         mod_obj = self.pool.get('ir.model.data')
44         partner_obj = self.pool.get('res.partner')
45         case_obj = self.pool.get('hr.applicant')
46
47         if context is None:
48             context = {}
49         data = self.read(cr, uid, ids, [], context=context)[0]
50         result = mod_obj._get_id(cr, uid, 'base', 'view_res_partner_filter')
51         res = mod_obj.read(cr, uid, result, ['res_id'], context=context)
52
53         for case in case_obj.browse(cr, uid, context['active_ids'], context=context):
54             partner_id = partner_obj.search(cr, uid, [('name', '=', case.partner_name or case.name)], context=context)
55             if partner_id:
56                 raise osv.except_osv(_('Error!'),_('A contact is already existing with the same name.'))
57             partner_id = partner_obj.create(cr, uid, {
58                 'name': case.partner_name or case.name,
59                 'user_id': case.user_id.id,
60                 'comment': case.description,
61                 'phone': case.partner_phone,
62                 'mobile': case.partner_mobile,
63                 'email': case.email_from
64             }, context=context)
65
66             case_obj.write(cr, uid, [case.id], {
67                 'partner_id': partner_id,
68             }, context=context)
69         if data['close']:
70             case_obj.case_close(cr, uid, context['active_ids'])
71
72         return {
73             'domain': "[]",
74             'view_type': 'form',
75             'view_mode': 'form,tree',
76             'res_model': 'res.partner',
77             'res_id': int(partner_id),
78             'view_id': False,
79             'type': 'ir.actions.act_window',
80             'search_view_id': res['res_id']
81                 }
82
83 hr_recruitment_partner_create()
84
85 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: