[ADD] hr_timesheet,project_issue,purchase : help added on menus
[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-2010 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
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', help="Specifies employee's designation as a product with type 'service'."),
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', required=True),
47         'partner_id': fields.related('account_id', 'partner_id', type='many2one', string='Partner Id', relation='account.analytic.account', store=True),
48     }
49
50     def unlink(self, cr, uid, ids, context=None):
51         if context is None:
52             context = {}
53         toremove = {}
54         for obj in self.browse(cr, uid, ids, context=context):
55             toremove[obj.line_id.id] = True
56         self.pool.get('account.analytic.line').unlink(cr, uid, toremove.keys(), context=context)
57         return super(hr_analytic_timesheet, self).unlink(cr, uid, ids, context=context)
58
59
60     def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount, unit, context=None):
61         if context is None:
62             context = {}
63         res = {'value':{}}
64         if prod_id and unit_amount:
65             # find company
66             company_id = self.pool.get('res.company')._company_default_get(cr, uid, 'account.analytic.line', context=context)
67             res = self.pool.get('account.analytic.line').on_change_unit_amount(cr, uid, id, prod_id, unit_amount, company_id, unit, context=context)
68         # update unit of measurement
69         if prod_id:
70             uom = self.pool.get('product.product').browse(cr, uid, prod_id, context=context)
71             if uom.uom_id:
72                 res['value'].update({'product_uom_id': uom.uom_id.id})
73         else:
74             res['value'].update({'product_uom_id': False})
75         return res
76
77     def _getEmployeeProduct(self, cr, uid, context=None):
78         if context is None:
79             context = {}
80         emp_obj = self.pool.get('hr.employee')
81         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
82         if emp_id:
83             emp=emp_obj.browse(cr, uid, emp_id[0], context=context)
84             if emp.product_id:
85                 return emp.product_id.id
86         return False
87
88     def _getEmployeeUnit(self, cr, uid, context=None):
89         emp_obj = self.pool.get('hr.employee')
90         if context is None:
91             context = {}
92         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
93         if emp_id:
94             emp=emp_obj.browse(cr, uid, emp_id[0], context=context)
95             if emp.product_id:
96                 return emp.product_id.uom_id.id
97         return False
98
99     def _getGeneralAccount(self, cr, uid, context=None):
100         emp_obj = self.pool.get('hr.employee')
101         if context is None:
102             context = {}
103         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
104         if emp_id:
105             emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
106             if bool(emp.product_id):
107                 a =  emp.product_id.product_tmpl_id.property_account_expense.id
108                 if not a:
109                     a = emp.product_id.categ_id.property_account_expense_categ.id
110                 if a:
111                     return a
112         return False
113
114     def _getAnalyticJournal(self, cr, uid, context=None):
115         emp_obj = self.pool.get('hr.employee')
116         if context is None:
117             context = {}
118         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
119         if emp_id:
120             emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
121             if emp.journal_id:
122                 return emp.journal_id.id
123         return False
124
125
126     _defaults = {
127         'product_uom_id' : _getEmployeeUnit,
128         'product_id' : _getEmployeeProduct,
129         'general_account_id' : _getGeneralAccount,
130         'journal_id' : _getAnalyticJournal,
131         'date' : lambda self, cr, uid, ctx : ctx.get('date', time.strftime('%Y-%m-%d')),
132         'user_id' : lambda obj, cr, uid, ctx : ctx.get('user_id', uid),
133     }
134     def on_change_account_id(self, cr, uid, ids, account_id):
135         return {'value':{}}
136
137     def on_change_date(self, cr, uid, ids, date):
138         if ids:
139             new_date = self.read(cr, uid, ids[0], ['date'])['date']
140             if date != new_date:
141                 warning = {'title':'User Alert!','message':'Changing the date will let this entry appear in the timesheet of the new date.'}
142                 return {'value':{},'warning':warning}
143         return {'value':{}}
144
145     def create(self, cr, uid, vals, context=None):
146         if context is None:
147             context = {}
148         emp_obj = self.pool.get('hr.employee')
149         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
150         ename = ''
151         if emp_id:
152             ename = emp_obj.browse(cr, uid, emp_id[0], context=context).name
153         if not vals.get('journal_id',False):
154            raise osv.except_osv(_('Warning !'), _('Analytic journal is not defined for employee %s \nDefine an employee for the selected user and assign an analytic journal!')%(ename,))
155         if not vals.get('account_id',False):
156            raise osv.except_osv(_('Warning !'), _('No analytic account defined on the project.\nPlease set one or we can not automatically fill the timesheet.'))
157         return super(hr_analytic_timesheet, self).create(cr, uid, vals, context=context)
158
159     def on_change_user_id(self, cr, uid, ids, user_id):
160         if not user_id:
161             return {}
162         return {'value' : {
163             'product_id' : self._getEmployeeProduct(cr,user_id, context= {}),
164             'product_uom_id' : self._getEmployeeUnit(cr, user_id, context= {}),
165             'general_account_id' :self. _getGeneralAccount(cr, user_id, context= {}),
166             'journal_id' : self._getAnalyticJournal(cr, user_id, context= {}),
167         }}
168 hr_analytic_timesheet()
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: