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