def create_new_shipment(order_id): sql = "SELECT batchorders.batchorder_id FROM batchorders WHERE batchorders.order_id = :order_id" result = db.session.execute(sql, {"order_id": order_id}) batchorder_ids = result.fetchall() if batchorder_ids == None: flash("Invalid order id", "danger") return False user_id = users.get_user_id() inserts = [] for id in batchorder_ids: insert = { "user_id": user_id, "batchorder_id": id[0], "order_id": order_id } inserts.append(insert) sql = "INSERT INTO shipments (batchorder_id, user_id, order_id, done, collected) VALUES (:batchorder_id, :user_id, :order_id, NOW(), TRUE)" db.session.execute(sql, inserts) db.session.commit() flash("Order %s completed" % order_id, "success") return True
def finalize_order(): security.has_csrf_token(request.form["csrf_token"]) security.has_role([1, 4, 5, 6]) company_id = None if session["auth_lvl"] == 4: company_id = request.form["company_id"] random_number = str(randint(0, 9999999)) order_id = random_number.zfill(7) user_id = users.get_user_id() cart = session["cart"] orderList = [] for i in range(len(cart)): order = None if security.has_auth([4]): order = {'order_id': order_id, 'company_id': company_id, 'item_id': cart[i]["item_id"], 'qty': cart[i]["qty"], 'user_id': user_id, 'price': cart[i]["price"]} if security.has_auth([1, 5, 6]): order = {'order_id': order_id, 'company_id': cart[i]["company_id"], 'item_id': cart[i]["item_id"], 'qty': cart[i]["qty"], 'user_id': user_id, 'price': cart[i]["price"]} orderList.append(order) session["cart"] = [] if security.has_auth([5, 6]): orders.create_supply_order(orderList) flash("Order was successful", "success") return order_id if security.has_auth([1, 4]): success = orders.create_sale_order(order_id, orderList) if success: return order_id else: flash("Sale order unsuccessful", "danger")
def add_item_to_sale_order(order_id, company_id, item_id, qty): added_item = item.get_item_by_id(item_id) if added_item == None: flash("Invalid item_id", "danger") return sql = "SELECT qty FROM orders WHERE order_id = :order_id AND item_id = :item_id AND company_id = :company_id" result = db.session.execute(sql, { "order_id": order_id, "item_id": item_id, "company_id": company_id }) sale = result.fetchone() qty = int(qty) if sale == None: sql = """INSERT INTO orders (order_id, company_id, item_id, supply, orderDate, dispatchDate, qty, price, user_id) VALUES (:order_id, :company_id, :item_id, FALSE, NOW(), NOW() + INTERVAL '1 DAY', :qty, :price, :user_id);""" price = round((float(qty) * float(item.get_price(item_id)) * 1.0), 2) else: sql = "UPDATE orders SET price = :price, qty = :qty WHERE company_id = :company_id AND item_id = :item_id" qty += int(sale[0]) price = round(float(item.get_price(item_id)) * float(qty), 2) user_id = users.get_user_id() db.session.execute( sql, { "order_id": order_id, "company_id": company_id, "item_id": item_id, "qty": qty, "price": price, "user_id": user_id }) db.session.commit() flash("%s added to order" % added_item[1], "success")
def add_item_to_cart(): security.has_csrf_token(request.form["csrf_token"]) security.has_role([1, 4, 5, 6]) item = None if session["auth_lvl"] == 1: company_id = users.get_company_id() else: company_id = request.form["company_id"] item_name = request.form["item_name"] item_id = request.form["item_id"] qty = request.form["qty"] price = request.form["price"] user_id = users.get_user_id() cart = list(session["cart"]) if session["auth_lvl"] == 6 or session["auth_lvl"] == 5 or session["auth_lvl"] == 1: item = {"company_id": company_id, "item_id": item_id, "item_name": item_name, "qty": qty, "price": price, "user_id": user_id} if session["auth_lvl"] == 4: item = {"item_id": item_id, "item_name": item_name, "qty": qty, "price": price, "user_id": user_id} new_item = True for i in range(len(cart)): if cart[i]["item_id"] == item_id: cart[i]["qty"] = int(cart[i]["qty"]) + int(qty) cart[i]["price"] = price new_item = False if new_item: cart.append(item) session["cart"] = cart return str(len(cart))
def create_new_batch(order_id, qty): if "e" in qty: flash("Invalid quantity", "danger") return qty = int(qty) if qty <= 0: flash("Quantity must be larger than zero", "danger") return False order = get_supply_order_by_id(order_id) if qty > order[4]: flash("Quantity exceeds order quantity", "danger") return False if order == None: flash("Order %s does not exist" % order_id, "danger") return False current_date = date.today().strftime("%d%m%y") item = order[2] prefix = item[0].upper() + item[len(item) - 1].upper() batch_nr = prefix + current_date + order[3] if check_if_batch_exists(batch_nr): order_qty = int(order[4]) updated_qty = order_qty - qty if updated_qty < 0: updated_qty = 0 add_batch_qty(batch_nr, qty) update_supply_order_qty(order_id, updated_qty, False) flash("Batch %s updated succesfully" % batch_nr, "success") return False order_qty = int(order[4]) updated_qty = order_qty - qty if updated_qty < 0: flash("Transferred quantity exceeds order quantity", "danger") return False user_id = users.get_user_id() sql2 = """INSERT INTO batches (batch_nr, order_id, item_id, qty, date, user_id) VALUES (:batch_nr, :order_id, :item_id, :qty, :now, :user_id)""" db.session.execute( sql2, { "batch_nr": batch_nr, "order_id": order_id, "item_id": order[1], "qty": qty, "now": order[5], "user_id": user_id }) sql3 = "UPDATE orders SET qty = :updated_qty WHERE order_id = :order_id" db.session.execute(sql3, { "updated_qty": updated_qty, "order_id": order_id }) db.session.commit() flash("Batch %s created successfully" % batch_nr, "success") return True