def createNohinsho(): xlsdata = session['new_supply_data'] #session['new_supply_data'] = None date = xlsdata['date'] customer = Customer.query.filter_by(id=xlsdata['customer']).first() products = [] pr = xlsdata['products'] if not pr: flash(gettext('No product data!')) return redirect(url_for("supplies")) for p in pr: products.append({'product': Product.query.filter_by(id=p[0]).first(), 'qty': p[1]}) products = sorted(products, key=lambda k: (k['product'].maker_id, k['product'].code)) if xlsdata: xls = CreateXls() nohinsho = xls.nohinsho(date, customer, products) match = re.search(r"[^a-zA-Z](nohinsho)[^a-zA-Z]", nohinsho) pos = match.start(1) filename = nohinsho[pos:] return redirect(url_for('download_file', filename=filename)) flash(gettext('Invalid data received.')) return redirect(url_for("supplies"))
def placeOrder(): formQuantities = ProductQuantityForm(request.form) maker_id = request.form['maker_id'] if not maker_id: flash(gettext("Maker not found.")) return redirect(url_for("orders")) if formQuantities.validate_on_submit(): new_order = Order() #TODO other date than now new_order.created_dt = new_order.maker_id = maker_id new_order.user_id = g.user.id db.session.add(new_order) db.session.commit() #add ordered products to new order form_data = formQuantities.data['fields'] for product in form_data: new_quantity = int(product['qty_order']) if new_quantity > 0: new_product = Product.query.filter_by(id=int(product['product_id'])).first() if new_product: op = OrderedProducts(quantity=new_quantity) op.product = new_product op.order_id = new_order.id new_order.products.append(op) db.session.commit() # Create order sheet xls xls = CreateXls() maker = Maker.query.filter_by(id=maker_id).first() maker_name = maker.name data = [] for item in form_data: if item['qty_order'] < 1: continue product = Product.query.filter_by(id=item['product_id']).first() if product: data.append({'product_code': product.code, 'product_maker_code': product.maker_code,\ 'product_name': product.desc_CS, 'quantity': item['qty_order']}) order_xls = xls.order_sheet(maker_name, data) match = re.search(r"[^a-zA-Z](ordersheet)[^a-zA-Z]", order_xls) pos = match.start(1) filename = order_xls[pos:] flash(gettext('New order to maker was successfully created.')) return render_template('orders/ordersheet.html', title=gettext("Order Sheet Management"), ordersheet_file=filename, maker=maker, mail_sent=False) #if not validated return to maker select products = Product.query.order_by(Product.maker_id, Product.code)\ .filter_by(maker_id=int(maker_id), active_flg=True).all() formMaker = SelectMakerForm() formMaker.maker.choices = [(a.id, a.name) for a in Maker.query.all()] return render_template('orders/createOrder.html', title=gettext("Place new order to maker"), formMaker=formMaker, formQuantities=formQuantities, products=products)