Files
WKVS/www/intern/wk-leitung/rechnungen.php

648 lines
22 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
// Show all errors except deprecation notices (these come from vendor libraries
// that aren't yet typed for newer PHP versions). Long-term fix: update
// dependencies to versions compatible with your PHP runtime.
error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
if (!isset($baseDir)) {
$baseDir = $_SERVER['DOCUMENT_ROOT'];
}
require_once $baseDir . '/../scripts/session_functions.php';
ini_wkvs_session(true);
$csrf_token = $_SESSION['csrf_token'] ?? '';
$access_granted_wkl = check_user_permission('wk_leitung', true) ?? false;
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="png" href="/intern/img/icon.png">
<title>Intern - Rechnungen</title>
<link rel="stylesheet" href="/intern/css/rechnungen.css">
<link rel="stylesheet" href="/intern/css/sidebar.css">
<link rel="stylesheet" href="/files/fonts/fonts.css">
<script src="/intern/js/jquery/jquery-3.7.1.min.js"></script>
<script>
(function () {
let lastWrite = 0;
const interval = 200;
window.addEventListener('scroll', function () {
const now = Date.now();
if (now - lastWrite >= interval) {
sessionStorage.setItem('scrollY', window.scrollY);
lastWrite = now;
}
}, { passive: true });
})();
</script>
</head>
<body>
<?php
if ( ! $access_granted_wkl ) :
$logintype = 'wk_leitung';
require $baseDir . '/../scripts/login/login.php';
$logintype = '';
else :
require $baseDir . '/../scripts/db/db-functions.php';
require $baseDir . '/../scripts/db/db-tables.php';
$type = 'wkl';
$dbconnection = require $baseDir . '/../scripts/db/db-verbindung-script.php';
if ($dbconnection['success'] !== true){
echo 'Critical DB Error.';
exit;
}
$currentPage = 'rechnungen';
require $baseDir . '/intern/scripts/sidebar/sidebar.php';
setlocale(LC_TIME, 'de_DE.UTF-8');
$svgbezahlt = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="12" cy="12" r="10" fill="#28a745"/><path d="M7 12l3 3 7-7" stroke="white" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></svg>';
$svgpending = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6 2h12v2a6 6 0 0 1-6 6 6 6 0 0 1-6-6V2zm0 20h12v-2a6 6 0 0 0-6-6 6 6 0 0 0-6 6v2zm6-10c1.657 0 3-1.343 3-3H9c0 1.657 1.343 3 3 3z" fill="#0073aa"/></svg>';
$svgnichtbezahlt = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="12" cy="12" r="10" fill="#dc3545"/><path d="M8 8l8 8M16 8l-8 8" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
$currentYear = (date('n') > 6) ? date('Y') + 1 : date('Y');
$sql = "SELECT
o.*,
iu.username AS order_creator
FROM
$tableOrders o
LEFT JOIN
$tableInternUsers iu
ON iu.id = o.user_id
WHERE o.order_status <> 0
ORDER BY
o.order_status ASC,
o.timestamp DESC;
";
$sel = $mysqli->prepare($sql);
if (!$sel->execute()) {
http_response_code(500);
exit;
}
$allRechnungenUnsorted = [];
$res = $sel->get_result();
while ($row = $res->fetch_assoc()) {
$allRechnungenUnsorted[] = $row;
}
foreach ($allRechnungenUnsorted as $r) {
$allRechnungen[$r['order_status']][] = $r;
}
function titleStatus(int $int) : string {
switch ($int) {
case 1:
return "Zahlungseingang überprüfen";
case 2:
return "Bezahlte Rechnungen";
default:
return "FEHLER BEI NAMENSGENERRIERUNG";
}
}
function classStatus(int $int) : string {
switch ($int) {
case 1:
return "inProcess";
case 2:
return "payed";
default:
return "notPayed";
}
}
?>
<div class="internMenuDiv">
<div class="closeInternMenuMobileDiv"></div>
<div class="innerInternMenuDiv">
<div class="header-text-conatiner"></div>
<div class="referenzDiv">
<h3 class="containerHeading">Rechnung als bezahlt eintagen</h3>
<label for="scorNumber">Referenznummer<br><br>
<div class="referenzDivInputDiv">
<span>RF</span>
<input
type="text"
id="scorNumber"
inputmode="numeric"
autocomplete="off"
placeholder="12 3456 7890"
maxlength="19"
>
</div>
</label>
<!-- Hidden field for backend -->
<input type="hidden" id="scorNumberRaw" name="scor_reference">
<button id="submitScorNumber">Bestätigen</button>
</div>
</div>
</div>
<section class="bgSection">
<?php sidebarRender('modal'); ?>
<div class="headerDivTrainer">
<div class="headingPanelDiv">
<h2 class="headingPanel">Rechnungen</h2>
<h3 class="headingPanelUser"><?= $currentYear ?></h3>
</div>
<div class="menuWrapper">
<div class="trainerBurgerMenuDiv">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
<path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"/>
</svg>
</div>
<?php sidebarRender('button'); ?>
</div>
</div>
<script>
const menuburger = document.querySelector('.trainerBurgerMenuDiv');
const menudiv = document.querySelector('.internMenuDiv');
//const menubg = document.querySelector('.menuBg');
const content = document.querySelector('.bgSection');
const closeMenuMobile = document.querySelector('.closeInternMenuMobileDiv');
const storageKey = "trainerInternMenuOpen";
const isOpen = localStorage.getItem(storageKey) === "true";
document.addEventListener('keydown', function (e) {
// Mac = Option, Windows/Linux = Alt
const isOptionOrAlt = e.altKey || e.metaKey;
if (e.key.toLowerCase() === 'm') {
menuburger.classList.add("menuTransition");
menudiv.classList.add("menuTransition");
content.classList.add("menuTransition");
menuburger.classList.toggle("open");
menudiv.classList.toggle("open");
content.classList.toggle("open");
const isOpenEl =
menudiv.classList.contains("open") &&
menuburger.classList.contains("open");
localStorage.setItem(storageKey, isOpenEl);
}
});
if (isOpen) {
menuburger.classList.add("open");
menudiv.classList.add("open");
content.classList.add("open");
}
menuburger.addEventListener("click", function () {
menuburger.classList.add("menuTransition");
menudiv.classList.add("menuTransition");
content.classList.add("menuTransition");
menuburger.classList.toggle("open");
menudiv.classList.toggle("open");
content.classList.toggle("open");
const isOpenEl =
menudiv.classList.contains("open") &&
menuburger.classList.contains("open");
localStorage.setItem(storageKey, isOpenEl);
});
closeMenuMobile.addEventListener("click", function () {
const isOpenEl =
menudiv.classList.contains("open") &&
menuburger.classList.contains("open");
if (isOpenEl) {
menuburger.classList.remove("open");
menudiv.classList.remove("open");
content.classList.remove("open");
localStorage.setItem(storageKey, false);
}
});
</script>
</div>
<div class="containerDiv">
<h3 class="containerHeading">Alle Rechnungen:</h3>
<?php if (!isset($allRechnungen) || !is_array($allRechnungen) || count($allRechnungen) < 1) : ?>
<h3>Keine erstellten Rechungen vorhanden</h3>
<?php else :?>
<div>
<p class="labelBulkSelect">Bulk Select:</p>
<div class="bulkSelectDiv">
<div class="customSelect" id="bulkSelectedOption" data-value="">
<button type="button" class="selectTrigger bulkSelect" aria-expanded="false">
<span class="selectLabel">Aktion auswählen</span>
<svg class="selectArrow" xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' height="14" width="14">
<path d='M6 9L12 15L18 9' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/>\
</svg>
</button>
<ul class="selectOptions">
<li data-value="deleteEntrys">Rechnungen stornieren</li>
</ul>
</div>
<input type="submit" name="apply_bulk_action" class="bulkSelectSubmit" value="Anwenden">
</div>
<?php foreach ($allRechnungen as $status => $oneRechnungsStatus) : ?>
<?php $totalOrderVolume = 0; $totalPayedVolume = 0; ?>
<h2 class="titleSingleProg"><?= titleStatus(intval($status)) ?></h2>
<table class="wkvsTabelle widefat striped">
<thead>
<tr>
<th style="width:20px;">
<label class="checkbox">
<input type="checkbox" id="check_all_<?= titleStatus(intval($status)) ?>">
<span class="checkbox-ui"></span>
</label>
</th>
<th>Rechnungsnummer</th>
<th>Status</th>
<th>Typ</th>
<th>Ersteller</th>
<th>Datum</th>
<th>Betrag</th>
</tr>
</thead>
<tbody>
<?php foreach ($oneRechnungsStatus as $row) : ?>
<tr>
<td>
<label class="checkbox">
<input type="checkbox" name="bulk_ids[]" class="row_check_<?= titleStatus(intval($status)) ?>" value="<?= intval($row['order_id']) ?>">
<span class="checkbox-ui"></span>
</label>
</td>
<td>
<?php if (file_exists($baseDir . "/../private-files/rechnungen/" . intval($row['order_id']) . ".pdf")) : ?>
<a href="/intern/wk-leitung/rechnungen_viewer?order_id=<?= intval($row['order_id']) ?>" target="_blank">
<?= intval($row['order_id']) ?>
</a>
<?php else: ?>
<?= intval($row['order_id']) ?>
<?php endif; ?>
</td>
<td><?php
switch (intval($status)) {
case '2':
echo $svgbezahlt;
break;
case '1':
echo $svgpending;
break;
default:
echo $svgnichtbezahlt;
break;
}?> </td>
<td><?= htmlspecialchars($row['order_type']) ?></td>
<td><?= htmlspecialchars($row['order_creator']) ?></td>
<td><?= htmlspecialchars(strftime('%d. %B %Y', (new DateTime($row['timestamp']))->getTimestamp())) ?></td>
<td><?= htmlspecialchars(number_format(floatval($row['preis']), 2)) ?> CHF</td>
<?php $totalOrderVolume += floatval($row['preis']); ?>
</tr>
<?php endforeach; ?>
<tr class="totalTr">
<td colspan="6" class="totalTd"></td>
<td class="totalValue"><span class="<?= classStatus(intval($status)) ?>"><?= htmlspecialchars(number_format(floatval($totalOrderVolume), 2))?> CHF</span></td>
</tr>
</tbody>
</table>
<?php endforeach; ?>
<?php endif; ?>
</div>
<!--<div class="containerDiv"><h3 class="headingAlleTurnerinnen">Alle Turnerinnen:</h3>
<?php //else : ?>
<p>Noch keine Datensätze vorhanden.</p>
<?php // endif; ?>
-->
<div class="msgDiv"></div>
</section>
<?php endif; ?>
<script>
const csrf_token = "<?= $csrf_token ?>";
const $input = $('#scorNumber');
const $rawInput = $('#scorNumberRaw');
$input.on('input', function() {
let value = $(this).val().replace(/\D/g, ''); // remove non-digits
// Optional: limit total digits to 21 (SCOR standard)
value = value.slice(0, 21);
// Update hidden raw value
$rawInput.val(value);
// Format: first 2 digits, then groups of 4
let formatted = '';
if (value.length > 0) {
// First 2 digits
formatted += value.substr(0, 2);
// Remaining digits
let remaining = value.substr(2);
if (remaining.length > 0) {
formatted += ' ' + remaining.match(/.{1,4}/g).join(' ');
}
}
$(this).val(formatted);
});
$('#submitScorNumber').on('click', function(e) {
e.preventDefault(); // prevent form submission if it's a button
const SCOR = $('#scorNumberRaw').val();
fetch('/intern/scripts/rechnungen/ajax_update_order_status.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
csrf_token,
scor: SCOR
})
})
.then(res => res.json().then(data => ({ status: res.status, ok: res.ok, data })))
.then(({ status, ok, data }) => {
if (!ok) {
throw {
status,
message: data.message || 'Update failed'
};
}
window.location.reload();
})
.catch(err => {
displayMsg(0, `${err.message}`);
});
});
$('.bulkSelectSubmit').on('click', function (e) {
e.preventDefault();
const valBulkSelect = $('#bulkSelectedOption').data('value');
console.log(valBulkSelect);
if (valBulkSelect === 'deleteEntrys') {
let arrayIds = [];
$('[class^="row_check_"]').each(function () {
if ($(this).prop('checked')) {
arrayIds.push($(this).val());
}
});
if (Array.isArray(arrayIds) && arrayIds.length) {
const params = new URLSearchParams();
arrayIds.forEach(id => {
params.append('ids[]', id);
});
params.append('csrf_token', csrf_token);
fetch('/intern/scripts/rechnungen/ajax_delete_order.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: params
})
.then(res => res.json().then(data => ({ status: res.status, ok: res.ok, data })))
.then(({ status, ok, data }) => {
if (!ok) {
throw {
status,
message: data.message || 'Update failed'
};
}
window.location.reload();
})
.catch(err => {
displayMsg(0, `${err.message} (HTTP ${err.status ?? 'unknown'})`);
});
}
}
});
document.querySelectorAll('[id^="check_all_"]').forEach(master => {
const programm = master.id.replace("check_all_", "");
const checkboxes = document.querySelectorAll(".row_check_" + programm);
// toggle all rows when master is clicked
master.addEventListener("change", function() {
checkboxes.forEach(cb => cb.checked = master.checked);
});
// update master if all rows are manually checked/unchecked
checkboxes.forEach(cb => {
cb.addEventListener("change", function() {
master.checked = [...checkboxes].every(c => c.checked);
});
});
});
function displayMsg(type, msg) {
const colors = ["#900000ff", "#00b200ff"];
if (type !== 0 && type !== 1) return;
// idx is ALWAYS a valid non-negative index now
const $div = $('<div class="msgBox"></div>')
.css({'border-color': colors[type]})
.text(msg);
$('.msgDiv').append($div);
// trigger entry animation
setTimeout(() => {
$div.addClass('show');
}, 200);
setTimeout(() => {
// First: set the transition properties
$div.css({
'transition': 'all 1s ease'
});
// Next frame: apply the transform so the transition animates
requestAnimationFrame(() => {
$div.removeClass('show');
$div.css({
'transform': 'translateX(calc(100% + 40px)) scaleY(0)' //scaleY(0.5)
});
});
}, 5000);
// auto-remove
setTimeout(() => {
$div.remove();
}, 6500);
}
</script>
<style>
.customSelect {
position: relative;
width: 240px;
font-family: system-ui, sans-serif;
}
.customSelect > * {
margin: 0;
}
.bulkSelectDiv {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 30px;
}
.bulkSelect:focus .selectArrow {
rotate: 180deg
}
.selectTrigger {
width: 100%;
border: 1px solid #ccc;
background: #fff;
display: flex;
justify-content: space-between;
align-items: center;
cursor: pointer;
}
.selectOptions {
position: absolute;
top: 100%;
left: 0;
right: 0;
padding: 0;
border: 1px solid #ccc;
border-top: none;
list-style: none;
background: #fff;
display: none;
z-index: 1000;
}
.selectOptions li {
padding: 10px 12px;
cursor: pointer;
}
.selectOptions li:hover,
.selectOptions li.selected {
background-color: #f2f2f2;
}
</style>
<script>
$(function () {
$(".selectTrigger").on("click", function (e) {
e.stopPropagation();
const $select = $(this).closest(".customSelect");
const $options = $select.find(".selectOptions");
$(".selectOptions").not($options).hide();
$(".selectTrigger").not(this).attr("aria-expanded", "false");
$options.toggle();
$(this).attr("ariaExpanded", $options.is(":visible"));
});
$(".selectOptions li").on("click", function () {
const $item = $(this);
const $select = $item.closest(".customSelect");
$select.find(".selectLabel").text($item.text());
$select.attr("data-value", $item.data("value"));
$item
.addClass("selected")
.siblings()
.removeClass("selected");
$select.find(".selectOptions").hide();
$select.find(".selectTrigger").attr("aria-expanded", "false");
});
$(document).on("click", function () {
$(".selectOptions").hide();
$(".selectTrigger").attr("aria-expanded", "false");
});
});
</script>