merged with trunk
[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 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
36     def _getAnalyticJournal(self, cr, uid, context=None):
37         md = self.pool.get('ir.model.data')
38         try:
39             result = md.get_object_reference(cr, uid, 'hr_timesheet', 'analytic_journal')
40             return result[1]
41         except ValueError:
42             pass
43         return False
44
45     def _getEmployeeProduct(self, cr, uid, context=None):
46         md = self.pool.get('ir.model.data')
47         try:
48             result = md.get_object_reference(cr, uid, 'hr_timesheet', 'product_consultant')
49             return result[1]
50         except ValueError:
51             pass
52         return False
53
54     _defaults = {
55         'journal_id': _getAnalyticJournal,
56         'product_id': _getEmployeeProduct
57     }
58 hr_employee()
59
60
61 class hr_analytic_timesheet(osv.osv):
62     _name = "hr.analytic.timesheet"
63     _table = 'hr_analytic_timesheet'
64     _description = "Timesheet Line"
65     _inherits = {'account.analytic.line': 'line_id'}
66     _order = "id desc"
67     _columns = {
68         'line_id': fields.many2one('account.analytic.line', 'Analytic line', ondelete='cascade', required=True),
69         'partner_id': fields.related('account_id', 'partner_id', type='many2one', string='Partner Id', relation='res.partner', store=True),
70     }
71
72     def unlink(self, cr, uid, ids, context=None):
73         toremove = {}
74         for obj in self.browse(cr, uid, ids, context=context):
75             toremove[obj.line_id.id] = True
76         self.pool.get('account.analytic.line').unlink(cr, uid, toremove.keys(), context=context)
77         return super(hr_analytic_timesheet, self).unlink(cr, uid, ids, context=context)
78
79
80     def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount, company_id, unit=False, journal_id=False, context=None):
81         res = {'value':{}}
82         if prod_id and unit_amount:
83             # find company
84             company_id = self.pool.get('res.company')._company_default_get(cr, uid, 'account.analytic.line', context=context)
85             r = self.pool.get('account.analytic.line').on_change_unit_amount(cr, uid, id, prod_id, unit_amount, company_id, unit, journal_id, context=context)
86             if r:
87                 res.update(r)
88         # update unit of measurement
89         if prod_id:
90             uom = self.pool.get('product.product').browse(cr, uid, prod_id, context=context)
91             if uom.uom_id:
92                 res['value'].update({'product_uom_id': uom.uom_id.id})
93         else:
94             res['value'].update({'product_uom_id': False})
95         return res
96
97     def _getEmployeeProduct(self, cr, uid, context=None):
98         if context is None:
99             context = {}
100         emp_obj = self.pool.get('hr.employee')
101         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
102         if emp_id:
103             emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
104             if emp.product_id:
105                 return emp.product_id.id
106         return False
107
108     def _getEmployeeUnit(self, cr, uid, context=None):
109         emp_obj = self.pool.get('hr.employee')
110         if context is None:
111             context = {}
112         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
113         if emp_id:
114             emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
115             if emp.product_id:
116                 return emp.product_id.uom_id.id
117         return False
118
119     def _getGeneralAccount(self, cr, uid, context=None):
120         emp_obj = self.pool.get('hr.employee')
121         if context is None:
122             context = {}
123         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
124         if emp_id:
125             emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
126             if bool(emp.product_id):
127                 a = emp.product_id.product_tmpl_id.property_account_expense.id
128                 if not a:
129                     a = emp.product_id.categ_id.property_account_expense_categ.id
130                 if a:
131                     return a
132         return False
133
134     def _getAnalyticJournal(self, cr, uid, context=None):
135         emp_obj = self.pool.get('hr.employee')
136         if context is None:
137             context = {}
138         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
139         if emp_id:
140             emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
141             if emp.journal_id:
142                 return emp.journal_id.id
143         return False
144
145
146     _defaults = {
147         'product_uom_id': _getEmployeeUnit,
148         'product_id': _getEmployeeProduct,
149         'general_account_id': _getGeneralAccount,
150         'journal_id': _getAnalyticJournal,
151         'date': lambda self, cr, uid, ctx: ctx.get('date', time.strftime('%Y-%m-%d')),
152         'user_id': lambda obj, cr, uid, ctx: ctx.get('user_id', uid),
153     }
154     def on_change_account_id(self, cr, uid, ids, account_id):
155         return {'value':{}}
156
157     def on_change_date(self, cr, uid, ids, date):
158         if ids:
159             new_date = self.read(cr, uid, ids[0], ['date'])['date']
160             if date != new_date:
161                 warning = {'title':'User Alert!','message':'Changing the date will let this entry appear in the timesheet of the new date.'}
162                 return {'value':{},'warning':warning}
163         return {'value':{}}
164
165     def create(self, cr, uid, vals, context=None):
166         if context is None:
167             context = {}
168         emp_obj = self.pool.get('hr.employee')
169         emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id', uid))], context=context)
170         ename = ''
171         if emp_id:
172             ename = emp_obj.browse(cr, uid, emp_id[0], context=context).name
173         if not vals.get('journal_id',False):
174            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,))
175         if not vals.get('account_id',False):
176            raise osv.except_osv(_('Warning !'), _('No analytic account defined on the project.\nPlease set one or we can not automatically fill the timesheet.'))
177         return super(hr_analytic_timesheet, self).create(cr, uid, vals, context=context)
178
179     def on_change_user_id(self, cr, uid, ids, user_id):
180         if not user_id:
181             return {}
182         context = {'user_id': user_id}
183         return {'value': {
184             'product_id': self. _getEmployeeProduct(cr, uid, context),
185             'product_uom_id': self._getEmployeeUnit(cr, uid, context),
186             'general_account_id': self._getGeneralAccount(cr, uid, context),
187             'journal_id': self._getAnalyticJournal(cr, uid, context),
188         }}
189
190 hr_analytic_timesheet()
191
192 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: