=== modified file 'project_timesheet/project_timesheet.py' --- old/project_timesheet/project_timesheet.py 2009-04-15 14:03:32 +0000 +++ new/project_timesheet/project_timesheet.py 2009-07-03 11:22:57 +0000 @@ -30,19 +30,24 @@ class project_work(osv.osv): _inherit = "project.task.work" _description = "Task Work" - def create(self, cr, uid, vals, *args, **kwargs): - - obj = self.pool.get('hr.analytic.timesheet') - vals_line = {} - obj_task = self.pool.get('project.task').browse(cr, uid, vals['task_id']) - + + def _get_user_product(self, cr, uid, target_user, *args, **kwargs): + """ Return product informations for given user + + @param target_user: (int/id) selected user (not always logged user) + + @return: multiple values : + product_id (int/id) : user --> employee --> product + account_id (int/id) : general account linked to product (or its category) + journal_id (int/id) : timesheet journal of the employee + standard_price (float) : cost price of the product + """ emp_obj = self.pool.get('hr.employee') - emp_id = emp_obj.search(cr, uid, [('user_id', '=', vals.get('user_id', uid))]) - + emp_id = emp_obj.search(cr, uid, [('user_id', '=', target_user)]) if not emp_id: raise osv.except_osv(_('Bad Configuration !'), _('No employee defined for this user. You must create one.')) - emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0]) + emp = emp_obj.browse(cr, uid, emp_id[0]) if not emp.product_id: raise osv.except_osv(_('Bad Configuration !'), _('No product defined on the related employee.\nFill in the timesheet tab of the employee form.')) @@ -51,30 +56,41 @@ raise osv.except_osv(_('Bad Configuration !'), _('No journal defined on the related employee.\nFill in the timesheet tab of the employee form.')) - a = emp.product_id.product_tmpl_id.property_account_expense.id - if not a: - a = emp.product_id.categ_id.property_account_expense_categ.id + acc_id = emp.product_id.property_account_expense.id + if not acc_id: + acc_id = emp.product_id.categ_id.property_account_expense_categ.id + + return emp.product_id.id, acc_id, emp.journal_id.id, emp.product_id.standard_price + + + def create(self, cr, uid, vals, *args, **kwargs): + """ New task.work (timesheet) and analytics lines in casacde + """ + vals_line = {} + obj_task = self.pool.get('project.task').browse(cr, uid, vals['task_id']) vals_line['name'] = '%s: %s' % (tools.ustr(obj_task.name), tools.ustr(vals['name']) or '/') vals_line['user_id'] = vals['user_id'] vals_line['date'] = vals['date'][:10] vals_line['unit_amount'] = vals['hours'] + vals_line['product_id'], vals_line['general_account_id'], vals_line['journal_id'], pdt_price = self._get_user_product(cr, uid, vals.get('user_id', uid)) + vals_line['amount'] = (-1) * vals['hours'] * pdt_price + + obj = self.pool.get('hr.analytic.timesheet') acc_id = obj_task.project_id.category_id.id vals_line['account_id'] = acc_id res = obj.on_change_account_id(cr, uid, False, acc_id) if res.get('value'): vals_line.update(res['value']) - vals_line['general_account_id'] = a - vals_line['journal_id'] = emp.journal_id.id - vals_line['amount'] = 00.0 + timeline_id = obj.create(cr, uid, vals_line, {}) - - vals_line['amount'] = (-1) * vals['hours'] * obj.browse(cr, uid, timeline_id).product_id.standard_price - obj.write(cr, uid,[timeline_id], vals_line, {}) vals['hr_analytic_timesheet_id'] = timeline_id return super(project_work,self).create(cr, uid, vals, *args, **kwargs) + def write(self, cr, uid, ids, vals, context=None): + """ Update task.work (timesheet) and analytics lines in casacde + """ vals_line = {} task = self.pool.get('project.task.work').browse(cr, uid, ids)[0] @@ -82,20 +98,30 @@ # in case,if a record is deleted from timesheet,but we change it from tasks! list_avail_ids = self.pool.get('hr.analytic.timesheet').search(cr, uid, []) if line_id in list_avail_ids: + time_amount = False + pdt_price = False obj = self.pool.get('hr.analytic.timesheet') if 'name' in vals: vals_line['name'] = '%s: %s' % (tools.ustr(task.name), tools.ustr(vals['name']) or '/') - if 'user_id' in vals: - vals_line['user_id'] = vals['user_id'] if 'date' in vals: vals_line['date'] = vals['date'][:10] if 'hours' in vals: vals_line['unit_amount'] = vals['hours'] - vals_line['amount'] = (-1) * vals['hours'] * obj.browse(cr, uid, line_id).product_id.standard_price + time_amount = vals['hours'] + pdt_price = obj.browse(cr, uid, line_id).product_id.standard_price + if 'user_id' in vals: + vals_line['user_id'] = vals['user_id'] + vals_line['product_id'], vals_line['general_account_id'], vals_line['journal_id'], pdt_price = self._get_user_product(cr, uid, vals['user_id']) + if not time_amount: + time_amount = obj.browse(cr, uid, line_id).hours + print "DEBUG: project_work::write > old time = %r" % time_amount + if time_amount or pdt_price: # recompute total + vals_line['amount'] = (-1) * time_amount * pdt_price obj.write(cr, uid, [line_id], vals_line, {}) return super(project_work,self).write(cr, uid, ids, vals, context) + def unlink(self, cr, uid, ids, *args, **kwargs): timesheet_id = self.pool.get('project.task.work').browse(cr, uid, ids)[0].hr_analytic_timesheet_id # delete entry from timesheet too while deleting entry to task.