Exemple #1
0
    def draw(self):
        colors = choice_colors(len(self._datas.keys()))
        user_color = {}
        for user in self._datas.keys():
            user_color[user] = colors.pop()

        names = []
        for n in self._names:
            names.append((self._names[n], n))
        names.sort()
        names.reverse()
        def _interval_get(*args):
            result = []
            for i in range(20):
                d = localtime(self._min + (((self._max-self._min)/20)*(i+1)))
                res = DateTime(d.year, d.month, d.day).ticks()
                if (not result) or result[-1]<>res:
                    result.append(res)
            return result

        ar = area.T(y_coord = category_coord.T(names, 1),
            x_grid_style=line_style.gray50_dash1,
            x_grid_interval=_interval_get,
            x_range = (self._min,self._max),
            x_axis=axis.X(label="Date", format=self._conv[1]),
            y_axis=axis.Y(label="Tasks"),
            legend = legend.T(), size = (680,450))

        for user in self._datas:
            chart_object.set_defaults(interval_bar_plot.T, direction="horizontal", data=self._datas[user])
            f = fill_style.Plain()
            f.bgcolor = user_color[user]
            ar.add_plot(interval_bar_plot.T(fill_styles = [f, None], label=user, cluster=(0,1)))

        ar.draw(self._canvas)
