-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
223 lines (186 loc) ยท 6.32 KB
/
main.py
File metadata and controls
223 lines (186 loc) ยท 6.32 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
import os, json, copy
from datetime import datetime
from my_utils import *
cart = {}
total_amount = 0
try:
with open("inventory.json", "r") as file:
warehouse = json.load(file)
backup_warehouse = copy.deepcopy(warehouse)
except FileNotFoundError:
print("Error: File 'inventory.json' doesn't exist, check path")
except json.JSONDecodeError:
print("Error: File 'inventory.json' has invalid format, check json")
def ask_option() -> None:
while True:
show_menu()
option = input("Option: ")
av_options = ["1", "2", "3", "4", "5", "6", "7"]
if option in av_options:
clean_console()
return option
else:
print("Last error: Not valid option, type option number.")
clean_console()
@function_frame
def show_catalogue() -> None:
global warehouse
print(""" ๐ช Product list ๐\n""")
for code, info in warehouse.items():
print(f''' โ
{code} : {info["name"]}
๐ฒ Price : {info["price"]}
๐ฆ Availability : {info["stock"]}
''')
def add_to_cart() -> None:
global cart, warehouse
frame("๐ช Adding products ๐")
while True:
product_id = input("Write ID-Product: ").upper()
if product_id in warehouse:
break
else:
print("Id product not found")
while True:
stock = warehouse[product_id]["stock"]
try:
qty = input("Enter the quantity: ")
if not qty.isdigit():
raise ValueError("โ Please enter a valid number.")
qty = int(qty)
if str(qty).isdigit() and qty != 0:
if qty <= stock:
break
else:
print(f'There is not enough stock, ({stock} units) at warehouse, try again')
else:
print("Enter a positive valid integer number")
except ValueError as e:
print(e)
if product_id in cart:
cart[product_id]["quantity"] += qty
else:
cart[product_id] = {
"name": warehouse[product_id]["name"],
"price": warehouse[product_id]["price"],
"quantity": qty,
"subtotal": warehouse[product_id]["price"] * qty
}
warehouse[product_id]["stock"] -= qty
frame(f'โ
Product: "{warehouse[product_id]["name"]}" added')
def remove_from_cart() -> None:
global total_amount
frame("๐ฎ Remove all items ๐๏ธ")
while True:
product_id = input("Write ID-Product: ").upper()
if product_id in cart:
cart_info = cart[product_id]
if product_id in warehouse:
warehouse_info = warehouse[product_id]
warehouse_info["stock"] += cart_info["quantity"]
total_amount -= cart[product_id]["subtotal"]
del cart[product_id]
frame(f"""โ
Product {cart_info["name"]}
Successfully removed""")
break
else:
frame("๐ค Product-ID not found")
def blank_cart() -> None:
global cart, total_amount, warehouse
frame("๐โ Vaciar carrito")
if cart == {}:
frame("โ ๏ธ Your cart is already empty")
return
while True:
confirmation = input(""" Are you sure you want to remove
all items? (Y/N): """).upper()
if confirmation == "Y":
cart = {}
total_amount = 0
warehouse = backup_warehouse
frame("๐ฎ Your cart is now empty ๐งน")
return
elif confirmation =="N":
frame("โ Operation cancelled ๐")
counter_to_zero_from(2)
return
else:
frame("""โ That's not a valid input
Just write "Y" or "N""")
@function_frame
def show_cart() -> None:
global total_amount
total_amount = 0
print(" ๐ Shopping cart ๐ \n")
if cart == {}:
print(" Cart is empty")
return
for code, info in cart.items():
subtotal = info["price"] * info["quantity"]
info["subtotal"] = subtotal
total_amount += info["subtotal"]
print(f''' โ
{code} : {info["name"]}
๐ฒ Price : {info["price"]}
๐ฆ Quantity : {info["quantity"]}
๐ฒ Subtotal : {info["subtotal"]}
''')
frame(f"Total Order amount: {total_amount}")
def finish_order() -> None:
global cart
show_cart()
frame("Buying process form")
if not cart or len(cart) == 0:
frame("""โ Sorry, your cart is empty.
Insert some items ๐""")
counter_to_zero_from(3)
return
while True:
confirmation = input(""" Please confirm payment
Do you proceed to pay? (Y/N): """).upper()
if confirmation == "Y":
frame("โ
Successfully payed! ๐ณ๐")
break
elif confirmation =="N":
frame("โ Operation cancelled ๐")
counter_to_zero_from(2)
return
else:
frame("""โ That's not a valid input
Just write "Y" or "N""")
with open("inventory.json", "w") as file:
json.dump(warehouse, file, indent=4)
sales_data = {
"date_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"products": cart,
"total": total_amount
}
historic = "sales_historic.txt"
if not os.path.exists(historic):
with open(historic, "w") as file:
file.write("")
with open(historic, "r+") as file:
file.seek(0, os.SEEK_END)
file.write(json.dumps(sales_data, indent=4) + "\n")
cart = {}
counter_to_zero_from(3)
return
def process(option: str) -> None:
commands = {
"1" : show_catalogue,
"2" : add_to_cart,
"3" : remove_from_cart,
"4" : blank_cart,
"5" : show_cart,
"6" : finish_order
}
commands.get(option)()
def start_menu() -> None:
option = 0
clean_console()
welcome_message()
while option != "7":
option = ask_option()
if option == "7":
goodbye_message()
return
process(option)
start_menu()