[IMP] footer on forms
[odoo/odoo.git] / addons / crm / report / report_businessopp.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 os, time
23 import netsvc
24
25 import random
26 import StringIO
27
28 from report.render import render
29 from report.interface import report_int
30 from pychart import *
31
32 theme.use_color = 1
33
34 class external_pdf(render):
35
36     """ Generate External PDF """
37
38     def __init__(self, pdf):
39         render.__init__(self)
40         self.pdf = pdf
41         self.output_type = 'pdf'
42
43     def _render(self):
44         return self.pdf
45
46 class report_custom(report_int):
47
48     """ Create Custom Report """
49
50     def create(self, cr, uid, ids, datas, context=None):
51
52         """ @param cr: the current row, from the database cursor,
53             @param uid: the current user’s ID for security checks,
54             @param ids: List of IDs
55             @param context: A standard dictionary for contextual values """
56
57         assert len(ids), 'You should provide some ids!'
58         responsible_data = {}
59         responsible_names = {}
60         data = []
61         minbenef = 999999999999999999999
62         maxbenef = 0
63
64         cr.execute('select probability, planned_revenue, planned_cost, user_id,\
65                  res_users.name as name from crm_case left join res_users on \
66                  (crm_case.user_id=res_users.id) where crm_case.id IN %s order by user_id',(tuple(ids),))
67
68         res = cr.dictfetchall()
69         for row in res:
70             proba = row['probability'] or 0 / 100.0
71             cost = row['planned_cost'] or 0
72             revenue = row['planned_revenue'] or 0
73             userid = row['user_id'] or 0
74
75             benefit = revenue - cost
76             if benefit > maxbenef:
77                 maxbenef = benefit
78             if benefit < minbenef:
79                 minbenef = benefit
80
81             tuple_benefit = (proba * 100,  benefit)
82             responsible_data.setdefault(userid, [])
83             responsible_data[userid].append(tuple_benefit)
84
85             tuple_benefit = (proba * 100, cost, benefit)
86             data.append(tuple_benefit)
87
88             responsible_names[userid] = (row['name'] or '/').replace('/','//')
89
90         minbenef -= maxbenef * 0.05
91         maxbenef *= 1.2
92
93         ratio = 0.5
94         minmaxdiff2 = (maxbenef - minbenef)/2
95
96         for l in responsible_data.itervalues():
97             for i in range(len(l)):
98                 percent, benef = l[i]
99                 proba = percent/100
100
101                 current_ratio = 1 + (ratio-1) * proba
102
103                 newbenef = minmaxdiff2 + ((benef - minbenef - minmaxdiff2) * current_ratio)
104
105                 l[i] = (percent, newbenef)
106
107 #TODO:
108 #-group by "categorie de probabilites ds graphe du haut"
109 #-echelle variable
110
111         pdf_string = StringIO.StringIO()
112         can = canvas.init(fname = pdf_string, format = 'pdf')
113
114         chart_object.set_defaults(line_plot.T, line_style=None)
115
116         xaxis = axis.X(label=None, format="%d%%", tic_interval=20)
117         yaxis = axis.Y()
118
119         x_range_a, x_range_b = (0, 100)
120         y_range_a, y_range_b = (minbenef, maxbenef)
121         if y_range_a == 0.0:
122             y_range_a += 0.0001
123
124         ar = area.T(
125             size = (300,200),
126             y_grid_interval = 10000,
127             y_grid_style = None,
128             x_range = (x_range_a, x_range_b),
129             y_range = (y_range_a, y_range_b),
130             x_axis = xaxis,
131             y_axis = None,
132             legend = legend.T()
133         )
134
135         #import pydb; pydb.debugger()
136         for k, d in responsible_data.iteritems():
137             fill = fill_style.Plain(bgcolor=color.T(r=random.random(), g=random.random(), b=random.random()))
138             tick = tick_mark.Square(size=6, fill_style=fill)
139             ar.add_plot(line_plot.T(label=responsible_names[k], data=d, tick_mark=tick))
140
141         ar.draw(can)
142
143         # second graph (top right)
144         ar = area.T(legend = legend.T(),
145                     size = (200,100),
146                     loc = (100,250),
147                     x_grid_interval = lambda min, max: [40,60,80,100],
148                     x_grid_style = line_style.gray70_dash1,
149                     x_range = (33, 100),
150                     x_axis = axis.X(label=None, minor_tic_interval = lambda min,max: [50, 70, 90],\
151                                      format=lambda x: ""),
152                     y_axis = axis.Y(label="Planned amounts"))
153
154         bar_plot.fill_styles.reset();
155         plot1 = bar_plot.T(label="Cost", data=data, fill_style=fill_style.red)
156         plot2 = bar_plot.T(label="Revenue", data=data, hcol=2, stack_on = plot1, fill_style=fill_style.blue)
157
158         ar.add_plot(plot1, plot2)
159
160         ar.draw(can)
161
162         # diagonal "pipeline" lines
163         can.line(line_style.black, 0, 200, 300, 150)
164         can.line(line_style.black, 0, 0, 300, 50)
165
166         # vertical lines
167         ls = line_style.T(width=0.4, color=color.gray70, dash=(2, 2))
168         for x in range(120, 300, 60):
169             can.line(ls, x, 0, x, 250)
170
171         # draw arrows to the right
172         a = arrow.fat1
173         for y in range(60, 150, 10):
174             a.draw([(285, y), (315, y)], can=can)
175
176         # close canvas so that the file is written to "disk"
177         can.close()
178
179         self.obj = external_pdf(pdf_string.getvalue())
180         self.obj.render()
181
182         pdf_string.close()
183         return (self.obj.pdf, 'pdf')
184
185 report_custom('report.crm.case')
186
187 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
188