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