[FIX] 3 security rules, [IMP] object names for logs
[odoo/odoo.git] / addons / project_timesheet / project_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 import time
22 import datetime
23
24 from osv import fields, osv
25 import pooler
26 import tools
27 from tools.translate import _
28
29 class project_work(osv.osv):
30     _inherit = "project.task.work"
31
32     def get_user_related_details(self, cr, uid, user_id):
33         res = {}
34         emp_obj = self.pool.get('hr.employee')
35         emp_id = emp_obj.search(cr, uid, [('user_id', '=', user_id)])
36         if not emp_id:
37             user_name = self.pool.get('res.users').read(cr, uid, [user_id], ['name'])[0]['name']
38             raise osv.except_osv(_('Bad Configuration !'),
39                  _('No employee defined for user "%s". You must create one.')% (user_name,))
40         emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0])
41         if not emp.product_id:
42             raise osv.except_osv(_('Bad Configuration !'),
43                  _('No product defined on the related employee.\nFill in the timesheet tab of the employee form.'))
44
45         if not emp.journal_id:
46             raise osv.except_osv(_('Bad Configuration !'),
47                  _('No journal defined on the related employee.\nFill in the timesheet tab of the employee form.'))
48
49         a =  emp.product_id.product_tmpl_id.property_account_expense.id
50         if not a:
51             a = emp.product_id.categ_id.property_account_expense_categ.id
52             if not a:
53                 raise osv.except_osv(_('Bad Configuration !'),
54                         _('No product and product category property account defined on the related employee.\nFill in the timesheet tab of the employee form.'))
55         res['product_id'] = emp.product_id.id
56         res['journal_id'] = emp.journal_id.id
57         res['general_account_id'] = a
58         res['product_uom_id'] = emp.product_id.uom_id.id
59         return res
60
61     def create(self, cr, uid, vals, *args, **kwargs):
62         obj_timesheet = self.pool.get('hr.analytic.timesheet')
63         task_obj = self.pool.get('project.task')
64         vals_line = {}
65         obj_task = task_obj.browse(cr, uid, vals['task_id'])
66         result = self.get_user_related_details(cr, uid, vals.get('user_id', uid))
67         vals_line['name'] = '%s: %s' % (tools.ustr(obj_task.name), tools.ustr(vals['name']) or '/')
68         vals_line['user_id'] = vals['user_id']
69         vals_line['product_id'] = result['product_id']
70         vals_line['date'] = vals['date'][:10]
71         vals_line['unit_amount'] = vals['hours']
72         acc_id = obj_task.project_id.category_id.id
73         vals_line['account_id'] = acc_id
74         res = obj_timesheet.on_change_account_id(cr, uid, False, acc_id)
75         if res.get('value'):
76             vals_line.update(res['value'])
77         vals_line['general_account_id'] = result['general_account_id']
78         vals_line['journal_id'] = result['journal_id']
79         vals_line['amount'] = 00.0
80         vals_line['product_uom_id'] = result['product_uom_id']
81         amount = vals_line['unit_amount']
82         prod_id = vals_line['product_id']
83         unit = False
84         timeline_id = obj_timesheet.create(cr, uid, vals=vals_line, context=kwargs['context'])
85
86         # Compute based on pricetype
87         amount_unit=obj_timesheet.on_change_unit_amount(cr, uid, timeline_id,
88             prod_id, amount, unit, context=kwargs['context'])
89
90         vals_line['amount'] = (-1) * vals['hours']* (amount_unit['value']['amount'] or 0.0)
91         obj_timesheet.write(cr, uid,[timeline_id], vals_line, {})
92         vals['hr_analytic_timesheet_id'] = timeline_id
93         return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
94
95     def write(self, cr, uid, ids, vals, context=None):
96         vals_line = {}
97         obj = self.pool.get('hr.analytic.timesheet')
98         timesheet_obj = self.pool.get('hr.analytic.timesheet')
99         task = self.pool.get('project.task.work').browse(cr, uid, ids, context=context)[0]
100         line_id = task.hr_analytic_timesheet_id
101         # in case,if a record is deleted from timesheet,but we change it from tasks!
102         list_avail_ids = timesheet_obj.search(cr, uid, [], context=context)
103         if line_id in list_avail_ids:
104             if 'name' in vals:
105                 vals_line['name'] = '%s: %s' % (tools.ustr(task.task_id.name), tools.ustr(vals['name']) or '/')
106             if 'user_id' in vals:
107                 vals_line['user_id'] = vals['user_id']
108                 result = self.get_user_related_details(cr, uid, vals['user_id'])
109                 vals_line['product_id'] = result['product_id']
110                 vals_line['general_account_id'] = result['general_account_id']
111                 vals_line['journal_id'] = result['journal_id']
112                 vals_line['product_uom_id'] = result['product_uom_id']
113             if 'date' in vals:
114                 vals_line['date'] = vals['date'][:10]
115             if 'hours' in vals:
116                 vals_line['unit_amount'] = vals['hours']
117                 # Compute based on pricetype
118                 unit = False
119                 amount_unit=obj.on_change_unit_amount(cr, uid, line_id,
120                     vals_line['product_id'], vals_line['unit_amount'], unit, context=context)
121
122                 vals_line['amount'] = (-1) * vals['hours'] * (amount_unit['value']['amount'] or 0.0)
123             obj.write(cr, uid, [line_id], vals_line, context=context)
124
125         return super(project_work,self).write(cr, uid, ids, vals, context)
126
127     def unlink(self, cr, uid, ids, *args, **kwargs):
128         pool_analytic_timesheet = self.pool.get('hr.analytic.timesheet')
129         for work_id in ids:
130             timesheet_id = self.read(cr, uid, work_id, ['hr_analytic_timesheet_id'])['hr_analytic_timesheet_id']
131 #            delete entry from timesheet too while deleting entry to task.
132             list_avail_ids = pool_analytic_timesheet.search(cr, uid, [])
133             if timesheet_id in list_avail_ids:
134                 obj = pool_analytic_timesheet.unlink(cr, uid, [timesheet_id], *args, **kwargs)
135
136         return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
137
138     _columns={
139         'hr_analytic_timesheet_id':fields.integer('Related Timeline Id')
140     }
141
142 project_work()
143
144 class task(osv.osv):
145     _inherit = "project.task"
146
147     def unlink(self, cr, uid, ids, *args, **kwargs):
148         for task_obj in self.browse(cr, uid, ids, *args, **kwargs):
149             if task_obj.work_ids:
150                 work_ids = [x.id for x in task_obj.work_ids]
151                 self.pool.get('project.task.work').unlink(cr, uid, work_ids, *args, **kwargs)
152
153         return super(task,self).unlink(cr, uid, ids, *args, **kwargs)
154
155     def write(self, cr, uid, ids,vals,context={}):
156         if (vals.has_key('project_id') and vals['project_id']) or (vals.has_key('name') and vals['name']):
157             vals_line = {}
158             hr_anlytic_timesheet = self.pool.get('hr.analytic.timesheet')
159             task_obj_l = self.browse(cr, uid, ids, context)
160             if (vals.has_key('project_id') and vals['project_id']):
161                 project_obj = self.pool.get('project.project').browse(cr, uid, vals['project_id'])
162                 acc_id = project_obj.category_id.id
163
164             for task_obj in task_obj_l:
165                 if len(task_obj.work_ids):
166                     for task_work in task_obj.work_ids:
167                         line_id = task_work.hr_analytic_timesheet_id
168                         if (vals.has_key('project_id') and vals['project_id']):
169                             vals_line['account_id'] = acc_id
170                         if (vals.has_key('name') and vals['name']):
171                             vals_line['name'] = '%s: %s' % (tools.ustr(vals['name']), tools.ustr(task_work.name) or '/')
172                         hr_anlytic_timesheet.write(cr, uid, [line_id], vals_line, {})
173         return super(task,self).write(cr, uid, ids, vals, context)
174
175 task()
176
177 class project_project(osv.osv):
178     _inherit = "project.project"
179
180     def name_get(self, cr, user, ids, context=None):
181         result = []
182         for project in self.browse(cr, user, ids, context):
183             name = "[%s] %s" % (project.category_id and project.category_id.code or '?', project.name)
184             result.append((project.id, name))
185         return result
186
187 project_project()
188
189 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
190