1.3.0 Geburtstagslogik verbessert
This commit is contained in:
13
index.php
13
index.php
@@ -180,10 +180,10 @@ if ($row) {
|
||||
}
|
||||
|
||||
// --- ZAHLENDE PERSON BESTIMMEN ---
|
||||
$next_payer_username = null;
|
||||
$next_payer_info = null;
|
||||
if ($total_accepted > 0) {
|
||||
include_once('zahler.php');
|
||||
$next_payer_username = get_next_payer_username($conn, $meeting_id);
|
||||
$next_payer_info = get_next_payer_info($conn, $meeting_id);
|
||||
}
|
||||
|
||||
|
||||
@@ -369,10 +369,15 @@ $german_weekdays = [
|
||||
<!-- Horizontale Linie hier einfügen -->
|
||||
<hr class="my-1">
|
||||
|
||||
<?php if ($next_payer_username): ?>
|
||||
<?php if ($next_payer_info): ?>
|
||||
<div class="text-center my-2 mx-auto" style="max-width: 500px; border-radius: 0.5rem;">
|
||||
<p class="fw-bold mb-1">Rechnung wird bezahlt von:</p>
|
||||
<h4 class="text-muted fw-bold mb-0"><?= htmlspecialchars($next_payer_username); ?></h4>
|
||||
<h4 class="text-muted fw-bold mb-0">
|
||||
<?= htmlspecialchars($next_payer_info['username']); ?>
|
||||
<?php if ($next_payer_info['is_birthday_payer']): ?>
|
||||
<span class="material-symbols-outlined align-text-bottom" style="font-size: 1.1em; color: #ff6f00;" title="Geburtstagsvorschlag">cake</span>
|
||||
<?php endif; ?>
|
||||
</h4>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
|
||||
@@ -85,17 +85,15 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
}
|
||||
mysqli_stmt_close($stmt_insert);
|
||||
|
||||
// 🔹 GEBURTSTAGS-REGEL: last_birthday_year NUR setzen, wenn Geburtstag bereits war
|
||||
$current_year = (int)date('Y');
|
||||
$meeting_date_for_bday = $meeting['meeting_date'];
|
||||
|
||||
// 🔹 GEBURTSTAGS-ZAHLUNG BEHANDELN
|
||||
foreach ($_POST['user_id'] as $user_id) {
|
||||
$user_id = (int)$user_id;
|
||||
$paid = isset($_POST['paid'][$user_id]) && $_POST['paid'][$user_id] == 1;
|
||||
|
||||
if (!$paid) continue;
|
||||
|
||||
$user_stmt = mysqli_prepare($conn, "SELECT birthday FROM users WHERE id = ? AND birthday IS NOT NULL");
|
||||
// Hole aktuellen Status
|
||||
$user_stmt = mysqli_prepare($conn, "SELECT birthday_payer_pending FROM users WHERE id = ?");
|
||||
mysqli_stmt_bind_param($user_stmt, "i", $user_id);
|
||||
mysqli_stmt_execute($user_stmt);
|
||||
$user_row = mysqli_fetch_assoc(mysqli_stmt_get_result($user_stmt));
|
||||
@@ -103,20 +101,28 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
|
||||
if (!$user_row) continue;
|
||||
|
||||
$birthday = $user_row['birthday'];
|
||||
$birth_month = (int)date('m', strtotime($birthday));
|
||||
$birth_day = (int)date('d', strtotime($birthday));
|
||||
$birthday_this_year = "$current_year-$birth_month-$birth_day";
|
||||
$was_birthday_payer = (bool)$user_row['birthday_payer_pending'];
|
||||
|
||||
if (strtotime($birthday_this_year) <= strtotime($meeting_date_for_bday)) {
|
||||
$update_stmt = mysqli_prepare($conn, "
|
||||
if ($was_birthday_payer) {
|
||||
// 🎂 Geburtstags-Zahlung: NICHT zählen, aber Flag zurücksetzen
|
||||
$update = mysqli_prepare($conn, "
|
||||
UPDATE users
|
||||
SET last_birthday_year = ?
|
||||
SET birthday_payer_pending = 0
|
||||
WHERE id = ?
|
||||
");
|
||||
mysqli_stmt_bind_param($update_stmt, "ii", $current_year, $user_id);
|
||||
mysqli_stmt_execute($update_stmt);
|
||||
mysqli_stmt_close($update_stmt);
|
||||
mysqli_stmt_bind_param($update, "i", $user_id);
|
||||
mysqli_stmt_execute($update);
|
||||
mysqli_stmt_close($update);
|
||||
} else {
|
||||
// 🔢 Normale Zahlung: reguläre Anzahl erhöhen
|
||||
$update = mysqli_prepare($conn, "
|
||||
UPDATE users
|
||||
SET regular_paid_count = regular_paid_count + 1
|
||||
WHERE id = ?
|
||||
");
|
||||
mysqli_stmt_bind_param($update, "i", $user_id);
|
||||
mysqli_stmt_execute($update);
|
||||
mysqli_stmt_close($update);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
86
zahler.php
86
zahler.php
@@ -1,59 +1,73 @@
|
||||
<?php
|
||||
function get_next_payer_username($conn, $meeting_id)
|
||||
{
|
||||
$current_year = (int)date('Y');
|
||||
|
||||
// Alle zugesagten Nutzer mit paid_count
|
||||
/**
|
||||
* Gibt den Vorschlag für den nächsten Rechnungszahler zurück.
|
||||
* Liefert ein Array: ['username' => ..., 'is_birthday_payer' => true/false]
|
||||
*/
|
||||
function get_next_payer_info($conn, $meeting_id)
|
||||
{
|
||||
// Meeting-Datum holen
|
||||
$stmt = mysqli_prepare($conn, "SELECT meeting_date FROM meetings WHERE id = ?");
|
||||
mysqli_stmt_bind_param($stmt, "i", $meeting_id);
|
||||
mysqli_stmt_execute($stmt);
|
||||
$meeting = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));
|
||||
mysqli_stmt_close($stmt);
|
||||
|
||||
if (!$meeting) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$meeting_date = $meeting['meeting_date'];
|
||||
$current_year = (int)date('Y', strtotime($meeting_date));
|
||||
|
||||
// Alle ZUSAGENDEN Teilnehmer dieses Meetings
|
||||
$sql = "
|
||||
SELECT
|
||||
u.id,
|
||||
u.username,
|
||||
u.birthday,
|
||||
u.last_birthday_year,
|
||||
(SELECT COUNT(*) FROM meeting_teilnehmer WHERE user_id = u.id AND paid = 1) AS paid_count
|
||||
u.birthday_payer_pending,
|
||||
u.regular_paid_count
|
||||
FROM meeting_teilnehmer mt
|
||||
JOIN users u ON mt.user_id = u.id
|
||||
WHERE mt.meeting_id = ? AND mt.rsvp_status = 'accepted'
|
||||
ORDER BY u.username
|
||||
";
|
||||
|
||||
$stmt = mysqli_prepare($conn, $sql);
|
||||
mysqli_stmt_bind_param($stmt, "i", $meeting_id);
|
||||
mysqli_stmt_execute($stmt);
|
||||
$candidates = mysqli_fetch_all(mysqli_stmt_get_result($stmt), MYSQLI_ASSOC);
|
||||
mysqli_stmt_close($stmt);
|
||||
|
||||
if (empty($candidates)) return null;
|
||||
|
||||
// Finde minimale paid_count
|
||||
$min_paid = min(array_column($candidates, 'paid_count'));
|
||||
$eligible = array_filter($candidates, fn($c) => $c['paid_count'] == $min_paid);
|
||||
|
||||
$birthday_eligible = [];
|
||||
$others = [];
|
||||
|
||||
foreach ($eligible as $c) {
|
||||
$is_birthday_payer_eligible = (
|
||||
!empty($c['birthday'])
|
||||
&& $c['birthday'] !== '0000-00-00'
|
||||
&& $c['last_birthday_year'] != $current_year
|
||||
);
|
||||
|
||||
if ($is_birthday_payer_eligible) {
|
||||
$birthday_eligible[] = $c;
|
||||
} else {
|
||||
$others[] = $c;
|
||||
}
|
||||
if (empty($candidates)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Sortiere alphabetisch
|
||||
$sort = fn($a, $b) => strcmp($a['username'], $b['username']);
|
||||
|
||||
if (!empty($birthday_eligible)) {
|
||||
usort($birthday_eligible, $sort);
|
||||
return $birthday_eligible[0]['username'];
|
||||
// 🔹 PRIORITÄT 1: Geburtstags-Zahler (birthday_payer_pending = 1)
|
||||
$birthday_candidates = array_filter($candidates, fn($c) => $c['birthday_payer_pending'] == 1);
|
||||
if (!empty($birthday_candidates)) {
|
||||
usort($birthday_candidates, fn($a, $b) => strcmp($a['username'], $b['username']));
|
||||
$first = $birthday_candidates[0];
|
||||
return [
|
||||
'username' => $first['username'],
|
||||
'is_birthday_payer' => true
|
||||
];
|
||||
}
|
||||
|
||||
$all = array_merge($birthday_eligible, $others);
|
||||
usort($all, $sort);
|
||||
return $all[0]['username'];
|
||||
// 🔹 PRIORITÄT 2: Normale Rotation (nach regular_paid_count)
|
||||
$min_paid = min(array_column($candidates, 'regular_paid_count'));
|
||||
$regular_candidates = array_filter($candidates, fn($c) => $c['regular_paid_count'] == $min_paid);
|
||||
usort($regular_candidates, fn($a, $b) => strcmp($a['username'], $b['username']));
|
||||
$first = $regular_candidates[0];
|
||||
return [
|
||||
'username' => $first['username'],
|
||||
'is_birthday_payer' => false
|
||||
];
|
||||
}
|
||||
|
||||
function get_next_payer_username($conn, $meeting_id)
|
||||
{
|
||||
$info = get_next_payer_info($conn, $meeting_id);
|
||||
return $info ? $info['username'] : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user