[MERGE] project: copy attachments when delegate task
[odoo/odoo.git] / addons / project / project.py
index 45a0406..0046dea 100644 (file)
@@ -25,11 +25,12 @@ from datetime import datetime, date
 
 from tools.translate import _
 from osv import fields, osv
+from resource.faces import task as Task
 
-class project_project(osv.osv):
-    _name = 'project.project'
-    
-project_project()    
+# I think we can remove this in v6.1 since VMT's improvements in the framework ?
+#class project_project(osv.osv):
+#    _name = 'project.project'
+#project_project()
 
 class project_task_type(osv.osv):
     _name = 'project.task.type'
@@ -39,13 +40,13 @@ class project_task_type(osv.osv):
         'name': fields.char('Stage Name', required=True, size=64, translate=True),
         'description': fields.text('Description'),
         'sequence': fields.integer('Sequence'),
+        'project_default': fields.boolean('Common to All Projects', help="If you check this field, this stage will be proposed by default on each new project. It will not assign this stage to existing projects."),
         'project_ids': fields.many2many('project.project', 'project_task_type_rel', 'type_id', 'project_id', 'Projects'),
     }
-
     _defaults = {
         'sequence': 1
     }
-
+    _order = 'sequence'
 project_task_type()
 
 class project(osv.osv):
@@ -56,7 +57,7 @@ class project(osv.osv):
     def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
         if user == 1:
             return super(project, self).search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)