Exemple #2
0
    def draw(self):
        colors = choice_colors(len(self._datas.keys()))
        user_color = {}
        for user in self._datas.keys():
            user_color[user] = colors.pop()

        val_min = int(time.mktime(time.strptime(self.val_min,'%Y-%m-%d')))
        val_max = int(time.mktime(time.strptime(self.val_max,'%Y-%m-%d')))

        plots = []
        for product_id in self._datas:
            f = fill_style.Plain()
            f.bgcolor = user_color[user]
            datas = self._datas[product_id].items()
            datas = map(lambda x: (int(time.mktime(time.strptime(x[0],'%Y-%m-%d'))),x[1]), datas)
            datas.sort()
            datas2 = []
            val = 0
            for d in datas:
                val+=d[1]

                if len(datas2):
                    d2 = d[0]-60*61*24
                    if datas2[-1][0]<d2-1000:
                        datas2.append((d2,datas2[-1][1]))
                datas2.append((d[0],val))
            if len(datas2) and datas2[-1][0]<val_max-100:
                datas2.append((val_max, datas2[-1][1]))
            if len(datas2)==1:
                datas2.append( (datas2[0][0]+100, datas2[0][1]) )
            st = line_style.T()
            st.color = user_color[product_id]
            st.width = 1
            st.cap_style=1
            st.join_style=1
            plot = line_plot.T(label=self._names[product_id], data=datas2, line_style=st)
            plots.append(plot)

        # Cheating to make them available in _(...) calls
        context = {'lang': self._lang}
        cr = self._cr

        interval = max((val_max-val_min)/15, 86400)
        x_axis = axis.X(format=lambda x:'/a60{}'+time.strftime('%Y-%m-%d',time.gmtime(x)), tic_interval=interval, label=None)
        # For add the report header on the top of the report.
        tb = text_box.T(loc=(300, 500), text="/hL/15/b%s" % _("Stock Level Forecast"), line_style=None)
        tb.draw()
        ar = area.T(size = (620,435), x_range=(val_min,val_max+1), y_axis = axis.Y(format="%d", label=_("Virtual Stock (Unit)")), x_axis=x_axis)
        for plot in plots:
            ar.add_plot(plot)
        ar.draw(self._canvas)
    def draw(self):
        colors = choice_colors(len(self._datas.keys()))
        user_color = {}
        for user in self._datas.keys():
            user_color[user] = colors.pop()

        val_min = int(time.mktime(time.strptime(self.val_min, "%Y-%m-%d")))
        val_max = int(time.mktime(time.strptime(self.val_max, "%Y-%m-%d")))

        plots = []
        for product_id in self._datas:
            f = fill_style.Plain()
            f.bgcolor = user_color[user]
            datas = self._datas[product_id].items()
            datas = map(lambda x: (int(time.mktime(time.strptime(x[0], "%Y-%m-%d"))), x[1]), datas)
            datas.sort()
            datas2 = []
            val = 0
            for d in datas:
                val += d[1]

                if len(datas2):
                    d2 = d[0] - 60 * 61 * 24
                    if datas2[-1][0] < d2 - 1000:
                        datas2.append((d2, datas2[-1][1]))
                datas2.append((d[0], val))
            if len(datas2) and datas2[-1][0] < val_max - 100:
                datas2.append((val_max, datas2[-1][1]))
            if len(datas2) == 1:
                datas2.append((datas2[0][0] + 100, datas2[0][1]))
            st = line_style.T()
            st.color = user_color[product_id]
            st.width = 1
            st.cap_style = 1
            st.join_style = 1
            plot = line_plot.T(label=self._names[product_id], data=datas2, line_style=st)
            plots.append(plot)

        interval = max((val_max - val_min) / 15, 86400)
        x_axis = axis.X(
            format=lambda x: "/a60{}" + time.strftime("%Y-%m-%d", time.gmtime(x)), tic_interval=interval, label=None
        )
        ar = area.T(
            size=(620, 435),
            x_range=(val_min, val_max + 1),
            y_axis=axis.Y(format="%d", label="Virtual Stock (Unit)"),
            x_axis=x_axis,
        )
        for plot in plots:
            ar.add_plot(plot)
        ar.draw(self._canvas)
    def create(self, cr, uid, ids, datas, context={}):
        datas = datas['form']
        if datas['users_id']:
            datas['users_id'] = datas['users_id'][0][2]
        else:
            cr.execute('select id from res_users limit 10')
            datas['users_id'] = [x[0] for x in cr.fetchall()]
        cr.execute('select id,name from res_users where id in ('+','.join(map(str,datas['users_id']))+')')
        users_name = dict(cr.fetchall())
        colors = choice_colors(len(datas['users_id']))
        dates = self._compute_dates(datas['type'], datas['date1'], datas['date2'])
        dates_list = dates.keys()
        dates_list.sort()
        x_index = map(lambda x: (dates[x]['name'],x), dates_list)
        pdf_string = StringIO.StringIO()
        can = canvas.init(fname=pdf_string, format='pdf')
        chart_object.set_defaults(line_plot.T, line_style=None)
        y_label = 'Number of Objects'

        ar = area.T(legend      = legend.T(),
                    x_grid_style= line_style.gray70_dash1,
                    x_axis      = axis.X(label=None, format="/a90/hC%s"),
                    x_coord     = category_coord.T(x_index, 0),
                    y_axis      = axis.Y(label=y_label),
                    y_range     = (0, None), size = (680,450))
        bar_plot.fill_styles.reset();

        data = []
        for date in dates_list:
            cr.execute(("SELECT create_uid,count(*) FROM %s WHERE (to_char(create_date, 'YYYY-MM-DD') BETWEEN '%s' AND '%s') and id in ("+','.join(map(str,datas['users_id']))+") GROUP BY create_uid") % (datas['model'].replace('.','_'), dates[date]['start'], dates[date]['stop']))
            res = dict(cr.fetchall())

            vals = [dates[date]['name']]
            for user in datas['users_id']:
                vals.append(res.get(user, 0.0))
            data.append(vals)

        for user_id in range(len(datas['users_id'])):
            user = datas['users_id'][user_id]

            f = fill_style.Plain()
            f.bgcolor = colors[user_id]
            ar.add_plot(bar_plot.T(label=users_name.get(user,'Unknown').replace('/', '//'), data=data, hcol=user_id+1, cluster=(user_id, len(datas['users_id'])), fill_style=f ))

        ar.draw(can)
        can.close()
        self.obj = external_pdf(pdf_string.getvalue())
        self.obj.render()
        pdf_string.close()
        return (self.obj.pdf, 'pdf')
