~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to scrum/report/task_burndown.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-dedd7f8a42bd4557112a0513082691b8590ad6cc
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# $Id: gantt_report.py 1005 2005-07-25 08:41:42Z nicoe $
 
6
#
 
7
# WARNING: This program as such is intended to be used by professional
 
8
# programmers who take the whole responsability of assessing all potential
 
9
# consequences resulting from its eventual inadequacies and bugs
 
10
# End users who are looking for a ready-to-use solution with commercial
 
11
# garantees and support are strongly adviced to contract a Free Software
 
12
# Service Company
 
13
#
 
14
# This program is Free Software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU General Public License
 
16
# as published by the Free Software Foundation; either version 2
 
17
# of the License, or (at your option) any later version.
 
18
#
 
19
# This program is distributed in the hope that it will be useful,
 
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
# GNU General Public License for more details.
 
23
#
 
24
# You should have received a copy of the GNU General Public License
 
25
# along with this program; if not, write to the Free Software
 
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
27
#
 
28
##############################################################################
 
29
 
 
30
import StringIO
 
31
 
 
32
from report.render import render
 
33
from report.interface import report_int
 
34
 
 
35
from mx import DateTime
 
36
import time
 
37
 
 
38
from pychart import *
 
39
import pychart.legend
 
40
 
 
41
import _burndown
 
42
 
 
43
class external_pdf(render):
 
44
        def __init__(self, pdf):
 
45
                render.__init__(self)
 
46
                self.pdf = pdf
 
47
                self.output_type='pdf'
 
48
        def _render(self):
 
49
                return self.pdf
 
50
 
 
51
def burndown_chart(cr, uid, tasks_id, date_start, date_stop):
 
52
        latest = False
 
53
        cr.execute('select id,date_start,state,planned_hours from project_task where id in ('+','.join(map(str,tasks_id))+') order by date_start')
 
54
        tasks = cr.fetchall()
 
55
        cr.execute('select id,date_close,state,planned_hours*progress/100 from project_task where id in ('+','.join(map(str,tasks_id))+') and state in (%s,%s) order by date_close', ('progress','done'))
 
56
        tasks2 = cr.fetchall()
 
57
        current_date = date_start
 
58
        total = 0
 
59
        done = 0
 
60
        result = []
 
61
        while current_date<=date_stop:
 
62
                while len(tasks) and tasks[0][1] and tasks[0][1][:10]<=current_date:
 
63
                        total += tasks.pop(0)[3]
 
64
                while len(tasks2) and tasks2[0][1] and tasks2[0][1][:10]<=current_date:
 
65
                        t2 = tasks2.pop(0)
 
66
                        if t2[2]<>'cancel':
 
67
                                done += t2[3]
 
68
                        else:
 
69
                                total -= t2[3]
 
70
                result.append( (int(time.mktime(time.strptime(current_date,'%Y-%m-%d'))), total-done) )
 
71
                current_date = (DateTime.strptime(current_date, '%Y-%m-%d') + DateTime.RelativeDateTime(days=1)).strftime('%Y-%m-%d')
 
72
                if not len(tasks) and not len(tasks2):
 
73
                        break
 
74
        result.append( (int(time.mktime(time.strptime(date_stop,'%Y-%m-%d'))), 0) )
 
75
        return result
 
76
 
 
77
class report_tasks(report_int):
 
78
        def create(self, cr, uid, ids, datas, context={}):
 
79
                io = StringIO.StringIO()
 
80
 
 
81
                if 'date_start' not in datas:
 
82
                        cr.execute('select min(date_start) from project_task where id in ('+','.join(map(str,ids))+')')
 
83
                        dt = cr.fetchone()[0]
 
84
                        if dt:
 
85
                                datas['date_start'] = dt[:10]
 
86
                        else:
 
87
                                datas['date_start'] = time.strftime('%Y-%m-%d')
 
88
                if 'date_stop' not in datas:
 
89
                        cr.execute('select max(date_start),max(date_close) from project_task where id in ('+','.join(map(str,ids))+')')
 
90
                        res = cr.fetchone()
 
91
                        datas['date_stop'] = (res[0] and res[0][:10]) or time.strftime('%Y-%m-%d')
 
92
                        if res[1] and datas['date_stop']<res[1]:
 
93
                                datas['date_stop'] = res[1][:10]
 
94
 
 
95
                date_to_int = lambda x: int(x.ticks())
 
96
                int_to_date = lambda x: '/a60{}'+DateTime.localtime(x).strftime('%d/%m/%Y')
 
97
 
 
98
                datas = _burndown.compute_burndown(cr, uid, ids, datas['date_start'], datas['date_stop'])
 
99
 
 
100
                canv = canvas.init(fname=io, format='pdf')
 
101
                canv.set_author("Tiny ERP")
 
102
 
 
103
                max_hour = reduce(lambda x,y: max(y[1],x), datas, 0)
 
104
 
 
105
                date_to_int = lambda x: int(x.ticks())
 
106
                int_to_date = lambda x: '/a60{}'+DateTime.localtime(x).strftime('%d %m %Y')
 
107
 
 
108
                def _interval_get(*args):
 
109
                        result = []
 
110
                        for i in range(20):
 
111
                                d = DateTime.localtime(datas[0][0] + (((datas[-1][0]-datas[0][0])/20)*(i+1)))
 
112
                                res = DateTime.DateTime(d.year, d.month, d.day).ticks()
 
113
                                if (not result) or result[-1]<>res:
 
114
                                        result.append(res)
 
115
                        return result
 
116
 
 
117
                ar = area.T(x_grid_style=line_style.gray50_dash1,
 
118
                        x_axis=axis.X(label="Date", format=int_to_date),
 
119
                        y_axis=axis.Y(label="Burndown Chart - Planned Hours"),
 
120
                        x_grid_interval=_interval_get,
 
121
                        x_range = (datas[0][0],datas[-1][0]),
 
122
                        y_range = (0,max_hour),
 
123
                        legend = None,
 
124
                        size = (680,450))
 
125
                ar.add_plot(line_plot.T(data=datas))
 
126
                ar.draw(canv)
 
127
                canv.close()
 
128
 
 
129
                self.obj = external_pdf(io.getvalue())
 
130
                self.obj.render()
 
131
                return (self.obj.pdf,'pdf')
 
132
report_tasks('report.project.tasks.burndown')