f22082fcd86af3aa805a112a09b942fdabc30252
[odoo/odoo.git] / addons / project_scrum / report / sprint_burndown.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
22 import StringIO
23 import pooler
24
25 from report.render import render
26 from report.interface import report_int
27
28 from datetime import datetime
29 import time
30
31 from pychart import *
32 import pychart.legend
33
34 import _burndown
35 class report_tasks(report_int):
36     def create(self, cr, uid, ids, datas, context=None):
37         if context is None:
38             context = {}
39         io = StringIO.StringIO()
40
41         canv = canvas.init(fname=io, format='pdf')
42         canv.set_author("OpenERP")
43         canv.set_title("Burndown Chart")
44         pool = pooler.get_pool(cr.dbname)
45         sprint_pool = pool.get('project.scrum.sprint')
46         task_pool = pool.get('project.task')
47         int_to_date = lambda x: '/a60{}' + datetime(time.localtime(x).tm_year, time.localtime(x).tm_mon, time.localtime(x).tm_mday).strftime('%d %m %Y')
48         for sprint in sprint_pool.browse(cr, uid, ids, context=context):
49             task_ids = task_pool.search(cr, uid, [('sprint_id','=',sprint.id)], context=context)
50             datas = _burndown.compute_burndown(cr, uid, task_ids, sprint.date_start, sprint.date_stop)
51             max_hour = reduce(lambda x,y: max(y[1],x), datas, 0)
52             def _interval_get(*args):
53                 result = []
54                 for i in range(20):
55                     d = time.localtime(datas[0][0] + (((datas[-1][0]-datas[0][0])/20)*(i+1)))
56                     res = time.mktime(d)
57                     if (not result) or result[-1]<>res:
58                         result.append(res)
59                 return result
60
61             guideline__data=[(datas[0][0],max_hour), (datas[-1][0],0)]
62
63             ar = area.T(x_grid_style=line_style.gray50_dash1,
64                 x_axis=axis.X(label="Date", format=int_to_date),
65                 y_axis=axis.Y(label="Burndown Chart - Planned Hours"),
66                 x_grid_interval=_interval_get,
67                 x_range = (datas[0][0],datas[-1][0]),
68                 y_range = (0,max_hour),
69                 legend = None,
70                 size = (680,450))
71             ar.add_plot(line_plot.T(data=guideline__data, line_style=line_style.red))
72             ar.add_plot(line_plot.T(data=datas, line_style=line_style.green))
73
74             entr1 = pychart.legend.Entry(label="guideline", line_style=line_style.red)
75             entr2 = pychart.legend.Entry(label="burndownchart",line_style=line_style.green)
76             legend = pychart.legend.T(nr_rows=2, inter_row_sep=5)
77             legend.draw(ar,[entr1,entr2],canv)
78
79             ar.draw(canv)
80         canv.close()
81
82         self.obj = _burndown.external_pdf(io.getvalue())
83         self.obj.render()
84         return (self.obj.pdf, 'pdf')
85 report_tasks('report.scrum.sprint.burndown')
86
87 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
88