LuminaWebsite/assets/js/shop.js
Maaxxs ebc9e2367e Lumishop: trade Lumipunkte for items, with approval
Members request items from the shop. Their points and the stock are
earmarked but not taken until someone with shop.manage approves. Approving
books a negative "Lumishop" entry in the Lumipunkte history and takes the
stock. For warehouse-backed items it also books a withdrawal to the member
in the warehouse. Approved orders are then marked as handed over in the
game, or can be reversed, which restores points, stock and the warehouse
withdrawal.

The management page handles categories and items (price, description,
stock). Stock is either the shop's own count, unlimited, or live from a
linked warehouse item. Items in stock can be imported from the warehouse,
and an item's own stock can be moved into the warehouse to link it there.
Warehouse cards show when an item is sold in the shop.

Shop bookings can't be deleted from the Lumipunkte page, only reversed in
the shop, so orders and points stay in sync. The new permissions go to the
Administrator group only, so the shop stays hidden until it is assigned.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 10:49:05 +02:00

178 lines
7.6 KiB
JavaScript

(function () {
'use strict';
function formatPoints(value) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.');
}
// ---- Dialoge ----
function closeModal(modal) { modal.hidden = true; }
document.querySelectorAll('.modal-overlay').forEach(function (modal) {
var close = modal.querySelector('.modal-close');
if (close) close.addEventListener('click', function () { closeModal(modal); });
modal.addEventListener('click', function (event) {
if (event.target === modal) closeModal(modal);
});
});
document.addEventListener('keydown', function (event) {
if (event.key !== 'Escape') return;
document.querySelectorAll('.modal-overlay:not([hidden])').forEach(closeModal);
});
// ---- Shop: Beantragen ----
var buyModal = document.getElementById('shop-buy-modal');
if (buyModal) {
var buyItem = document.getElementById('shop-buy-item');
var buyName = document.getElementById('shop-buy-name');
var buyQuantity = document.getElementById('shop-buy-quantity');
var buyTotal = document.getElementById('shop-buy-total');
var price = 0;
var updateTotal = function () {
var qty = parseInt(buyQuantity.value, 10) || 0;
buyTotal.textContent = formatPoints(Math.max(0, qty) * price);
};
var openBuy = function (button, keepValues) {
buyItem.value = button.dataset.itemId;
buyName.textContent = button.dataset.itemName;
price = parseInt(button.dataset.price, 10) || 0;
buyQuantity.max = button.dataset.max;
if (!keepValues) {
buyQuantity.value = 1;
document.getElementById('shop-buy-note').value = '';
}
updateTotal();
buyModal.hidden = false;
buyQuantity.focus();
buyQuantity.select();
};
document.querySelectorAll('.shop-buy').forEach(function (button) {
button.addEventListener('click', function () { openBuy(button, false); });
});
buyQuantity.addEventListener('input', updateTotal);
// Nach einem Fehler ist der Dialog schon offen - Preis und Hoechstmenge nachladen.
if (!buyModal.hidden) {
var current = document.querySelector('.shop-buy[data-item-id="' + buyItem.value + '"]');
if (current) openBuy(current, true); else buyModal.hidden = true;
}
}
// ---- Shop: Suche und Kategorie ----
var search = document.getElementById('shop-search');
var grid = document.getElementById('shop-items');
if (search && grid) {
var category = document.getElementById('shop-category');
var noMatch = document.getElementById('shop-no-match');
var filter = function () {
var term = search.value.trim().toLowerCase();
var cat = category ? category.value : '';
var visible = 0;
grid.querySelectorAll('.shop-card').forEach(function (card) {
var show = (term === '' || card.dataset.name.indexOf(term) !== -1)
&& (cat === '' || card.dataset.category === cat);
card.hidden = !show;
if (show) visible++;
});
if (noMatch) noMatch.hidden = visible > 0;
};
search.addEventListener('input', filter);
if (category) category.addEventListener('change', filter);
}
// ---- Antraege: Grund abfragen (Ablehnen / Stornieren) ----
document.querySelectorAll('form[data-prompt]').forEach(function (form) {
form.addEventListener('submit', function (event) {
var reason = window.prompt(form.dataset.prompt, '');
if (reason === null) {
event.preventDefault();
return;
}
form.querySelector('input[name="reason"]').value = reason;
});
});
// ---- Verwaltung: Gegenstand anlegen / bearbeiten ----
var itemModal = document.getElementById('shop-item-modal');
if (itemModal) {
var form = document.getElementById('shop-item-form');
var title = document.getElementById('shop-item-title');
var mode = form.elements.stock_mode;
var applyMode = function () {
itemModal.querySelectorAll('[data-mode-field]').forEach(function (field) {
field.hidden = field.dataset.modeField !== mode.value;
});
var hint = itemModal.querySelector('[data-mode-hint]');
if (hint) hint.textContent = mode.value === 'warehouse' ? '(Pflicht - daraus kommt der Bestand)' : '(optional, für das Bild)';
form.elements.warehouse_item.required = mode.value === 'warehouse';
};
mode.addEventListener('change', applyMode);
var fill = function (data) {
data = data || {};
form.elements.item_id.value = data.id || '';
form.elements.name.value = data.name || '';
form.elements.category_id.value = data.category_id || '';
form.elements.description.value = data.description || '';
form.elements.price.value = data.price !== undefined ? data.price : '';
mode.value = data.stock_mode || 'manual';
form.elements.stock.value = data.stock !== undefined ? data.stock : 0;
form.elements.warehouse_item.value = data.warehouse_item || '';
form.elements.active.checked = data.active === undefined ? true : !!data.active;
title.textContent = data.id ? 'Gegenstand bearbeiten' : 'Gegenstand anlegen';
applyMode();
};
document.querySelectorAll('[data-open-modal="shop-item-modal"]').forEach(function (button) {
button.addEventListener('click', function () {
fill(button.dataset.item ? JSON.parse(button.dataset.item) : null);
itemModal.hidden = false;
form.elements.name.focus();
});
});
if (itemModal.dataset.old) {
fill(JSON.parse(itemModal.dataset.old));
} else {
applyMode();
}
}
// ---- Verwaltung: aus dem Lagerhaus uebernehmen ----
var importModal = document.getElementById('shop-import-modal');
if (importModal) {
document.querySelectorAll('[data-open-modal="shop-import-modal"]').forEach(function (button) {
button.addEventListener('click', function () { importModal.hidden = false; });
});
var checks = importModal.querySelectorAll('.shop-import-check');
var all = document.getElementById('shop-import-all');
var priceFor = function (check) {
return importModal.querySelector('input[name="prices[' + check.value + ']"]');
};
// Wer einen Gegenstand auswaehlt, muss einen Preis angeben.
checks.forEach(function (check) {
check.addEventListener('change', function () {
priceFor(check).required = check.checked;
all.checked = Array.prototype.every.call(checks, function (c) { return c.checked; });
});
});
all.addEventListener('change', function () {
checks.forEach(function (check) {
check.checked = all.checked;
priceFor(check).required = all.checked;
});
});
document.getElementById('shop-import-apply').addEventListener('click', function () {
var value = document.getElementById('shop-import-price').value;
if (value === '') return;
checks.forEach(function (check) {
if (check.checked) priceFor(check).value = value;
});
});
}
})();