add Copyright and GPL license into the Header of Python files
[odoo/odoo.git] / addons / stock / report / stock_graph.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 ###############################################################################
28 from mx.DateTime import *
29 from pychart import *
30 import pychart.legend
31 import time
32 from report.misc import choice_colors
33
34 #
35 # dRAw a graph for stocks
36 #
37 class stock_graph(object):
38         def __init__(self, io):
39                 self._datas = {}
40                 self._canvas = canvas.init(fname=io, format='pdf')
41                 self._canvas.set_author("Tiny ERP")
42                 self._names = {}
43                 self.val_min = ''
44                 self.val_max = ''
45
46         def add(self, product_id, product_name, datas):
47                 if hasattr(product_name, 'replace'):
48                         product_name=product_name.replace('/', '//')
49                 if product_id not in self._datas:
50                         self._datas[product_id] = {}
51                 self._names[product_id] = product_name
52                 for (dt,stock) in datas:
53                         if not dt in self._datas[product_id]:
54                                 self._datas[product_id][dt]=0
55                         self._datas[product_id][dt]+=stock
56                         if self.val_min:
57                                 self.val_min = min(self.val_min,dt)
58                         else:
59                                 self.val_min = dt
60                         self.val_max = max(self.val_max,dt)
61
62         def draw(self):
63                 colors = choice_colors(len(self._datas.keys()))
64                 user_color = {}
65                 for user in self._datas.keys():
66                         user_color[user] = colors.pop()
67
68                 val_min = int(time.mktime(time.strptime(self.val_min,'%Y-%m-%d')))
69                 val_max = int(time.mktime(time.strptime(self.val_max,'%Y-%m-%d')))
70
71                 plots = []
72                 for product_id in self._datas:
73                         f = fill_style.Plain()
74                         f.bgcolor = user_color[user]
75                         datas = self._datas[product_id].items()
76                         datas = map(lambda x: (int(time.mktime(time.strptime(x[0],'%Y-%m-%d'))),x[1]), datas)
77                         datas.sort()
78                         datas2 = []
79                         val = 0
80                         for d in datas:
81                                 val+=d[1]
82
83                                 if len(datas2):
84                                         d2 = d[0]-60*61*24
85                                         if datas2[-1][0]<d2-1000:
86                                                 datas2.append((d2,datas2[-1][1]))
87                                 datas2.append((d[0],val))
88                         if len(datas2) and datas2[-1][0]<val_max-100:
89                                 datas2.append((val_max, datas2[-1][1]))
90                         if len(datas2)==1:
91                                 datas2.append( (datas2[0][0]+100, datas2[0][1]) )
92                         st = line_style.T()
93                         st.color = user_color[product_id]
94                         st.width = 1
95                         st.cap_style=1
96                         st.join_style=1
97                         plot = line_plot.T(label=self._names[product_id], data=datas2, line_style=st)
98                         plots.append(plot)
99
100                 interval = max((val_max-val_min)/15, 86400)
101                 x_axis = axis.X(format=lambda x:'/a60{}'+time.strftime('%Y-%m-%d',time.gmtime(x)), tic_interval=interval, label=None)
102                 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)
103                 for plot in plots:
104                         ar.add_plot(plot)
105                 ar.draw(self._canvas)
106
107         def close(self):
108                 self._canvas.close()
109
110 if __name__ == '__main__':
111         gt = stock_graph('test.pdf')
112         gt.add(1, 'Pomme', [('2005-07-29', 6), ('2005-07-30', -2), ('2005-07-31', 4)])
113         gt.add(2, 'Cailloux', [('2005-07-29', 9), ('2005-07-30', -4), ('2005-07-31', 2)])
114         gt.draw()
115         gt.close()