forked from ACKspace/ACKbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
215 lines (185 loc) · 7.79 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
#!/usr/bin/python
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_deposit
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("")
print(f"Hello {users[0].name}, you have {(users[0].balance/100):.2f} on your account.")
print(f"")
print(info("Commands:"))
print(info(" accept - Accept transaction"))
print(info(" bank - Deposit with wire transfer"))
print(info(" cash - Deposit with cash"))
print(info(" cancel - Cancel transaction"))
print("")
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)
print(f"{'new balance'.ljust(25)} {(total/100):6.2f}")
def startScreen(font):
logo(font)
print("")
print(info(f"{font.name} {font.score}"))
print()
print("Commands:")
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
# Can the user afford this?
if totalPrice > (user.balance + deposit):
return False
# The user has enough money and the transaction is going to be executed
else:
user.balance += deposit
user.balance -= totalPrice
for transfer in transfers:
user.balance += transfer[0]
# If cash has been deposited, we record a kas mutation
if 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 True
def main(Session):
isRunning = True
with Session() as session:
font = randomFont(session)
startScreen(font)
scanned = input(question("\nType nickname or scan barcode: "))
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.score += 1
font = None
# User wants to downvote the logo
elif scanned == "d":
font.score -= 1
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() == "cash":
deposit += input_deposit()
# User wants to transfer money
elif scanned.lower() == "bank":
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_deposit()
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() == "accept":
if performCheckout(users[0], products, deposit, session,transfers):
print(succes("Transaction confirmed!"))
time.sleep(2)
userBusy = False
else:
print(warning(f"Not enough funds! Check the tab."))
time.sleep(1)
# User wants to cancel transaction
elif scanned.lower() == "cancel":
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)