-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.py
77 lines (58 loc) · 2.1 KB
/
scanner.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
import enum
import os
from math import ceil
from dotenv import load_dotenv
from core.database import Database
from core.items import Items, Item
from core.clients import Clients, Client
from core.seed import create_tables
from core.purchases import Purchases, Purchase
class BarcodeType(enum.Enum):
Client = 0
Item = 1
def barcode_enum(barcode):
if barcode.startswith("C_"):
return BarcodeType.Client
else:
return BarcodeType.Item
def main():
db_file = os.getenv("db_file")
database = Database(db_file)
items = Items(database)
clients = Clients(database)
purchases = Purchases(database)
create_tables(database)
while True:
client_barcodes = []
barcode = input().strip().upper()
while barcode_enum(barcode) is BarcodeType.Client:
client_barcodes.append(barcode)
barcode = input().strip().upper()
client_list = []
for client_barcode in client_barcodes:
client = clients.get_by_barcode(client_barcode)
client_list.append(client) if client else None
if not len(client_list):
continue
print("Clients: {}".format(", ".join([str(client) for client in client_list])))
product = items.get_by_barcode(barcode)
print("Scanned product: {}".format(str(product)))
if product:
price_per_person = product.price
if len(client_list) > 1:
price_per_person = ceil(product.price * 20 / len(client_list)) / 20 # ceil with 2 decimals
print("Price paid per person: {}".format(str(price_per_person)))
for client in client_list:
purchase = Purchase.create(product, client, price_per_person)
purchases.persist(purchase)
client.balance += price_per_person
clients.persist(client)
print(purchase)
product.stock -= 1
items.persist(product)
print("Purchase registered")
else:
print("No item scanned")
if __name__ == "__main__":
load_dotenv()
main()