diff --git a/app/app.py b/app/app.py index 01492269477f0391dca44b3e5dc850f2e5e04be6..c5433c27f89626a982d4e765ee96f9f7bfc6cfe8 100644 --- a/app/app.py +++ b/app/app.py @@ -134,6 +134,18 @@ with app.app_context(): # Kontextus beállítása db.session.add(topup_icon) db.session.commit() print('[INFO] Topup icon created') + # = Utalás ikon hozzáadása az adatbázishoz = + if not Icon.query.get(TRANSFER_ICON_ID): + transfer_icon = Icon( + id = TRANSFER_ICON_ID, + name = TRANSFER_NAME, + url = TRANSFER_ICON_URL, + data=open(DEFAULT_STATIC_FOLDER + TRANSFER_ICON_URL, "rb").read() + ) + # Adatbázisba írás + db.session.add(transfer_icon) + db.session.commit() + print('[INFO] Transfer icon created') # = Feltöltés termék hozzáadása az adatbázishoz = if not Item.query.get(TOPUP_ITEM_ID): topup_item = Item( @@ -160,4 +172,17 @@ with app.app_context(): # Kontextus beállítása # Adatbázisba írás db.session.add(buy_item) db.session.commit() - print('[INFO] Buy item created') \ No newline at end of file + print('[INFO] Buy item created') + # = Utalás termék hozzáadása az adatbázishoz = + if not Item.query.get(TRANSFER_ITEM_ID): + transfer_item = Item( + id=TRANSFER_ITEM_ID, + name=TRANSFER_NAME, + price=1, + icon_id=TRANSFER_ICON_ID, + stock=INACTIVE_ITEM_STOCK + ) + # Adatbázisba írás + db.session.add(transfer_item) + db.session.commit() + print('[INFO] Transfer item created') \ No newline at end of file diff --git a/app/config.py b/app/config.py index 03b71d1a1b9f481f267ea303d5f0f0ef7397d279..1573e5e891033b361903a8b1fa1c45feac9c3a28 100644 --- a/app/config.py +++ b/app/config.py @@ -53,6 +53,14 @@ BUY_ITEM_ID = -1 BUY_ICON_ID = DEFAULT_ICON_ID BUY_NAME = 'Vásárlás' +# Az utalás ikonja és itemje, ami a tranzakcióhoz tartozik +TRANSFER_ITEM_ID = -2 +TRANSFER_ICON_ID = -2 +TRANSFER_ICON_URL = 'imgs/transfer.png' +TRANSFER_NAME = 'Utalás' +TRANSFER_UNIT = '' + + # Termékek elérhetőségének kategorizálása a 'Item.stock' property által SOLD_OUT_ITEM_STOCK = 0 INFINITELY_AVALIBLE_ITEM_STOCK = -1 diff --git a/app/routes.py b/app/routes.py index ab3459dac56b29bce2d7901f5ae8f3a9b67bae52..902d48495264359920209c6dfc27cbca08594b12 100644 --- a/app/routes.py +++ b/app/routes.py @@ -95,7 +95,8 @@ def history(uuid): return redirect(url_for('routes.history')) - spendings = selected_user.received_transactions + #sent_trans = selected_user.sent_transactions + spendings = selected_user.received_transactions # + sent_trans.filter(Transaction.item_id == TRANSFER_ITEM_ID).all() searched_spendings = [s for s in spendings if search_field in s.item.name.lower()] @@ -499,6 +500,88 @@ def transactions_export(): response = Response(output.getvalue(), content_type="text/csv; charset=utf-8") response.headers["Content-Disposition"] = "attachment; filename=transactions.csv" return response + +@routing.route('/transfer', methods=['GET', 'POST']) +@token_required() +def transfer(): + """ + Egyenleg utalás oldal és POST feldolgozás + --- + tags: + - Oldalak + summary: Egyenleg utalás másik felhasználónak. + description: A felhasználó ezen az oldalon utalhat egyenleget egy másik felhasználónak. + responses: + 200: + description: Az oldal sikeresen megjelenik vagy az utalás sikeres. + content: + text/html: + schema: + type: string + application/json: + schema: + type: object + properties: + success: + type: boolean + message: + type: string + 400: + description: Hibás kérés vagy érvénytelen adatok. + """ + current_user = get_current_user() + if request.method == 'GET': + # Listázzuk az összes felhasználót, kivéve saját magát és a BOT-ot + users = User.query.filter(User.uuid != current_user.uuid, User.uuid != BOT_USER_UUID).all() + return render_template( + "transfer.html", + current_user=current_user, + release_version=RELEASE_VERSION, + users=users + ) + elif request.method == 'POST': + data = request.get_json() + recipient_uuid = data.get('recipient') + amount = data.get('amount') + note = data.get('note', '') + + if not recipient_uuid or not amount: + return {"success": False, "message": "Hiányzó adat!"}, 400 + + if recipient_uuid == current_user.uuid: + return {"success": False, "message": "Saját magadnak nem utalhatsz!"}, 400 + + try: + amount = int(amount) + except Exception: + return {"success": False, "message": "Az összegnek számnak kell lennie!"}, 400 + + if amount < 1: + return {"success": False, "message": "Az összegnek pozitívnak kell lennie!"}, 400 + + recipient = User.query.get(recipient_uuid) + if not recipient: + return {"success": False, "message": "A címzett nem található!"}, 400 + + if current_user.balance < amount: + return {"success": False, "message": "Nincs elég egyenleged az utaláshoz!"}, 400 + + # Tranzakció létrehozása + transaction = Transaction( + by_user_uuid=current_user.uuid, + to_user_uuid=recipient.uuid, + item_id=TRANSFER_ICON_ID, + amount=amount, + quantity=1, + # date=datetime.now() + # note=note + ) + current_user.balance -= amount + recipient.balance += amount + db.session.add(transaction) + db.session.commit() + return {"success": True, "message": f"Sikeres utalás {recipient.name} részére!"}, 200 + # Hiba oldal @routing.route('/error') def error(): diff --git a/app/static/imgs/transfer.png b/app/static/imgs/transfer.png new file mode 100644 index 0000000000000000000000000000000000000000..e4a80a712864d429c01a1eb0a47052ef096bc77e Binary files /dev/null and b/app/static/imgs/transfer.png differ diff --git a/app/templates/base.html b/app/templates/base.html index ee9df971001d59e695466e196f9cc633abf5db8a..3d883421087a16a94ebb0551f75796c4ade7b2c4 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -61,6 +61,7 @@ {% if current_user.is_authenticated %} <li class="nav-item"><a class="nav-link {% if request.path == '/' %}active{% endif %}" href="/"><i class="bi bi-bag me-1"></i>Vásárlás</a></li> <li class="nav-item"><a class="nav-link {% if '/history' in request.path %}active{% endif %}" href="/history/{{current_user.uuid}}"><i class="bi bi-journal-text me-1"></i>Költések</a></li> + <li class="nav-item"><a class="nav-link {% if '/transfer' in request.path %}active{% endif %}" href="/transfer"><i class="bi bi-arrow-left-right me-1"></i>Utalás</a></li> {% if current_user.role >= 1 %} <li class="nav-item"><a class="nav-link {% if '/bar' in request.path %}active{% endif %}" href="/bar"><i class="bi bi-cup-straw me-1"></i>Pult</a></li> {% endif %} {% if current_user.role == 2 %} diff --git a/app/templates/transfer.html b/app/templates/transfer.html new file mode 100644 index 0000000000000000000000000000000000000000..fd84e0ca0b5e21fe11b69a2327ff8cdf60ae8e0b --- /dev/null +++ b/app/templates/transfer.html @@ -0,0 +1,65 @@ +{% extends 'base.html' %} +{% block title %}BecskasszaSCH | Egyenleg utalás{% endblock %} + +{% block content %} +<div class="card shadow"> + <div class="card-body"> + <h1 class="mb-4"><i class="bi bi-arrow-left-right me-2"></i>Egyenleg utalás</h1> + <form id="transferForm" onsubmit="transferBalance(event)"> + <div class="mb-3"> + <label for="recipient" class="form-label">Címzett felhasználó</label> + <select class="form-select" id="recipient" name="recipient" required> + <option value="" disabled selected>Válassz felhasználót</option> + {% for user in users %} + {% if user.uuid != current_user.uuid %} + <option value="{{ user.uuid }}">{{ user.name }} ({{ user.balance }} JMF)</option> + {% endif %} + {% endfor %} + </select> + </div> + <div class="mb-3"> + <label for="amount" class="form-label">Utalni kívánt összeg (JMF)</label> + <input type="number" class="form-control" id="amount" name="amount" min="1" placeholder="Add meg az összeget" required> + </div> + <div class="mb-3"> + <label for="note" class="form-label">Megjegyzés (opcionális)</label> + <input type="text" class="form-control" id="note" name="note" maxlength="100" placeholder="Pl.: Szülinap, visszajáró, stb."> + </div> + <div class="d-flex gap-2"> + <button type="submit" class="btn btn-success"><i class="bi bi-send me-1"></i>Utalás</button> + <a href="/" class="btn btn-secondary">Mégsem</a> + </div> + </form> + </div> +</div> + +<script> +function transferBalance(event) { + event.preventDefault(); + const recipient = document.getElementById('recipient').value; + const amount = parseInt(document.getElementById('amount').value); + const note = document.getElementById('note').value; + + if (!recipient || !amount || amount < 1) { + showToast('Hiba', 'Adj meg minden szükséges adatot!', 'danger'); + return; + } + + fetch('/transfer', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ recipient, amount, note }) + }) + .then(res => res.json()) + .then(data => { + if (data.success) { + showToast('Siker', data.message, 'success'); + setTimeout(() => { window.location.href = '/history/{{ current_user.uuid }}'; }, 1500); + } else { + showToast('Hiba', data.message, 'danger'); + } + }) + .catch(() => showToast('Hiba', 'Ismeretlen hiba történt!', 'danger')); +} +</script> +{% endblock %} \ No newline at end of file