def create(self, cr, uid, ids, datas, context={}): product_ids = ids location_id = context.get('location_id', False) if not location_id: warehouse_id = pooler.get_pool(cr.dbname).get('stock.warehouse').search(cr, uid, [])[0] location_id = pooler.get_pool(cr.dbname).get('stock.warehouse').browse(cr, uid, warehouse_id).lot_stock_id.id loc_ids = pooler.get_pool(cr.dbname).get('stock.location').search(cr, uid, [('location_id','child_of',[location_id])]) now = time.strftime('%Y-%m-%d') dt_from = now dt_to = now names = dict(pooler.get_pool(cr.dbname).get('product.product').name_get(cr, uid, product_ids, context=context),) for name in names: names[name] = names[name].encode('utf8') products = {} prods = pooler.get_pool(cr.dbname).get('stock.location')._product_all_get(cr, uid, location_id, product_ids, context=context) for p in prods: products[p] = [(now,prods[p])] prods[p] = 0 if not loc_ids or not product_ids: return (False, 'pdf') cr.execute("select sum(r.product_qty * u.factor), r.date, r.product_id " "from stock_move r left join product_uom u on (r.product_uom=u.id) " "where state IN %s" "and location_id IN %s" "and product_id IN %s" "group by date,product_id",(('confirmed','assigned','waiting'),tuple(loc_ids) ,tuple(product_ids),)) for (qty, dt, prod_id) in cr.fetchall(): if dt<=dt_from: dt= (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d') else: dt = dt[:10] products.setdefault(prod_id, []) products[prod_id].append((dt,-qty)) cr.execute("select sum(r.product_qty * u.factor), r.date, r.product_id " "from stock_move r left join product_uom u on (r.product_uom=u.id) " "where state IN %s" "and location_dest_id IN %s" "and product_id IN %s" "group by date,product_id",(('confirmed','assigned','waiting'),tuple(loc_ids) ,tuple(product_ids),)) for (qty, dt, prod_id) in cr.fetchall(): if dt<=dt_from: dt= (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d') else: dt = dt[:10] products.setdefault(prod_id, []) products[prod_id].append((dt,qty)) dt = dt_from qty = 0 io = StringIO.StringIO() gt = stock_graph.stock_graph(io, context.get('lang', 'en_US'), cr) for prod_id in products: gt.add(prod_id, names.get(prod_id, 'Unknown'), products[prod_id]) gt.draw() gt.close() self.obj = external_pdf(io.getvalue()) self.obj.render() return (self.obj.pdf, 'pdf')
def create(self, cr, uid, ids, datas, context=None): if context is None: context = {} registry = openerp.registry(cr.dbname) product_ids = ids if 'location_id' in context: location_id = context['location_id'] else: warehouse_id = registry['stock.warehouse'].search(cr, uid, [])[0] location_id = registry['stock.warehouse'].browse( cr, uid, warehouse_id).lot_stock_id.id loc_ids = registry['stock.location'].search( cr, uid, [('location_id', 'child_of', [location_id])]) now = time.strftime('%Y-%m-%d') dt_from = now dt_to = now names = dict(registry['product.product'].name_get( cr, uid, product_ids)) for name in names: names[name] = names[name].encode('utf8') products = {} prods = registry['stock.location']._product_all_get( cr, uid, location_id, product_ids) for p in prods: products[p] = [(now, prods[p])] prods[p] = 0 if not loc_ids or not product_ids: return (False, 'pdf') cr.execute( "select sum(r.product_qty * u.factor), r.date, r.product_id " "from stock_move r left join product_uom u on (r.product_uom=u.id) " "where state IN %s" "and location_id IN %s" "and product_id IN %s" "group by date,product_id", ( ('confirmed', 'assigned', 'waiting'), tuple(loc_ids), tuple(product_ids), )) for (qty, dt, prod_id) in cr.fetchall(): if dt <= dt_from: dt = (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d') else: dt = dt[:10] products.setdefault(prod_id, []) products[prod_id].append((dt, -qty)) cr.execute( "select sum(r.product_qty * u.factor), r.date, r.product_id " "from stock_move r left join product_uom u on (r.product_uom=u.id) " "where state IN %s" "and location_dest_id IN %s" "and product_id IN %s" "group by date,product_id", ( ('confirmed', 'assigned', 'waiting'), tuple(loc_ids), tuple(product_ids), )) for (qty, dt, prod_id) in cr.fetchall(): if dt <= dt_from: dt = (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d') else: dt = dt[:10] products.setdefault(prod_id, []) products[prod_id].append((dt, qty)) dt = dt_from qty = 0 io = StringIO.StringIO() gt = stock_graph.stock_graph(io) for prod_id in products: prod_name = names.get(prod_id, 'Unknown') if isinstance(prod_name, str): prod_name = prod_name.decode('utf-8') prod_name = unicodedata.normalize('NFKD', prod_name) prod_name = prod_name.encode('ascii', 'replace') gt.add(prod_id, prod_name, products[prod_id]) gt.draw() gt.close() self.obj = external_pdf(io.getvalue()) self.obj.render() return (self.obj.pdf, 'pdf')
def create(self, cr, uid, ids, datas, context=None): if context is None: context = {} registry = openerp.registry(cr.dbname) product_ids = ids if 'location_id' in context: location_id = context['location_id'] else: warehouse_id = registry['stock.warehouse'].search(cr, uid, [])[0] location_id = registry['stock.warehouse'].browse(cr, uid, warehouse_id).lot_stock_id.id loc_ids = registry['stock.location'].search(cr, uid, [('location_id','child_of',[location_id])]) now = time.strftime('%Y-%m-%d') dt_from = now dt_to = now names = dict(registry['product.product'].name_get(cr, uid, product_ids)) for name in names: names[name] = names[name].encode('utf8') products = {} ctx = context.copy() ctx['location_id'] = loc_ids prods = registry['product.product']._product_available(cr, uid, product_ids, context=ctx) for prod in prods.keys(): products[prod] = [(now, prods[prod]['qty_available'])] prods[prod] = 0 if not loc_ids or not product_ids: return (False, 'pdf') cr.execute("select sum(r.product_qty * u.factor), r.date, r.product_id " "from stock_move r left join product_uom u on (r.product_uom=u.id) " "where state IN %s" "and location_id IN %s" "and product_id IN %s" "group by date,product_id",(('confirmed','assigned','waiting'),tuple(loc_ids) ,tuple(product_ids),)) for (qty, dt, prod_id) in cr.fetchall(): if dt<=dt_from: dt= (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d') else: dt = dt[:10] products.setdefault(prod_id, []) products[prod_id].append((dt,-qty)) cr.execute("select sum(r.product_qty * u.factor), r.date, r.product_id " "from stock_move r left join product_uom u on (r.product_uom=u.id) " "where state IN %s" "and location_dest_id IN %s" "and product_id IN %s" "group by date,product_id",(('confirmed','assigned','waiting'),tuple(loc_ids) ,tuple(product_ids),)) for (qty, dt, prod_id) in cr.fetchall(): if dt<=dt_from: dt= (datetime.now() + relativedelta(days=1)).strftime('%Y-%m-%d') else: dt = dt[:10] products.setdefault(prod_id, []) products[prod_id].append((dt,qty)) dt = dt_from qty = 0 io = StringIO.StringIO() gt = stock_graph.stock_graph(io) for prod_id in products: prod_name = names.get(prod_id,'Unknown') if isinstance(prod_name, str): prod_name = prod_name.decode('utf-8') prod_name = unicodedata.normalize('NFKD',prod_name) prod_name = prod_name.encode('ascii','replace') gt.add(prod_id, prod_name, products[prod_id]) gt.draw() gt.close() self.obj = external_pdf(io.getvalue()) self.obj.render() return (self.obj.pdf, 'pdf')