[IMP] Prefix the certificate ID with 00
[odoo/odoo.git] / addons / hr_timesheet / hr_timesheet.py
1 # -*- encoding: 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 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 General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import time
24 from osv import fields
25 from osv import osv
26 from osv.orm import except_orm
27
28
29 class hr_employee(osv.osv):
30     _name = "hr.employee"
31     _inherit = "hr.employee"
32     _columns = {
33         'product_id': fields.many2one('product.product', 'Product'),
34         'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal')
35     }
36 hr_employee()
37
38
39 class hr_analytic_timesheet(osv.osv):
40     _name = "hr.analytic.timesheet"
41     _table = 'hr_analytic_timesheet'
42     _description = "Timesheet line"
43     _inherits = {'account.analytic.line': 'line_id'}
44     _order = "id desc"
45     _columns = {
46         'line_id' : fields.many2one('account.analytic.line', 'Analytic line', ondelete='cascade'),
47     }
48
49     def unlink(self, cr, uid, ids, context={}):
50         toremove = {}
51         for obj in self.browse(cr, uid, ids, context):
52             toremove[obj.line_id.id] = True
53         self.pool.get('account.analytic.line').unlink(cr, uid, toremove.keys(), context)
54         return super(hr_analytic_timesheet, self).unlink(cr, uid, ids, context)
55
56
57     def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount, unit, context={}):
58         res = {}
59         if prod_id and unit_amount:
60             res = self.pool.get('account.analytic.line').on_change_unit_amount(cr, uid, id, prod_id, unit_amount,unit, context)
61         return res
62
63     def _getEmployeeProduct(self, cr, uid, context):
64         emp_obj = self.pool.get('hr.employee')
65         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
66         if emp_id:
67             emp=emp_obj.browse(cr, uid, emp_id[0], context)
68             if emp.product_id:
69                 return emp.product_id.id
70         return False
71
72     def _getEmployeeUnit(self, cr, uid, context):
73         emp_obj = self.pool.get('hr.employee')
74         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
75         if emp_id:
76             emp=emp_obj.browse(cr, uid, emp_id[0], context)
77             if emp.product_id:
78                 return emp.product_id.uom_id.id
79         return False
80
81     def _getGeneralAccount(self, cr, uid, context):
82         emp_obj = self.pool.get('hr.employee')
83         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
84         if emp_id:
85             emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
86             if bool(emp.product_id):
87                 a =  emp.product_id.product_tmpl_id.property_account_expense.id
88                 if not a:
89                     a = emp.product_id.categ_id.property_account_expense_categ.id
90                 if a:
91                     return a
92         return False
93
94     def _getAnalyticJournal(self, cr, uid, context):
95         emp_obj = self.pool.get('hr.employee')
96         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
97         if emp_id:
98             emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
99             if emp.journal_id:
100                 return emp.journal_id.id
101         return False
102
103
104     _defaults = {
105         'product_uom_id' : _getEmployeeUnit,
106         'product_id' : _getEmployeeProduct,
107         'general_account_id' : _getGeneralAccount,
108         'journal_id' : _getAnalyticJournal,
109         'date' : lambda self,cr,uid,ctx: ctx.get('date', time.strftime('%Y-%m-%d')),
110         'user_id' : lambda obj, cr, uid, ctx : ctx.get('user_id', uid),
111     }
112     def on_change_account_id(self, cr, uid, ids, account_id):
113         return {'value':{}}
114
115     def create(self, cr, uid, vals, context={}):
116         try:
117             res = super(hr_analytic_timesheet, self).create(cr, uid, vals, context)
118             return res
119         except Exception,e:
120             if '"journal_id" viol' in e.args[0]:
121                 raise except_orm(_('ValidateError'),
122                      _('No analytic journal available for this employee.\nDefine an employee for the selected user and assign an analytic journal.'))
123             elif '"account_id" viol' in e.args[0]:
124                 raise except_orm(_('ValidateError'),
125                      _('No analytic account defined on the project.\nPlease set one or we can not automatically fill the timesheet.'))
126             else:
127                 raise
128
129     def on_change_user_id(self, cr, uid, ids, user_id):
130         if not user_id:
131             return {}
132         return {'value' : {
133             'product_id' : self._getEmployeeProduct(cr,user_id, context= {}),
134             'product_uom_id' : self._getEmployeeUnit(cr, user_id, context= {}),
135             'general_account_id' :self. _getGeneralAccount(cr, user_id, context= {}),
136             'journal_id' : self._getAnalyticJournal(cr, user_id, context= {}),
137         }}
138 hr_analytic_timesheet()
139
140 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
141