-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
executable file
·247 lines (214 loc) · 9.19 KB
/
app.py
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
#!/usr/bin/env python3
from models import Base, User, Product, Barcode, Font
from models import KasMutatie, KasMutatieSoort
from models import BankStorting
from models import VoorraadMutatie, VoorraadMutatieSoort
from sqlalchemy import func
from database import Session, engine
import os, time
from console import clearConsole, succes, question, info, warning, error, input_yesno, input_currency
def logo(font):
clearConsole()
if font is None:
print("ACKbar with font issues")
else:
os.system(f"figlet -t -f {font.name} ACKbar | lolcat")
def purchaseScreen(users, products, deposit, transfers, total, font):
logo(font)
print("")
if users[0].name != "guest":
print(f"Hello {users[0].name}, you have {(users[0].balance/100):.2f} on your account.")
else:
print(f"Hello guest!")
print(f"")
print(info("Commands:"))
print(info(" cash - Deposit with cash"))
if users[0].name != "guest":
print(info(" bank - Deposit with wire transfer"))
print(info(" ------"))
print(info(" cancel - Cancel transaction"))
print(info(" accept - Accept transaction"))
print("")
if users[0].name != "guest":
print(f"{'balance' : <25} {(users[0].balance/100):6.2f}")
if deposit > 0:
print(f"{'deposit'.ljust(25)} {(deposit/100):6.2f}")
for transfer in transfers:
foo = f"transfer {transfer[1]}"
print(f"{foo.ljust(25)} {(transfer[0]/100):6.2f}")
if len(products) > 0:
for product in products:
print(f"{product.name.ljust(25)} {(-product.price/100):6.2f}")
print("-"*32)
if users[0].name == "guest":
if total >= 0:
print(f"{'your change'.ljust(25)} {(total/100):6.2f}")
else:
print(f"{'please deposit'.ljust(25)} {(-total/100):6.2f}")
else:
print(f"{'new balance'.ljust(25)} {(total/100):6.2f}")
def startScreen(font):
logo(font)
print("")
if font is not None:
print(info(f"{font.name} {font.score}"))
print()
print("Commands:")
print(" guest - use a guest account")
print(" u - upvote logo")
print(" d - downvote logo")
def randomFont(session):
return session.query(Font).order_by(func.random()).first()
def performCheckout(user, products, deposit, session, transfers):
# Add the prices of all the products
totalPrice = 0
for product in products:
totalPrice += product.price
# What balance would this checkout result in?
resulting_balance = user.balance + deposit - totalPrice
for transfer in transfers:
resulting_balance += transfer[0]
# Can the user afford this?
if resulting_balance < 0 and user.name != "guest":
return resulting_balance
# The user has enough money and the transaction is going to be executed
else:
if user.name == "guest":
user.balance = 0
else:
user.balance = resulting_balance
# If cash has been deposited, we record a kas mutation
if user.name == "guest": # Guests always deposit totalPrice
session.add( KasMutatie(mutatiesoort=KasMutatieSoort.storting, user_id=user.id, bedrag=totalPrice) )
elif deposit > 0:
session.add( KasMutatie(mutatiesoort=KasMutatieSoort.storting, user_id=user.id, bedrag=deposit) )
# If products have been bought, we record a voorraad mutation
voorraadmutaties = {}
for product in products:
if voorraadmutaties.get(product.id, None) is None:
voorraadmutaties[product.id] = {"count":1, "unitprice":product.price}
else:
voorraadmutaties[product.id]["count"] += 1
for product_id in voorraadmutaties:
hoeveelheid = voorraadmutaties[product_id]["count"]
bedrag = voorraadmutaties[product_id]["unitprice"] * hoeveelheid
voorraadmutatie = VoorraadMutatie(
mutatiesoort=VoorraadMutatieSoort.koop,
product_id=product_id,
user_id=user.id,
hoeveelheid=-hoeveelheid,
bedrag=bedrag
)
session.add(voorraadmutatie)
# We finalize the transaction
session.commit()
return resulting_balance
def main(Session):
isRunning = True
with Session() as session:
font = randomFont(session)
startScreen(font)
scanned = input(question("\nType nickname or scan barcode: "))
with Session() as session:
users = []
# User wants to quit the program
if scanned.lower() == "q" or scanned.lower() == "quit":
isRunning = False
# User wants to upvote the logo
elif scanned == "u":
font = session.query(Font).filter(Font.name==font.name).first()
font.score += 1
session.commit()
font = None
# User wants to downvote the logo
elif scanned == "d":
font = session.query(Font).filter(Font.name==font.name).first()
font.score -= 1
session.commit()
font = None
# User entered nothing, refresh screen
elif scanned == "":
pass
# User wants to login or create a new account
else:
for userQuery in session.query(User).filter(func.lower(User.name)==scanned.lower()):
users.append(userQuery)
assert len(users) < 2, "Database returned multiple users!"
# We could not find this user in our DB
if len(users) == 0:
print(f"{scanned} is not registered")
yn = input_yesno("Do you wish to register this user?")
if yn == "y":
user = User(name=scanned, balance=0)
users.append(user)
session.add(user)
session.commit()
# We found a user or they have just registered
if len(users) == 1:
userBusy = True
products = []
transfers = []
deposit = 0
while userBusy:
total = users[0].balance + deposit
for product in products:
total -= product.price
for transfer in transfers:
total += transfer[0]
purchaseScreen(users, products, deposit, transfers, total, font)
scanned = input(question("\nType command or scan product: "))
# User wants to deposit money
if scanned.lower() in ["cash", "deposit"]:
deposit += input_currency()
# User wants to transfer money
elif scanned.lower() == "bank" and users[0].name != "guest":
session.flush()
transactionNo = session.query(BankStorting).filter(BankStorting.user_id==users[0].id).count()
code = f"BAR-{users[0].id}-{transactionNo}"
print()
print(f"Please perform a wire transfer with the following info:")
print(f"Rekening - NL16 ABNA 0563 9410 06")
print(f"Ten name van - Stichting ACKspace")
print(f"Omschrijving - {code}")
print()
bedrag = input_currency()
isTransfer = input_yesno("Confirm transfer?") == "y"
if isTransfer:
transfers.append([bedrag, code])
session.add( BankStorting(user_id=users[0].id, bedrag=bedrag, code=code) )
# User wants to finish transaction
elif scanned.lower() in ["accept", "checkout"]:
resulting_balance = performCheckout(users[0], products, deposit, session,transfers)
if resulting_balance >= 0:
print(succes("Transaction confirmed!"))
time.sleep(2)
userBusy = False
elif users[0].name == "guest":
print(succes(f"Transaction confirmed!\nPlease deposit {(-total/100):1.2f}, thank you."))
time.sleep(10)
else:
print(warning(f"Not enough funds! Check the tab."))
time.sleep(1)
# User wants to cancel transaction
elif scanned.lower() in ["cancel", "abort"]:
print(warning(f"Transaction canceled!"))
session.rollback()
userBusy = False
time.sleep(2)
# User wants to scan a product
else:
productQueries = []
for barcodeQuery in session.query(Barcode).filter(Barcode.barcode==scanned):
productQueries.append(barcodeQuery.product)
if len(productQueries) == 0:
print(warning("Invalid product code or command."))
time.sleep(1)
else:
products.extend(productQueries)
return isRunning
if __name__ == "__main__":
Base.metadata.create_all(bind=engine)
isRunning = True
font = None
while isRunning:
isRunning = main(Session)