1
##############################################################################
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
5
# WARNING: This program as such is intended to be used by professional
6
# programmers who take the whole responsability of assessing all potential
7
# consequences resulting from its eventual inadequacies and bugs
8
# End users who are looking for a ready-to-use solution with commercial
9
# garantees and support are strongly adviced to contract a Free Software
12
# This program is Free Software; you can redistribute it and/or
13
# modify it under the terms of the GNU General Public License
14
# as published by the Free Software Foundation; either version 2
15
# of the License, or (at your option) any later version.
17
# This program is distributed in the hope that it will be useful,
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
# GNU General Public License for more details.
22
# You should have received a copy of the GNU General Public License
23
# along with this program; if not, write to the Free Software
24
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
##############################################################################
28
from report.render import render
29
from report.interface import report_int
31
from mx.DateTime import *
32
from report.misc import choice_colors
43
# Pinky: Bad code, seems buggy, TO CHECK !
46
class external_pdf(render):
47
def __init__(self, pdf):
50
self.output_type='pdf'
55
class report_custom(report_int):
56
def _compute_dates(self, time_unit, start, stop):
59
if time_unit == 'month':
61
a = Date(*map(int, start.split("-"))).year*12+Date(*map(int, start.split("-"))).month
62
z = Date(*map(int, stop.split("-"))).year*12+Date(*map(int, stop.split("-"))).month+1
69
months = {1:"January",2:"February",3:"March",4:"April",5:"May",6:"June",7:"July",8:"August",9:"September",10:"October",11:"November",12:"December"}
71
'name' :months[month],
72
'start':(Date(year, month, 2) + RelativeDateTime(day=1)).strftime('%Y-%m-%d'),
73
'stop' :(Date(year, month, 2) + RelativeDateTime(day=-1)).strftime('%Y-%m-%d'),
76
elif time_unit == 'week':
78
a = Date(*map(int, start.split("-"))).iso_week[0]*52+Date(*map(int, start.split("-"))).iso_week[1]
79
z = Date(*map(int, stop.split("-"))).iso_week[0]*52+Date(*map(int, stop.split("-"))).iso_week[1]
80
for i in range(a,z+1):
84
'name' :"Week #%d" % week,
85
'start':ISO.WeekTime(year, week, 1).strftime('%Y-%m-%d'),
86
'stop' :ISO.WeekTime(year, week, 7).strftime('%Y-%m-%d'),
89
else: # time_unit = day
91
a = Date(*map(int, start.split("-")))
92
z = Date(*map(int, stop.split("-")))
95
dates[map(int,i.strftime('%Y%m%d').split())[0]] = {
96
'name' :i.strftime('%Y-%m-%d'),
97
'start':i.strftime('%Y-%m-%d'),
98
'stop' :i.strftime('%Y-%m-%d'),
100
i = i + RelativeDateTime(days=+1)
104
def create(self, cr, uid, ids, datas, context={}):
105
assert len(ids), 'You should provide some ids!'
106
colors = choice_colors(len(ids))
107
ids_str = ','.join(map(str, ids))
109
"SELECT MAX(date_planned) AS stop "\
110
"FROM mrp_workcenter, mrp_production, mrp_production_workcenter_line "\
111
"WHERE mrp_production_workcenter_line.production_id=mrp_production.id "\
112
"AND mrp_production_workcenter_line.workcenter_id=mrp_workcenter.id "\
113
"AND mrp_production.state NOT IN ('cancel','done') "\
114
"AND mrp_workcenter.id IN (%s)" % ids_str)
115
res = cr.dictfetchone()
117
res['stop'] = time.strftime('%Y-%m-%d')
118
dates = self._compute_dates(datas['form']['time_unit'], time.strftime('%Y-%m-%d'), res['stop'])
119
dates_list = dates.keys()
122
for date in dates_list:
123
x_index.append((dates[date]['name'], date))
124
pdf_string = StringIO.StringIO()
125
can = canvas.init(fname=pdf_string, format='pdf')
126
chart_object.set_defaults(line_plot.T, line_style=None)
127
if datas['form']['measure_unit'] == 'cycles':
128
y_label = "Load (Cycles)"
130
y_label = "Load (Hours)"
131
ar = area.T(legend = legend.T(),
132
x_grid_style= line_style.gray70_dash1,
133
x_axis = axis.X(label="Periods", format="/a90/hC%s"),
134
x_coord = category_coord.T(x_index, 0),
135
y_axis = axis.Y(label=y_label),
138
bar_plot.fill_styles.reset();
142
"SELECT id, name FROM mrp_workcenter " \
144
"ORDER BY mrp_workcenter.id" % ids_str)
145
workcenters = cr.dictfetchall()
148
for date in dates_list:
150
for workcenter in workcenters:
151
cr.execute("SELECT SUM(mrp_production_workcenter_line.hour) AS hours, SUM(mrp_production_workcenter_line.cycle) AS cycles, \
152
mrp_workcenter.name AS name, mrp_workcenter.id AS id \
153
FROM mrp_production_workcenter_line, mrp_production, mrp_workcenter \
154
WHERE (mrp_production_workcenter_line.production_id=mrp_production.id) \
155
AND (mrp_production_workcenter_line.workcenter_id=mrp_workcenter.id) \
156
AND (mrp_workcenter.id=%d) \
157
AND (mrp_production.date_planned BETWEEN '%s' AND '%s') \
158
GROUP BY mrp_production_workcenter_line.workcenter_id, mrp_workcenter.name, mrp_workcenter.id \
159
ORDER BY mrp_workcenter.id" % (workcenter['id'], dates[date]['start'], dates[date]['stop']))
160
res = cr.dictfetchall()
164
if datas['form']['measure_unit'] == 'cycles':
165
vals.append(res[0]['cycles'] or 0.0)
167
vals.append(res[0]['hours'] or 0.0)
169
toto = [dates[date]['name']]
175
for workcenter in workcenters:
176
f = fill_style.Plain()
177
f.bgcolor = colors[workcenter_num]
178
ar.add_plot(bar_plot.T(label=workcenter['name'], data=data, fill_style=f, hcol=workcenter_num+1, cluster=(workcenter_num, len(res))))
181
#plot = bar_plot.T(label=workcenter['name'], data=data, hcol=1, fill_style=fill_style.white, cluster=(color_index,len(ids)))
182
if (len(data[0]) <= 1):
183
ar = self._empty_graph(data[0][0])
185
# close canvas so that the file is written to "disk"
187
self.obj = external_pdf(pdf_string.getvalue())
190
return (self.obj.pdf, 'pdf')
192
def _empty_graph(self, date):
194
ar = area.T(x_coord = category_coord.T(data, 0), y_range = (0, None),
195
x_axis = axis.X(label="Periods"),
196
y_axis = axis.Y(label="Load"))
197
ar.add_plot(bar_plot.T(data = data, label="No production order"))
200
report_custom('report.mrp.workcenter.load')