This commit is contained in:
Matěj Divecký 2023-02-25 18:22:03 +01:00
commit 3309fb435e
7 changed files with 238 additions and 0 deletions

140
main.py Normal file
View file

@ -0,0 +1,140 @@
from pygrocy import Grocy
from flask import Flask, render_template, request
from pprint import pprint
# Demo variables
BARCODE = 2222
USER_BAR_ID = 555
#Global variables
DEFAULT_AMOUNT = 1
CASHBOX_USERID = 3
g = Grocy("http://localhost", "BZzLkorATL66hSbKwIERoesALcIqNUPW91zONNNTHP0r9N6vk2", port=8080)
# Write money movement into the ledger. TODO
def write_ledger():
return True
# Subtract the price from user account or add it to the cashier. Also write it to ledger.
def prod_bill_user(prod, user):
start_balance = user['balance']
end_balance = start_balance - prod['price']
g.set_userfields('users', user['id'], 'balance', end_balance)
return(end_balance)
# Add money paid for the product to the cashbox.
def prod_bill_cash(prod):
for user in g.users():
if user.id == CASHBOX_USERID:
cashbox = user
break
start_balance = float(g.get_userfields('users', cashbox.id)['balance'])
end_balance = start_balance + prod['price']
g.set_userfields('users', cashbox.id, 'balance', end_balance)
# Receive barcode and user code from website and process it.
def prod_consume(prod):
g.consume_product(product_id = prod['id'], amount = DEFAULT_AMOUNT)
def user_by_barid(user_barid):
users = g.users()
users_data = []
# Get all users and their custom fields
for user in users:
user_fields = g.get_userfields('users', user.id)
if user_fields['accountingenabled']:
user_data = {
'accounting_enabled': user_fields['accountingenabled'],
'balance': float(user_fields['balance']),
'barid': user_fields['barid'],
'name': user.display_name,
'id': user.id
}
users_data.append(user_data)
# Find user with selected barID
for user in users_data:
if user['barid'] == str(user_barid):
return user
# If no user with this barID and enabled accounting is found, return false
return False
# Get data about product and price by barcode
def prod_by_barcode(barcode):
prod = g.product_by_barcode(barcode)
sell_price = g.get_userfields('products', prod.id)['sellprice']
prod_data = {
'id': prod.id,
'name': prod.name,
'available': prod.available_amount,
'price': float(sell_price),
'barcode': barcode
}
return prod_data
def main():
# Get product
prod = prod_by_barcode(BARCODE)
# print(prod)
# It would be good to check if the product is available here. In such case, throw some error and prompt for user action (write it on paper and report to staff)
# Find user
user = user_by_barid(USER_BAR_ID)
# print(user)
# Consume product
prod_consume(prod)
# Bill product to user
#prod_bill_user(prod, user)
# Bill product with cash
prod_bill_cash(prod)
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/checkout/', methods=['POST'])
def scan():
barcode = request.form.get('barcode')
prod = prod_by_barcode(barcode)
if not prod:
print('Item not found')
return render_template('checkout.html', prod = prod)
@app.route('/cash-payment/', methods=['POST'])
def cash_payment():
barcode = request.form.get('barcode')
prod = prod_by_barcode(barcode)
prod_consume(prod)
prod_bill_cash(prod)
return render_template('cash-payment.html', prod = prod)
@app.route('/account-payment/', methods=['POST'])
def account_payment():
user_barid = request.form.get('barid')
barcode = request.form.get('barcode')
prod = prod_by_barcode(barcode)
user = user_by_barid(user_barid)
prod_consume(prod)
end_balance = prod_bill_user(prod, user)
return render_template('account-payment.html', prod = prod, user = user, balance = end_balance)
if __name__ == '__main__':
app.run(host="0.0.0.0")

2
requirements.txt Normal file
View file

@ -0,0 +1,2 @@
flask
pygrocy

View file

@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% block content %}
<p>1ks {{prod.name}} odečten ze skladu</p>
<p>{{prod.price}}Kč bylo zaplaceno z účtu {{user.name}}</p>
<p>Zbývající kredit: {{balance}}Kč</p>
<form action="/">
<input type="submit" value="Další!" autofocus/>
</form>
{% endblock %}

28
templates/base.html Normal file
View file

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@1.*/css/pico.min.css">
<title>TrhBar</title>
<style>
.message {
padding: 10px;
margin: 5px;
background-color: #f3f3f3
}
nav a {
color: #d64161;
font-size: 3em;
margin-left: 50px;
text-decoration: none;
}
</style>
</head>
<body>
<main class="container">
{% block content %} {% endblock %}
</main>
</body>
</html>

View file

@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% block content %}
<p>1ks {{prod.name}} odečten ze skladu</p>
<p>Hoď prosím do kasičky <b>{{prod.price}},- Kč</b></p>
<form action="/">
<input type="submit" value="Další!" autofocus/>
</form>
{% endblock %}

34
templates/checkout.html Normal file
View file

@ -0,0 +1,34 @@
{% extends 'base.html' %}
{% block content %}
<section>
<div class="grid">
<div><h4>{{prod.name}}</h4></div>
<div><h4>Doporučená cena: {{prod.price}},- Kč</h4></div>
</div>
</section>
<section>
<div>
V hotovosti:
<form action="/cash-payment" method="post">
<input type="text" name="barcode" value="{{prod.barcode}}" hidden>
<input type="submit" value="Prachy do kasičky" >
</form>
</div>
</section>
<section>
<div>
<form action="/account-payment" method="post">
<input type="text" name="barcode" value="{{prod.barcode}}" hidden>
<br>
Odečíst z účtu:
<input type="text" name="barid" autofocus><br>
<input type="submit" hidden>
</form>
</div>
</section>
{% endblock %}

10
templates/index.html Normal file
View file

@ -0,0 +1,10 @@
{% extends 'base.html' %}
{% block content %}
<section>
Načti prosím čtečkou čárový kód
</section>
<form action="/checkout" method="post">
<input type="text" name="barcode" autofocus>
</form>
{% endblock %}