4d4c03fb80ca0d6e9e40f96ce795f32482c7fb88
[odoo/odoo.git] / addons / stock / report / stock_graph.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 from pychart import *
22 import pychart.legend
23 import time
24 from report.misc import choice_colors
25
26 #
27 # Draw a graph for stocks
28 #
29 class stock_graph(object):
30     def __init__(self, io):
31         self._datas = {}
32         self._canvas = canvas.init(fname=io, format='pdf')
33         self._canvas.set_author("OpenERP")
34         self._names = {}
35         self.val_min = ''
36         self.val_max = ''
37
38     def add(self, product_id, product_name, datas):
39         if hasattr(product_name, 'replace'):
40             product_name=product_name.replace('/', '//')
41         if product_id not in self._datas:
42             self._datas[product_id] = {}
43         self._names[product_id] = product_name
44         for (dt,stock) in datas:
45             if not dt in self._datas[product_id]:
46                 self._datas[product_id][dt]=0
47             self._datas[product_id][dt]+=stock
48             if self.val_min:
49                 self.val_min = min(self.val_min,dt)
50             else:
51                 self.val_min = dt
52             self.val_max = max(self.val_max,dt)
53
54     def draw(self):
55         colors = choice_colors(len(self._datas.keys()))
56         user_color = {}
57         for user in self._datas.keys():
58             user_color[user] = colors.pop()
59
60         val_min = int(time.mktime(time.strptime(self.val_min,'%Y-%m-%d')))
61         val_max = int(time.mktime(time.strptime(self.val_max,'%Y-%m-%d')))
62
63         plots = []
64         for product_id in self._datas:
65             f = fill_style.Plain()
66             f.bgcolor = user_color[user]
67             datas = self._datas[product_id].items()
68             datas = map(lambda x: (int(time.mktime(time.strptime(x[0],'%Y-%m-%d'))),x[1]), datas)
69             datas.sort()
70             datas2 = []
71             val = 0
72             for d in datas:
73                 val+=d[1]
74
75                 if len(datas2):
76                     d2 = d[0]-60*61*24
77                     if datas2[-1][0]<d2-1000:
78                         datas2.append((d2,datas2[-1][1]))
79                 datas2.append((d[0],val))
80             if len(datas2) and datas2[-1][0]<val_max-100:
81                 datas2.append((val_max, datas2[-1][1]))
82             if len(datas2)==1:
83                 datas2.append( (datas2[0][0]+100, datas2[0][1]) )
84             st = line_style.T()
85             st.color = user_color[product_id]
86             st.width = 1
87             st.cap_style=1
88             st.join_style=1
89             plot = line_plot.T(label=self._names[product_id], data=datas2, line_style=st)
90             plots.append(plot)
91
92         interval = max((val_max-val_min)/15, 86400)
93         x_axis = axis.X(format=lambda x:'/a60{}'+time.strftime('%Y-%m-%d',time.gmtime(x)), tic_interval=interval, label=None)
94         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)
95         for plot in plots:
96             ar.add_plot(plot)
97         ar.draw(self._canvas)
98
99     def close(self):
100         self._canvas.close()
101
102 if __name__ == '__main__':
103     gt = stock_graph('test.pdf')
104     gt.add(1, 'Pomme', [('2005-07-29', 6), ('2005-07-30', -2), ('2005-07-31', 4)])
105     gt.add(2, 'Cailloux', [('2005-07-29', 9), ('2005-07-30', -4), ('2005-07-31', 2)])
106     gt.draw()
107     gt.close()
108
109 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
110