-        if context and context.has_key('user_prefence') and context['user_prefence']:
+        if context and context.get('user_preference'):
                 cr.execute("""SELECT project.id FROM project_project project
                            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
@@ -74,11 +75,14 @@ class project(osv.osv):
     def onchange_partner_id(self, cr, uid, ids, part=False, context=None):
         partner_obj = self.pool.get('res.partner')
         if not part:
-            return {'value':{'contact_id': False, 'pricelist_id': False}}
+            return {'value':{'contact_id': False}}
         addr = partner_obj.address_get(cr, uid, [part], ['contact'])
-        pricelist = partner_obj.read(cr, uid, part, ['property_product_pricelist'], context=context)
-        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}}
+        val = {'contact_id': addr['contact']}
+        if 'pricelist_id' in self.fields_get(cr, uid, context=context):
+            pricelist = partner_obj.read(cr, uid, part, ['property_product_pricelist'], context=context)
+            pricelist_id = pricelist.get('property_product_pricelist', False) and pricelist.get('property_product_pricelist')[0] or False
+            val['pricelist_id'] = pricelist_id
+        return {'value': val}
 
     def _progress_rate(self, cr, uid, ids, names, arg, context=None):
         res = {}.fromkeys(ids, 0.0)
@@ -110,6 +114,7 @@ class project(osv.osv):
             if task.project_id: result[task.project_id.id] = True
         return result.keys()
 
+    #dead code
     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):
@@ -117,15 +122,13 @@ class project(osv.osv):
         return result.keys()
 
     def unlink(self, cr, uid, ids, *args, **kwargs):
-        analytic_obj = self.pool.get('account.analytic.account')
         for proj in self.browse(cr, uid, 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.'))
-            analytic_obj.unlink(cr, uid, [proj.analytic_account_id.id])
+                raise osv.except_osv(_('Operation Not Permitted !'), _('You cannot delete a project containing tasks. I suggest you to desactivate 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),
+        'complete_name': fields.function(_complete_name, string="Project Name", type='char', size=250),
         '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),
@@ -135,28 +138,34 @@ class project(osv.osv):
         '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", 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.",
+        'effective_hours': fields.function(_progress_rate, multi="progress", string='Time Spent', help="Sum of spent hours of all tasks related to this project and its child projects."),
+        'resource_calendar_id': fields.many2one('resource.calendar', 'Working Time', help="Timetable working hours to adjust the gantt diagram report", states={'close':[('readonly',True)]} ),
+        'total_hours': fields.function(_progress_rate, multi="progress", 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."),
+        'progress_rate': fields.function(_progress_rate, multi="progress", 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)]}),
      }
+    def _get_type_common(self, cr, uid, context):
+        ids = self.pool.get('project.task.type').search(cr, uid, [('project_default','=',1)], context=context)
+        return ids
+
     _order = "sequence"
     _defaults = {
         'active': True,
         'priority': 1,
         'sequence': 10,
+        'type_ids': _get_type_common
     }
 
     # TODO: Why not using a SQL contraints ?
@@ -302,6 +311,105 @@ class project(osv.osv):
                 self.setActive(cr, uid, child_ids, value, context=None)
         return True
 
+    def _schedule_header(self, cr, uid, ids, force_members=True, context=None):
+        context = context or {}
+        if type(ids) in (long, int,):
+            ids = [ids]
+        projects = self.browse(cr, uid, ids, context=context)
+
+        for project in projects:
+            if (not project.members) and force_members:
+                raise osv.except_osv(_('Warning !'),_("You must assign members on the project '%s' !") % (project.name,))
+
+        resource_pool = self.pool.get('resource.resource')
+
+        result = "from resource.faces import *\n"
+        result += "import datetime\n"
+        for project in self.browse(cr, uid, ids, context=context):
+            u_ids = [i.id for i in project.members]
+            if project.user_id and (project.user_id.id not in u_ids):
+                u_ids.append(project.user_id.id)
+            for task in project.tasks:
+                if task.state in ('done','cancelled'):
+                    continue
+                if task.user_id and (task.user_id.id not in u_ids):
+                    u_ids.append(task.user_id.id)
+            calendar_id = project.resource_calendar_id and project.resource_calendar_id.id or False
+            resource_objs = resource_pool.generate_resources(cr, uid, u_ids, calendar_id, context=context)
+            for key, vals in resource_objs.items():
+                result +='''
+class User_%s(Resource):
+    efficiency = %s
+''' % (key,  vals.get('efficiency', False))
+
+        result += '''
+def Project():
+        '''
+        return result
+
+    def _schedule_project(self, cr, uid, project, context=None):
+        resource_pool = self.pool.get('resource.resource')
+        calendar_id = project.resource_calendar_id and project.resource_calendar_id.id or False
+        working_days = resource_pool.compute_working_calendar(cr, uid, calendar_id, context=context)
+        # TODO: check if we need working_..., default values are ok.
+        puids = [x.id for x in project.members]
+        if project.user_id:
+            puids.append(project.user_id.id)
+        result = """
+  def Project_%d():
+    start = \'%s\'
+    working_days = %s
+    resource = %s
+"""       % (
+            project.id,
+            project.date_start, working_days,
+            '|'.join(['User_'+str(x) for x in puids])
+        )
+        vacation = calendar_id and tuple(resource_pool.compute_vacation(cr, uid, calendar_id, context=context)) or False
+        if vacation:
+            result+= """
+    vacation = %s
+""" %   ( vacation, )
+        return result
+
+    #TODO: DO Resource allocation and compute availability
+    def compute_allocation(self, rc, uid, ids, start_date, end_date, context=None):
+        if context ==  None:
+            context = {}
+        allocation = {}
+        return allocation
+
+    def schedule_tasks(self, cr, uid, ids, context=None):
+        context = context or {}
+        if type(ids) in (long, int,):
+            ids = [ids]
+        projects = self.browse(cr, uid, ids, context=context)
+        result = self._schedule_header(cr, uid, ids, False, context=context)
+        for project in projects:
+            result += self._schedule_project(cr, uid, project, context=context)
+            result += self.pool.get('project.task')._generate_task(cr, uid, project.tasks, ident=4, context=context)
+
+        local_dict = {}
+        exec result in local_dict
+        projects_gantt = Task.BalancedProject(local_dict['Project'])
+
+        for project in projects:
+            project_gantt = getattr(projects_gantt, 'Project_%d' % (project.id,))
+            for task in project.tasks:
+                if task.state in ('done','cancelled'):
+                    continue
+
+                p = getattr(project_gantt, 'Task_%d' % (task.id,))
+
+                self.pool.get('project.task').write(cr, uid, [task.id], {
+                    'date_start': p.start.strftime('%Y-%m-%d %H:%M:%S'),
+                    'date_end': p.end.strftime('%Y-%m-%d %H:%M:%S')
+                }, context=context)
+                if (not task.user_id) and (p.booked_resource):
+                    self.pool.get('project.task').write(cr, uid, [task.id], {
+                        'user_id': int(p.booked_resource[0].name[5:]),
+                    }, context=context)
+        return True
 project()
 
 class users(osv.osv):
@@ -317,6 +425,62 @@ class task(osv.osv):
     _log_create = True
     _date_name = "date_start"
 
+
+    def _resolve_project_id_from_context(self, cr, uid, context=None):
+        """Return ID of project based on the value of 'project_id'
+           context key, or None if it cannot be resolved to a single project.
+        """
+        if context is None: context = {}
+        if type(context.get('project_id')) in (int, long):
+            project_id = context['project_id']
+            return project_id
+        if isinstance(context.get('project_id'), basestring):
+            project_name = context['project_id']
+            project_ids = self.pool.get('project.project').name_search(cr, uid, name=project_name)
+            if len(project_ids) == 1:
+                return project_ids[0][0]
+
+    def _read_group_type_id(self, cr, uid, ids, domain, read_group_order=None, access_rights_uid=None, context=None):
+        stage_obj = self.pool.get('project.task.type')
+        project_id = self._resolve_project_id_from_context(cr, uid, context=context)
+        order = stage_obj._order
+        access_rights_uid = access_rights_uid or uid
+        if read_group_order == 'type_id desc':
+            # lame way to allow reverting search, should just work in the trivial case
+            order = '%s desc' % order
+        if project_id:
+            domain = ['|', ('id','in',ids), ('project_ids','in',project_id)]
+        else:
+            domain = ['|', ('id','in',ids), ('project_default','=',1)]
+        stage_ids = stage_obj._search(cr, uid, domain, order=order, access_rights_uid=access_rights_uid, context=context)
+        result = stage_obj.name_get(cr, access_rights_uid, stage_ids, context=context)
+        # restore order of the search
+        result.sort(lambda x,y: cmp(stage_ids.index(x[0]), stage_ids.index(y[0])))
+        return result
+
+    def _read_group_user_id(self, cr, uid, ids, domain, read_group_order=None, access_rights_uid=None, context=None):
+        res_users = self.pool.get('res.users')
+        project_id = self._resolve_project_id_from_context(cr, uid, context=context)
+        access_rights_uid = access_rights_uid or uid
+        if project_id:
+            ids += self.pool.get('project.project').read(cr, access_rights_uid, project_id, ['members'], context=context)['members']
+            order = res_users._order
+            # lame way to allow reverting search, should just work in the trivial case
+            if read_group_order == 'user_id desc':
+                order = '%s desc' % order
+            # de-duplicate and apply search order
+            ids = res_users._search(cr, uid, [('id','in',ids)], order=order, access_rights_uid=access_rights_uid, context=context)
+        result = res_users.name_get(cr, access_rights_uid, ids, context=context)
+        # restore order of the search
+        result.sort(lambda x,y: cmp(ids.index(x[0]), ids.index(y[0])))
+        return result
+
+    _group_by_full = {
+        'type_id': _read_group_type_id,
+        'user_id': _read_group_user_id
+    }
+
+
     def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
         obj_project = self.pool.get('project.project')
         for domain in args:
@@ -422,41 +586,47 @@ class task(osv.osv):
         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."),
+        'active': fields.function(_is_template, 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','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'),
-        'state': fields.selection([('draft', 'Draft'),('open', 'In Progress'),('pending', 'Pending'), ('cancelled', 'Cancelled'), ('done', 'Done')], 'State', readonly=True, required=True,
+        'state': fields.selection([('draft', 'New'),('open', 'In Progress'),('pending', 'Pending'), ('done', 'Done'), ('cancelled', 'Cancelled')], '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\'.'),
+        'kanban_state': fields.selection([('normal', 'Normal'),('blocked', 'Blocked'),('done', 'Ready To Pull')], 'Kanban State',
+                                         help="A task's kanban state indicates special situations affecting it:\n"
+                                              " * Normal is the default situation\n"
+                                              " * Blocked indicates something is preventing the progress of this task\n"
+                                              " * Ready To Pull indicates the task is ready to be pulled to the next stage",
+                                         readonly=True, required=False),
         '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'),
+        'project_id': fields.many2one('project.project', 'Project', ondelete='set null', select="1"),
         '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', 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.",
+        'effective_hours': fields.function(_hours_get, 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', help="Computed as: Time Spent + Remaining Time.",
+        'total_hours': fields.function(_hours_get, 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.",
+        'progress': fields.function(_hours_get, string='Progress (%)', multi='hours', group_operator="avg", help="If the task has a progress of 99.99% you should close the task if it's finished or reevaluate the 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.",
+        'delay_hours': fields.function(_hours_get, 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),
@@ -467,11 +637,14 @@ class task(osv.osv):
         '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'),
+        'id': fields.integer('ID', readonly=True),
+        'color': fields.integer('Color Index'),
+        'user_email': fields.related('user_id', 'user_email', type='char', string='User Email', readonly=True),
     }
 
     _defaults = {
         'state': 'draft',
+        'kanban_state': 'normal',
         'priority': '2',
         'progress': 0,
         'sequence': 10,
@@ -481,7 +654,22 @@ 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, name, id"
+    _order = "priority, sequence, date_start, name, id"
+
+    def set_priority(self, cr, uid, ids, priority):
+        """Set task priority
+        """
+        return self.write(cr, uid, ids, {'priority' : priority})
+
+    def set_high_priority(self, cr, uid, ids, *args):
+        """Set task priority to high
+        """
+        return self.set_priority(cr, uid, ids, '1')
+
+    def set_normal_priority(self, cr, uid, ids, *args):
+        """Set task priority to normal
+        """
+        return self.set_priority(cr, uid, ids, '3')
 
     def _check_recursion(self, cr, uid, ids, context=None):
         for id in ids:
@@ -531,8 +719,6 @@ class task(osv.osv):
     #
     # 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')
 
@@ -564,31 +750,47 @@ class task(osv.osv):
                 res['fields'][f]['string'] = res['fields'][f]['string'].replace('Hours',tm)
         return res
 
+    def _check_child_task(self, cr, uid, ids, context=None):
+        if context == None:
+            context = {}
+        tasks = self.browse(cr, uid, ids, context=context)
+        for task in tasks:
+            if task.child_ids:
+                for child in task.child_ids:
+                    if child.state in ['draft', 'open', 'pending']:
+                        raise osv.except_osv(_("Warning !"), _("Child task still open.\nPlease cancel or complete child task first."))
+        return True
+
     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)
+        if context == None:
+            context = {}
+        task_id = len(ids) and ids[0] or False
+        self._check_child_task(cr, uid, ids, context=context)
+        if not task_id: return False
+        task = self.browse(cr, uid, task_id, context=context)
         project = task.project_id
-        res = self.do_close(cr, uid, [project_id], context=context)
+        res = self.do_close(cr, uid, [task_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',
+                'res_model': 'mail.compose.message',
                 'type': 'ir.actions.act_window',
                 'target': 'new',
                 'nodestroy': True,
-                'context': {'active_id': task.id}
+                'context': {'active_id': task.id,
+                            'active_model': 'project.task'}
            }
         return res
 
-    def do_close(self, cr, uid, ids, context=None):
+    def do_close(self, cr, uid, ids, context={}):
         """
         Close Task
         """
         request = self.pool.get('res.request')
+        if not isinstance(ids,list): ids = [ids]
         for task in self.browse(cr, uid, ids, context=context):
             vals = {}
             project = task.project_id
@@ -603,7 +805,7 @@ class task(osv.osv):
                         'ref_partner_id': task.partner_id.id,
                         'ref_doc1': 'project.task,%d'% (task.id,),
                         'ref_doc2': 'project.project,%d'% (project.id,),
-                    })
+                    }, context=context)
 
             for parent_id in task.parent_ids:
                 if parent_id.state in ('pending','draft'):
@@ -612,12 +814,12 @@ class task(osv.osv):
                         if child.id != task.id and child.state not in ('done','cancelled'):
                             reopen = False
                     if reopen:
-                        self.do_reopen(cr, uid, [parent_id.id])
+                        self.do_reopen(cr, uid, [parent_id.id], context=context)
             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)
+            self.write(cr, uid, [task.id],vals, context=context)
             message = _("The task '%s' is done") % (task.name,)
             self.log(cr, uid, task.id, message)
         return True
@@ -636,15 +838,15 @@ class task(osv.osv):
                     'ref_partner_id': task.partner_id.id,
                     'ref_doc1': 'project.task,%d' % task.id,
                     'ref_doc2': 'project.project,%d' % project.id,
-                })
-
-            self.write(cr, uid, [task.id], {'state': 'open'})
+                }, context=context)
 
+            self.write(cr, uid, [task.id], {'state': 'open'}, context=context)
         return True
 
-    def do_cancel(self, cr, uid, ids, *args):
+    def do_cancel(self, cr, uid, ids, context={}):
         request = self.pool.get('res.request')
-        tasks = self.browse(cr, uid, ids)
+        tasks = self.browse(cr, uid, ids, context=context)
+        self._check_child_task(cr, uid, ids, context=context)
         for task in tasks:
             project = task.project_id
             if project.warn_manager and project.user_id and (project.user_id.id != uid):
@@ -656,88 +858,208 @@ class task(osv.osv):
                     'ref_partner_id': task.partner_id.id,
                     'ref_doc1': 'project.task,%d' % task.id,
                     'ref_doc2': 'project.project,%d' % project.id,
-                })
+                }, context=context)
             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})
+            self.write(cr, uid, [task.id], {'state': 'cancelled', 'remaining_hours':0.0}, context=context)
         return True
 
-    def do_open(self, cr, uid, ids, *args):
-        tasks= self.browse(cr,uid,ids)
+    def do_open(self, cr, uid, ids, context={}):
+        if not isinstance(ids,list): ids = [ids]
+        tasks= self.browse(cr, uid, ids, context=context)
         for t in tasks:
             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)
+            self.write(cr, uid, [t.id], data, context=context)
             message = _("The task '%s' is opened.") % (t.name,)
             self.log(cr, uid, t.id, message)
         return True
 
-    def do_draft(self, cr, uid, ids, *args):
-        self.write(cr, uid, ids, {'state': 'draft'})
+    def do_draft(self, cr, uid, ids, context={}):
+        self.write(cr, uid, ids, {'state': 'draft'}, context=context)
         return True
 
-    def do_delegate(self, cr, uid, task_id, delegate_data={}, context=None):
+
+    def _delegate_task_attachments(self, cr, uid, task_id, delegated_task_id, context=None):
+        attachment = self.pool.get('ir.attachment')
+        attachment_ids = attachment.search(cr, uid, [('res_model', '=', self._name), ('res_id', '=', task_id)], context=context)
+        new_attachment_ids = []
+        for attachment_id in attachment_ids:
+            new_attachment_ids.append(attachment.copy(cr, uid, attachment_id, default={'res_id': delegated_task_id}, context=context))
+        return new_attachment_ids
+        
+
+    def do_delegate(self, cr, uid, ids, 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
+        assert delegate_data['user_id'], _("Delegated User should be specified")
+        delegated_tasks = {}
+        for task in self.browse(cr, uid, ids, context=context):
+            delegated_task_id = self.copy(cr, uid, task.id, {
+                'name': delegate_data['name'],
+                'project_id': delegate_data['project_id'] and delegate_data['project_id'][0] or False,
+                'user_id': delegate_data['user_id'] and delegate_data['user_id'][0] or False,
+                'planned_hours': delegate_data['planned_hours'] or 0.0,
+                'parent_ids': [(6, 0, [task.id])],
+                'state': 'draft',
+                'description': delegate_data['new_task_description'] or '',
+                'child_ids': [],
+                'work_ids': []
+            }, context=context)
+            self._delegate_task_attachments(cr, uid, task.id, delegated_task_id, context=context)
+            newname = delegate_data['prefix'] or ''
+            task.write({
+                '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=context)
+            elif delegate_data['state'] == 'done':
+                self.do_close(cr, uid, task.id, context=context)
+            
+            message = _("The task '%s' has been delegated to %s.") % (delegate_data['name'], delegate_data['user_id'][1])
+            self.log(cr, uid, task.id, message)
+            delegated_tasks[task.id] = delegated_task_id
+        return delegated_tasks
 
-    def do_pending(self, cr, uid, ids, *args):
-        self.write(cr, uid, ids, {'state': 'pending'})
+    def do_pending(self, cr, uid, ids, context={}):
+        self.write(cr, uid, ids, {'state': 'pending'}, context=context)
         for (id, name) in self.name_get(cr, uid, ids):
             message = _("The task '%s' is pending.") % name
             self.log(cr, uid, id, message)
         return True
 
-    def next_type(self, cr, uid, ids, *args):
+    def set_remaining_time(self, cr, uid, ids, remaining_time=1.0, context=None):
+        for task in self.browse(cr, uid, ids, context=context):
+            if (task.state=='draft') or (task.planned_hours==0.0):
+                self.write(cr, uid, [task.id], {'planned_hours': remaining_time}, context=context)
+        self.write(cr, uid, ids, {'remaining_hours': remaining_time}, context=context)
+        return True
+
+    def set_remaining_time_1(self, cr, uid, ids, context=None):
+        return self.set_remaining_time(cr, uid, ids, 1.0, context)
+
+    def set_remaining_time_2(self, cr, uid, ids, context=None):
+        return self.set_remaining_time(cr, uid, ids, 2.0, context)
+
+    def set_remaining_time_5(self, cr, uid, ids, context=None):
+        return self.set_remaining_time(cr, uid, ids, 5.0, context)
+
+    def set_remaining_time_10(self, cr, uid, ids, context=None):
+        return self.set_remaining_time(cr, uid, ids, 10.0, context)
+
+    def set_kanban_state_blocked(self, cr, uid, ids, context=None):
+        self.write(cr, uid, ids, {'kanban_state': 'blocked'}, context=context)
+
+    def set_kanban_state_normal(self, cr, uid, ids, context=None):
+        self.write(cr, uid, ids, {'kanban_state': 'normal'}, context=context)
+
+    def set_kanban_state_done(self, cr, uid, ids, context=None):
+        self.write(cr, uid, ids, {'kanban_state': 'done'}, context=context)
+
+    def _change_type(self, cr, uid, ids, next, *args):
+        """
+            go to the next stage
+            if next is False, go to previous stage
+        """
         for task in self.browse(cr, uid, ids):
-            typeid = task.type_id.id
-            types = map(lambda x:x.id, task.project_id.type_ids or [])
-            if types:
+            if  task.project_id.type_ids:
+                typeid = task.type_id.id
+                types_seq={}
+                for type in task.project_id.type_ids :
+                    types_seq[type.id] = type.sequence
+                if next:
+                    types = sorted(types_seq.items(), lambda x, y: cmp(x[1], y[1]))
+                else:
+                    types = sorted(types_seq.items(), lambda x, y: cmp(y[1], x[1]))
+                sorted_types = [x[0] for x in 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:
-                    index = types.index(typeid)
-                    self.write(cr, uid, task.id, {'type_id': types[index+1]})
+                    self.write(cr, uid, task.id, {'type_id': sorted_types[0]})
+                elif typeid and typeid in sorted_types and sorted_types.index(typeid) != len(sorted_types)-1:
+                    index = sorted_types.index(typeid)
+                    self.write(cr, uid, task.id, {'type_id': sorted_types[index+1]})
         return True
 
+    def next_type(self, cr, uid, ids, *args):
+        return self._change_type(cr, uid, ids, True, *args)
+
     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 and task.project_id.type_ids or [])
-            if types:
-                if typeid and typeid in types:
-                    index = types.index(typeid)
-                    self.write(cr, uid, task.id, {'type_id': index and types[index-1] or False})
+        return self._change_type(cr, uid, ids, False, *args)
+
+    def _store_history(self, cr, uid, ids, context=None):
+        for task in self.browse(cr, uid, ids, context=context):
+            self.pool.get('project.task.history').create(cr, uid, {
+                'task_id': task.id,
+                'remaining_hours': task.remaining_hours,
+                'planned_hours': task.planned_hours,
+                'kanban_state': task.kanban_state,
+                'type_id': task.type_id.id,
+                'state': task.state,
+                'user_id': task.user_id.id
+
+            }, context=context)
         return True
 
+    def create(self, cr, uid, vals, context=None):
+        result = super(task, self).create(cr, uid, vals, context=context)
+        self._store_history(cr, uid, [result], context=context)
+        return result
+
+    # Overridden to reset the kanban_state to normal whenever
+    # the stage (type_id) of the task changes.
+    def write(self, cr, uid, ids, vals, context=None):
+        if isinstance(ids, (int, long)):
+            ids = [ids]
+        if vals and not 'kanban_state' in vals and 'type_id' in vals:
+            new_stage = vals.get('type_id')
+            vals_reset_kstate = dict(vals, kanban_state='normal')
+            for t in self.browse(cr, uid, ids, context=context):
+                write_vals = vals_reset_kstate if t.type_id != new_stage else vals 
+                super(task,self).write(cr, uid, [t.id], write_vals, context=context)
+            result = True
+        else:
+            result = super(task,self).write(cr, uid, ids, vals, context=context)
+        if ('type_id' in vals) or ('remaining_hours' in vals) or ('user_id' in vals) or ('state' in vals) or ('kanban_state' in vals):
+            self._store_history(cr, uid, ids, context=context)
+        return result
+
+    def unlink(self, cr, uid, ids, context=None):
+        if context == None:
+            context = {}
+        self._check_child_task(cr, uid, ids, context=context)
+        res = super(task, self).unlink(cr, uid, ids, context)
+        return res
+
+    def _generate_task(self, cr, uid, tasks, ident=4, context=None):
+        context = context or {}
+        result = ""
+        ident = ' '*ident
+        for task in tasks:
+            if task.state in ('done','cancelled'):
+                continue
+            result += '''
+%sdef Task_%s():
+%s  todo = \"%.2fH\"
+%s  effort = \"%.2fH\"''' % (ident,task.id, ident,task.remaining_hours, ident,task.total_hours)
+            start = []
+            for t2 in task.parent_ids:
+                start.append("up.Task_%s.end" % (t2.id,))
+            if start:
+                result += '''
+%s  start = max(%s)
+''' % (ident,','.join(start))
+
+            if task.user_id:
+                result += '''
+%s  resource = %s
+''' % (ident, 'User_'+str(task.user_id.id))
+
+        result += "\n"
+        return result
+
 task()
 
 class project_work(osv.osv):
@@ -745,10 +1067,10 @@ class project_work(osv.osv):
     _description = "Project Task Work"
     _columns = {
         'name': fields.char('Work summary', size=128),
-        'date': fields.datetime('Date'),
-        'task_id': fields.many2one('project.task', 'Task', ondelete='cascade', required=True),
+        'date': fields.datetime('Date', select="1"),
+        'task_id': fields.many2one('project.task', 'Task', ondelete='cascade', required=True, select="1"),
         'hours': fields.float('Time Spent'),
-        'user_id': fields.many2one('res.users', 'Done by', required=True),
+        'user_id': fields.many2one('res.users', 'Done by', required=True, select="1"),
         'company_id': fields.related('task_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True)
     }
 
@@ -791,6 +1113,101 @@ class account_analytic_account(osv.osv):
             vals['child_ids'] = []
         return super(account_analytic_account, self).create(cr, uid, vals, context=context)
 
+    def unlink(self, cr, uid, ids, *args, **kwargs):
+        project_obj = self.pool.get('project.project')
+        analytic_ids = project_obj.search(cr, uid, [('analytic_account_id','in',ids)])
+        if analytic_ids:
+            raise osv.except_osv(_('Warning !'), _('Please delete the project linked with this account first.'))
+        return super(account_analytic_account, self).unlink(cr, uid, ids, *args, **kwargs)
+
 account_analytic_account()
 
-# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
+#
+# Tasks History, used for cumulative flow charts (Lean/Agile)
+#
+
+class project_task_history(osv.osv):
+    _name = 'project.task.history'
+    _description = 'History of Tasks'
+    _rec_name = 'task_id'
+    _log_access = False
+    def _get_date(self, cr, uid, ids, name, arg, context=None):
+        result = {}
+        for history in self.browse(cr, uid, ids, context=context):
+            if history.state in ('done','cancelled'):
+                result[history.id] = history.date
+                continue
+            cr.execute('''select
+                    date
+                from
+                    project_task_history
+                where
+                    task_id=%s and
+                    id>%s
+                order by id limit 1''', (history.task_id.id, history.id))
+            res = cr.fetchone()
+            result[history.id] = res and res[0] or False
+        return result
+
+    def _get_related_date(self, cr, uid, ids, context=None):
+        result = []
+        for history in self.browse(cr, uid, ids, context=context):
+            cr.execute('''select
+                    id
+                from 
+                    project_task_history
+                where
+                    task_id=%s and
+                    id<%s
+                order by id desc limit 1''', (history.task_id.id, history.id))
+            res = cr.fetchone()
+            if res:
+                result.append(res[0])
+        return result
+
+    _columns = {
+        'task_id': fields.many2one('project.task', 'Task', ondelete='cascade', required=True, select=True),
+        'type_id': fields.many2one('project.task.type', 'Stage'),
+        'state': fields.selection([('draft', 'New'),('open', 'In Progress'),('pending', 'Pending'), ('done', 'Done'), ('cancelled', 'Cancelled')], 'State'),
+        'kanban_state': fields.selection([('normal', 'Normal'),('blocked', 'Blocked'),('done', 'Ready To Pull')], 'Kanban State', required=False),
+        'date': fields.date('Date', select=True),
+        'end_date': fields.function(_get_date, string='End Date', type="date", store={
+            'project.task.history': (_get_related_date, None, 20)
+        }),
+        'remaining_hours': fields.float('Remaining Time', digits=(16,2)),
+        'planned_hours': fields.float('Planned Time', digits=(16,2)),
+        'user_id': fields.many2one('res.users', 'Responsible'),
+    }
+    _defaults = {
+        'date': lambda s,c,u,ctx: time.strftime('%Y-%m-%d')
+    }
+project_task_history()
+
+class project_task_history_cumulative(osv.osv):
+    _name = 'project.task.history.cumulative'
+    _table = 'project_task_history_cumulative'
+    _inherit = 'project.task.history'
+    _auto = False
+    _columns = {
+        'end_date': fields.date('End Date'),
+        'project_id': fields.related('task_id', 'project_id', string='Project', type='many2one', relation='project.project')
+    }
+    def init(self, cr):
+        cr.execute(""" CREATE OR REPLACE VIEW project_task_history_cumulative AS (
+            SELECT
+                history.date::varchar||'-'||history.history_id::varchar as id,
+                history.date as end_date,
+                *
+            FROM (
+                SELECT
+                    id as history_id,
+                    date+generate_series(0, CAST((coalesce(end_date,DATE 'tomorrow')::date - date)AS integer)-1) as date,
+                    task_id, type_id, user_id, kanban_state, state,
+                    remaining_hours, planned_hours
+                FROM
+                    project_task_history
+            ) as history
+        )
+        """)
+project_task_history_cumulative()
+