def createRequest(): cust = flask.request.args.get('cust') if not cust: cust = 'cust' if cust == 'cust': formCustomer = SelectCustomerForm() cust_customers = Customer.query.filter_by(customer_type=CUSTOMER_TYPES['TYPE_CUSTOMER']).all() cust_customers = [(a.id, a.name) for a in cust_customers] cust_customers = [(0, '')] + cust_customers formCustomer.customer.choices = cust_customers else: formCustomer = OrderNumberForm() makers = Maker.query.all() maker_choices = [(a.id, a.name) for a in makers] maker_choices = [(0, '')] + maker_choices formCustomer.maker.choices = maker_choices if formCustomer.is_submitted(): ids = {} for attr in flask.request.form: if attr.startswith("req_qty-"): key = attr.split('-')[1] ids[key] = flask.request.form[attr] if ids: if cust == 'cust': cust_id = int(formCustomer.customer.data) if not cust_id: flash(gettext("No customer id.")) return redirect(url_for("requests")) req_cust = db.session.query(Customer).filter_by(id=cust_id).first() if not req_cust: flash(gettext("Customer not found.")) return redirect(url_for("requests")) if req_cust.customer_type != CUSTOMER_TYPES['TYPE_CUSTOMER']: flash(gettext("Invalid customer!")) return redirect(url_for("requests")) elif cust == 'axm': order_no = formCustomer.order_no.data if not order_no: flash(gettext("No order number.")) return redirect(url_for("axm_requests")) req_cust = db.session.query(Customer).filter_by(order_no=order_no).first() if req_cust: flash(gettext("Order number already exists!")) return redirect(url_for("axm_requests")) req_cust = Customer() req_cust.customer_type = CUSTOMER_TYPES['TYPE_AXM'] req_cust.order_no = order_no db.session.add(req_cust) db.session.commit() else: flash(gettext("Invalid data received!")) return redirect(url_for("requests")) new_request = Request() new_request.user_id = g.user.id new_request.customer_id = req_cust.id if formCustomer.datetime.data: new_request.created_dt = formCustomer.datetime.data else: flash("Date and time date of request was entered incorrectly, request saved with current date and time.") if cust == 'axm': if formCustomer.payment_method.data is not None: new_request.payment_method = formCustomer.payment_method.data else: new_request.payment_method = 1 if formCustomer.paid_for_flg is not None: new_request.paid_for_flg = formCustomer.paid_for_flg.data else: new_request.paid_for_flg = 0 new_request.active_flg = True db.session.add(new_request) db.session.commit() for id in ids: new_product = db.session.query(Product).filter_by(id=id).first() if not new_product: flash(gettext("Product not found.")) return redirect(url_for("requests")) rp = RequestedProducts(quantity=int(ids[id])) rp.product = new_product rp.request_id = new_request.id new_request.products.append(rp) db.session.commit() flash(gettext("Order created successfully.")) if cust == 'axm': return redirect(url_for("axm_requests")) return redirect(url_for("requests")) else: flash(gettext("Order data not sent.")) return redirect(url_for("requests")) formCustomer.datetime.data = datetime.utcnow() return render_template('requests/createRequest.html', title=gettext("Accept order from customer"), formCustomer=formCustomer, custType=cust)
def placeorder(): cart_items = Cart.query.filter_by(user_id=current_user.id).all() form = ShopOrderConfirmForm() if form.validate_on_submit(): new_request = Request() new_request.user_id = current_user.id if not current_user.customer: flash(gettext("We apologize but your customer data is insufficient. Please, contact our customer support.")) return redirect(url_for("shop")) customer_note = form.note.data if customer_note: new_request.note = customer_note new_request.customer_id = current_user.customer.id new_request.active_flg = True db.session.add(new_request) db.session.commit() for item in cart_items: new_product = db.session.query(Product).filter_by(id=item.product_id).first() if not new_product: flash(gettext("Product not found.")) return redirect(url_for("shop")) rp = RequestedProducts(quantity=item.quantity) rp.product = new_product rp.request_id = new_request.id new_request.products.append(rp) db.session.delete(item) db.session.commit() # send confirmation email mailer.order_confirmation(current_user, new_request) flash(gettext("Order created successfully.")) return redirect(url_for("orderconfirm", req_id=new_request.id)) discount = 0 if current_user.role == USER_ROLES['ROLE_CUSTOMER'] and current_user.customer: discount = current_user.customer.base_discount total = 0 pieces = 0 for item in cart_items: if not item.product.price_retail: item.product.price_retail = 0 unrounded_price = item.product.price_retail * (1.0 - discount) item.customer_price = int(5 * round(float(unrounded_price)/5)) total += item.customer_price * item.quantity pieces += item.quantity urls = getImgUrls(item.product.id) if urls: item.img_url = urls[0].split('app')[1] else: item.img_url = NO_PHOTO_URL.split('app')[1] return render_template('/shop/placeorder.html', title=gettext('Place order'), cart_items=cart_items, total=total, pieces=pieces, form=form)