1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
21 from osv import fields, osv
26 from tools.translate import _
29 class project_work(osv.osv):
30 _inherit = "project.task.work"
31 _description = "Task Work"
33 def get_user_related_details(self, cr, uid, user_id):
35 emp_obj = self.pool.get('hr.employee')
36 emp_id = emp_obj.search(cr, uid, [('user_id', '=', user_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.'))
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.'))
50 a = emp.product_id.product_tmpl_id.property_account_expense.id
52 a = emp.product_id.categ_id.property_account_expense_categ.id
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 res['product_uom_id'] = emp.product_id.uom_id.id
62 def create(self, cr, uid, vals, *args, **kwargs):
63 obj_timesheet = self.pool.get('hr.analytic.timesheet')
64 task_obj = self.pool.get('project.task')
66 obj_task = task_obj.browse(cr, uid, vals['task_id'])
67 result = self.get_user_related_details(cr, uid, vals.get('user_id', uid))
68 vals_line['name'] = '%s: %s' % (tools.ustr(obj_task.name), tools.ustr(vals['name']) or '/')
69 vals_line['user_id'] = vals['user_id']
70 vals_line['product_id'] = result['product_id']
71 vals_line['date'] = vals['date'][:10]
72 vals_line['unit_amount'] = vals['hours']
73 acc_id = obj_task.project_id.category_id.id
74 vals_line['account_id'] = acc_id
75 res = obj_timesheet.on_change_account_id(cr, uid, False, acc_id)
77 vals_line.update(res['value'])
78 vals_line['general_account_id'] = result['general_account_id']
79 vals_line['journal_id'] = result['journal_id']
80 vals_line['amount'] = 00.0
81 vals_line['product_uom_id'] = result['product_uom_id']
82 amount = vals_line['unit_amount']
83 prod_id = vals_line['product_id']
85 timeline_id = obj_timesheet.create(cr, uid, vals=vals_line, context=kwargs['context'])
87 # Compute based on pricetype
88 amount_unit=obj_timesheet.on_change_unit_amount(cr, uid, timeline_id,
89 prod_id, amount, unit, context=kwargs['context'])
91 vals_line['amount'] = (-1) * vals['hours']* (amount_unit['value']['amount'] or 0.0)
92 obj_timesheet.write(cr, uid,[timeline_id], vals_line, {})
93 vals['hr_analytic_timesheet_id'] = timeline_id
94 return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
96 def write(self, cr, uid, ids, vals, context=None):
98 obj = self.pool.get('hr.analytic.timesheet')
99 timesheet_obj = self.pool.get('hr.analytic.timesheet')
100 task = self.pool.get('project.task.work').browse(cr, uid, ids, context=context)[0]
101 line_id = task.hr_analytic_timesheet_id
102 # in case,if a record is deleted from timesheet,but we change it from tasks!
103 list_avail_ids = timesheet_obj.search(cr, uid, [], context=context)
104 if line_id in list_avail_ids:
106 vals_line['name'] = '%s: %s' % (tools.ustr(task.task_id.name), tools.ustr(vals['name']) or '/')
107 if 'user_id' in vals:
108 vals_line['user_id'] = vals['user_id']
109 result = self.get_user_related_details(cr, uid, vals['user_id'])
110 vals_line['product_id'] = result['product_id']
111 vals_line['general_account_id'] = result['general_account_id']
112 vals_line['journal_id'] = result['journal_id']
113 vals_line['product_uom_id'] = result['product_uom_id']
115 vals_line['date'] = vals['date'][:10]
117 vals_line['unit_amount'] = vals['hours']
118 # Compute based on pricetype
120 amount_unit=obj.on_change_unit_amount(cr, uid, line_id,
121 vals_line['product_id'], vals_line['unit_amount'], unit, context=context)
123 vals_line['amount'] = (-1) * vals['hours'] * (amount_unit['value']['amount'] or 0.0)
124 obj.write(cr, uid, [line_id], vals_line, context=context)
126 return super(project_work,self).write(cr, uid, ids, vals, context)
128 def unlink(self, cr, uid, ids, *args, **kwargs):
129 pool_analytic_timesheet = self.pool.get('hr.analytic.timesheet')
131 timesheet_id = self.read(cr, uid, work_id, ['hr_analytic_timesheet_id'])['hr_analytic_timesheet_id']
132 # delete entry from timesheet too while deleting entry to task.
133 list_avail_ids = pool_analytic_timesheet.search(cr, uid, [])
134 if timesheet_id in list_avail_ids:
135 obj = pool_analytic_timesheet.unlink(cr, uid, [timesheet_id], *args, **kwargs)
137 return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
140 'hr_analytic_timesheet_id':fields.integer('Related Timeline Id')
146 _inherit = "project.task"
147 _description = "Tasks"
149 def unlink(self, cr, uid, ids, *args, **kwargs):
150 for task_obj in self.browse(cr, uid, ids, *args, **kwargs):
151 if task_obj.work_ids:
152 work_ids = [x.id for x in task_obj.work_ids]
153 self.pool.get('project.task.work').unlink(cr, uid, work_ids, *args, **kwargs)
155 return super(task,self).unlink(cr, uid, ids, *args, **kwargs)
157 def write(self, cr, uid, ids,vals,context={}):
158 if (vals.has_key('project_id') and vals['project_id']) or (vals.has_key('name') and vals['name']):
160 hr_anlytic_timesheet = self.pool.get('hr.analytic.timesheet')
161 task_obj_l = self.browse(cr, uid, ids, context)
162 if (vals.has_key('project_id') and vals['project_id']):
163 project_obj = self.pool.get('project.project').browse(cr, uid, vals['project_id'])
164 acc_id = project_obj.category_id.id
166 for task_obj in task_obj_l:
167 if len(task_obj.work_ids):
168 for task_work in task_obj.work_ids:
169 line_id = task_work.hr_analytic_timesheet_id
170 if (vals.has_key('project_id') and vals['project_id']):
171 vals_line['account_id'] = acc_id
172 if (vals.has_key('name') and vals['name']):
173 vals_line['name'] = '%s: %s' % (tools.ustr(vals['name']), tools.ustr(task_work.name) or '/')
174 hr_anlytic_timesheet.write(cr, uid, [line_id], vals_line, {})
175 return super(task,self).write(cr, uid, ids, vals, context)
179 class project_project(osv.osv):
180 _inherit = "project.project"
181 def name_get(self, cr, user, ids, context=None):
183 for project in self.browse(cr, user, ids, context):
184 name = "[%s] %s" % (project.category_id and project.category_id.code or '?', project.name)
185 result.append((project.id, name))
189 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: