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 ##############################################################################
24 from osv import fields, osv
27 from tools.translate import _
29 class project_work(osv.osv):
30 _inherit = "project.task.work"
32 def get_user_related_details(self, cr, uid, user_id):
34 emp_obj = self.pool.get('hr.employee')
35 emp_id = emp_obj.search(cr, uid, [('user_id', '=', user_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.'))
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.'))
49 a = emp.product_id.product_tmpl_id.property_account_expense.id
51 a = emp.product_id.categ_id.property_account_expense_categ.id
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
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')
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)
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']
84 timeline_id = obj_timesheet.create(cr, uid, vals=vals_line, context=kwargs['context'])
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'])
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)
95 def write(self, cr, uid, ids, vals, context=None):
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:
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']
116 vals_line['date'] = vals['date'][:10]
118 vals_line['unit_amount'] = vals['hours']
119 # Compute based on pricetype
121 amount_unit=obj.on_change_unit_amount(cr, uid, line_id,
122 vals_line['product_id'], vals_line['unit_amount'], unit, context=context)
125 vals_line['amount'] = (-1) * vals['hours'] * (amount_unit.get('value',{}).get('amount',0.0) or 0.0)
127 obj.write(cr, uid, [line_id], vals_line, context=context)
129 return super(project_work,self).write(cr, uid, ids, vals, context)
131 def unlink(self, cr, uid, ids, *args, **kwargs):
132 pool_analytic_timesheet = self.pool.get('hr.analytic.timesheet')
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)
140 return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
143 'hr_analytic_timesheet_id':fields.integer('Related Timeline Id')
149 _inherit = "project.task"
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)
157 return super(task,self).unlink(cr, uid, ids, *args, **kwargs)
159 def write(self, cr, uid, ids,vals,context=None):
162 if (vals.has_key('project_id') and vals['project_id']) or (vals.has_key('name') and vals['name']):
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
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)
183 class project_project(osv.osv):
184 _inherit = "project.project"
186 def name_get(self, cr, user, ids, context=None):
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))
197 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: