~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to mrp/report/workcenter_load.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
# 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
 
10
# Service Company
 
11
#
 
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.
 
16
#
 
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.
 
21
#
 
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.
 
25
#
 
26
##############################################################################
 
27
 
 
28
from report.render import render 
 
29
from report.interface import report_int
 
30
from pychart import *
 
31
from mx.DateTime import *
 
32
from report.misc import choice_colors
 
33
import time, mx
 
34
import random
 
35
import StringIO
 
36
 
 
37
 
 
38
theme.use_color = 1
 
39
#theme.scale = 2
 
40
random.seed(0)
 
41
 
 
42
#
 
43
# Pinky: Bad code, seems buggy, TO CHECK !
 
44
#
 
45
 
 
46
class external_pdf(render):
 
47
        def __init__(self, pdf):
 
48
                render.__init__(self)
 
49
                self.pdf = pdf
 
50
                self.output_type='pdf'
 
51
                
 
52
        def _render(self):
 
53
                return self.pdf
 
54
 
 
55
class report_custom(report_int):
 
56
        def _compute_dates(self, time_unit, start, stop):
 
57
                if not stop:
 
58
                        stop = start
 
59
                if time_unit == 'month':
 
60
                        dates = {}
 
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
 
63
                        for i in range(a,z):
 
64
                                year = i/12
 
65
                                month = i%12
 
66
                                if month == 0:
 
67
                                        year -= 1
 
68
                                        month = 12
 
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"}
 
70
                                dates[i] = {
 
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'),
 
74
                                }
 
75
                        return dates
 
76
                elif time_unit == 'week':
 
77
                        dates = {}
 
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):
 
81
                                year = i/52
 
82
                                week = i%52
 
83
                                dates[i] = {
 
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'),
 
87
                                }
 
88
                        return dates
 
89
                else: # time_unit = day
 
90
                        dates = {}
 
91
                        a = Date(*map(int, start.split("-")))
 
92
                        z = Date(*map(int, stop.split("-")))
 
93
                        i = a
 
94
                        while i <= z:
 
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'),
 
99
                                }
 
100
                                i = i + RelativeDateTime(days=+1)
 
101
                        return dates
 
102
                return {}
 
103
 
 
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))
 
108
                cr.execute(
 
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()
 
116
                if not res['stop']:
 
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()
 
120
                dates_list.sort()
 
121
                x_index = []
 
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)"
 
129
                else:
 
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),
 
136
                                        y_range         = (0, None),
 
137
                                        size            = (640,480))
 
138
                bar_plot.fill_styles.reset();
 
139
 
 
140
                # select workcenters
 
141
                cr.execute(
 
142
                        "SELECT id, name FROM mrp_workcenter " \
 
143
                        "WHERE id in (%s) "\
 
144
                        "ORDER BY mrp_workcenter.id" % ids_str)
 
145
                workcenters = cr.dictfetchall()
 
146
 
 
147
                data = []
 
148
                for date in dates_list:
 
149
                        vals = []
 
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()
 
161
                                if not res:
 
162
                                        vals.append(0.0)
 
163
                                else:
 
164
                                        if datas['form']['measure_unit'] == 'cycles':
 
165
                                                vals.append(res[0]['cycles'] or 0.0)
 
166
                                        else:
 
167
                                                vals.append(res[0]['hours'] or 0.0)
 
168
                                        
 
169
                        toto = [dates[date]['name']]
 
170
                        for val in vals:
 
171
                                toto.append(val)
 
172
                        data.append(toto)
 
173
 
 
174
                workcenter_num = 0
 
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))))
 
179
                        workcenter_num += 1
 
180
 
 
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])
 
184
                ar.draw(can)
 
185
                # close canvas so that the file is written to "disk"
 
186
                can.close()
 
187
                self.obj = external_pdf(pdf_string.getvalue())
 
188
                self.obj.render()
 
189
                pdf_string.close()
 
190
                return (self.obj.pdf, 'pdf')
 
191
        
 
192
        def _empty_graph(self, date):
 
193
                data = [[date, 0]]
 
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"))
 
198
                return ar
 
199
 
 
200
report_custom('report.mrp.workcenter.load')
 
201