1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
28 from report.render import render
29 from report.interface import report_int
34 class external_pdf(render):
36 """ Generate External PDF """
38 def __init__(self, pdf):
41 self.output_type = 'pdf'
46 class report_custom(report_int):
48 """ Create Custom Report """
50 def create(self, cr, uid, ids, datas, context=None):
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 """
57 assert len(ids), 'You should provide some ids!'
59 responsible_names = {}
61 minbenef = 999999999999999999999
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),))
68 res = cr.dictfetchall()
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
75 benefit = revenue - cost
76 if benefit > maxbenef:
78 if benefit < minbenef:
81 tuple_benefit = (proba * 100, benefit)
82 responsible_data.setdefault(userid, [])
83 responsible_data[userid].append(tuple_benefit)
85 tuple_benefit = (proba * 100, cost, benefit)
86 data.append(tuple_benefit)
88 responsible_names[userid] = (row['name'] or '/').replace('/','//')
90 minbenef -= maxbenef * 0.05
94 minmaxdiff2 = (maxbenef - minbenef)/2
96 for l in responsible_data.itervalues():
97 for i in range(len(l)):
101 current_ratio = 1 + (ratio-1) * proba
103 newbenef = minmaxdiff2 + ((benef - minbenef - minmaxdiff2) * current_ratio)
105 l[i] = (percent, newbenef)
108 #-group by "categorie de probabilites ds graphe du haut"
111 pdf_string = StringIO.StringIO()
112 can = canvas.init(fname = pdf_string, format = 'pdf')
114 chart_object.set_defaults(line_plot.T, line_style=None)
116 xaxis = axis.X(label=None, format="%d%%", tic_interval=20)
119 x_range_a, x_range_b = (0, 100)
120 y_range_a, y_range_b = (minbenef, maxbenef)
126 y_grid_interval = 10000,
128 x_range = (x_range_a, x_range_b),
129 y_range = (y_range_a, y_range_b),
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))
143 # second graph (top right)
144 ar = area.T(legend = legend.T(),
147 x_grid_interval = lambda min, max: [40,60,80,100],
148 x_grid_style = line_style.gray70_dash1,
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"))
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)
158 ar.add_plot(plot1, plot2)
162 # diagonal "pipeline" lines
163 can.line(line_style.black, 0, 200, 300, 150)
164 can.line(line_style.black, 0, 0, 300, 50)
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)
171 # draw arrows to the right
173 for y in range(60, 150, 10):
174 a.draw([(285, y), (315, y)], can=can)
176 # close canvas so that the file is written to "disk"
179 self.obj = external_pdf(pdf_string.getvalue())
183 return (self.obj.pdf, 'pdf')
185 report_custom('report.crm.case')
187 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: