-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
341 lines (279 loc) · 14.3 KB
/
main.py
File metadata and controls
341 lines (279 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from decimal import Decimal
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import pymysql.cursors
attempts = 0
# Establish connection to MySQL database
connection = pymysql.connect(
host='localhost',
user='root',
password='abcd@1234',
database='aditya',
cursorclass=pymysql.cursors.DictCursor
)
def authenticate():
global attempts
username = username_entry.get()
password = password_entry.get()
try:
# Query the database to check if the provided username and ration card match
with connection.cursor() as cursor:
sql = "SELECT * FROM users WHERE User_name=%s AND RationCard_Number=%s"
cursor.execute(sql, (username, password))
result = cursor.fetchone()
if result:
# If authentication successful
welcome_message = "Welcome, " + result['Name'] # Retrieve user's name from the 'Name' column
messagebox.showinfo("Welcome", welcome_message)
# Reset attempts counter
attempts = 0
# Open new window for user options
open_user_options_window(result['User_ID'], result['Name']) # Pass user ID and name to the next window
else:
# If authentication failed
attempts += 1
if attempts >= 3:
messagebox.showerror("Login Failed", "Maximum attempts reached. Exiting application.")
root.destroy()
else:
messagebox.showerror("Login Failed", "Incorrect username or password.")
except Exception as e:
# If there's an error with the database connection or query
messagebox.showerror("Error", str(e))
def open_user_options_window(user_id, user_name):
user_options_window = Toplevel(root)
user_options_window.title("User Options")
# Display user's name
welcome_label = tk.Label(user_options_window, text="Welcome, " + user_name)
welcome_label.pack()
# Create buttons for user options
check_cart_button = tk.Button(user_options_window, text="Check Cart", command=lambda: view_cart_items(user_id))
check_cart_button.pack(pady=10)
view_order_history_button = tk.Button(user_options_window, text="View Order History", command=lambda: view_order_history(user_id))
view_order_history_button.pack(pady=10)
view_product_directory_button = tk.Button(user_options_window, text="View Product Directory", command=view_product_directory)
view_product_directory_button.pack(pady=10)
order_item_button = tk.Button(user_options_window, text="Order Item", command=lambda: order_item(user_id))
order_item_button.pack(pady=10)
def view_product_directory():
try:
# Query the database to retrieve the product directory
with connection.cursor() as cursor:
sql = "SELECT Product_ID, PRODUCT_NAME, Description, Price, Stock_Quantity, Availability FROM products WHERE Availability=1"
cursor.execute(sql)
results = cursor.fetchall()
if results:
# Display the product directory
products = "Product Directory:\n\n"
for result in results:
products += f"Product ID: {result['Product_ID']}\nProduct Name: {result['PRODUCT_NAME']}\nDescription: {result['Description']}\nPrice: {result['Price']}\nStock Quantity: {result['Stock_Quantity']}\nAvailability: {'Available' if result['Availability'] else 'Not Available'}\n\n"
messagebox.showinfo("Product Directory", products)
else:
messagebox.showinfo("Product Directory", "No products found.")
except Exception as e:
# If there's an error with the database connection or query
messagebox.showerror("Error", str(e))
def order_item(user_id):
order_window = Toplevel(root)
order_window.title("Order Item")
# Create labels and entry fields for product ID and quantity
product_id_label = Label(order_window, text="Product ID:")
product_id_label.pack()
product_id_entry = Entry(order_window)
product_id_entry.pack()
quantity_label = Label(order_window, text="Quantity:")
quantity_label.pack()
quantity_entry = Entry(order_window)
quantity_entry.pack()
def add_to_cart_click():
product_id = int(product_id_entry.get())
quantity = int(quantity_entry.get())
try:
# Query the database to retrieve the product details based on the product ID
with connection.cursor() as cursor:
sql = "SELECT PRODUCT_NAME, Stock_Quantity, Price, SUPPLIER_ID FROM products WHERE Product_ID = %s"
cursor.execute(sql, (product_id,))
result = cursor.fetchone()
if result:
product_name = result['PRODUCT_NAME']
stock_quantity = result['Stock_Quantity']
price = result['Price']
supplier_id = result['SUPPLIER_ID']
if stock_quantity >= quantity:
# Add the product to the user's cart
with connection.cursor() as cursor:
cart_sql = "INSERT INTO cart (user_id, items, product_id, price, supplier_id) VALUES (%s, %s, %s, %s, %s)"
item_info = f"{product_name}"
cursor.execute(cart_sql, (user_id, item_info, product_id, str(price * quantity), supplier_id))
# Decrease the stock quantity in the products table
with connection.cursor() as cursor:
update_sql = "UPDATE products SET Stock_Quantity = Stock_Quantity - %s WHERE Product_ID = %s"
cursor.execute(update_sql, (quantity, product_id))
connection.commit()
messagebox.showinfo("Item Added", f"{product_name} added to cart successfully!")
order_window.destroy()
else:
messagebox.showwarning("Insufficient Stock", "The selected product has insufficient stock.")
else:
messagebox.showwarning("Invalid Product", "The entered product ID is invalid.")
except Exception as e:
# If there's an error with the database connection or query
messagebox.showerror("Error", str(e))
# Create a button to add the item to the cart
add_to_cart_button = Button(order_window, text="Add to Cart", command=add_to_cart_click)
add_to_cart_button.pack()
def place_order_from_cart(user_id):
try:
# Query the database to retrieve the cart items of the logged-in user
with connection.cursor() as cursor:
sql = """
SELECT c.product_id, c.price, c.supplier_id
FROM cart c
WHERE c.user_id = %s
"""
cursor.execute(sql, (user_id,))
cart_items = cursor.fetchall()
if cart_items:
# Begin a transaction
connection.begin()
# Insert the order details into the orders table
with connection.cursor() as cursor:
insert_sql = """
INSERT INTO orders (user_id, Product_ID, Order_Date, supplier_id, Order_Price)
VALUES (%s, %s, NOW(), %s, %s)
"""
for item in cart_items:
# Convert price to Decimal before inserting
price_decimal = Decimal(item['price'])
cursor.execute(insert_sql, (user_id, item['product_id'], item['supplier_id'], price_decimal))
# Decrease the stock quantity in the products table
update_sql = "UPDATE products SET Stock_Quantity = Stock_Quantity - %s WHERE Product_ID = %s"
cursor.execute(update_sql, (1, item['product_id'])) # Assuming each item has a quantity of 1
# Clear the user's cart
with connection.cursor() as cursor:
delete_sql = "DELETE FROM cart WHERE user_id = %s"
cursor.execute(delete_sql, (user_id,))
# Commit the transaction
connection.commit()
messagebox.showinfo("Order Placed", "Your order has been placed successfully!")
else:
messagebox.showinfo("Empty Cart", "Your cart is empty.")
except Exception as e:
# If there's an error, rollback the transaction
connection.rollback()
messagebox.showerror("Error", str(e))
def delete_from_cart(user_id, product_id):
try:
# Delete the product from the user's cart
with connection.cursor() as cursor:
delete_sql = "DELETE FROM cart WHERE user_id = %s AND product_id = %s"
cursor.execute(delete_sql, (user_id, product_id))
connection.commit()
messagebox.showinfo("Product Removed", "Product removed from cart successfully!")
except Exception as e:
# If there's an error with the database connection or query
messagebox.showerror("Error", str(e))
def view_cart_items(user_id):
cart_window = Toplevel(root)
cart_window.title("Cart")
try:
# Query the database to retrieve the cart items of the logged-in user
with connection.cursor() as cursor:
sql = """
SELECT pd.PRODUCT_NAME, c.items AS Quantity, c.price AS Price, c.product_id
FROM cart c
JOIN products pd ON c.product_id = pd.Product_ID
WHERE c.user_id = %s
"""
cursor.execute(sql, (user_id,))
results = cursor.fetchall()
if results:
# Display the cart items
cart_tree = ttk.Treeview(cart_window, columns=("Product Name", "Quantity", "Price"), show="headings")
cart_tree.heading("Product Name", text="Product Name")
cart_tree.heading("Quantity", text="Quantity")
cart_tree.heading("Price", text="Price")
cart_tree.pack()
for result in results:
cart_tree.insert("", tk.END, values=(result['PRODUCT_NAME'], result['Quantity'], result['Price']))
def place_order_click():
# Place the order
place_order_from_cart(user_id)
cart_window.destroy()
def remove_from_cart():
if not cart_tree.selection():
messagebox.showerror("Error", "Please select an item to remove.")
return
selected_item = cart_tree.selection()[0]
product_id = cart_tree.item(selected_item, 'values')[2] # Get the product ID from the selected item
print("Removing product:", product_id)
delete_from_cart(user_id, product_id)
connection.commit()
print("Product removed from database.")
cart_window.destroy()
print("Reopening cart window...")
view_cart_items(user_id)
# Create a button to place the order
place_order_button = Button(cart_window, text="Place Order", command=place_order_click)
place_order_button.pack(pady=10)
# Create a button to remove item from cart
remove_from_cart_button = Button(cart_window, text="Remove from Cart", command=remove_from_cart)
remove_from_cart_button.pack(pady=10)
else:
messagebox.showinfo("Cart", "Your cart is empty.")
cart_window.destroy()
except Exception as e:
# If there's an error with the database connection or query
messagebox.showerror("Error", str(e))
def view_order_history(user_id):
order_history_window = Toplevel(root)
order_history_window.title("Order History")
try:
# Query the database to retrieve the order history of the logged-in user
with connection.cursor() as cursor:
sql = """
SELECT oh.ORDER_ID, pd.PRODUCT_NAME, oh.ORDER_PRICE, oh.ORDER_DATE
FROM orders oh
JOIN products pd ON oh.Product_ID = pd.Product_ID
WHERE oh.User_ID = %s
"""
cursor.execute(sql, (user_id,))
results = cursor.fetchall()
if results:
# Display the order history
order_history_tree = ttk.Treeview(order_history_window, columns=("Order ID", "Product Name", "Price", "Order Date"), show="headings")
order_history_tree.heading("Order ID", text="Order ID")
order_history_tree.heading("Product Name", text="Product Name")
order_history_tree.heading("Price", text="Price")
order_history_tree.heading("Order Date", text="Order Date")
order_history_tree.pack()
for result in results:
order_history_tree.insert("", tk.END, values=(result['ORDER_ID'], result['PRODUCT_NAME'], result['ORDER_PRICE'], result['ORDER_DATE']))
else:
messagebox.showinfo("Order History", "No order history found.")
except Exception as e:
# If there's an error with the database connection or query
messagebox.showerror("Error", str(e))
# Create main window
root = tk.Tk()
root.title("Rapid Ration")
root.geometry("500x800")
# Create username label and entry
username_label = tk.Label(root, text="Username:")
username_label.grid(row=0, column=0, padx=5, pady=50, sticky=tk.E)
username_entry = tk.Entry(root)
username_entry.grid(row=0, column=1, padx=5, pady=50, sticky=tk.W+E+N+S) # Center horizontally and vertically
# Create password label and entry
password_label = tk.Label(root, text="Ration Card Number:")
password_label.grid(row=1, column=0, padx=5, pady=50, sticky=tk.E)
password_entry = tk.Entry(root)
password_entry.grid(row=1, column=1, padx=5, pady=50, sticky=tk.W+E+N+S) # Center horizontally and vertically
# Create authenticate button
authenticate_button = tk.Button(root, text="ENTER", command=authenticate)
authenticate_button.grid(row=2, column=0, columnspan=2, padx=200, pady=5, sticky=tk.W+E) # Center horizontally
# Run the main event loop
root.mainloop()
# Close the database connection when the program ends
connection.close()