-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactions.py
More file actions
346 lines (212 loc) · 13 KB
/
transactions.py
File metadata and controls
346 lines (212 loc) · 13 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
342
343
344
345
346
# Author: Reid Moline
import sqlite3
from datetime import date
import random as rd
def create_empty_transactions_table():
'''
Function that connects to the bikeshop.sqlite database and creates a table called transactions used to keep record of all sales for the store
Parameters: None
Returns: None
'''
conn = sqlite3.connect('bikeshop.sqlite')
cur = conn.cursor()
cur.execute('PRAGMA foreign_keys = ON;')
cur.execute("""CREATE TABLE IF NOT EXISTS Transactions
(Transaction_Number INT NOT NULL,
customer_id INTEGER NOT NULL,
Item TEXT NOT NULL,
UPC INTEGER NOT NULL,
Item_Cost REAL,
Serial_Number TEXT DEFAULT NULL,
Salesperson_ID INT,
Payment_Type TEXT DEFAULT NULL,
Date TEXT,
FOREIGN KEY (customer_id) REFERENCES Customers (customer_id),
FOREIGN KEY (UPC) REFERENCES Products (UPC)
)""")
if __name__ == "__main__":
connection = sqlite3.connect('bikeshop.sqlite')
cursor = connection.cursor()
cursor.execute('PRAGMA foreign_keys = ON;')
line = '_' * 40 + '\n'
#Below line if removed from docstrings will delete all transaction history
'''
cursor.execute("""CREATE TABLE IF NOT EXISTS Transactions
(Transaction_Number INT NOT NULL,
customer_id INTEGER NOT NULL,
Item TEXT NOT NULL,
UPC INTEGER NOT NULL,
Item_Cost REAL,
Serial_Number TEXT DEFAULT NULL,
Salesperson_ID INT,
Payment_Type TEXT DEFAULT NULL,
Date TEXT,
FOREIGN KEY (customer_id) REFERENCES Customers (customer_id),
FOREIGN KEY (UPC) REFERENCES Products (UPC)
)""")
'''
while True:
print(line)
employee_making_sale = input('Please Enter your Employee ID: ')
attempt = 1
cursor.execute("SELECT * FROM employees WHERE employee_code = ?", (employee_making_sale,))
employee_details = cursor.fetchall()
while employee_details == [] and attempt < 4: # Creating a basic password protection system like would be seen in a retail setting
print(line)
print("Invalid Employee Code Please Try Again")
print(f'You have {3 - attempt} attempt(s) left')
employee_making_sale = int(input('Please Enter your Employee ID: '))
attempt += 1
cursor.execute("SELECT * FROM employees WHERE employee_code = ?", (employee_making_sale,))
employee_details = cursor.fetchall()
if employee_details == []: # Ends program if employee doesn't input correct credentials in 3 tries
break
print(line)
print("Please select what to search for the customer using the numbered options below or enter 0 to continue with a Guest Customer or 4 to quit\n")
print("1) Phone Number")
print("2) Customer ID") # Primary Key so UNIQUE
print("3) Create New Customer")
print(line)
cursor.execute("SELECT customer_id, customer_phone from Customers;")
all_customer_ids_and_phones = cursor.fetchall()
list_all_customer_ids = [all_customer_ids_and_phones[i][0] for i in range(len(all_customer_ids_and_phones))]
list_all_customer_phones = [all_customer_ids_and_phones[i][1] for i in range(len(all_customer_ids_and_phones))]
option = int(input("Please Enter a numbered option from above: "))
while option not in range(5):
print(line)
print("Please select an option listed above.")
option = int(input("Please Enter a numbered option from above: "))
print(line)
if option == 4:
break
elif option == 3:
new_first_name = input("First Name: ")
new_last_name = input("Last Name: ")
new_email = input("Email (not required suggested): ")
new_phone = input("Phone Number (required): ")
customer_id = rd.randint(100000, 999999)
while customer_id in list_all_customer_ids:
customer_id = rd.randint(100000, 999999)
while new_phone in list_all_customer_phones:
print("Phone number already in use. Enter New Number")
new_phone = input("Phone Number (required): ")
cursor.execute("INSERT INTO Customers VALUES(?,?,?,?,?,?);", (customer_id, new_first_name, new_last_name, new_email, new_phone, employee_making_sale))
elif option == 0:
customer_id = str(0) # Must be type casted to meet foreign key constraint
elif option == 1:
cust_phone = input("Please enter the phone number: ")
cursor.execute("SELECT * FROM Customers WHERE customer_phone = ?", (cust_phone,))
phone_search_results = cursor.fetchall()
while len(phone_search_results) != 1 and cust_phone != '':
print(line)
if len(phone_search_results) == 0:
print("No Results try again or press ENTER to quit")
elif len(phone_search_results) > 1: # Allows for revert to search by customer ID if family using same phone number for account
print("Too many results Please try searching by Customer ID")
option = 2
break
cust_phone = input("Please enter the phone number: ")
cursor.execute("SELECT * FROM Customers WHERE customer_phone = ?", (cust_phone,))
phone_search_results = cursor.fetchall()
if cust_phone == '': # Gives option to quit
break
customer_id = phone_search_results[0][0] # Once refined to one account we can grab the customer ID
if option == 2:
customer_id = input("Please enter the Customer ID: ")
cursor.execute("SELECT * FROM Customers WHERE customer_id = ?", (customer_id,))
id_search_results = cursor.fetchall()
while len(id_search_results) == 0 and customer_id != '':
print(line)
if len(id_search_results) == 0:
print("No Results try again or press ENTER to quit")
customer_id = input("Please enter the Customer ID: ")
cursor.execute("SELECT * FROM Customers WHERE customer_id = ?", (customer_id,))
id_search_results = cursor.fetchall()
if customer_id == '': # Gives option to quit
break
customer_id = id_search_results[0][0] # Once refined to one account we can grab the customer ID
#Now Dealing with the Sale itself
cursor.execute("SELECT Transaction_Number from Transactions;")
transaction_nums = cursor.fetchall()
list_of_transaction_numbers = [transaction_nums[i][0] for i in range(len(transaction_nums))]
transaction_number = rd.randint(1000000, 9999999)
while transaction_number in list_of_transaction_numbers:
transaction_number = rd.randint(1000000, 9999999) # Ensures the uniqueness of the generated transaction ID (it is a primary key and needs to be unique)
date_of_sale = date.today()
print(line)
print("To stop scanning items press ENTER without scanning anything")
scanning = True
cursor.execute("SELECT UPC FROM Products;") # Creates a list of all product UPC to cross reference scanned item
all_upcs = cursor.fetchall()
list_all_upcs = [all_upcs[i][0] for i in range(len(all_upcs))]
while scanning:
upc = input("Scan: ")
if upc == '': # Way to stop scanning through items
break
upc = int(upc)
if upc not in list_all_upcs:
print("UPC Not Found. Item not in inventory")
continue
elif upc in list_all_upcs:
cursor.execute("SELECT * FROM Products WHERE UPC = ?", (upc,))
all_info = cursor.fetchall()[0]
description = all_info[1]
quantity = all_info[2]
price = all_info[3]
serialized = all_info[5]
new_quantity = int(quantity) - 1 # Makes quantity one less in inventory
cursor.execute("UPDATE Products SET Quantity = ? WHERE UPC = ?", (new_quantity, upc)) # Makes quantity one less in inventory
if new_quantity < 0 and serialized: # if a bike is scanned through and not in inventory this warrants a manager because the bike can't be sold if not in inventory. Too high ticket an item to sell without being in the systems inventory
print("Bike not in Inventory. Manager Assistance Needed")
print("Bike has not been added to transaction until manager reconciles issue")
print(line)
continue
elif new_quantity < 0 and not serialized: # Issue with inventory that needs to be reconciled. item can still be sold if not serialized. Sometimes things get misplaced and/or stolen and inventory is wrong
cursor.execute("SELECT Issue_ID FROM Reconcile;")
issue_ids = cursor.fetchall()
list_issue_ids = [issue_ids[i][0] for i in range(len(issue_ids))]
new_issue_id = rd.randint(1000000, 9999999) # Creates an ID to reference the issue by
while new_issue_id in list_issue_ids:
new_issue_id = rd.randint(1000000, 9999999) # Ensures uniqueness of issue ID
cursor.execute("INSERT INTO Reconcile VALUES(?,?,?,?)", (new_issue_id, upc, date_of_sale, 0)) # Creates new inventory issue to be reconciled
print('Item sold but Invnetory Issue needs to be Reconciled')
if serialized:
serial_number = input("Please Enter the Serial Number of the bike exactly as seen: ")
cursor.execute("SELECT Sold FROM Serialization WHERE Serial_Number = ? AND UPC = ?;", (serial_number, upc))
sold_status = cursor.fetchall() # Grabs list that should be one of three things [], [(1,)], [(0,)]
if sold_status == []:
print("Serial Number not in System Please Scan UPC and try again")
elif sold_status[0][0] == 1:
print("Bike already registered as sold. Bike can not be resold until manager reconciles this issue ")
elif sold_status[0][0] == 0:
cursor.execute("INSERT INTO Transactions VALUES(?,?,?,?,?,?,?,?,?)", (transaction_number, customer_id, description, upc, price, serial_number, employee_making_sale, None, date_of_sale )) # Payment detail will be updated when scanning complete and total is "paid"
cursor.execute("UPDATE Serialization SET Sold = 1 WHERE Serial_Number = ?;", (serial_number,))
print("Bike Added")
print(line)
elif not serialized:
serial_number = None
cursor.execute("INSERT INTO Transactions VALUES(?,?,?,?,?,?,?,?,?)", (transaction_number, customer_id, description, upc, price, serial_number, employee_making_sale, None, date_of_sale )) # Payment detail will be updated when scanning complete and total is "paid"
print("Item Added")
print(line)
cursor.execute("SELECT SUM(Item_Cost) FROM Transactions WHERE Transaction_Number = ?;", (transaction_number,))
subtotal = cursor.fetchone()[0] #Accessing first index of single tuple (fetchone)
if subtotal == None:
print("No sale was complete because no items were scanned")
else:
total = subtotal * 1.05 # Adding Tax
print(line)
print(f"Subtotal: {subtotal}")
print(f"Total: {total:.2f}")
print(line)
print("Please enter in total in credit/debit machine")
payment_types = ['MC', 'VISA', 'AMEX', 'DEBIT'] # Standard Types of Payment (Cashless because that adds another element of returning change)
payment = input("Payment Type (VISA, MC, AMEX, DEBIT): ").upper()
while payment not in payment_types:
print("Please enter proper paymeny type.")
payment = input("Payment Type (VISA, MC, AMEX, DEBIT): ").upper()
cursor.execute("UPDATE Transactions SET Payment_Type = ? WHERE Transaction_Number = ?", (payment, transaction_number))
print(line)
print("Sale Complete")
break
connection.commit()
connection.close()