1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from datetime import datetime
23 from dateutil.relativedelta import relativedelta
26 from openerp import osv
28 from openerp.report.interface import report_int
29 from openerp.report.render import render
35 class external_pdf(render):
36 def __init__(self, pdf):
39 self.output_type='pdf'
45 class report_stock(report_int):
46 def create(self, cr, uid, ids, datas, context=None):
49 registry = openerp.registry(cr.dbname)
51 if 'location_id' in context:
52 location_id = context['location_id']
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
57 loc_ids = registry['stock.location'].search(cr, uid, [('location_id','child_of',[location_id])])
59 now = time.strftime('%Y-%m-%d')
63 names = dict(registry['product.product'].name_get(cr, uid, product_ids))
65 names[name] = names[name].encode('utf8')
67 prods = registry['stock.location']._product_all_get(cr, uid, location_id, product_ids)
70 products[p] = [(now,prods[p])]
73 if not loc_ids or not product_ids:
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) "
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():
84 dt= (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d')
87 products.setdefault(prod_id, [])
88 products[prod_id].append((dt,-qty))
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) "
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),))
97 for (qty, dt, prod_id) in cr.fetchall():
99 dt= (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d')
102 products.setdefault(prod_id, [])
103 products[prod_id].append((dt,qty))
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])
119 self.obj = external_pdf(io.getvalue())
121 return (self.obj.pdf, 'pdf')
122 report_stock('report.stock.product.history')
125 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: