TrhlinaBar/main.py

140 lines
3.9 KiB
Python
Raw Normal View History

2023-02-25 18:22:03 +01:00
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
2023-02-25 18:25:40 +01:00
# 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)
2023-02-25 18:22:03 +01:00
2023-02-25 18:25:40 +01:00
# # Find user
# user = user_by_barid(USER_BAR_ID)
# # print(user)
2023-02-25 18:22:03 +01:00
2023-02-25 18:25:40 +01:00
# # Consume product
# prod_consume(prod)
2023-02-25 18:22:03 +01:00
2023-02-25 18:25:40 +01:00
# # Bill product to user
# #prod_bill_user(prod, user)
2023-02-25 18:22:03 +01:00
2023-02-25 18:25:40 +01:00
# # Bill product with cash
# prod_bill_cash(prod)
2023-02-25 18:22:03 +01:00
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")