Hardik Ansodariya (OpenERP) has proposed merging 
lp:~openerp-dev/openobject-addons/6.1-opw-574768-han into 
lp:openobject-addons/6.1.

Requested reviews:
  Naresh(OpenERP) (nch-openerp)
Related bugs:
  Bug #997656 in OpenERP Addons: "Multiplication of tasks when using template 
and phases"
  https://bugs.launchpad.net/openobject-addons/+bug/997656

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-addons/6.1-opw-574768-han/+merge/107713

Hello,

Fixed the issue of Multiplication of tasks when using template and phases.

With reference of Maintanence case- 574768 and LP bug #997656

Steps:

when we define a project template that contains phases and tasks linked to the 
phases and we create a new project based on this templace, the number of tasks 
is multiplied.

Step to reproduce:
- Create a project & save it as a template
- Define phases for this project
- Define tasks for this project and link them to a phase

- Create a new project based on the template
- Create another project still based on the templace

--> You have two times more tasks then needed

- Create a new project based on the templace

--> Now you have 3times more tasks then needed.

The tasks that are duplicated stay linked to the 'template' phase. So when we 
duplicate them, it creates as many tasks as we created project based on the 
template.


Kindly review it.

Thanks
-- 
https://code.launchpad.net/~openerp-dev/openobject-addons/6.1-opw-574768-han/+merge/107713
Your team OpenERP R&D Team is subscribed to branch 
lp:~openerp-dev/openobject-addons/6.1-opw-574768-han.
=== modified file 'project_long_term/project_long_term.py'
--- project_long_term/project_long_term.py	2012-01-31 13:36:57 +0000
+++ project_long_term/project_long_term.py	2012-05-29 05:37:19 +0000
@@ -198,6 +198,25 @@
             result += "\n"
 
         return result
+
+    def duplicate_phase(self, cr, uid, map_ids, context=None):
+        for new in map_ids.values():
+            phase = self.browse(cr, uid, new, context)
+            next_phase_ids = [ next.id for next in phase.next_phase_ids]
+            if phase.next_phase_ids:
+                for next in phase.next_phase_ids:
+                    if next.id in map_ids.keys():
+                        next_phase_ids.remove(next.id)
+                        next_phase_ids.append(map_ids[next.id])
+
+            previous_phase_ids = [ previous.id for previous in phase.previous_phase_ids]
+            if phase.previous_phase_ids:
+                for previous in phase.previous_phase_ids:
+                    if previous.id in map_ids.keys():
+                        previous_phase_ids.remove(previous.id)
+                        previous_phase_ids.append(map_ids[previous.id])
+            self.write(cr, uid, new, {'previous_phase_ids':[(6,0,set(previous_phase_ids))], 'next_phase_ids':[(6,0, set(next_phase_ids))]})
+
 project_phase()
 
 class project_user_allocation(osv.osv):
@@ -240,7 +259,7 @@
                 # Maybe it's better to update than unlink/create if it already exists ?
                 p = getattr(project_gantt, 'Phase_%d' % (phase.id,))
 
-                self.pool.get('project.user.allocation').unlink(cr, uid, 
+                self.pool.get('project.user.allocation').unlink(cr, uid,
                     [x.id for x in phase.user_ids],
                     context=context
                 )
@@ -257,6 +276,50 @@
                     'date_end': p.end.strftime('%Y-%m-%d %H:%M:%S')
                 }, context=context)
         return True
+
+    def map_tasks(self, cr, uid, old_project_id, new_project_id, context=None):
+        map_task_id = {}
+        default = {}
+        task_obj = self.pool.get('project.task')
+        proj = self.browse(cr, uid, old_project_id, context=context)
+        for task in proj.tasks:
+            if not task.phase_id:
+                map_task_id[task.id] =  task_obj.copy(cr, uid, task.id, default, context=context)
+        self.write(cr, uid, [new_project_id], {'tasks':[(6,0, map_task_id.values())]})
+        task_obj.duplicate_task(cr, uid, map_task_id, context=context)
+        return True
+
+
+    def map_phase(self, cr, uid, old_project_id, new_project_id,context=None):
+        """ copy and map phases to new project """
+        if context is None:
+            context = {}
+        phase_ids=[]
+        default = {}
+        map_phase_id = {}
+        default['task_ids'] = []
+        proj = self.browse(cr, uid, old_project_id, context=context)
+        phase_obj = self.pool.get('project.phase')
+        task_obj = self.pool.get('project.task')
+        for phase_id in proj.phase_ids:
+            default['name'] = False
+            map_phase_id[phase_id.id] =  phase_obj.copy(cr, uid, phase_id.id, default, context=context)
+        self.write(cr, uid, [new_project_id], {'phase_ids':[(6,0, map_phase_id.values())]})
+        phase_obj.duplicate_phase(cr, uid, map_phase_id, context=context)
+        for task in proj.tasks:
+            if task.phase_id:
+                default = {}
+                default['project_id'] = new_project_id
+                default['phase_id'] = map_phase_id.get(task.phase_id.id)
+                task_obj.copy(cr, uid, task.id, default, context=context)
+        return True
+
+    def copy(self, cr, uid, id, default={}, context=None):
+        default['phase_ids'] = []
+        res = super(project, self).copy(cr, uid, id, default, context)
+        self.map_phase(cr,uid,id,res,context)
+        return res
+
 project()
 
 class project_task(osv.osv):

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to