[MERGE] Sync with trunk, until revision 8927
[odoo/odoo.git] / addons / stock / report / product_stock.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
22 from datetime import datetime
23 from dateutil.relativedelta import relativedelta
24
25 import openerp
26 from openerp import osv
27 import time
28 from openerp.report.interface import report_int
29 from openerp.report.render import render
30
31 import stock_graph
32 import StringIO
33 import unicodedata
34
35 class external_pdf(render):
36     def __init__(self, pdf):
37         render.__init__(self)
38         self.pdf = pdf
39         self.output_type='pdf'
40
41     def _render(self):
42         return self.pdf
43
44
45 class report_stock(report_int):
46     def create(self, cr, uid, ids, datas, context=None):
47         if context is None:
48             context = {}
49         registry = openerp.registry(cr.dbname)
50         product_ids = ids
51         if 'location_id' in context:
52             location_id = context['location_id']
53         else:
54             warehouse_id = registry['stock.warehouse'].search(cr, uid, [])[0]
55             location_id = registry['stock.warehouse'].browse(cr, uid, warehouse_id).lot_stock_id.id
56
57         loc_ids = registry['stock.location'].search(cr, uid, [('location_id','child_of',[location_id])])
58
59         now = time.strftime('%Y-%m-%d')
60         dt_from = now
61         dt_to = now
62
63         names = dict(registry['product.product'].name_get(cr, uid, product_ids))
64         for name in names:
65             names[name] = names[name].encode('utf8')
66         products = {}
67         prods = registry['stock.location']._product_all_get(cr, uid, location_id, product_ids)
68
69         for p in prods:
70             products[p] = [(now,prods[p])]
71             prods[p] = 0
72
73         if not loc_ids or not product_ids:
74             return (False, 'pdf')
75
76         cr.execute("select sum(r.product_qty * u.factor), r.date, r.product_id "
77                    "from stock_move r left join product_uom u on (r.product_uom=u.id) "
78                    "where state IN %s"
79                    "and location_id IN %s"
80                    "and product_id IN %s"
81                    "group by date,product_id",(('confirmed','assigned','waiting'),tuple(loc_ids) ,tuple(product_ids),))
82         for (qty, dt, prod_id) in cr.fetchall():
83             if dt<=dt_from:
84                 dt= (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d')
85             else:
86                 dt = dt[:10]
87             products.setdefault(prod_id, [])
88             products[prod_id].append((dt,-qty))
89
90         cr.execute("select sum(r.product_qty * u.factor), r.date, r.product_id "
91                    "from stock_move r left join product_uom u on (r.product_uom=u.id) "
92                    "where state IN %s"
93                    "and location_dest_id IN %s"
94                    "and product_id IN %s"
95                    "group by date,product_id",(('confirmed','assigned','waiting'),tuple(loc_ids) ,tuple(product_ids),))
96
97         for (qty, dt, prod_id) in cr.fetchall():
98             if dt<=dt_from:
99                 dt= (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d')
100             else:
101                 dt = dt[:10]
102             products.setdefault(prod_id, [])
103             products[prod_id].append((dt,qty))
104
105         dt = dt_from
106         qty = 0
107
108         io = StringIO.StringIO()
109         gt = stock_graph.stock_graph(io)
110         for prod_id in products:
111             prod_name = names.get(prod_id,'Unknown')
112             if isinstance(prod_name, str):
113                  prod_name = prod_name.decode('utf-8')
114                  prod_name = unicodedata.normalize('NFKD',prod_name)
115                  prod_name = prod_name.encode('ascii','replace')
116             gt.add(prod_id, prod_name, products[prod_id])   
117         gt.draw()
118         gt.close()
119         self.obj = external_pdf(io.getvalue())
120         self.obj.render()
121         return (self.obj.pdf, 'pdf')
122 report_stock('report.stock.product.history')
123
124
125 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
126