[MERGE] lp881356
[odoo/odoo.git] / addons / project_scrum / project_scrum.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21 from osv import fields, osv
22 from tools.translate import _
23 import re
24 import time
25 import tools
26 from datetime import datetime
27 from dateutil.relativedelta import relativedelta
28
29 class project_scrum_project(osv.osv):
30     _inherit = 'project.project'
31     _columns = {
32         'product_owner_id': fields.many2one('res.users', 'Product Owner', help="The person who is responsible for the product"),
33         'sprint_size': fields.integer('Sprint Days', help="Number of days allocated for sprint"),
34         'scrum': fields.integer('Is a Scrum Project'),
35     }
36     _defaults = {
37         'product_owner_id': lambda self, cr, uid, context={}: uid,
38         'sprint_size': 15,
39         'scrum': 1
40     }
41 project_scrum_project()
42
43 class project_scrum_sprint(osv.osv):
44     _name = 'project.scrum.sprint'
45     _description = 'Project Scrum Sprint'
46     _order = 'date_start desc'
47     def _compute(self, cr, uid, ids, fields, arg, context=None):
48         res = {}.fromkeys(ids, 0.0)
49         progress = {}
50         if not ids:
51             return res
52         if context is None:
53             context = {}
54         for sprint in self.browse(cr, uid, ids, context=context):
55             tot = 0.0
56             prog = 0.0
57             effective = 0.0
58             progress = 0.0
59             for bl in sprint.backlog_ids:
60                 tot += bl.expected_hours
61                 effective += bl.effective_hours
62                 prog += bl.expected_hours * bl.progress / 100.0
63             if tot>0:
64                 progress = round(prog/tot*100)
65             res[sprint.id] = {
66                 'progress' : progress,
67                 'expected_hours' : tot,
68                 'effective_hours': effective,
69             }
70         return res
71
72     def button_cancel(self, cr, uid, ids, context=None):
73         self.write(cr, uid, ids, {'state':'cancel'}, context=context)
74         return True
75
76     def button_draft(self, cr, uid, ids, context=None):
77         self.write(cr, uid, ids, {'state':'draft'}, context=context)
78         return True
79
80     def button_open(self, cr, uid, ids, context=None):
81         self.write(cr, uid, ids, {'state':'open'}, context=context)
82         for (id, name) in self.name_get(cr, uid, ids):
83             message = _("The sprint '%s' has been opened.") % (name,)
84             self.log(cr, uid, id, message)
85         return True
86
87     def button_close(self, cr, uid, ids, context=None):
88         self.write(cr, uid, ids, {'state':'done'}, context=context)
89         for (id, name) in self.name_get(cr, uid, ids):
90             message = _("The sprint '%s' has been closed.") % (name,)
91             self.log(cr, uid, id, message)
92         return True
93
94     def button_pending(self, cr, uid, ids, context=None):
95         self.write(cr, uid, ids, {'state':'pending'}, context=context)
96         return True
97
98     _columns = {
99         'name' : fields.char('Sprint Name', required=True, size=64),
100         'date_start': fields.date('Starting Date', required=True),
101         'date_stop': fields.date('Ending Date', required=True),
102         'project_id': fields.many2one('project.project', 'Project', required=True, domain=[('scrum','=',1)], help="If you have [?] in the project name, it means there are no analytic account linked to this project."),
103         'product_owner_id': fields.many2one('res.users', 'Product Owner', required=True,help="The person who is responsible for the product"),
104         'scrum_master_id': fields.many2one('res.users', 'Scrum Master', required=True,help="The person who is maintains the processes for the product"),
105         'meeting_ids': fields.one2many('project.scrum.meeting', 'sprint_id', 'Daily Scrum'),
106         'review': fields.text('Sprint Review'),
107         'retrospective': fields.text('Sprint Retrospective'),
108         'backlog_ids': fields.one2many('project.scrum.product.backlog', 'sprint_id', 'Sprint Backlog'),
109         'progress': fields.function(_compute, group_operator="avg", type='float', multi="progress", string='Progress (0-100)', help="Computed as: Time Spent / Total Time."),
110         'effective_hours': fields.function(_compute, multi="effective_hours", string='Effective hours', help="Computed using the sum of the task work done."),
111         'expected_hours': fields.function(_compute, multi="expected_hours", string='Planned Hours', help='Estimated time to do the task.'),
112         'state': fields.selection([('draft','Draft'),('open','Open'),('pending','Pending'),('cancel','Cancelled'),('done','Done')], 'State', required=True),
113     }
114     _defaults = {
115         'state': 'draft',
116         'date_start' : lambda *a: time.strftime('%Y-%m-%d'),
117     }
118
119     def copy(self, cr, uid, id, default=None, context=None):
120         """Overrides orm copy method
121         @param self: The object pointer
122         @param cr: the current row, from the database cursor,
123         @param uid: the current user’s ID for security checks,
124         @param ids: List of case’s IDs
125         @param context: A standard dictionary for contextual values
126         """
127         if default is None:
128             default = {}
129         default.update({'backlog_ids': [], 'meeting_ids': []})
130         return super(project_scrum_sprint, self).copy(cr, uid, id, default=default, context=context)
131
132     def onchange_project_id(self, cr, uid, ids, project_id=False):
133         v = {}
134         if project_id:
135             proj = self.pool.get('project.project').browse(cr, uid, [project_id])[0]
136             v['product_owner_id']= proj.product_owner_id and proj.product_owner_id.id or False
137             v['scrum_master_id']= proj.user_id and proj.user_id.id or False
138             v['date_stop'] = (datetime.now() + relativedelta(days=int(proj.sprint_size or 14))).strftime('%Y-%m-%d')
139         return {'value':v}
140
141 project_scrum_sprint()
142
143 class project_scrum_product_backlog(osv.osv):
144     _name = 'project.scrum.product.backlog'
145     _description = 'Product Backlog'
146
147     def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
148         if not args:
149             args=[]
150         if name:
151             match = re.match('^S\(([0-9]+)\)$', name)
152             if match:
153                 ids = self.search(cr, uid, [('sprint_id','=', int(match.group(1)))], limit=limit, context=context)
154                 return self.name_get(cr, uid, ids, context=context)
155         return super(project_scrum_product_backlog, self).name_search(cr, uid, name, args, operator,context, limit=limit)
156
157     def _compute(self, cr, uid, ids, fields, arg, context=None):
158         res = {}.fromkeys(ids, 0.0)
159         progress = {}
160         if not ids:
161             return res
162         for backlog in self.browse(cr, uid, ids, context=context):
163             tot = 0.0
164             prog = 0.0
165             effective = 0.0
166             task_hours = 0.0
167             progress = 0.0
168             for task in backlog.tasks_id:
169                 task_hours += task.total_hours
170                 effective += task.effective_hours
171                 tot += task.planned_hours
172                 prog += task.planned_hours * task.progress / 100.0
173             if tot>0:
174                 progress = round(prog/tot*100)
175             res[backlog.id] = {
176                 'progress' : progress,
177                 'effective_hours': effective,
178                 'task_hours' : task_hours
179             }
180         return res
181
182     def button_cancel(self, cr, uid, ids, context=None):
183         obj_project_task = self.pool.get('project.task')
184         self.write(cr, uid, ids, {'state':'cancel'}, context=context)
185         for backlog in self.browse(cr, uid, ids, context=context):
186             obj_project_task.write(cr, uid, [i.id for i in backlog.tasks_id], {'state': 'cancelled'})
187         return True
188
189     def button_draft(self, cr, uid, ids, context=None):
190         self.write(cr, uid, ids, {'state':'draft'}, context=context)
191         return True
192
193     def button_open(self, cr, uid, ids, context=None):
194         self.write(cr, uid, ids, {'state':'open'}, context=context)
195         return True
196
197     def button_close(self, cr, uid, ids, context=None):
198         obj_project_task = self.pool.get('project.task')
199         self.write(cr, uid, ids, {'state':'done'}, context=context)
200         for backlog in self.browse(cr, uid, ids, context=context):
201             obj_project_task.write(cr, uid, [i.id for i in backlog.tasks_id], {'state': 'done'})
202         return True
203
204     def button_pending(self, cr, uid, ids, context=None):
205         self.write(cr, uid, ids, {'state':'pending'}, context=context)
206         return True
207
208     def button_postpone(self, cr, uid, ids, context=None):
209         for product in self.browse(cr, uid, ids, context=context):
210             tasks_id = []
211             for task in product.tasks_id:
212                 if task.state != 'done':
213                     tasks_id.append(task.id)
214
215             clone_id = self.copy(cr, uid, product.id, {
216                 'name': 'PARTIAL:'+ product.name ,
217                 'sprint_id':False,
218                 'tasks_id':[(6, 0, tasks_id)],
219                                 })
220         self.write(cr, uid, ids, {'state':'cancel'}, context=context)
221         return True
222
223     _columns = {
224         'name' : fields.char('Feature', size=64, required=True),
225         'note' : fields.text('Note'),
226         'active' : fields.boolean('Active', help="If Active field is set to true, it will allow you to hide the product backlog without removing it."),
227         'project_id': fields.many2one('project.project', 'Project', required=True, domain=[('scrum','=',1)]),
228         'user_id': fields.many2one('res.users', 'Author'),
229         'sprint_id': fields.many2one('project.scrum.sprint', 'Sprint'),
230         'sequence' : fields.integer('Sequence', help="Gives the sequence order when displaying a list of product backlog."),
231         'tasks_id': fields.one2many('project.task', 'product_backlog_id', 'Tasks Details'),
232         'state': fields.selection([('draft','Draft'),('open','Open'),('pending','Pending'),('done','Done'),('cancel','Cancelled')], 'State', required=True),
233         'progress': fields.function(_compute, multi="progress", group_operator="avg", type='float', string='Progress', help="Computed as: Time Spent / Total Time."),
234         'effective_hours': fields.function(_compute, multi="effective_hours", string='Spent Hours', help="Computed using the sum of the time spent on every related tasks", store=True),
235         'expected_hours': fields.float('Planned Hours', help='Estimated total time to do the Backlog'),
236         'create_date': fields.datetime("Creation Date", readonly=True),
237         'task_hours': fields.function(_compute, multi="task_hours", string='Task Hours', help='Estimated time of the total hours of the tasks')
238     }
239     _defaults = {
240         'state': 'draft',
241         'active':  1,
242         'user_id': lambda self, cr, uid, context: uid,
243     }
244     _order = "sequence"
245 project_scrum_product_backlog()
246
247 class project_scrum_task(osv.osv):
248     _name = 'project.task'
249     _inherit = 'project.task'
250
251     def _get_task(self, cr, uid, ids, context=None):
252         result = {}
253         for line in self.pool.get('project.scrum.product.backlog').browse(cr, uid, ids, context=context):
254             for task in line.tasks_id:
255                 result[task.id] = True
256         return result.keys()
257
258     _columns = {
259         'product_backlog_id': fields.many2one('project.scrum.product.backlog', 'Product Backlog',help="Related product backlog that contains this task. Used in SCRUM methodology"),
260         'sprint_id': fields.related('product_backlog_id','sprint_id', type='many2one', relation='project.scrum.sprint', string='Sprint',
261             store={
262                 'project.task': (lambda self, cr, uid, ids, c={}: ids, ['product_backlog_id'], 10),
263                 'project.scrum.product.backlog': (_get_task, ['sprint_id'], 10)
264             }),
265     }
266
267 project_scrum_task()
268
269 class project_scrum_meeting(osv.osv):
270     _name = 'project.scrum.meeting'
271     _description = 'Scrum Meeting'
272     _order = 'date desc'
273     _columns = {
274         'name' : fields.char('Meeting Name', size=64),
275         'date': fields.date('Meeting Date', required=True),
276         'sprint_id': fields.many2one('project.scrum.sprint', 'Sprint', required=True),
277         'project_id': fields.many2one('project.project', 'Project'),
278         'question_yesterday': fields.text('Tasks since yesterday'),
279         'question_today': fields.text('Tasks for today'),
280         'question_blocks': fields.text('Blocks encountered'),
281         'question_backlog': fields.text('Backlog Accurate'),
282         'task_ids': fields.many2many('project.task', 'meeting_task_rel', 'metting_id', 'task_id', 'Tasks'),
283         'user_id': fields.related('sprint_id', 'scrum_master_id', type='many2one', relation='res.users', string='Scrum Master', readonly=True),
284     }
285     #
286     # TODO: Find the right sprint thanks to users and date
287     #
288     _defaults = {
289         'date' : lambda *a: time.strftime('%Y-%m-%d'),
290     }
291
292 project_scrum_meeting()
293
294
295 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: