[FIX] Project_timesheet : Analytic line creation/edition takes user based information
[odoo/odoo.git] / addons / project_timesheet / project_timesheet.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution    
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22 from osv import fields, osv
23 import time
24 import datetime
25 import pooler
26 import tools
27 from tools.translate import _
28
29
30 class project_work(osv.osv):
31     _inherit = "project.task.work"
32     _description = "Task Work"
33
34     def get_user_related_details(self, cr, uid, user_id):
35         res = {}
36         emp_obj = self.pool.get('hr.employee')
37         emp_id = emp_obj.search(cr, uid, [('user_id', '=', user_id)])
38         if not emp_id:
39             user_name = self.pool.get('res.users').read(cr, uid, [user_id], ['name'])[0]['name']
40             raise osv.except_osv(_('Bad Configuration !'),
41                  _('No employee defined for user "%s". You must create one.')% (user_name,))
42         emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0])
43         if not emp.product_id:
44             raise osv.except_osv(_('Bad Configuration !'),
45                  _('No product defined on the related employee.\nFill in the timesheet tab of the employee form.'))
46
47         if not emp.journal_id:
48             raise osv.except_osv(_('Bad Configuration !'),
49                  _('No journal defined on the related employee.\nFill in the timesheet tab of the employee form.'))
50
51         a =  emp.product_id.product_tmpl_id.property_account_expense.id
52         if not a:
53             a = emp.product_id.categ_id.property_account_expense_categ.id
54             if not a:
55                 raise osv.except_osv(_('Bad Configuration !'),
56                         _('No product and product category property account defined on the related employee.\nFill in the timesheet tab of the employee form.'))
57         res['product_id'] = emp.product_id.id
58         res['journal_id'] = emp.journal_id.id
59         res['general_account_id'] = a
60         return res
61         
62     def create(self, cr, uid, vals, *args, **kwargs):
63         obj = self.pool.get('hr.analytic.timesheet')
64         vals_line = {}
65         obj_task = self.pool.get('project.task').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.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         timeline_id = obj.create(cr, uid, vals_line, {})
81
82         vals_line['amount'] = (-1) * vals['hours'] * obj.browse(cr, uid, timeline_id).product_id.standard_price
83         obj.write(cr, uid,[timeline_id], vals_line, {})
84         vals['hr_analytic_timesheet_id'] = timeline_id
85         return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
86
87     def write(self, cr, uid, ids, vals, context=None):
88         vals_line = {}
89
90         task = self.pool.get('project.task.work').browse(cr, uid, ids)[0]
91         line_id = task.hr_analytic_timesheet_id
92         # in case,if a record is deleted from timesheet,but we change it from tasks!
93         list_avail_ids = self.pool.get('hr.analytic.timesheet').search(cr, uid, [])
94         if line_id in list_avail_ids:
95             obj = self.pool.get('hr.analytic.timesheet')
96             if 'name' in vals:
97                 vals_line['name'] = '%s: %s' % (tools.ustr(task.task_id.name), tools.ustr(vals['name']) or '/')
98             if 'user_id' in vals:
99                 vals_line['user_id'] = vals['user_id']
100                 result = self.get_user_related_details(cr, uid, vals['user_id'])
101                 vals_line['product_id'] = result['product_id']
102                 vals_line['general_account_id'] = result['general_account_id']
103                 vals_line['journal_id'] = result['journal_id']
104             if 'date' in vals:
105                 vals_line['date'] = vals['date'][:10]
106             if 'hours' in vals:
107                 vals_line['unit_amount'] = vals['hours']
108                 vals_line['amount'] = (-1) * vals['hours'] * obj.browse(cr, uid, line_id).product_id.standard_price
109             obj.write(cr, uid, [line_id], vals_line, {})
110
111         return super(project_work,self).write(cr, uid, ids, vals, context)
112
113     def unlink(self, cr, uid, ids, *args, **kwargs):
114         timesheet_id = self.pool.get('project.task.work').browse(cr, uid, ids)[0].hr_analytic_timesheet_id
115 #         delete entry from timesheet too while deleting entry to task.
116         list_avail_ids = self.pool.get('hr.analytic.timesheet').search(cr, uid, [])
117         if timesheet_id in list_avail_ids:
118             obj = self.pool.get('hr.analytic.timesheet').unlink(cr, uid, [timesheet_id], *args, **kwargs)
119
120         return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
121
122     _columns={
123         'hr_analytic_timesheet_id':fields.integer('Related Timeline Id')
124     }
125
126 project_work()
127
128 class project_project(osv.osv):
129     _inherit = "project.project"
130     def name_get(self, cr, user, ids, context=None):
131         result = []
132         for project in self.browse(cr, user, ids, context):
133             name = "[%s] %s" % (project.category_id and project.category_id.code or '?', project.name)
134             result.append((project.id, name))
135         return result
136 project_project()
137
138 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
139