[FIX] Change the year of the copyright
[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         timesheet_id = self.pool.get('project.task.work').browse(cr, uid, ids)[0].hr_analytic_timesheet_id
114 #         delete entry from timesheet too while deleting entry to task.
115         list_avail_ids = self.pool.get('hr.analytic.timesheet').search(cr, uid, [])
116         if timesheet_id in list_avail_ids:
117             obj = self.pool.get('hr.analytic.timesheet').unlink(cr, uid, [timesheet_id], *args, **kwargs)
118
119         return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
120
121     _columns={
122         'hr_analytic_timesheet_id':fields.integer('Related Timeline Id')
123     }
124
125 project_work()
126
127 class project_project(osv.osv):
128     _inherit = "project.project"
129     def name_get(self, cr, user, ids, context=None):
130         result = []
131         for project in self.browse(cr, user, ids, context):
132             name = "[%s] %s" % (project.category_id and project.category_id.code or '?', project.name)
133             result.append((project.id, name))
134         return result
135 project_project()
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
138