bf0e0d1426b2d9a2a97a9181c9365a21df63f593
[odoo/odoo.git] / addons / hr_timesheet / hr_timesheet.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 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 import time
23 from osv import fields
24 from osv import osv
25 from osv.orm import except_orm
26 from tools.translate import _
27
28 class hr_employee(osv.osv):
29     _name = "hr.employee"
30     _inherit = "hr.employee"
31     _columns = {
32         'product_id': fields.many2one('product.product', 'Product', help="Specifies employee's designation as a product with type 'service'."),
33         'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal')
34     }
35 hr_employee()
36
37
38 class hr_analytic_timesheet(osv.osv):
39     _name = "hr.analytic.timesheet"
40     _table = 'hr_analytic_timesheet'
41     _description = "Timesheet line"
42     _inherits = {'account.analytic.line': 'line_id'}
43     _order = "id desc"
44     _columns = {
45         'line_id' : fields.many2one('account.analytic.line', 'Analytic line', ondelete='cascade'),
46     }
47
48     def unlink(self, cr, uid, ids, context={}):
49         toremove = {}
50         for obj in self.browse(cr, uid, ids, context):
51             toremove[obj.line_id.id] = True
52         self.pool.get('account.analytic.line').unlink(cr, uid, toremove.keys(), context)
53         return super(hr_analytic_timesheet, self).unlink(cr, uid, ids, context)
54
55
56     def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount, unit, context={}):
57         res = {}
58 #        if prod_id and unit_amount:
59         if prod_id and date:
60             # find company
61             company_id=self.pool.get('res.company')._company_default_get(cr, uid, 'account.analytic.line', context)
62             res = self.pool.get('account.analytic.line').on_change_unit_amount(cr, uid, id, prod_id, unit_amount,company_id,unit, context)
63         return res
64
65     def _getEmployeeProduct(self, cr, uid, context):
66         emp_obj = self.pool.get('hr.employee')
67         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
68         if emp_id:
69             emp=emp_obj.browse(cr, uid, emp_id[0], context)
70             if emp.product_id:
71                 return emp.product_id.id
72         return False
73
74     def _getEmployeeUnit(self, cr, uid, context):
75         emp_obj = self.pool.get('hr.employee')
76         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
77         if emp_id:
78             emp=emp_obj.browse(cr, uid, emp_id[0], context)
79             if emp.product_id:
80                 return emp.product_id.uom_id.id
81         return False
82
83     def _getGeneralAccount(self, cr, uid, context):
84         emp_obj = self.pool.get('hr.employee')
85         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
86         if emp_id:
87             emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
88             if bool(emp.product_id):
89                 a =  emp.product_id.product_tmpl_id.property_account_expense.id
90                 if not a:
91                     a = emp.product_id.categ_id.property_account_expense_categ.id
92                 if a:
93                     return a
94         return False
95
96     def _getAnalyticJournal(self, cr, uid, context):
97         emp_obj = self.pool.get('hr.employee')
98         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
99         if emp_id:
100             emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
101             if emp.journal_id:
102                 return emp.journal_id.id
103         return False
104
105
106     _defaults = {
107         'product_uom_id' : _getEmployeeUnit,
108         'product_id' : _getEmployeeProduct,
109         'general_account_id' : _getGeneralAccount,
110         'journal_id' : _getAnalyticJournal,
111         'date' : lambda self,cr,uid,ctx: ctx.get('date', time.strftime('%Y-%m-%d')),
112         'user_id' : lambda obj, cr, uid, ctx : ctx.get('user_id', uid),
113     }
114     def on_change_account_id(self, cr, uid, ids, account_id):
115         return {'value':{}}
116     
117     def on_change_date(self, cr, uid, ids, date):
118         if ids:
119             new_date = self.read(cr,uid,ids[0],['date'])['date']
120             if date != new_date:
121                 warning = {'title':'User Alert!','message':'Changing the date will let this entry appear in the timesheet of the new date.'}
122                 return {'value':{},'warning':warning}
123         return {'value':{}}
124     
125     def create(self, cr, uid, vals, context={}):
126         try:
127             res = super(hr_analytic_timesheet, self).create(cr, uid, vals, context)
128             return res
129         except Exception,e:
130             if '"journal_id" viol' in e.args[0]:
131                 raise except_orm(_('ValidateError'),
132                      _('No analytic journal available for this employee.\nDefine an employee for the selected user and assign an analytic journal.'))
133             elif '"account_id" viol' in e.args[0]:
134                 raise except_orm(_('ValidateError'),
135                      _('No analytic account defined on the project.\nPlease set one or we can not automatically fill the timesheet.'))
136             else:
137                 raise except_orm(_('UnknownError'), str(e))
138
139     def on_change_user_id(self, cr, uid, ids, user_id):
140         if not user_id:
141             return {}
142         return {'value' : {
143             'product_id' : self._getEmployeeProduct(cr,user_id, context= {}),
144             'product_uom_id' : self._getEmployeeUnit(cr, user_id, context= {}),
145             'general_account_id' :self. _getGeneralAccount(cr, user_id, context= {}),
146             'journal_id' : self._getAnalyticJournal(cr, user_id, context= {}),
147         }}
148 hr_analytic_timesheet()
149
150 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
151