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