[IMP]:renamed category_id to analytic_account_id in project and project_* modules
[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.analytic_account_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         if amount_unit:
90             vals_line['amount'] = (-1) * vals['hours']* (amount_unit.get('value',{}).get('amount',0.0) 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         if context is None:
97             context = {}
98         vals_line = {}
99         obj = self.pool.get('hr.analytic.timesheet')
100         timesheet_obj = self.pool.get('hr.analytic.timesheet')
101         task = self.pool.get('project.task.work').browse(cr, uid, ids, context=context)[0]
102         line_id = task.hr_analytic_timesheet_id
103         # in case,if a record is deleted from timesheet,but we change it from tasks!
104         list_avail_ids = timesheet_obj.search(cr, uid, [], context=context)
105         if line_id in list_avail_ids:
106             if 'name' in vals:
107                 vals_line['name'] = '%s: %s' % (tools.ustr(task.task_id.name), tools.ustr(vals['name']) or '/')
108             if 'user_id' in vals:
109                 vals_line['user_id'] = vals['user_id']
110                 result = self.get_user_related_details(cr, uid, vals['user_id'])
111                 vals_line['product_id'] = result['product_id']
112                 vals_line['general_account_id'] = result['general_account_id']
113                 vals_line['journal_id'] = result['journal_id']
114                 vals_line['product_uom_id'] = result['product_uom_id']
115             if 'date' in vals:
116                 vals_line['date'] = vals['date'][:10]
117             if 'hours' in vals:
118                 vals_line['unit_amount'] = vals['hours']
119                 # Compute based on pricetype
120                 unit = False
121                 amount_unit=obj.on_change_unit_amount(cr, uid, line_id,
122                     vals_line['product_id'], vals_line['unit_amount'], unit, context=context)
123                 
124                 if amount_unit:
125                      vals_line['amount'] = (-1) * vals['hours'] * (amount_unit.get('value',{}).get('amount',0.0) or 0.0)
126                      
127             obj.write(cr, uid, [line_id], vals_line, context=context)
128
129         return super(project_work,self).write(cr, uid, ids, vals, context)
130
131     def unlink(self, cr, uid, ids, *args, **kwargs):
132         pool_analytic_timesheet = self.pool.get('hr.analytic.timesheet')
133         for work_id in ids:
134             timesheet_id = self.read(cr, uid, work_id, ['hr_analytic_timesheet_id'])['hr_analytic_timesheet_id']
135 #            delete entry from timesheet too while deleting entry to task.
136             list_avail_ids = pool_analytic_timesheet.search(cr, uid, [])
137             if timesheet_id in list_avail_ids:
138                 obj = pool_analytic_timesheet.unlink(cr, uid, [timesheet_id], *args, **kwargs)
139
140         return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
141
142     _columns={
143         'hr_analytic_timesheet_id':fields.integer('Related Timeline Id')
144     }
145
146 project_work()
147
148 class task(osv.osv):
149     _inherit = "project.task"
150
151     def unlink(self, cr, uid, ids, *args, **kwargs):
152         for task_obj in self.browse(cr, uid, ids, *args, **kwargs):
153             if task_obj.work_ids:
154                 work_ids = [x.id for x in task_obj.work_ids]
155                 self.pool.get('project.task.work').unlink(cr, uid, work_ids, *args, **kwargs)
156
157         return super(task,self).unlink(cr, uid, ids, *args, **kwargs)
158
159     def write(self, cr, uid, ids,vals,context=None):
160         if context is None:
161             context = {}
162         if (vals.has_key('project_id') and vals['project_id']) or (vals.has_key('name') and vals['name']):
163             vals_line = {}
164             hr_anlytic_timesheet = self.pool.get('hr.analytic.timesheet')
165             task_obj_l = self.browse(cr, uid, ids, context)
166             if (vals.has_key('project_id') and vals['project_id']):
167                 project_obj = self.pool.get('project.project').browse(cr, uid, vals['project_id'])
168                 acc_id = project_obj.analytic_account_id.id
169
170             for task_obj in task_obj_l:
171                 if len(task_obj.work_ids):
172                     for task_work in task_obj.work_ids:
173                         line_id = task_work.hr_analytic_timesheet_id
174                         if (vals.has_key('project_id') and vals['project_id']):
175                             vals_line['account_id'] = acc_id
176                         if (vals.has_key('name') and vals['name']):
177                             vals_line['name'] = '%s: %s' % (tools.ustr(vals['name']), tools.ustr(task_work.name) or '/')
178                         hr_anlytic_timesheet.write(cr, uid, [line_id], vals_line, {})
179         return super(task,self).write(cr, uid, ids, vals, context)
180
181 task()
182
183 class project_project(osv.osv):
184     _inherit = "project.project"
185
186     def name_get(self, cr, user, ids, context=None):
187         if context is None:
188             context = {}
189         result = []
190         for project in self.browse(cr, user, ids, context):
191             name = "[%s] %s" % (project.analytic_account_id and project.analytic_account_id.code or '?', project.name)
192             result.append((project.id, name))
193         return result
194
195 project_project()
196
197 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: