[MERGE]: Merge with lp:~openerp-dev/openobject-addons/trunk-import_sugarcrm
[odoo/odoo.git] / addons / project / project.py
index df20562..14ee94e 100644 (file)
@@ -25,7 +25,7 @@ from datetime import datetime, date
 
 from tools.translate import _
 from osv import fields, osv
-from tools import email_send as email
+
 
 class project_task_type(osv.osv):
     _name = 'project.task.type'
@@ -75,85 +75,43 @@ class project(osv.osv):
         pricelist_id = pricelist.get('property_product_pricelist', False) and pricelist.get('property_product_pricelist')[0] or False
         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),))
-
-        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)
-        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 = {}
         if not ids:
             return res
-
-        par_child_projects={}
-        all_projects = list(ids)
-
-        for id in ids:
-            child_projects = self.get_all_child_projects(cr, uid, [id], context)
-            child_projects = [x for x in child_projects]
-            par_child_projects[id] = child_projects
-            all_projects.extend(child_projects)
-
-        all_projects = dict.fromkeys(all_projects).keys()
         cr.execute('''SELECT
-                project_id, sum(planned_hours), sum(total_hours), sum(effective_hours)
+                project_id, sum(planned_hours), sum(total_hours), sum(effective_hours), SUM(remaining_hours)
             FROM
                 project_task
             WHERE
-                project_id IN %s AND
+                project_id in %s AND
                 state<>'cancelled'
             GROUP BY
-                project_id''',(tuple(all_projects),))
-        progress = dict(map(lambda x: (x[0], (x[1], x[2], x[3])), cr.fetchall()))
-
-        for project in self.browse(cr, uid, par_child_projects.keys(), context=context):
-            s = [0.0, 0.0, 0.0]
-            tocompute = par_child_projects[project.id]
-            while tocompute:
-                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')
-            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)
-
+                project_id''', (tuple(ids),))
+        progress = dict(map(lambda x: (x[0], (x[1],x[2],x[3],x[4])), cr.fetchall()))
+        for project in self.browse(cr, uid, ids, context=context):
+            s = progress.get(project.id, (0.0,0.0,0.0,0.0))
             res[project.id] = {
                 'planned_hours': s[0],
                 'effective_hours': s[2],
                 'total_hours': s[1],
-                'progress_rate': progress_rate
+                'progress_rate': s[1] and round(100.0*s[2]/s[1],2) or 0.0
             }
         return res
 
+    def _get_project_task(self, cr, uid, ids, context=None):
+        result = {}
+        for task in self.pool.get('project.task').browse(cr, uid, ids, context=context):
+            if task.project_id: result[task.project_id.id] = True
+        return result.keys()
+
+    def _get_project_work(self, cr, uid, ids, context=None):
+        result = {}
+        for work in self.pool.get('project.task.work').browse(cr, uid, ids, context=context):
+            if work.task_id and work.task_id.project_id: result[work.task_id.project_id.id] = True
+        return result.keys()
+
     def unlink(self, cr, uid, ids, *args, **kwargs):
         for proj in self.browse(cr, uid, ids):
             if proj.tasks:
@@ -162,38 +120,46 @@ class project(osv.osv):
 
     _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."),
+        'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the project without removing it."),
         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of Projects."),
         'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account', help="Link this project to an analytic account if you need financial management on projects. It enables you to connect projects with budgets, planning, cost and revenue analysis, timesheets on projects, etc.", ondelete="cascade", required=True),
-        'priority': fields.integer('Sequence', help="Gives the sequence order when displaying a list of task"),
+        'priority': fields.integer('Sequence', help="Gives the sequence order when displaying the list of projects"),
         'warn_manager': fields.boolean('Warn Manager', help="If you check this field, the project manager will receive a request each time a task is completed by his team.", states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}),
-        'members': fields.many2many('res.users', 'project_user_rel', 'project_id', 'uid', 'Project Members', help="Project's member. Not used in any computation, just for information purpose.", states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}),
+
+        'members': fields.many2many('res.users', 'project_user_rel', 'project_id', 'uid', 'Project Members',
+            help="Project's members are users who can have an access to the tasks related to this project.", states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}),
         'tasks': fields.one2many('project.task', 'project_id', "Project tasks"),
-        'planned_hours': fields.function(_progress_rate, multi="progress", method=True, string='Planned Time', help="Sum of planned hours of all tasks related to this project and its child projects."),
+        'planned_hours': fields.function(_progress_rate, multi="progress", method=True, string='Planned Time', help="Sum of planned hours of all tasks related to this project and its child projects.",
+            store = {
+                'project.project': (lambda self, cr, uid, ids, c={}: ids, ['tasks'], 10),
+                'project.task': (_get_project_task, ['planned_hours', 'effective_hours', 'remaining_hours', 'total_hours', 'progress', 'delay_hours','state'], 10),
+            }),
         'effective_hours': fields.function(_progress_rate, multi="progress", method=True, string='Time Spent', help="Sum of spent hours of all tasks related to this project and its child projects."),
-        'total_hours': fields.function(_progress_rate, multi="progress", method=True, string='Total Time', help="Sum of total hours of all tasks related to this project and its child projects."),
-        'progress_rate': fields.function(_progress_rate, multi="progress", method=True, string='Progress', type='float', help="Percent of tasks closed according to the total of tasks todo."),
+        'total_hours': fields.function(_progress_rate, multi="progress", method=True, string='Total Time', help="Sum of total hours of all tasks related to this project and its child projects.",
+            store = {
+                'project.project': (lambda self, cr, uid, ids, c={}: ids, ['tasks'], 10),
+                'project.task': (_get_project_task, ['planned_hours', 'effective_hours', 'remaining_hours', 'total_hours', 'progress', 'delay_hours','state'], 10),
+            }),
+        'progress_rate': fields.function(_progress_rate, multi="progress", method=True, string='Progress', type='float', group_operator="avg", help="Percent of tasks closed according to the total of tasks todo."),
         'warn_customer': fields.boolean('Warn Partner', help="If you check this, the user will have a popup when closing a task that propose a message to send by email to the customer.", states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}),
         'warn_header': fields.text('Mail Header', help="Header added at the beginning of the email for the warning message sent to the customer when a task is closed.", states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}),
         'warn_footer': fields.text('Mail Footer', help="Footer added at the beginning of the email for the warning message sent to the customer when a task is closed.", states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}),
         'type_ids': fields.many2many('project.task.type', 'project_task_type_rel', 'project_id', 'type_id', 'Tasks Stages', states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}),
      }
-
     _order = "sequence"
-
     _defaults = {
         'active': True,
         'priority': 1,
         'sequence': 10,
-        'warn_manager': True,
     }
 
-    def _check_dates(self, cr, uid, ids):
-         leave = self.read(cr, uid, ids[0], ['date_start', 'date'])
-         if leave['date_start'] and leave['date']:
-             if leave['date_start'] > leave['date']:
-                 return False
-         return True
+    # TODO: Why not using a SQL contraints ?
+    def _check_dates(self, cr, uid, ids, context=None):
+        for leave in self.read(cr, uid, ids, ['date_start', 'date'], context=context):
+             if leave['date_start'] and leave['date']:
+                 if leave['date_start'] > leave['date']:
+                     return False
+        return True
 
     _constraints = [
         (_check_dates, 'Error! project start-date must be lower then project end-date.', ['date_start', 'date'])
@@ -209,7 +175,7 @@ class project(osv.osv):
         task_obj.write(cr, uid, task_ids, {'state': 'done', 'date_end':time.strftime('%Y-%m-%d %H:%M:%S'), 'remaining_hours': 0.0})
         self.write(cr, uid, ids, {'state':'close'}, context=context)
         for (id, name) in self.name_get(cr, uid, ids):
-            message = _('Project ') + " '" + name + "' "+ _("is Closed.")
+            message = _("The project '%s' has been closed.") % name
             self.log(cr, uid, id, message)
         return True
 
@@ -231,7 +197,7 @@ class project(osv.osv):
     def reset_project(self, cr, uid, ids, context=None):
         res = self.setActive(cr, uid, ids, value=True, context=context)
         for (id, name) in self.name_get(cr, uid, ids):
-            message = _('Project ') + " '" + name + "' "+ _("is Open.")
+            message = _("The project '%s' has been opened.") % name
             self.log(cr, uid, id, message)
         return res
 
@@ -239,23 +205,39 @@ class project(osv.osv):
         if context is None:
             context = {}
 
-        task_obj = self.pool.get('project.task')
-        proj = self.browse(cr, uid, id, context=context)
         default = default or {}
         context['active_test'] = False
         default['state'] = 'open'
+        proj = self.browse(cr, uid, id, context=context)
         if not default.get('name', False):
-            default['name'] = proj.name+_(' (copy)')
+            default['name'] = proj.name + _(' (copy)')
+
         res = super(project, self).copy(cr, uid, id, default, context)
+        return res
+
+
+    def template_copy(self, cr, uid, id, default={}, context=None):
+        task_obj = self.pool.get('project.task')
+        proj = self.browse(cr, uid, id, context=context)
+
+        default['tasks'] = [] #avoid to copy all the task automaticly
+        res = self.copy(cr, uid, id, default=default, context=context)
+
+        #copy all the task manually
+        map_task_id = {}
+        for task in proj.tasks:
+            map_task_id[task.id] =  task_obj.copy(cr, uid, task.id, {}, context=context)
+
+        self.write(cr, uid, res, {'tasks':[(6,0, map_task_id.values())]})
+        task_obj.duplicate_task(cr, uid, map_task_id, context=context)
 
         return res
 
     def duplicate_template(self, cr, uid, ids, context=None):
         if context is None:
             context = {}
-        project_obj = self.pool.get('project.project')
+        task_pool = self.pool.get('project.task')
         data_obj = self.pool.get('ir.model.data')
-        task_obj = self.pool.get('project.task')
         result = []
         for proj in self.browse(cr, uid, ids, context=context):
             parent_id = context.get('parent_id', False)
@@ -266,15 +248,15 @@ class project(osv.osv):
                 start_date = date(*time.strptime(proj.date_start,'%Y-%m-%d')[:3])
                 end_date = date(*time.strptime(proj.date,'%Y-%m-%d')[:3])
                 new_date_end = (datetime(*time.strptime(new_date_start,'%Y-%m-%d')[:3])+(end_date-start_date)).strftime('%Y-%m-%d')
-            new_id = project_obj.copy(cr, uid, proj.id, default = {
+            context.update({'copy':True})
+            new_id = self.template_copy(cr, uid, proj.id, default = {
                                     'name': proj.name +_(' (copy)'),
                                     'state':'open',
                                     'date_start':new_date_start,
                                     'date':new_date_end,
                                     'parent_id':parent_id}, context=context)
             result.append(new_id)
-            cr.execute('select id from project_task where project_id=%s', (proj.id,))
-            res = cr.fetchall()
+
             child_ids = self.search(cr, uid, [('parent_id','=', proj.analytic_account_id.id)], context=context)
             parent_id = self.read(cr, uid, new_id, ['analytic_account_id'])['analytic_account_id'][0]
             if child_ids:
@@ -347,21 +329,11 @@ class task(osv.osv):
 
     # Compute: effective_hours, total_hours, progress
     def _hours_get(self, cr, uid, ids, field_names, args, context=None):
-        project_obj = self.pool.get('project.project')
         res = {}
         cr.execute("SELECT task_id, COALESCE(SUM(hours),0) FROM project_task_work WHERE task_id IN %s GROUP BY task_id",(tuple(ids),))
         hours = dict(cr.fetchall())
-
-        uom_obj = self.pool.get('product.uom')
-        user_uom, default_uom = project_obj._get_user_and_default_uom_ids(cr, uid)
-        if user_uom != default_uom:
-            for task in self.browse(cr, uid, ids, context=context):
-                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] = {'effective_hours': hours.get(task.id, 0.0), 'total_hours': (task.remaining_hours or 0.0) + hours.get(task.id, 0.0)}
             res[task.id]['delay_hours'] = res[task.id]['total_hours'] - task.planned_hours
             res[task.id]['progress'] = 0.0
             if (task.remaining_hours + hours.get(task.id, 0.0)):
@@ -371,9 +343,23 @@ class task(osv.osv):
         return res
 
 
+    def onchange_remaining(self, cr, uid, ids, remaining=0.0, planned = 0.0):
+        if remaining and not planned:
+            return {'value':{'planned_hours': remaining}}
+        return {}
+
     def onchange_planned(self, cr, uid, ids, planned = 0.0, effective = 0.0):
         return {'value':{'remaining_hours': planned - effective}}
 
+    def onchange_project(self, cr, uid, id, project_id):
+        if not project_id:
+            return {}
+        data = self.pool.get('project.project').browse(cr, uid, [project_id])
+        partner_id=data and data[0].parent_id.partner_id
+        if partner_id:
+            return {'value':{'partner_id':partner_id.id}}
+        return {}
+
     def _default_project(self, cr, uid, context=None):
         if context is None:
             context = {}
@@ -381,12 +367,37 @@ class task(osv.osv):
             return int(context['project_id'])
         return False
 
+    def duplicate_task(self, cr, uid, map_ids, context=None):
+        for new in map_ids.values():
+            task = self.browse(cr, uid, new, context)
+            child_ids = [ ch.id for ch in task.child_ids]
+            if task.child_ids:
+                for child in task.child_ids:
+                    if child.id in map_ids.keys():
+                        child_ids.remove(child.id)
+                        child_ids.append(map_ids[child.id])
+
+            parent_ids = [ ch.id for ch in task.parent_ids]
+            if task.parent_ids:
+                for parent in task.parent_ids:
+                    if parent.id in map_ids.keys():
+                        parent_ids.remove(parent.id)
+                        parent_ids.append(map_ids[parent.id])
+            #FIXME why there is already the copy and the old one
+            self.write(cr, uid, new, {'parent_ids':[(6,0,set(parent_ids))], 'child_ids':[(6,0, set(child_ids))]})
+
     def copy_data(self, cr, uid, id, default={}, context=None):
         default = default or {}
         default.update({'work_ids':[], 'date_start': False, 'date_end': False, 'date_deadline': False})
         if not default.get('remaining_hours', False):
             default['remaining_hours'] = float(self.read(cr, uid, id, ['planned_hours'])['planned_hours'])
         default['active'] = True
+        default['type_id'] = False
+        if not default.get('name', False):
+            default['name'] = self.browse(cr, uid, id, context=context).name or ''
+            if not context.get('copy',False):
+                 new_name = _("%s (copy)")%default.get('name','')
+                 default.update({'name':new_name})
         return super(task, self).copy_data(cr, uid, id, default, context)
 
     def _check_dates(self, cr, uid, ids, context=None):
@@ -405,38 +416,59 @@ class task(osv.osv):
                     res[task.id] = False
         return res
 
+    def _get_task(self, cr, uid, ids, context=None):
+        result = {}
+        for work in self.pool.get('project.task.work').browse(cr, uid, ids, context=context):
+            if work.task_id: result[work.task_id.id] = True
+        return result.keys()
+
     _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),
         'description': fields.text('Description'),
-        'priority' : fields.selection([('4','Very Low'), ('3','Low'), ('2','Medium'), ('1','Urgent'), ('0','Very urgent')], 'Importance'),
+        'priority': fields.selection([('4','Very Low'), ('3','Low'), ('2','Medium'), ('1','Important'), ('0','Very important')], 'Priority'),
         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of tasks."),
-        'type_id': fields.many2one('project.task.type', 'Stage',),
+        'type_id': fields.many2one('project.task.type', 'Stage'),
         'state': fields.selection([('draft', 'Draft'),('open', 'In Progress'),('pending', 'Pending'), ('cancelled', 'Cancelled'), ('done', 'Done')], 'State', readonly=True, required=True,
                                   help='If the task is created the state is \'Draft\'.\n If the task is started, the state becomes \'In Progress\'.\n If review is needed the task is in \'Pending\' state.\
                                   \n If the task is over, the states is set to \'Done\'.'),
-        'create_date': fields.datetime('Create Date', readonly=True),
-        'date_start': fields.datetime('Starting Date'),
-        'date_end': fields.datetime('Ending Date'),
-        'date_deadline': fields.date('Deadline'),
-        'project_id': fields.many2one('project.project', 'Project', ondelete='cascade',
-            help="If you have [?] in the project name, it means there are no analytic account linked to this project."),
+        'create_date': fields.datetime('Create Date', readonly=True,select=True),
+        'date_start': fields.datetime('Starting Date',select=True),
+        'date_end': fields.datetime('Ending Date',select=True),
+        'date_deadline': fields.date('Deadline',select=True),
+        'project_id': fields.many2one('project.project', 'Project', ondelete='cascade'),
         'parent_ids': fields.many2many('project.task', 'project_task_parent_rel', 'task_id', 'parent_id', 'Parent Tasks'),
         'child_ids': fields.many2many('project.task', 'project_task_parent_rel', 'parent_id', 'task_id', 'Delegated Tasks'),
         'notes': fields.text('Notes'),
-        'planned_hours': fields.float('Planned Hours', required=True, help='Estimated time to do the task, usually set by the project manager when the task is in draft state.'),
-        'effective_hours': fields.function(_hours_get, method=True, string='Hours Spent', multi='hours', store=True, help="Computed using the sum of the task work done."),
+        'planned_hours': fields.float('Planned Hours', help='Estimated time to do the task, usually set by the project manager when the task is in draft state.'),
+        'effective_hours': fields.function(_hours_get, method=True, string='Hours Spent', multi='hours', help="Computed using the sum of the task work done.",
+            store = {
+                'project.task': (lambda self, cr, uid, ids, c={}: ids, ['work_ids', 'remaining_hours', 'planned_hours'], 10),
+                'project.task.work': (_get_task, ['hours'], 10),
+            }),
         'remaining_hours': fields.float('Remaining Hours', digits=(16,2), help="Total remaining time, can be re-estimated periodically by the assignee of the task."),
-        'total_hours': fields.function(_hours_get, method=True, string='Total Hours', multi='hours', store=True, help="Computed as: Time Spent + Remaining Time."),
-        'progress': fields.function(_hours_get, method=True, string='Progress (%)', multi='hours', store=True, help="Computed as: Time Spent / Total Time."),
-        'delay_hours': fields.function(_hours_get, method=True, string='Delay Hours', multi='hours', store=True, help="Computed as difference of the time estimated by the project manager and the real time to close the task."),
-
+        'total_hours': fields.function(_hours_get, method=True, string='Total Hours', multi='hours', help="Computed as: Time Spent + Remaining Time.",
+            store = {
+                'project.task': (lambda self, cr, uid, ids, c={}: ids, ['work_ids', 'remaining_hours', 'planned_hours'], 10),
+                'project.task.work': (_get_task, ['hours'], 10),
+            }),
+        'progress': fields.function(_hours_get, method=True, string='Progress (%)', multi='hours', group_operator="avg", help="Computed as: Time Spent / Total Time.",
+            store = {
+                'project.task': (lambda self, cr, uid, ids, c={}: ids, ['work_ids', 'remaining_hours', 'planned_hours','state'], 10),
+                'project.task.work': (_get_task, ['hours'], 10),
+            }),
+        'delay_hours': fields.function(_hours_get, method=True, string='Delay Hours', multi='hours', help="Computed as difference of the time estimated by the project manager and the real time to close the task.",
+            store = {
+                'project.task': (lambda self, cr, uid, ids, c={}: ids, ['work_ids', 'remaining_hours', 'planned_hours'], 10),
+                'project.task.work': (_get_task, ['hours'], 10),
+            }),
         'user_id': fields.many2one('res.users', 'Assigned to'),
         'delegated_user_id': fields.related('child_ids', 'user_id', type='many2one', relation='res.users', string='Delegated To'),
         'partner_id': fields.many2one('res.partner', 'Partner'),
         'work_ids': fields.one2many('project.task.work', 'task_id', 'Work done'),
         'manager_id': fields.related('project_id', 'analytic_account_id', 'user_id', type='many2one', relation='res.users', string='Project Manager'),
         'company_id': fields.many2one('res.company', 'Company'),
+        'id': fields.integer('ID'),
     }
 
     _defaults = {
@@ -450,18 +482,64 @@ class task(osv.osv):
         'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'project.task', context=c)
     }
 
-    _order = "sequence, priority, date_start, id"
+    _order = "sequence,priority, date_start, name, id"
+
+    def _check_recursion(self, cr, uid, ids, context=None):
+        for id in ids:
+            visited_branch = set()
+            visited_node = set()
+            res = self._check_cycle(cr, uid, id, visited_branch, visited_node, context=context)
+            if not res:
+                return False
+
+        return True
+
+    def _check_cycle(self, cr, uid, id, visited_branch, visited_node, context=None):
+        if id in visited_branch: #Cycle
+            return False
+
+        if id in visited_node: #Already tested don't work one more time for nothing
+            return True
+
+        visited_branch.add(id)
+        visited_node.add(id)
+
+        #visit child using DFS
+        task = self.browse(cr, uid, id, context=context)
+        for child in task.child_ids:
+            res = self._check_cycle(cr, uid, child.id, visited_branch, visited_node, context=context)
+            if not res:
+                return False
+
+        visited_branch.remove(id)
+        return True
+
+    def _check_dates(self, cr, uid, ids, context=None):
+        if context == None:
+            context = {}
+        obj_task = self.browse(cr, uid, ids[0], context=context)
+        start = obj_task.date_start or False
+        end = obj_task.date_end or False
+        if start and end :
+            if start > end:
+                return False
+        return True
 
     _constraints = [
-        (_check_dates, 'Error! Task start-date must be lower then task end-date.', ['date_start', 'date_end'])
+        (_check_recursion, 'Error ! You cannot create recursive tasks.', ['parent_ids']),
+        (_check_dates, 'Error ! Task end-date must be greater then task start-date', ['date_start','date_end'])
     ]
     #
     # Override view according to the company definition
     #
 
+
     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
+
+        # read uom as admin to avoid access rights issues, e.g. for portal/share users,
+        # this should be safe (no context passed to avoid side-effects)
+        obj_tm = users_obj.browse(cr, 1, uid, context=context).company_id.project_time_mode_id
         tm = obj_tm and obj_tm.name or 'Hours'
 
         res = super(task, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu=submenu)
@@ -487,18 +565,36 @@ class task(osv.osv):
                 res['fields'][f]['string'] = res['fields'][f]['string'].replace('Hours',tm)
         return res
 
-    def do_close(self, cr, uid, ids, *args):
-        mail_send = False
-        mod_obj = self.pool.get('ir.model.data')
+    def action_close(self, cr, uid, ids, context=None):
+        # This action open wizard to send email to partner or project manager after close task.
+        project_id = len(ids) and ids[0] or False
+        if not project_id: return False
+        task = self.browse(cr, uid, project_id, context=context)
+        project = task.project_id
+        res = self.do_close(cr, uid, [project_id], context=context)
+        if project.warn_manager or project.warn_customer:
+           return {
+                'name': _('Send Email after close task'),
+                'view_type': 'form',
+                'view_mode': 'form',
+                'res_model': 'project.task.close',
+                'type': 'ir.actions.act_window',
+                'target': 'new',
+                'nodestroy': True,
+                'context': {'active_id': task.id}
+           }
+        return res
+
+    def do_close(self, cr, uid, ids, context=None):
+        """
+        Close Task
+        """
         request = self.pool.get('res.request')
-        tasks = self.browse(cr, uid, ids)
-        task_id = ids[0]
-        cntx = {}
-        if len(args):
-            cntx = args[0]
-        for task in tasks:
+        for task in self.browse(cr, uid, ids, context=context):
+            vals = {}
             project = task.project_id
             if project:
+                # Send request to project manager
                 if project.warn_manager and project.user_id and (project.user_id.id != uid):
                     request.create(cr, uid, {
                         'name': _("Task '%s' closed") % task.name,
@@ -509,11 +605,6 @@ class task(osv.osv):
                         'ref_doc1': 'project.task,%d'% (task.id,),
                         'ref_doc2': 'project.project,%d'% (project.id,),
                     })
-                elif (project.warn_manager or project.warn_customer) and cntx.get('mail_send',True):
-                    cntx.update({'send_manager': project.warn_manager, 'send_partner': project.warn_customer})
-                    mail_send = True
-            message = _('Task ') + " '" + task.name + "' "+ _("is Done.")
-            self.log(cr, uid, task.id, message)
 
             for parent_id in task.parent_ids:
                 if parent_id.state in ('pending','draft'):
@@ -523,29 +614,19 @@ class task(osv.osv):
                             reopen = False
                     if reopen:
                         self.do_reopen(cr, uid, [parent_id.id])
-        if mail_send:
-            model_data_ids = mod_obj.search(cr,uid,[('model','=','ir.ui.view'),('name','=','view_project_close_task')])
-            resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'])[0]['res_id']
-            cntx.update({'task_id': task_id})
-            return {
-                'name': _('Email Send to Customer'),
-                'view_type': 'form',
-                'context': cntx, # improve me
-                'view_mode': 'tree,form',
-                'res_model': 'close.task',
-                'views': [(resource_id,'form')],
-                'type': 'ir.actions.act_window',
-                'target': 'new',
-                'nodestroy': True
-            }
-        else:
-            self.write(cr, uid, [task_id], {'state': 'done', 'date_end':time.strftime('%Y-%m-%d %H:%M:%S'), 'remaining_hours': 0.0})
-        return False
+            vals.update({'state': 'done'})
+            vals.update({'remaining_hours': 0.0})
+            if not task.date_end:
+                vals.update({ 'date_end':time.strftime('%Y-%m-%d %H:%M:%S')})
+            self.write(cr, uid, [task.id],vals)
+            message = _("The task '%s' is done") % (task.name,)
+            self.log(cr, uid, task.id, message)
+        return True
 
-    def do_reopen(self, cr, uid, ids, *args):
+    def do_reopen(self, cr, uid, ids, context=None):
         request = self.pool.get('res.request')
-        tasks = self.browse(cr, uid, ids)
-        for task in tasks:
+
+        for task in self.browse(cr, uid, ids, context=context):
             project = task.project_id
             if project and project.warn_manager and project.user_id.id and (project.user_id.id != uid):
                 request.create(cr, uid, {
@@ -559,6 +640,7 @@ class task(osv.osv):
                 })
 
             self.write(cr, uid, [task.id], {'state': 'open'})
+
         return True
 
     def do_cancel(self, cr, uid, ids, *args):
@@ -576,7 +658,7 @@ class task(osv.osv):
                     'ref_doc1': 'project.task,%d' % task.id,
                     'ref_doc2': 'project.project,%d' % project.id,
                 })
-            message = _('Task ') + " '" + task.name + "' "+ _("is Cancelled.")
+            message = _("The task '%s' is cancelled.") % (task.name,)
             self.log(cr, uid, task.id, message)
             self.write(cr, uid, [task.id], {'state': 'cancelled', 'remaining_hours':0.0})
         return True
@@ -584,8 +666,11 @@ class task(osv.osv):
     def do_open(self, cr, uid, ids, *args):
         tasks= self.browse(cr,uid,ids)
         for t in tasks:
-            self.write(cr, uid, [t.id], {'state': 'open',  'date_start': time.strftime('%Y-%m-%d %H:%M:%S'),})
-            message = _('Task ') + " '" + t.name + "' "+ _("is Open.")
+            data = {'state': 'open'}
+            if not t.date_start:
+                data['date_start'] = time.strftime('%Y-%m-%d %H:%M:%S')
+            self.write(cr, uid, [t.id], data)
+            message = _("The task '%s' is opened.") % (t.name,)
             self.log(cr, uid, t.id, message)
         return True
 
@@ -593,10 +678,42 @@ class task(osv.osv):
         self.write(cr, uid, ids, {'state': 'draft'})
         return True
 
+    def do_delegate(self, cr, uid, task_id, delegate_data={}, context=None):
+        """
+        Delegate Task to another users.
+        """
+        task = self.browse(cr, uid, task_id, context=context)
+        self.copy(cr, uid, task.id, {
+            'name': delegate_data['name'],
+            'user_id': delegate_data['user_id'],
+            'planned_hours': delegate_data['planned_hours'],
+            'remaining_hours': delegate_data['planned_hours'],
+            'parent_ids': [(6, 0, [task.id])],
+            'state': 'draft',
+            'description': delegate_data['new_task_description'] or '',
+            'child_ids': [],
+            'work_ids': []
+        }, context=context)
+        newname = delegate_data['prefix'] or ''
+        self.write(cr, uid, [task.id], {
+            'remaining_hours': delegate_data['planned_hours_me'],
+            'planned_hours': delegate_data['planned_hours_me'] + (task.effective_hours or 0.0),
+            'name': newname,
+        }, context=context)
+        if delegate_data['state'] == 'pending':
+            self.do_pending(cr, uid, [task.id], context)
+        else:
+            self.do_close(cr, uid, [task.id], context=context)
+        user_pool = self.pool.get('res.users')
+        delegate_user = user_pool.browse(cr, uid, delegate_data['user_id'], context=context)
+        message = _("The task '%s' has been delegated to %s.") % (delegate_data['name'], delegate_user.name)
+        self.log(cr, uid, task.id, message)
+        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):
-            message = _('Task ') + " '" + name + "' "+ _("is Pending.")
+            message = _("The task '%s' is pending.") % name
             self.log(cr, uid, id, message)
         return True
 
@@ -607,7 +724,7 @@ class task(osv.osv):
             if types:
                 if not typeid:
                     self.write(cr, uid, task.id, {'type_id': types[0]})
-                elif typeid and typeid in types and types.index(typeid) != len(types)-1 :
+                elif typeid and typeid in types and types.index(typeid) != len(types)-1:
                     index = types.index(typeid)
                     self.write(cr, uid, task.id, {'type_id': types[index+1]})
         return True
@@ -615,7 +732,7 @@ class task(osv.osv):
     def prev_type(self, cr, uid, ids, *args):
         for task in self.browse(cr, uid, ids):
             typeid = task.type_id.id
-            types = map(lambda x:x.id, task.project_id.type_ids)
+            types = map(lambda x:x.id, task.project_id and task.project_id.type_ids or [])
             if types:
                 if typeid and typeid in types:
                     index = types.index(typeid)
@@ -633,166 +750,36 @@ class project_work(osv.osv):
         'task_id': fields.many2one('project.task', 'Task', ondelete='cascade', required=True),
         'hours': fields.float('Time Spent'),
         'user_id': fields.many2one('res.users', 'Done by', required=True),
-        'company_id': fields.related('task_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True)
+        'company_id': fields.related('task_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True)
     }
 
     _defaults = {
         'user_id': lambda obj, cr, uid, context: uid,
-        'date': time.strftime('%Y-%m-%d %H:%M:%S')
+        'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S')
     }
 
     _order = "date desc"
-
     def create(self, cr, uid, vals, *args, **kwargs):
-        project_obj = self.pool.get('project.project')
-        uom_obj = self.pool.get('product.uom')
-        if vals.get('hours', False):
-            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)
+        if 'hours' in vals and (not vals['hours']):
+            vals['hours'] = 0.00
+        if 'task_id' in vals:
+            cr.execute('update project_task set remaining_hours=remaining_hours - %s where id=%s', (vals.get('hours',0.0), 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 vals.get('hours', False):
-            old_hours = self.browse(cr, uid, ids, context=context)
-            user_uom, default_uom = project_obj._get_user_and_default_uom_ids(cr, uid)
-            duration = vals['hours']
-            for old in old_hours:
-                if vals.get('hours') != old.hours:
-                    # this code is only needed when we update the hours of the project
-                    # TODO: it may still a second calculation if the task.id is changed
-                    # at this task.
-                    if user_uom == default_uom:
-                        for work in self.browse(cr, uid, ids, context=context):
-                            cr.execute('update project_task set remaining_hours=remaining_hours - %s + (%s) where id=%s', (duration, work.hours, work.task_id.id))
-                    else:
-                        for work in self.browse(cr, uid, ids, context=context):
-                            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=context)
+        if 'hours' in vals and (not vals['hours']):
+            vals['hours'] = 0.00
+        if 'hours' in vals:
+            for work in self.browse(cr, uid, ids, context=context):
+                cr.execute('update project_task set remaining_hours=remaining_hours - %s + (%s) where id=%s', (vals.get('hours',0.0), work.hours, 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')
-        uom_obj = self.pool.get('product.uom')
-        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))
-        else:
-            for work in self.browse(cr, uid, ids, context):
-                duration =  uom_obj._compute_qty(cr, uid, default_uom, work.hours, user_uom)
-                cr.execute('update project_task set remaining_hours=remaining_hours + %s where id=%s', (duration, work.task_id.id))
-        return super(project_work, self).unlink(cr, uid, ids, *args, **kwargs)
-
+        for work in self.browse(cr, uid, ids):
+            cr.execute('update project_task set remaining_hours=remaining_hours + %s where id=%s', (work.hours, work.task_id.id))
+        return super(project_work,self).unlink(cr, uid, ids,*args, **kwargs)
 project_work()
 
-class config_compute_remaining(osv.osv_memory):
-    _name='config.compute.remaining'
-
-    def _get_remaining(self,cr, uid, context=None):
-        if context and 'active_id' in context:
-            return self.pool.get('project.task').browse(cr, uid, context['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."),
-        'email':fields.boolean('Email',  help="If True then send a email of assigned user and description"),
-    }
-
-    _defaults = {
-        'remaining_hours': _get_remaining,
-        'email':lambda *a : True,
-    }
-
-    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):
-        data=self.read(cr,uid,ids)[0]
-        if context is None:
-            context = {}
-        task_obj = self.pool.get('project.task')
-        request = self.pool.get('res.request')
-        user_obj=self.pool.get('res.users')
-        if 'active_id' in context:
-            user_name = self.pool.get('res.users').browse(cr, uid, uid).name
-            description = _("Reopen By ") + user_name + _(" At ") + time.strftime('%Y-%m-%d %H:%M:%S')
-            description += "\n" + "=================================" + "\n"      
-            remaining_hrs = self.browse(cr,uid,ids)[0].remaining_hours
-            task_obj.write(cr,uid,context['active_id'],{'remaining_hours':remaining_hrs,'description':description})
-        if context.get('button_reactivate', False):
-            tasks = task_obj.browse(cr, uid, [context['active_id']], context=context)
-            for task in tasks:
-                project = task.project_id
-                if project and project.warn_manager and project.user_id.id and (project.user_id.id != uid):
-                    request.create(cr, uid, {
-                        'name': _("Task '%s' set in progress") % task.name,
-                        'state': 'waiting',
-                        'act_from': uid,
-                        'act_to': project.user_id.id,
-                        'ref_partner_id': task.partner_id.id,
-                        'ref_doc1': 'project.task,%d' % task.id,
-                        'ref_doc2': 'project.project,%d' % project.id,
-                    })
-                task_obj.write(cr, uid, [task.id], {'state': 'open'})
-                if data['email']:
-                    if not task.user_id.user_email: 
-                        raise osv.except_osv(_('Error'), _("Couldn't send mail because email address is not configured!"))
-                    else:
-                        val = {
-                            'name': task.name,
-                            'user_id': task.user_id.name,
-                            'task_id': "%d/%d" % (project.id, task.id),
-                            'state': task.state
-                            }
-                        subject = "Reopen Task '%s' " % task.name
-                        user_email= user_obj.browse(cr, uid, uid).address_id.email 
-                        signature=user_obj.browse(cr, uid, uid).signature                   
-                        header = (project.warn_header or '') % val
-                        footer = (project.warn_footer or '') % val
-                        body = u'%s\n%s\n%s\n\n-- \n%s' % (header, task.description, footer, signature)                        
-                        mail_id = email(user_email,[task.user_id.user_email], subject, body.encode('utf-8'), email_bcc=[user_email])
-                        if not mail_id:
-                            raise osv.except_osv(_('Error'), _("Couldn't send mail! Check the email ids and smtp configuration settings"))
-                    
-        return {
-            'type': 'ir.actions.act_window_close',
-        }
-
-config_compute_remaining()
-
-
 class account_analytic_account(osv.osv):
 
     _inherit = 'account.analytic.account'