8ef4b78ece626276365b1f8accc7cfa54c5e92d6
[odoo/odoo.git] / addons / hr_timesheet / hr_timesheet.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 #
28 ##############################################################################
29
30 import time
31 from osv import fields
32 from osv import osv
33
34
35 class hr_employee(osv.osv):
36         _name = "hr.employee"
37         _inherit = "hr.employee"
38         _columns = {
39                 'product_id': fields.many2one('product.product', 'Product'),
40                 'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal')
41         }
42 hr_employee()
43
44
45 class hr_analytic_timesheet(osv.osv):
46         _name = "hr.analytic.timesheet"
47         _table = 'hr_analytic_timesheet'
48         _description = "Timesheet line"
49         _inherits = {'account.analytic.line': 'line_id'}
50         _order = "id desc"
51         _columns = {
52                 'line_id' : fields.many2one('account.analytic.line', 'Analytic line', ondelete='cascade'),
53         }
54
55         def unlink(self, cr, uid, ids, context={}):
56                 toremove = {}
57                 for obj in self.browse(cr, uid, ids, context):
58                         toremove[obj.line_id.id] = True
59                 self.pool.get('account.analytic.line').unlink(cr, uid, toremove.keys(), context)
60                 return super(hr_analytic_timesheet, self).unlink(cr, uid, ids, context)
61
62
63         def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount, unit, context={}):
64                 res = {}
65                 if prod_id and unit_amount:
66                         res = self.pool.get('account.analytic.line').on_change_unit_amount(cr, uid, id, prod_id, unit_amount,unit, context)
67                 return res
68
69         def _getEmployeeProduct(self, cr, uid, context):
70                 emp_obj = self.pool.get('hr.employee')
71                 emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
72                 if emp_id:
73                         emp=emp_obj.browse(cr, uid, emp_id[0], context)
74                         if emp.product_id:
75                                 return emp.product_id.id
76                 return False
77
78         def _getEmployeeUnit(self, cr, uid, context):
79                 emp_obj = self.pool.get('hr.employee')
80                 emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
81                 if emp_id:
82                         emp=emp_obj.browse(cr, uid, emp_id[0], context)
83                         if emp.product_id:
84                                 return emp.product_id.uom_id.id
85                 return False
86
87         def _getGeneralAccount(self, cr, uid, context):
88                 emp_obj = self.pool.get('hr.employee')
89                 emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
90                 if emp_id:
91                         emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
92                         if bool(emp.product_id):
93                                 a =  emp.product_id.product_tmpl_id.property_account_expense.id
94                                 if not a:
95                                         a = emp.product_id.categ_id.property_account_expense_categ.id
96                                 if a:
97                                         return a
98                 return False
99
100         def _getAnalyticJournal(self, cr, uid, context):
101                 emp_obj = self.pool.get('hr.employee')
102                 emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))])
103                 if emp_id:
104                         emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
105                         if emp.journal_id:
106                                 return emp.journal_id.id
107                 return False
108
109
110         _defaults = {
111                 'product_uom_id' : _getEmployeeUnit,
112                 'product_id' : _getEmployeeProduct,
113                 'general_account_id' : _getGeneralAccount,
114                 'journal_id' : _getAnalyticJournal,
115                 'date' : lambda self,cr,uid,ctx: ctx.get('date', time.strftime('%Y-%m-%d')),
116                 'user_id' : lambda obj, cr, uid, ctx : ctx.get('user_id', uid),
117         }
118
119
120         def on_change_user_id(self, cr, uid, ids, user_id):
121                 if not user_id:
122                         return {}
123                 return {'value' : {
124                         'product_id' : self._getEmployeeProduct(cr,user_id, context= {}),
125                         'product_uom_id' : self._getEmployeeUnit(cr, user_id, context= {}),
126                         'general_account_id' :self. _getGeneralAccount(cr, user_id, context= {}),
127                         'journal_id' : self._getAnalyticJournal(cr, user_id, context= {}),
128                                                    }}
129
130
131
132 hr_analytic_timesheet()