1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
24 from osv import fields
26 from osv.orm import except_orm
29 class hr_employee(osv.osv):
31 _inherit = "hr.employee"
33 'product_id': fields.many2one('product.product', 'Product'),
34 'journal_id': fields.many2one('account.analytic.journal', 'Analytic Journal')
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'}
46 'line_id' : fields.many2one('account.analytic.line', 'Analytic line', ondelete='cascade'),
49 def unlink(self, cr, uid, ids, context={}):
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)
57 def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount, unit, context={}):
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)
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))])
67 emp=emp_obj.browse(cr, uid, emp_id[0], context)
69 return emp.product_id.id
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))])
76 emp=emp_obj.browse(cr, uid, emp_id[0], context)
78 return emp.product_id.uom_id.id
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))])
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
89 a = emp.product_id.categ_id.property_account_expense_categ.id
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))])
98 emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0], context=context)
100 return emp.journal_id.id
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),
112 def on_change_account_id(self, cr, uid, ids, account_id):
115 def create(self, cr, uid, vals, context={}):
117 res = super(hr_analytic_timesheet, self).create(cr, uid, vals, context)
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.'))
129 def on_change_user_id(self, cr, uid, ids, user_id):
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= {}),
138 hr_analytic_timesheet()
140 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: