7ceca8495d59ff38459cf3e3ff245df1e99290be
[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 from tools.translate import _
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         if prod_id:
61             res = self.pool.get('account.analytic.line').on_change_unit_amount(cr, uid, id, prod_id, unit_amount,unit, context)
62         return res
63
64     def _getEmployeeProduct(self, cr, uid, context):
65         emp_obj = self.pool.get('hr.employee')
66         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
67         if emp_id:
68             emp=emp_obj.browse(cr, uid, emp_id[0], context)
69             if emp.product_id:
70                 return emp.product_id.id
71         return False
72
73     def _getEmployeeUnit(self, cr, uid, context):
74         emp_obj = self.pool.get('hr.employee')
75         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
76         if emp_id:
77             emp=emp_obj.browse(cr, uid, emp_id[0], context)
78             if emp.product_id:
79                 return emp.product_id.uom_id.id
80         return False
81
82     def _getGeneralAccount(self, cr, uid, context):
83         emp_obj = self.pool.get('hr.employee')
84         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
85         if emp_id:
86             emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
87             if bool(emp.product_id):
88                 a =  emp.product_id.product_tmpl_id.property_account_expense.id
89                 if not a:
90                     a = emp.product_id.categ_id.property_account_expense_categ.id
91                 if a:
92                     return a
93         return False
94
95     def _getAnalyticJournal(self, cr, uid, context):
96         emp_obj = self.pool.get('hr.employee')
97         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
98         if emp_id:
99             emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
100             if emp.journal_id:
101                 return emp.journal_id.id
102         return False
103
104
105     _defaults = {
106         'product_uom_id' : _getEmployeeUnit,
107         'product_id' : _getEmployeeProduct,
108         'general_account_id' : _getGeneralAccount,
109         'journal_id' : _getAnalyticJournal,
110         'date' : lambda self,cr,uid,ctx: ctx.get('date', time.strftime('%Y-%m-%d')),
111         'user_id' : lambda obj, cr, uid, ctx : ctx.get('user_id', uid),
112     }
113     def on_change_account_id(self, cr, uid, ids, account_id):
114         return {'value':{}}
115     
116     def on_change_date(self, cr, uid, ids, date):
117         if ids:
118             new_date = self.read(cr,uid,ids[0],['date'])['date']
119             if date != new_date:
120                 warning = {'title':'User Alert!','message':'Changing the date will let this entry appear in the timesheet of the new date.'}
121                 return {'value':{},'warning':warning}
122         return {'value':{}}
123     
124     def create(self, cr, uid, vals, context={}):
125         try:
126             res = super(hr_analytic_timesheet, self).create(cr, uid, vals, context)
127             return res
128         except Exception,e:
129             if '"journal_id" viol' in e.args[0]:
130                 raise except_orm(_('ValidateError'),
131                      _('No analytic journal available for this employee.\nDefine an employee for the selected user and assign an analytic journal.'))
132             elif '"account_id" viol' in e.args[0]:
133                 raise except_orm(_('ValidateError'),
134                      _('No analytic account defined on the project.\nPlease set one or we can not automatically fill the timesheet.'))
135             else:
136                 raise except_orm(_('UnknownError'), str(e))
137
138     def on_change_user_id(self, cr, uid, ids, user_id):
139         if not user_id:
140             return {}
141         return {'value' : {
142             'product_id' : self._getEmployeeProduct(cr,user_id, context= {}),
143             'product_uom_id' : self._getEmployeeUnit(cr, user_id, context= {}),
144             'general_account_id' :self. _getGeneralAccount(cr, user_id, context= {}),
145             'journal_id' : self._getAnalyticJournal(cr, user_id, context= {}),
146         }}
147 hr_analytic_timesheet()
148
149 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
150