[IMP] Project: improve code
bzr revid: mra@mra-laptop-20100805072023-lhqjd5eb6g38d6dn
This commit is contained in:
parent
90fd4877f7
commit
1cf2f0aeb5
|
@ -29,12 +29,12 @@ from osv import fields, osv
|
|||
class project_task_type(osv.osv):
|
||||
_name = 'project.task.type'
|
||||
_description = 'Task Stage'
|
||||
_order = 'sequence'
|
||||
_columns = {
|
||||
'name': fields.char('Stage Name', required=True, size=64, translate=True),
|
||||
'description': fields.text('Description'),
|
||||
'sequence': fields.integer('Sequence'),
|
||||
}
|
||||
_order = 'sequence'
|
||||
|
||||
_defaults = {
|
||||
'sequence': 1
|
||||
|
@ -55,8 +55,7 @@ class project(osv.osv):
|
|||
LEFT JOIN account_analytic_account account ON account.id = project.analytic_account_id
|
||||
LEFT JOIN project_user_rel rel ON rel.project_id = project.analytic_account_id
|
||||
WHERE (account.user_id = %s or rel.uid = %s)"""%(user, user))
|
||||
res = cr.fetchall()
|
||||
return [(r[0]) for r in res]
|
||||
return [(r[0]) for r in cr.fetchall()]
|
||||
return super(project, self).search(cr, user, args, offset=offset, limit=limit, order=order,
|
||||
context=context, count=count)
|
||||
|
||||
|
@ -66,10 +65,6 @@ class project(osv.osv):
|
|||
res[m.id] = (m.parent_id and (m.parent_id.name + '/') or '') + m.name
|
||||
return res
|
||||
|
||||
def check_recursion(self, cursor, user, ids, parent=None):
|
||||
return super(project, self).check_recursion(cursor, user, ids,
|
||||
parent=parent)
|
||||
|
||||
def onchange_partner_id(self, cr, uid, ids, part=False, context=None):
|
||||
partner_obj = self.pool.get('res.partner')
|
||||
if not part:
|
||||
|
@ -80,30 +75,29 @@ class project(osv.osv):
|
|||
return {'value':{'contact_id': addr['contact'], 'pricelist_id': pricelist_id}}
|
||||
|
||||
def get_all_child_projects(self, cr, uid, ids, context=None):
|
||||
# Calculate child project for Given project id => For progress rate + planned time + Time spent
|
||||
cr.execute('''select prpc.id as id from account_analytic_account as p
|
||||
join account_analytic_account as c on p.id = c.parent_id
|
||||
join project_project as prp on prp.analytic_account_id = p.id
|
||||
join project_project as prpc on prpc.analytic_account_id = c.id
|
||||
where prp.id in %s''',(tuple(ids),))
|
||||
join account_analytic_account as c on p.id = c.parent_id
|
||||
join project_project as prp on prp.analytic_account_id = p.id
|
||||
join project_project as prpc on prpc.analytic_account_id = c.id
|
||||
where prp.id in %s''',(tuple(ids),))
|
||||
|
||||
child_ids = cr.fetchall()
|
||||
if child_ids:
|
||||
child_ids = [x[0] for x in child_ids]
|
||||
child_ids = self.get_all_child_projects(cr, uid, child_ids)
|
||||
child_ids = [x[0] for x in child_ids]
|
||||
child_ids = self.get_all_child_projects(cr, uid, child_ids)
|
||||
return ids + child_ids
|
||||
|
||||
return ids+child_ids
|
||||
|
||||
def _get_user_and_default_uom_ids(self, cr, uid):
|
||||
users_obj = self.pool.get('res.users')
|
||||
model_data_obj = self.pool.get('ir.model.data')
|
||||
|
||||
model_data_id = model_data_obj._get_id(cr, uid, 'product', 'uom_hour')
|
||||
default_uom = user_uom = model_data_obj.read(cr, uid, [model_data_id], ['res_id'])[0]['res_id']
|
||||
obj_tm = users_obj.browse(cr, uid, uid).company_id.project_time_mode_id
|
||||
if obj_tm:
|
||||
user_uom = obj_tm.id
|
||||
return user_uom, default_uom
|
||||
|
||||
|
||||
def _progress_rate(self, cr, uid, ids, names, arg, context=None):
|
||||
res = {}.fromkeys(ids, 0.0)
|
||||
progress = {}
|
||||
|
@ -138,19 +132,19 @@ where prp.id in %s''',(tuple(ids),))
|
|||
p = tocompute.pop()
|
||||
for i in range(3):
|
||||
s[i] += progress.get(p, (0.0, 0.0, 0.0))[i]
|
||||
|
||||
uom_obj = self.pool.get('product.uom')
|
||||
|
||||
uom_obj = self.pool.get('product.uom')
|
||||
user_uom, def_uom = self._get_user_and_default_uom_ids(cr, uid)
|
||||
if user_uom != def_uom:
|
||||
s[0] = uom_obj._compute_qty(cr, uid, user_uom, s[0], def_uom)
|
||||
s[1] = uom_obj._compute_qty(cr, uid, user_uom, s[1], def_uom)
|
||||
s[2] = uom_obj._compute_qty(cr, uid, user_uom, s[2], def_uom)
|
||||
|
||||
|
||||
if project.state == 'close':
|
||||
progress_rate = 100.0
|
||||
else:
|
||||
progress_rate = s[1] and round(min(100.0 * s[2] / s[1], 99.99), 2)
|
||||
|
||||
|
||||
res[project.id] = {
|
||||
'planned_hours': s[0],
|
||||
'effective_hours': s[2],
|
||||
|
@ -164,7 +158,7 @@ where prp.id in %s''',(tuple(ids),))
|
|||
if proj.tasks:
|
||||
raise osv.except_osv(_('Operation Not Permitted !'), _('You can not delete a project with tasks. I suggest you to deactivate it.'))
|
||||
return super(project, self).unlink(cr, uid, ids, *args, **kwargs)
|
||||
|
||||
|
||||
_columns = {
|
||||
'complete_name': fields.function(_complete_name, method=True, string="Project Name", type='char', size=250),
|
||||
'active': fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the project without removing it."),
|
||||
|
@ -253,21 +247,20 @@ where prp.id in %s''',(tuple(ids),))
|
|||
default['name'] = proj.name+_(' (copy)')
|
||||
res = super(project, self).copy(cr, uid, id, default, context)
|
||||
|
||||
task_ids = task_obj.search(cr, uid, [('project_id','=', res), ('active','=',True)])
|
||||
tasks = task_obj.browse(cr, uid, task_ids)
|
||||
for task in tasks:
|
||||
date_deadline = None
|
||||
if task.date_start:
|
||||
ds = date(*time.strptime(task.date_start,'%Y-%m-%d %H:%M:%S')[:3])
|
||||
if task.date_deadline:
|
||||
dd = date(*time.strptime(task.date_deadline,'%Y-%m-%d')[:3])
|
||||
diff = dd-ds
|
||||
date_deadline = (datetime.now()+diff).strftime('%Y-%m-%d %H:%M:%S')
|
||||
task_obj.write(cr, uid, task.id, {'active':True,
|
||||
'date_start': False,
|
||||
'date_end': False,
|
||||
'date_deadline':date_deadline,
|
||||
})
|
||||
# task_ids = task_obj.search(cr, uid, [('project_id','=', res), ('active','=',True)])
|
||||
# tasks = task_obj.browse(cr, uid, task_ids)
|
||||
# for task in tasks:
|
||||
# date_deadline = None
|
||||
# if task.date_start:
|
||||
# ds = date(*time.strptime(task.date_start,'%Y-%m-%d %H:%M:%S')[:3])
|
||||
# if task.date_deadline:
|
||||
# dd = date(*time.strptime(task.date_deadline,'%Y-%m-%d')[:3])
|
||||
# diff = dd-ds
|
||||
# date_deadline = (datetime.now()+diff).strftime('%Y-%m-%d %H:%M:%S')
|
||||
# task_obj.write(cr, uid, task.id, {'date_start': False,
|
||||
# 'date_end': False,
|
||||
# 'date_deadline':date_deadline,
|
||||
# })
|
||||
|
||||
return res
|
||||
|
||||
|
@ -365,7 +358,7 @@ class task(osv.osv):
|
|||
if hours.get(task.id, False):
|
||||
dur_in_user_uom = uom_obj._compute_qty(cr, uid, default_uom, hours.get(task.id, 0.0), user_uom)
|
||||
hours[task.id] = dur_in_user_uom
|
||||
|
||||
|
||||
for task in self.browse(cr, uid, ids, context=context):
|
||||
res[task.id] = {'effective_hours': hours.get(task.id, 0.0), 'total_hours': task.remaining_hours + hours.get(task.id, 0.0)}
|
||||
res[task.id]['delay_hours'] = res[task.id]['total_hours'] - task.planned_hours
|
||||
|
@ -413,7 +406,7 @@ class task(osv.osv):
|
|||
if task.project_id.active == False or task.project_id.state == 'template':
|
||||
res[task.id] = False
|
||||
return res
|
||||
|
||||
|
||||
_columns = {
|
||||
'active': fields.function(_is_template, method=True, store=True, string='Not a Template Task', type='boolean', help="This field is computed automatically and have the same behavior than the boolean 'active' field: if the task is linked to a template or unactivated project, it will be hidden unless specifically asked."),
|
||||
'name': fields.char('Task Summary', size=128, required=True),
|
||||
|
@ -461,7 +454,7 @@ class task(osv.osv):
|
|||
_order = "sequence, priority, date_start, id"
|
||||
|
||||
_constraints = [
|
||||
(_check_dates, 'Error! task start-date must be lower then task end-date.', ['date_start', 'date_end'])
|
||||
(_check_dates, 'Error! Task start-date must be lower then task end-date.', ['date_start', 'date_end'])
|
||||
]
|
||||
#
|
||||
# Override view according to the company definition
|
||||
|
@ -493,7 +486,6 @@ class task(osv.osv):
|
|||
for f in res['fields']:
|
||||
if 'Hours' in res['fields'][f]['string']:
|
||||
res['fields'][f]['string'] = res['fields'][f]['string'].replace('Hours',tm)
|
||||
|
||||
return res
|
||||
|
||||
def do_close(self, cr, uid, ids, *args):
|
||||
|
@ -602,7 +594,6 @@ class task(osv.osv):
|
|||
self.write(cr, uid, ids, {'state': 'draft'})
|
||||
return True
|
||||
|
||||
|
||||
def do_pending(self, cr, uid, ids, *args):
|
||||
self.write(cr, uid, ids, {'state': 'pending'})
|
||||
for (id, name) in self.name_get(cr, uid, ids):
|
||||
|
@ -636,7 +627,7 @@ task()
|
|||
|
||||
class project_work(osv.osv):
|
||||
_name = "project.task.work"
|
||||
_description = "Task Work"
|
||||
_description = "Project Task Work"
|
||||
_columns = {
|
||||
'name': fields.char('Work summary', size=128),
|
||||
'date': fields.datetime('Date'),
|
||||
|
@ -652,34 +643,36 @@ class project_work(osv.osv):
|
|||
}
|
||||
|
||||
_order = "date desc"
|
||||
|
||||
|
||||
def create(self, cr, uid, vals, *args, **kwargs):
|
||||
if (not 'task_id' in vals) or (not 'hours' in vals):
|
||||
return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
|
||||
if not vals['hours']:
|
||||
vals['hours'] = 0.00
|
||||
|
||||
project_obj = self.pool.get('project.project')
|
||||
uom_obj = self.pool.get('product.uom')
|
||||
|
||||
if (not 'task_id' in vals) or (not 'hours' in vals):
|
||||
return super(project_work, self).create(cr, uid, vals, *args, **kwargs)
|
||||
if not vals['hours']:
|
||||
vals['hours'] = 0.00
|
||||
|
||||
user_uom, default_uom = project_obj._get_user_and_default_uom_ids(cr, uid)
|
||||
duration = vals['hours']
|
||||
|
||||
|
||||
if user_uom != default_uom:
|
||||
duration = uom_obj._compute_qty(cr, uid, default_uom, duration, user_uom)
|
||||
cr.execute('update project_task set remaining_hours=remaining_hours - %s where id=%s', (duration, vals['task_id']))
|
||||
return super(project_work,self).create(cr, uid, vals, *args, **kwargs)
|
||||
|
||||
def write(self, cr, uid, ids, vals, context=None):
|
||||
project_obj = self.pool.get('project.project')
|
||||
uom_obj = self.pool.get('product.uom')
|
||||
|
||||
if context is None:
|
||||
context = {}
|
||||
if not vals['hours']:
|
||||
vals['hours'] = 0.00
|
||||
|
||||
project_obj = self.pool.get('project.project')
|
||||
uom_obj = self.pool.get('product.uom')
|
||||
user_uom, default_uom = project_obj._get_user_and_default_uom_ids(cr, uid)
|
||||
vals['hours'] = 0.00
|
||||
|
||||
user_uom, default_uom = project_obj._get_user_and_default_uom_ids(cr, uid)
|
||||
duration = vals['hours']
|
||||
|
||||
|
||||
if user_uom == default_uom:
|
||||
for work in self.browse(cr, uid, ids, context):
|
||||
cr.execute('update project_task set remaining_hours=remaining_hours - %s + (%s) where id=%s', (duration, work.hours, work.task_id.id))
|
||||
|
@ -688,14 +681,14 @@ class project_work(osv.osv):
|
|||
duration = uom_obj._compute_qty(cr, uid, default_uom, duration, user_uom)
|
||||
del_work = uom_obj._compute_qty(cr, uid, default_uom, work.hours, user_uom)
|
||||
cr.execute('update project_task set remaining_hours=remaining_hours - %s + (%s) where id=%s', (duration, del_work, work.task_id.id))
|
||||
|
||||
|
||||
return super(project_work,self).write(cr, uid, ids, vals, context)
|
||||
|
||||
def unlink(self, cr, uid, ids, *args, **kwargs):
|
||||
context = kwargs.get('context', {})
|
||||
project_obj = self.pool.get('project.project')
|
||||
user_uom, default_uom = project_obj._get_user_and_default_uom_ids(cr, uid)
|
||||
|
||||
user_uom, default_uom = project_obj._get_user_and_default_uom_ids(cr, uid)
|
||||
|
||||
if user_uom == default_uom:
|
||||
for work in self.browse(cr, uid, ids, context):
|
||||
cr.execute('update project_task set remaining_hours=remaining_hours + %s where id=%s', (work.hours, work.task_id.id))
|
||||
|
@ -710,64 +703,54 @@ project_work()
|
|||
class config_compute_remaining(osv.osv_memory):
|
||||
_name='config.compute.remaining'
|
||||
|
||||
def _get_remaining(self,cr, uid, ctx):
|
||||
task_obj = self.pool.get('project.task')
|
||||
if 'active_id' in ctx:
|
||||
return task_obj.browse(cr, uid, ctx['active_id'], context=ctx).remaining_hours
|
||||
def _get_remaining(self,cr, uid, context=None):
|
||||
if context and 'active_id' in context:
|
||||
return self.pool.get('project.task').browse(cr, uid, ctx['active_id'], context=context).remaining_hours
|
||||
return False
|
||||
|
||||
_columns = {
|
||||
'remaining_hours' : fields.float('Remaining Hours', digits=(16,2), help="Put here the remaining hours required to close the task."),
|
||||
}
|
||||
|
||||
def _get_analytic_account(self, cr, uid, context={}):
|
||||
if context.get('account_id', False):
|
||||
return context.get('account_id')
|
||||
return False
|
||||
|
||||
_defaults = {
|
||||
'account_id' : _get_analytic_account,
|
||||
}
|
||||
|
||||
_defaults = {
|
||||
'remaining_hours': _get_remaining
|
||||
}
|
||||
|
||||
|
||||
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
|
||||
users_obj = self.pool.get('res.users')
|
||||
obj_tm = users_obj.browse(cr, uid, uid, context).company_id.project_time_mode_id
|
||||
tm = obj_tm and obj_tm.name or 'Hours'
|
||||
|
||||
|
||||
res = super(config_compute_remaining, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu=submenu)
|
||||
|
||||
|
||||
if tm in ['Hours','Hour']:
|
||||
return res
|
||||
|
||||
|
||||
eview = etree.fromstring(res['arch'])
|
||||
|
||||
|
||||
def _check_rec(eview):
|
||||
if eview.attrib.get('widget','') == 'float_time':
|
||||
eview.set('widget','float')
|
||||
for child in eview:
|
||||
_check_rec(child)
|
||||
return True
|
||||
|
||||
|
||||
_check_rec(eview)
|
||||
|
||||
|
||||
res['arch'] = etree.tostring(eview)
|
||||
|
||||
|
||||
for f in res['fields']:
|
||||
if 'Hours' in res['fields'][f]['string']:
|
||||
res['fields'][f]['string'] = res['fields'][f]['string'].replace('Hours',tm)
|
||||
|
||||
return res
|
||||
|
||||
def compute_hours(self, cr, uid, ids, context=None):
|
||||
if context is None:
|
||||
context = {}
|
||||
task_obj = self.pool.get('project.task')
|
||||
request = self.pool.get('res.request')
|
||||
if 'active_id' in context:
|
||||
remaining_hrs=self.browse(cr,uid,ids)[0].remaining_hours
|
||||
remaining_hrs = self.browse(cr,uid,ids)[0].remaining_hours
|
||||
task_obj.write(cr,uid,context['active_id'],{'remaining_hours':remaining_hrs})
|
||||
if context.get('button_reactivate', False):
|
||||
tasks = task_obj.browse(cr, uid, [context['active_id']], context=context)
|
||||
|
@ -787,7 +770,7 @@ class config_compute_remaining(osv.osv_memory):
|
|||
return {
|
||||
'type': 'ir.actions.act_window_close',
|
||||
}
|
||||
|
||||
|
||||
config_compute_remaining()
|
||||
|
||||
class message(osv.osv):
|
||||
|
|
Loading…
Reference in New Issue