Merge from stable
[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         vals_line['amount'] = (-1) * vals['hours']* ( obj.browse(cr,uid,timeline_id).product_id.standard_price or 0.0)
82         obj.write(cr, uid,[timeline_id], vals_line, {})
83         vals['hr_analytic_timesheet_id'] = timeline_id
84         return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
85
86     def write(self, cr, uid, ids, vals, context=None):
87         vals_line = {}
88
89         task = self.pool.get('project.task.work').browse(cr, uid, ids)[0]
90         line_id = task.hr_analytic_timesheet_id
91         # in case,if a record is deleted from timesheet,but we change it from tasks!
92         list_avail_ids = self.pool.get('hr.analytic.timesheet').search(cr, uid, [])
93         if line_id in list_avail_ids:
94             obj = self.pool.get('hr.analytic.timesheet')
95             if 'name' in vals:
96                 vals_line['name'] = '%s: %s' % (tools.ustr(task.task_id.name), tools.ustr(vals['name']) or '/')
97             if 'user_id' in vals:
98                 vals_line['user_id'] = vals['user_id']
99                 result = self.get_user_related_details(cr, uid, vals['user_id'])
100                 vals_line['product_id'] = result['product_id']
101                 vals_line['general_account_id'] = result['general_account_id']
102                 vals_line['journal_id'] = result['journal_id']
103             if 'date' in vals:
104                 vals_line['date'] = vals['date'][:10]
105             if 'hours' in vals:
106                 vals_line['unit_amount'] = vals['hours']
107                 vals_line['amount'] = (-1) * vals['hours'] * (obj.browse(cr,uid,line_id).product_id.standard_price or 0.0)
108             obj.write(cr, uid, [line_id], vals_line, {})
109
110         return super(project_work,self).write(cr, uid, ids, vals, context)
111
112     def unlink(self, cr, uid, ids, *args, **kwargs):
113         pool_analytic_timesheet = self.pool.get('hr.analytic.timesheet')
114         for work_id in ids:
115             timesheet_id = self.read(cr, uid, work_id, ['hr_analytic_timesheet_id'])['hr_analytic_timesheet_id']
116 #            delete entry from timesheet too while deleting entry to task.
117             list_avail_ids = pool_analytic_timesheet.search(cr, uid, [])
118             if timesheet_id in list_avail_ids:
119                 obj = pool_analytic_timesheet.unlink(cr, uid, [timesheet_id], *args, **kwargs)
120
121         return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
122
123     _columns={
124         'hr_analytic_timesheet_id':fields.integer('Related Timeline Id')
125     }
126
127 project_work()
128
129 class task(osv.osv):
130     _inherit = "project.task"
131     _description = "Tasks"
132
133     def unlink(self, cr, uid, ids, *args, **kwargs):
134         for task_obj in self.browse(cr, uid, ids, *args, **kwargs):
135             if task_obj.work_ids:
136                 work_ids = [x.id for x in task_obj.work_ids]
137                 self.pool.get('project.task.work').unlink(cr, uid, work_ids, *args, **kwargs)
138         
139         return super(task,self).unlink(cr, uid, ids, *args, **kwargs)
140     
141     def write(self, cr, uid, ids,vals,context={}):
142         if (vals.has_key('project_id') and vals['project_id']) or (vals.has_key('name') and vals['name']):
143             vals_line = {}
144             hr_anlytic_timesheet = self.pool.get('hr.analytic.timesheet')
145             task_obj_l = self.browse(cr, uid, ids, context)
146             if (vals.has_key('project_id') and vals['project_id']):
147                 project_obj = self.pool.get('project.project').browse(cr, uid, vals['project_id'])
148                 acc_id = project_obj.category_id.id
149
150             for task_obj in task_obj_l:
151                 if len(task_obj.work_ids):
152                     for task_work in task_obj.work_ids:
153                         line_id = task_work.hr_analytic_timesheet_id
154                         if (vals.has_key('project_id') and vals['project_id']):
155                             vals_line['account_id'] = acc_id
156                         if (vals.has_key('name') and vals['name']):
157                             vals_line['name'] = '%s: %s' % (tools.ustr(vals['name']), tools.ustr(task_work.name) or '/')
158                         hr_anlytic_timesheet.write(cr, uid, [line_id], vals_line, {})
159         return super(task,self).write(cr, uid, ids, vals, context)
160
161 task()
162
163 class project_project(osv.osv):
164     _inherit = "project.project"
165     def name_get(self, cr, user, ids, context=None):
166         result = []
167         for project in self.browse(cr, user, ids, context):
168             name = "[%s] %s" % (project.category_id and project.category_id.code or '?', project.name)
169             result.append((project.id, name))
170         return result
171 project_project()
172
173 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
174