Exemple #5
0
    def create(self, cr, uid, ids, datas, context=None):
        assert len(ids), 'You should provide some ids!'
        colors = choice_colors(len(ids))
        cr.execute(
            "SELECT MAX(mrp_production.date_planned) AS stop,MIN(mrp_production.date_planned) AS start "\
            "FROM mrp_workcenter, mrp_production, mrp_production_workcenter_line "\
            "WHERE mrp_production_workcenter_line.production_id=mrp_production.id "\
            "AND mrp_production_workcenter_line.workcenter_id=mrp_workcenter.id "\
            "AND mrp_production.state NOT IN ('cancel','done') "\
            "AND mrp_workcenter.id IN %s",(tuple(ids),))
        res = cr.dictfetchone()
        if not res['stop']:
            res['stop'] = time.strftime('%Y-%m-%d %H:%M:%S')
        if not res['start']:
            res['start'] = time.strftime('%Y-%m-%d %H:%M:%S')
        dates = self._compute_dates(datas['form']['time_unit'],
                                    res['start'][:10], res['stop'][:10])
        dates_list = dates.keys()
        dates_list.sort()
        x_index = []
        for date in dates_list:
            x_index.append((dates[date]['name'], date))
        pdf_string = StringIO.StringIO()
        can = canvas.init(fname=pdf_string, format='pdf')
        can.set_title("Work Center Loads")
        chart_object.set_defaults(line_plot.T, line_style=None)
        if datas['form']['measure_unit'] == 'cycles':
            y_label = "Load (Cycles)"
        else:
            y_label = "Load (Hours)"

        # For add the report header on the top of the report.
        tb = text_box.T(loc=(300, 500),
                        text="/hL/15/bWork Center Loads",
                        line_style=None)
        tb.draw()
        ar = area.T(legend=legend.T(),
                    x_grid_style=line_style.gray70_dash1,
                    x_axis=axis.X(label="Periods", format="/a90/hC%s"),
                    x_coord=category_coord.T(x_index, 0),
                    y_axis=axis.Y(label=y_label),
                    y_range=(0, None),
                    size=(640, 480))
        bar_plot.fill_styles.reset()
        # select workcenters
        cr.execute(
            "SELECT mw.id, rs.name FROM mrp_workcenter mw, resource_resource rs " \
            "WHERE mw.id IN %s and mw.resource_id=rs.id " \
            "ORDER BY mw.id" ,(tuple(ids),))
        workcenters = cr.dictfetchall()

        data = []
        for date in dates_list:
            vals = []
            for workcenter in workcenters:
                cr.execute(
                    "SELECT SUM(mrp_production_workcenter_line.hour) AS hours, SUM(mrp_production_workcenter_line.cycle) AS cycles, \
                                resource_resource.name AS name, mrp_workcenter.id AS id \
                            FROM mrp_production_workcenter_line, mrp_production, mrp_workcenter, resource_resource \
                            WHERE (mrp_production_workcenter_line.production_id=mrp_production.id) \
                                AND (mrp_production_workcenter_line.workcenter_id=mrp_workcenter.id) \
                                AND (mrp_workcenter.resource_id=resource_resource.id) \
                                AND (mrp_workcenter.id=%s) \
                                AND (mrp_production.date_planned BETWEEN %s AND %s) \
                            GROUP BY mrp_production_workcenter_line.workcenter_id, resource_resource.name, mrp_workcenter.id \
                            ORDER BY mrp_workcenter.id",
                    (workcenter['id'], dates[date]['start'] + ' 00:00:00',
                     dates[date]['stop'] + ' 23:59:59'))
                res = cr.dictfetchall()
                if not res:
                    vals.append(0.0)
                else:
                    if datas['form']['measure_unit'] == 'cycles':
                        vals.append(res[0]['cycles'] or 0.0)
                    else:
                        vals.append(res[0]['hours'] or 0.0)

            toto = [dates[date]['name']]
            for val in vals:
                toto.append(val)
            data.append(toto)

        workcenter_num = 0
        for workcenter in workcenters:
            f = fill_style.Plain()
            f.bgcolor = colors[workcenter_num]
            ar.add_plot(
                bar_plot.T(label=workcenter['name'],
                           data=data,
                           fill_style=f,
                           hcol=workcenter_num + 1,
                           cluster=(workcenter_num, len(res))))
            workcenter_num += 1

        if (not data) or (len(data[0]) <= 1):
            ar = self._empty_graph(time.strftime('%Y-%m-%d'))
        ar.draw(can)
        # close canvas so that the file is written to "disk"
        can.close()
        self.obj = external_pdf(pdf_string.getvalue())
        self.obj.render()
        pdf_string.close()
        return (self.obj.pdf, 'pdf')
    def create(self, cr, uid, ids, datas, context={}):
        datas = datas['form']
        if datas['users_id']:
            datas['users_id'] = datas['users_id'][0][2]
        else:
            cr.execute('select id from res_users limit 10')
            datas['users_id'] = [x[0] for x in cr.fetchall()]
        cr.execute('select id,name from res_users where id in (' +
                   ','.join(map(str, datas['users_id'])) + ')')
        users_name = dict(cr.fetchall())
        colors = choice_colors(len(datas['users_id']))
        dates = self._compute_dates(datas['type'], datas['date1'],
                                    datas['date2'])
        dates_list = dates.keys()
        dates_list.sort()
        x_index = map(lambda x: (dates[x]['name'], x), dates_list)
        pdf_string = StringIO.StringIO()
        can = canvas.init(fname=pdf_string, format='pdf')
        chart_object.set_defaults(line_plot.T, line_style=None)
        y_label = 'Number of Objects'

        ar = area.T(legend=legend.T(),
                    x_grid_style=line_style.gray70_dash1,
                    x_axis=axis.X(label=None, format="/a90/hC%s"),
                    x_coord=category_coord.T(x_index, 0),
                    y_axis=axis.Y(label=y_label),
                    y_range=(0, None),
                    size=(680, 450))
        bar_plot.fill_styles.reset()

        data = []
        for date in dates_list:
            cr.execute((
                "SELECT create_uid,count(*) FROM %s WHERE (to_char(create_date, 'YYYY-MM-DD') BETWEEN '%s' AND '%s') and id in ("
                + ','.join(map(str, datas['users_id'])) +
                ") GROUP BY create_uid") % (datas['model'].replace(
                    '.', '_'), dates[date]['start'], dates[date]['stop']))
            res = dict(cr.fetchall())

            vals = [dates[date]['name']]
            for user in datas['users_id']:
                vals.append(res.get(user, 0.0))
            data.append(vals)

        for user_id in range(len(datas['users_id'])):
            user = datas['users_id'][user_id]

            f = fill_style.Plain()
            f.bgcolor = colors[user_id]
            ar.add_plot(
                bar_plot.T(label=users_name.get(user,
                                                'Unknown').replace('/', '//'),
                           data=data,
                           hcol=user_id + 1,
                           cluster=(user_id, len(datas['users_id'])),
                           fill_style=f))

        ar.draw(can)
        can.close()
        self.obj = external_pdf(pdf_string.getvalue())
        self.obj.render()
        pdf_string.close()
        return (self.obj.pdf, 'pdf')
    def create(self, cr, uid, ids, datas, context={}):
        assert len(ids), 'You should provide some ids!'
        colors = choice_colors(len(ids))
        cr.execute(
            "SELECT MAX(mrp_production.date_planned) AS stop,MIN(mrp_production.date_planned) AS start "\
            "FROM mrp_workcenter, mrp_production, mrp_production_workcenter_line "\
            "WHERE mrp_production_workcenter_line.production_id=mrp_production.id "\
            "AND mrp_production_workcenter_line.workcenter_id=mrp_workcenter.id "\
            "AND mrp_production.state NOT IN ('cancel','done') "\
            "AND mrp_workcenter.id IN %s", (tuple(ids),))
        res = cr.dictfetchone()
        if not res['stop']:
            res['stop'] = time.strftime('%Y-%m-%d %H:%M:%S')
        if not res['start']:
            res['start'] = time.strftime('%Y-%m-%d %H:%M:%S')
        dates = self._compute_dates(datas['form']['time_unit'], res['start'][:10], res['stop'][:10])
        dates_list = dates.keys()
        dates_list.sort()
        x_index = []
        for date in dates_list:
            x_index.append((dates[date]['name'], date))
        pdf_string = StringIO.StringIO()
        can = canvas.init(fname=pdf_string, format='pdf')
        chart_object.set_defaults(line_plot.T, line_style=None)
        if datas['form']['measure_unit'] == 'cycles':
            y_label = "Load (Cycles)"
        else:
            y_label = "Load (Hours)"
        ar = area.T(legend = legend.T(),
                    x_grid_style = line_style.gray70_dash1,
                    x_axis = axis.X(label="Periods", format="/a90/hC%s"),
                    x_coord = category_coord.T(x_index, 0),
                    y_axis = axis.Y(label=y_label),
                    y_range = (0, None),
                    size = (640,480))
        bar_plot.fill_styles.reset();

        # select workcenters
        cr.execute(
            "SELECT id, name FROM mrp_workcenter " \
            "WHERE id in %s "\
            "ORDER BY mrp_workcenter.id", (tuple(ids),))
        workcenters = cr.dictfetchall()

        data = []
        for date in dates_list:
            vals = []
            for workcenter in workcenters:
                cr.execute("SELECT SUM(mrp_production_workcenter_line.hour) AS hours, SUM(mrp_production_workcenter_line.cycle) AS cycles, \
                                mrp_workcenter.name AS name, mrp_workcenter.id AS id \
                            FROM mrp_production_workcenter_line, mrp_production, mrp_workcenter \
                            WHERE (mrp_production_workcenter_line.production_id=mrp_production.id) \
                                AND (mrp_production_workcenter_line.workcenter_id=mrp_workcenter.id) \
                                AND (mrp_workcenter.id=%s) \
                                AND (mrp_production.date_planned BETWEEN %s AND %s) \
                            GROUP BY mrp_production_workcenter_line.workcenter_id, mrp_workcenter.name, mrp_workcenter.id \
                            ORDER BY mrp_workcenter.id", (workcenter['id'], dates[date]['start'] + ' 00:00:00', dates[date]['stop'] + ' 23:59:59'))
                res = cr.dictfetchall()
                if not res:
                    vals.append(0.0)
                else:
                    if datas['form']['measure_unit'] == 'cycles':
                        vals.append(res[0]['cycles'] or 0.0)
                    else:
                        vals.append(res[0]['hours'] or 0.0)
                    
            toto = [dates[date]['name']]
            for val in vals:
                toto.append(val)
            data.append(toto)

        workcenter_num = 0
        for workcenter in workcenters:
            f = fill_style.Plain()
            f.bgcolor = colors[workcenter_num]
            ar.add_plot(bar_plot.T(label=workcenter['name'], data=data, fill_style=f, hcol=workcenter_num+1, cluster=(workcenter_num, len(res))))
            workcenter_num += 1

        #plot = bar_plot.T(label=workcenter['name'], data=data, hcol=1, fill_style=fill_style.white, cluster=(color_index,len(ids)))
        if (not data) or (len(data[0]) <= 1):
            ar = self._empty_graph(time.strftime('%Y-%m-%d'))
        ar.draw(can)
        # close canvas so that the file is written to "disk"
        can.close()
        self.obj = external_pdf(pdf_string.getvalue())
        self.obj.render()
        pdf_string.close()
        return (self.obj.pdf, 'pdf')