[FIX] Project_timesheet : Wrong synchro on analytic line creation fro tasks(for name).
[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     def create(self, cr, uid, vals, *args, **kwargs):
34
35         obj = self.pool.get('hr.analytic.timesheet')
36         vals_line = {}
37         obj_task = self.pool.get('project.task').browse(cr, uid, vals['task_id'])
38
39         emp_obj = self.pool.get('hr.employee')
40         emp_id = emp_obj.search(cr, uid, [('user_id', '=', vals.get('user_id', uid))])
41
42         if not emp_id:
43             raise osv.except_osv(_('Bad Configuration !'),
44                  _('No employee defined for this user. You must create one.'))
45         emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0])
46         if not emp.product_id:
47             raise osv.except_osv(_('Bad Configuration !'),
48                  _('No product defined on the related employee.\nFill in the timesheet tab of the employee form.'))
49
50         if not emp.journal_id:
51             raise osv.except_osv(_('Bad Configuration !'),
52                  _('No journal defined on the related employee.\nFill in the timesheet tab of the employee form.'))
53
54         a =  emp.product_id.product_tmpl_id.property_account_expense.id
55         if not a:
56             a = emp.product_id.categ_id.property_account_expense_categ.id
57
58         vals_line['name'] = '%s: %s' % (tools.ustr(obj_task.name), tools.ustr(vals['name']) or '/')
59         vals_line['user_id'] = vals['user_id']
60         vals_line['date'] = vals['date'][:10]
61         vals_line['unit_amount'] = vals['hours']
62         acc_id = obj_task.project_id.category_id.id
63         vals_line['account_id'] = acc_id
64         res = obj.on_change_account_id(cr, uid, False, acc_id)
65         if res.get('value'):
66             vals_line.update(res['value'])
67         vals_line['general_account_id'] = a
68         vals_line['journal_id'] = emp.journal_id.id
69         vals_line['amount'] = 00.0
70         timeline_id = obj.create(cr, uid, vals_line, {})
71
72         vals_line['amount'] = (-1) * vals['hours'] * obj.browse(cr, uid, timeline_id).product_id.standard_price
73         obj.write(cr, uid,[timeline_id], vals_line, {})
74         vals['hr_analytic_timesheet_id'] = timeline_id
75         return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
76
77     def write(self, cr, uid, ids, vals, context=None):
78         vals_line = {}
79
80         task = self.pool.get('project.task.work').browse(cr, uid, ids)[0]
81         line_id = task.hr_analytic_timesheet_id
82         # in case,if a record is deleted from timesheet,but we change it from tasks!
83         list_avail_ids = self.pool.get('hr.analytic.timesheet').search(cr, uid, [])
84         if line_id in list_avail_ids:
85             obj = self.pool.get('hr.analytic.timesheet')
86             if 'name' in vals:
87                 vals_line['name'] = '%s: %s' % (tools.ustr(task.task_id.name), tools.ustr(vals['name']) or '/')
88             if 'user_id' in vals:
89                 vals_line['user_id'] = vals['user_id']
90             if 'date' in vals:
91                 vals_line['date'] = vals['date'][:10]
92             if 'hours' in vals:
93                 vals_line['unit_amount'] = vals['hours']
94                 vals_line['amount'] = (-1) * vals['hours'] * obj.browse(cr, uid, line_id).product_id.standard_price
95             obj.write(cr, uid, [line_id], vals_line, {})
96
97         return super(project_work,self).write(cr, uid, ids, vals, context)
98
99     def unlink(self, cr, uid, ids, *args, **kwargs):
100         timesheet_id = self.pool.get('project.task.work').browse(cr, uid, ids)[0].hr_analytic_timesheet_id
101 #         delete entry from timesheet too while deleting entry to task.
102         list_avail_ids = self.pool.get('hr.analytic.timesheet').search(cr, uid, [])
103         if timesheet_id in list_avail_ids:
104             obj = self.pool.get('hr.analytic.timesheet').unlink(cr, uid, [timesheet_id], *args, **kwargs)
105
106         return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
107
108     _columns={
109         'hr_analytic_timesheet_id':fields.integer('Related Timeline Id')
110     }
111
112 project_work()
113
114 class project_project(osv.osv):
115     _inherit = "project.project"
116     def name_get(self, cr, user, ids, context=None):
117         result = []
118         for project in self.browse(cr, user, ids, context):
119             name = "[%s] %s" % (project.category_id and project.category_id.code or '?', project.name)
120             result.append((project.id, name))
121         return result
122 project_project()
123
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
125