[IMP] Replace tinyerp.com by openerp.com for the value of the website key in the...
[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-2008 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
27
28 class project_work(osv.osv):
29     _inherit = "project.task.work"
30     _description = "Task Work"
31     def create(self, cr, uid, vals, *args, **kwargs):
32
33         obj = self.pool.get('hr.analytic.timesheet')
34         vals_line={}
35         obj_task = self.pool.get('project.task').browse(cr, uid, vals['task_id'])
36
37         vals_line['name']=obj_task.name + ': ' + vals['name']
38         vals_line['user_id']=vals['user_id']
39         vals_line['date']=vals['date'][:10]
40         vals_line['unit_amount']=vals['hours']
41         vals_line['account_id']=obj_task.project_id.category_id.id
42         vals_line['amount']=00.0
43         timeline_id=obj.create(cr, uid,vals_line,{})
44
45         vals_line['amount']=(-1) * vals['hours']*obj.browse(cr,uid,timeline_id).product_id.standard_price
46         obj.write(cr, uid,[timeline_id],vals_line,{})
47         vals['hr_analytic_timesheet_id']=timeline_id
48         return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
49
50     def write(self, cr, uid, ids,vals,context={}):
51         vals_line={}
52
53         task=self.pool.get('project.task.work').browse(cr,uid,ids)[0]
54         line_id=task.hr_analytic_timesheet_id
55         # in case,if a record is deleted from timesheet,but we change it from tasks!
56         list_avail_ids=self.pool.get('hr.analytic.timesheet').search(cr,uid,[])
57         if line_id in list_avail_ids:
58             obj = self.pool.get('hr.analytic.timesheet')
59             if 'name' in vals:
60                                 vals_line['name']=task.name+': '+vals['name']
61             if 'user_id' in vals:
62                 vals_line['user_id']=vals['user_id']
63             if 'date' in vals:
64                 vals_line['date']=vals['date'][:10]
65             if 'hours' in vals:
66                 vals_line['unit_amount']=vals['hours']
67                 vals_line['amount']=(-1) * vals['hours'] * obj.browse(cr,uid,line_id).product_id.standard_price
68             obj.write(cr, uid,[line_id],vals_line,{})
69
70         return super(project_work,self).write(cr, uid, ids,vals,context)
71
72     def unlink(self, cr, uid, ids, *args, **kwargs):
73         timesheet_id=self.pool.get('project.task.work').browse(cr,uid,ids)[0].hr_analytic_timesheet_id
74         # delete entry from timesheet too while deleting entry to task.
75         list_avail_ids=self.pool.get('hr.analytic.timesheet').search(cr,uid,[])
76         if timesheet_id in list_avail_ids:
77             obj = self.pool.get('hr.analytic.timesheet').unlink(cr,uid,[timesheet_id],*args)
78
79         return super(project_work,self).unlink(cr, uid, ids,*args, **kwargs)
80
81     _columns={
82         'hr_analytic_timesheet_id':fields.integer('Related Timeline Id')
83     }
84
85
86 project_work()
87
88 class project_project(osv.osv):
89     _inherit = "project.project"
90     def name_get(self, cr, user, ids, context=None):
91         result = []
92         for project in self.browse(cr, user, ids, context):
93             if project.category_id and project.category_id.code:
94                 result.append((project.id, '['+(project.category_id.code or '')+'] '+project.name))
95             else:
96                 result.append((project.id, '[?] '+project.name))
97         return result
98 project_project()
99