Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.1 kB
1
Indexable
Never
# Place Order
def place_order(customer_name):
    try:
        display_menu()
        item_id = int(input("\nEnter the Item ID to order: "))
        quantity = int(input("Enter the quantity: "))
        packaging = input("Enter 'Dine-in' or 'Take away': ").capitalize()

        order_date = time.strftime('%Y-%m-%d')
        cur.execute('SELECT ItemID FROM Menu')
        data = cur.fetchall()

        if (item_id,) in data:
            cur.execute("INSERT INTO Orders (ItemID, CustomerName, OrderDate, Quantity, Packaging) "
                        "VALUES (%s, %s, %s, %s, %s)",
                        (item_id, customer_name, order_date, quantity, packaging))
            print(f"\nOrder placed successfully for {customer_name}!")
            con.commit()
        else:
            print('Item not found. ')

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Display Customer Orders
def display_customer_orders(customer_name):
    try:
        cur.execute("SELECT Menu.ItemName, Orders.OrderDate, Orders.Quantity, Orders.Packaging FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID "
                    "WHERE CustomerName = %s", (customer_name,))
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Order Date", "Quantity", "Packaging"])
        for order in orders:
            table.add_row(order)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Modify Order
def modify_order(customer_name):
    try:
        display_customer_orders(customer_name)
        order_id = int(input("Enter Order ID to modify: "))
        cur.execute("SELECT OrderID FROM Orders WHERE CustomerName = %s", (customer_name,))
        data = cur.fetchall()

        if (order_id,) in data:
            new_quantity = int(input("Enter the new quantity: "))
            new_packaging = input("Enter 'Dine-in' or 'Take away': ").capitalize()

            cur.execute("UPDATE Orders SET Quantity = %s, Packaging = %s WHERE OrderID = %s",
                        (new_quantity, new_packaging, order_id))
            con.commit()
            print(f"Order ID {order_id} modified successfully!")
        else:
            print('Sorry, this order does not exist.')

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - View Orders with Status
def admin_view_orders_with_status():
    try:
        cur.execute("SELECT Menu.ItemName, Orders.CustomerName, Orders.OrderDate, "
                    "Orders.Quantity, Orders.Packaging, Customer_Orders_Admin.Status "
                    "FROM Orders "
                    "JOIN Menu ON Orders.ItemID = Menu.ItemID "
                    "JOIN Customer_Orders_Admin ON Orders.OrderID = Customer_Orders_Admin.OrderID")
        orders = cur.fetchall()
        table = PrettyTable(["Item Name", "Customer Name", "Order Date", "Quantity", "Packaging", "Status"])
        for order in orders:
            table.add_row(order)
        print(table)

    except mysql.connector.Error as err:
        print(f"Error: {err}")
        con.rollback()

# Admin - Change Order Status
def admin_change_order_status():
    try:
        admin_view_orders_with_status()
        order_id = int(input("Enter Order ID to change status: "))
        cur.execute("SELECT OrderID FROM Customer_Orders_Admin")
        data = cur.fetchall()

        if (order_id,) in data:
            new_status = input("Enter 'Pending' or 'Delivered': ").capitalize()

            cur.execute("UPDATE Customer_Orders_Admin SET Status = %s WHERE OrderID = %s",
                        (new_status, order_id))
            con.commit()
            print(f"Order ID {order_id} status changed to {new_status} successfully!")
        else:
            print('Sorry, this order does not exist in the admin records.')

    except (mysql.connector.Error, ValueError) as err:
        print(f"Error: {err}")
        con.rollback()
Leave a Comment