[IMP] Update the copyright to 2009
[odoo/odoo.git] / addons / stock / report / stock_graph.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22 from mx.DateTime import *
23 from pychart import *
24 import pychart.legend
25 import time
26 from report.misc import choice_colors
27
28 #
29 # dRAw a graph for stocks
30 #
31 class stock_graph(object):
32     def __init__(self, io):
33         self._datas = {}
34         self._canvas = canvas.init(fname=io, format='pdf')
35         self._canvas.set_author("Open ERP")
36         self._names = {}
37         self.val_min = ''
38         self.val_max = ''
39
40     def add(self, product_id, product_name, datas):
41         if hasattr(product_name, 'replace'):
42             product_name=product_name.replace('/', '//')
43         if product_id not in self._datas:
44             self._datas[product_id] = {}
45         self._names[product_id] = product_name
46         for (dt,stock) in datas:
47             if not dt in self._datas[product_id]:
48                 self._datas[product_id][dt]=0
49             self._datas[product_id][dt]+=stock
50             if self.val_min:
51                 self.val_min = min(self.val_min,dt)
52             else:
53                 self.val_min = dt
54             self.val_max = max(self.val_max,dt)
55
56     def draw(self):
57         colors = choice_colors(len(self._datas.keys()))
58         user_color = {}
59         for user in self._datas.keys():
60             user_color[user] = colors.pop()
61
62         val_min = int(time.mktime(time.strptime(self.val_min,'%Y-%m-%d')))
63         val_max = int(time.mktime(time.strptime(self.val_max,'%Y-%m-%d')))
64
65         plots = []
66         for product_id in self._datas:
67             f = fill_style.Plain()
68             f.bgcolor = user_color[user]
69             datas = self._datas[product_id].items()
70             datas = map(lambda x: (int(time.mktime(time.strptime(x[0],'%Y-%m-%d'))),x[1]), datas)
71             datas.sort()
72             datas2 = []
73             val = 0
74             for d in datas:
75                 val+=d[1]
76
77                 if len(datas2):
78                     d2 = d[0]-60*61*24
79                     if datas2[-1][0]<d2-1000:
80                         datas2.append((d2,datas2[-1][1]))
81                 datas2.append((d[0],val))
82             if len(datas2) and datas2[-1][0]<val_max-100:
83                 datas2.append((val_max, datas2[-1][1]))
84             if len(datas2)==1:
85                 datas2.append( (datas2[0][0]+100, datas2[0][1]) )
86             st = line_style.T()
87             st.color = user_color[product_id]
88             st.width = 1
89             st.cap_style=1
90             st.join_style=1
91             plot = line_plot.T(label=self._names[product_id], data=datas2, line_style=st)
92             plots.append(plot)
93
94         interval = max((val_max-val_min)/15, 86400)
95         x_axis = axis.X(format=lambda x:'/a60{}'+time.strftime('%Y-%m-%d',time.gmtime(x)), tic_interval=interval, label=None)
96         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)
97         for plot in plots:
98             ar.add_plot(plot)
99         ar.draw(self._canvas)
100
101     def close(self):
102         self._canvas.close()
103
104 if __name__ == '__main__':
105     gt = stock_graph('test.pdf')
106     gt.add(1, 'Pomme', [('2005-07-29', 6), ('2005-07-30', -2), ('2005-07-31', 4)])
107     gt.add(2, 'Cailloux', [('2005-07-29', 9), ('2005-07-30', -4), ('2005-07-31', 2)])
108     gt.draw()
109     gt.close()
110
111 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
112