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_project(osv.osv):
30 _inherit = 'project.project'
32 def onchange_partner_id(self, cr, uid, ids, part=False, context=None):
33 res = super(project_project, self).onchange_partner_id(cr, uid, ids, part, context)
34 if part and res and ('value' in res):
35 # set Invoice Task Work to 100%
36 data_obj = self.pool.get('ir.model.data')
37 data_id = data_obj._get_id(cr, uid, 'hr_timesheet_invoice', 'timesheet_invoice_factor1')
39 factor_id = data_obj.browse(cr, uid, data_id).res_id
40 res['value'].update({'to_invoice': factor_id})
45 class project_work(osv.osv):
46 _inherit = "project.task.work"
48 def get_user_related_details(self, cr, uid, user_id):
50 emp_obj = self.pool.get('hr.employee')
51 emp_id = emp_obj.search(cr, uid, [('user_id', '=', user_id)])
53 user_name = self.pool.get('res.users').read(cr, uid, [user_id], ['name'])[0]['name']
54 raise osv.except_osv(_('Bad Configuration !'),
55 _('No employee defined for user "%s". You must create one.')% (user_name,))
56 emp = self.pool.get('hr.employee').browse(cr, uid, emp_id[0])
57 if not emp.product_id:
58 raise osv.except_osv(_('Bad Configuration !'),
59 _('No product defined on the related employee.\nFill in the timesheet tab of the employee form.'))
61 if not emp.journal_id:
62 raise osv.except_osv(_('Bad Configuration !'),
63 _('No journal defined on the related employee.\nFill in the timesheet tab of the employee form.'))
65 a = emp.product_id.product_tmpl_id.property_account_expense.id
67 a = emp.product_id.categ_id.property_account_expense_categ.id
69 raise osv.except_osv(_('Bad Configuration !'),
70 _('No product and product category property account defined on the related employee.\nFill in the timesheet tab of the employee form.'))
71 res['product_id'] = emp.product_id.id
72 res['journal_id'] = emp.journal_id.id
73 res['general_account_id'] = a
74 res['product_uom_id'] = emp.product_id.uom_id.id
77 def create(self, cr, uid, vals, *args, **kwargs):
78 obj_timesheet = self.pool.get('hr.analytic.timesheet')
79 project_obj = self.pool.get('project.project')
80 task_obj = self.pool.get('project.task')
81 uom_obj = self.pool.get('product.uom')
84 context = kwargs.get('context', {})
85 if not context.get('no_analytic_entry',False):
86 obj_task = task_obj.browse(cr, uid, vals['task_id'])
87 result = self.get_user_related_details(cr, uid, vals.get('user_id', uid))
88 vals_line['name'] = '%s: %s' % (tools.ustr(obj_task.name), tools.ustr(vals['name']) or '/')
89 vals_line['user_id'] = vals['user_id']
90 vals_line['product_id'] = result['product_id']
91 vals_line['date'] = vals['date'][:10]
93 #calculate quantity based on employee's product's uom
94 vals_line['unit_amount'] = vals['hours']
96 default_uom = self.pool.get('res.users').browse(cr, uid, uid).company_id.project_time_mode_id.id
97 if result['product_uom_id'] != default_uom:
98 vals_line['unit_amount'] = uom_obj._compute_qty(cr, uid, default_uom, vals['hours'], result['product_uom_id'])
99 acc_id = obj_task.project_id and obj_task.project_id.analytic_account_id.id or False
101 vals_line['account_id'] = acc_id
102 res = obj_timesheet.on_change_account_id(cr, uid, False, acc_id)
104 vals_line.update(res['value'])
105 vals_line['general_account_id'] = result['general_account_id']
106 vals_line['journal_id'] = result['journal_id']
107 vals_line['amount'] = 0.0
108 vals_line['product_uom_id'] = result['product_uom_id']
109 amount = vals_line['unit_amount']
110 prod_id = vals_line['product_id']
112 timeline_id = obj_timesheet.create(cr, uid, vals=vals_line, context=context)
114 # Compute based on pricetype
115 amount_unit = obj_timesheet.on_change_unit_amount(cr, uid, timeline_id,
116 prod_id, amount, False, unit, vals_line['journal_id'], context=context)
117 if amount_unit and 'amount' in amount_unit.get('value',{}):
118 updv = { 'amount': amount_unit['value']['amount'] }
119 obj_timesheet.write(cr, uid, [timeline_id], updv, context=context)
120 vals['hr_analytic_timesheet_id'] = timeline_id
121 return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
123 def write(self, cr, uid, ids, vals, context=None):
126 timesheet_obj = self.pool.get('hr.analytic.timesheet')
127 project_obj = self.pool.get('project.project')
128 uom_obj = self.pool.get('product.uom')
131 if isinstance(ids, (long, int)):
134 for task in self.browse(cr, uid, ids, context=context):
135 line_id = task.hr_analytic_timesheet_id
137 # if a record is deleted from timesheet, the line_id will become
138 # null because of the foreign key on-delete=set null
142 vals_line['name'] = '%s: %s' % (tools.ustr(task.task_id.name), tools.ustr(vals['name']) or '/')
143 if 'user_id' in vals:
144 vals_line['user_id'] = vals['user_id']
145 result = self.get_user_related_details(cr, uid, vals['user_id'])
146 for fld in ('product_id', 'general_account_id', 'journal_id', 'product_uom_id'):
147 if result.get(fld, False):
148 vals_line[fld] = result[fld]
151 vals_line['date'] = vals['date'][:10]
153 default_uom = self.pool.get('res.users').browse(cr, uid, uid).company_id.project_time_mode_id.id
154 vals_line['unit_amount'] = vals['hours']
155 prod_id = vals_line.get('product_id', line_id.product_id.id) # False may be set
157 if result.get('product_uom_id',False) and (not result['product_uom_id'] == default_uom):
158 vals_line['unit_amount'] = uom_obj._compute_qty(cr, uid, default_uom, vals['hours'], result['product_uom_id'])
160 # Compute based on pricetype
161 amount_unit = timesheet_obj.on_change_unit_amount(cr, uid, line_id.id,
162 prod_id=prod_id, company_id=False,
163 unit_amount=vals_line['unit_amount'], unit=False, journal_id=vals_line['journal_id'], context=context)
165 if amount_unit and 'amount' in amount_unit.get('value',{}):
166 vals_line['amount'] = amount_unit['value']['amount']
168 self.pool.get('hr.analytic.timesheet').write(cr, uid, [line_id.id], vals_line, context=context)
170 return super(project_work,self).write(cr, uid, ids, vals, context)
172 def unlink(self, cr, uid, ids, *args, **kwargs):
173 hat_obj = self.pool.get('hr.analytic.timesheet')
175 for task in self.browse(cr, uid, ids):
176 if task.hr_analytic_timesheet_id:
177 hat_ids.append(task.hr_analytic_timesheet_id.id)
178 # delete entry from timesheet too while deleting entry to task.
180 hat_obj.unlink(cr, uid, hat_ids, *args, **kwargs)
181 return super(project_work,self).unlink(cr, uid, ids, *args, **kwargs)
184 'hr_analytic_timesheet_id':fields.many2one('hr.analytic.timesheet','Related Timeline Id', ondelete='set null'),
190 _inherit = "project.task"
192 def unlink(self, cr, uid, ids, *args, **kwargs):
193 for task_obj in self.browse(cr, uid, ids, *args, **kwargs):
194 if task_obj.work_ids:
195 work_ids = [x.id for x in task_obj.work_ids]
196 self.pool.get('project.task.work').unlink(cr, uid, work_ids, *args, **kwargs)
198 return super(task,self).unlink(cr, uid, ids, *args, **kwargs)
200 def write(self, cr, uid, ids,vals,context=None):
203 if vals.get('project_id',False) or vals.get('name',False):
205 hr_anlytic_timesheet = self.pool.get('hr.analytic.timesheet')
206 task_obj_l = self.browse(cr, uid, ids, context=context)
207 if vals.get('project_id',False):
208 project_obj = self.pool.get('project.project').browse(cr, uid, vals['project_id'], context=context)
209 acc_id = project_obj.analytic_account_id.id
211 for task_obj in task_obj_l:
212 if len(task_obj.work_ids):
213 for task_work in task_obj.work_ids:
214 if not task_work.hr_analytic_timesheet_id:
216 line_id = task_work.hr_analytic_timesheet_id.id
217 if vals.get('project_id',False):
218 vals_line['account_id'] = acc_id
219 if vals.get('name',False):
220 vals_line['name'] = '%s: %s' % (tools.ustr(vals['name']), tools.ustr(task_work.name) or '/')
221 hr_anlytic_timesheet.write(cr, uid, [line_id], vals_line, {})
222 return super(task,self).write(cr, uid, ids, vals, context)
226 class res_partner(osv.osv):
227 _inherit = 'res.partner'
228 def unlink(self, cursor, user, ids, context=None):
229 parnter_id=self.pool.get('project.project').search(cursor, user, [('partner_id', 'in', ids)])
231 raise osv.except_osv(_('Invalid action !'), _('You cannot delete a partner which is assigned to project, we suggest you to uncheck the active box!'))
232 return super(res_partner,self).unlink(cursor, user, ids,
236 class account_analytic_line(osv.osv):
237 _inherit = "account.analytic.line"
238 def on_change_account_id(self, cr, uid, ids, account_id):
242 res.setdefault('value',{})
243 acc = self.pool.get('account.analytic.account').browse(cr, uid, account_id)
244 st = acc.to_invoice.id
245 res['value']['to_invoice'] = st or False
247 account_analytic_line()
248 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: