diff --git a/index.php b/index.php index 14c018d..3600807 100755 --- a/index.php +++ b/index.php @@ -23,7 +23,7 @@ function is_user_on_vacation($conn, $user_id, $meeting_date) return $found !== null; } -// 🔹 Funktion: Automatisch ablehnen, wenn im Urlaub (außer bei expliziter Zusage) +// 🔹 Funktion: Automatisch ablehnen, wenn im Urlaub function auto_decline_if_on_vacation($conn, $meeting_id, $user_id, $meeting_date) { if (!is_user_on_vacation($conn, $user_id, $meeting_date)) { @@ -39,12 +39,10 @@ function auto_decline_if_on_vacation($conn, $meeting_id, $user_id, $meeting_date $current_status = $existing ? $existing['rsvp_status'] : null; - // Wenn explizit "accepted" oder "maybe" gewählt wurde → NICHT überschreiben! if ($current_status === 'accepted' || $current_status === 'maybe') { return $current_status; } - // Nur wenn Status "declined" oder NULL → auf "declined" setzen if ($existing) { $upd = mysqli_prepare($conn, "UPDATE meeting_teilnehmer SET rsvp_status = 'declined', attended = 0 WHERE meeting_id = ? AND user_id = ?"); mysqli_stmt_bind_param($upd, "ii", $meeting_id, $user_id); @@ -181,36 +179,66 @@ if ($row) { mysqli_stmt_close($attendees_stmt); } - // --- ZAHLENDE PERSON BESTIMMEN --- + // --- ZAHLENDE PERSON BESTIMMEN (MIT GEBURTSTAGS-REGEL) --- $next_payer_username = null; if ($total_accepted > 0) { - $sql_next_payer = " + $current_year = date('Y'); + $meeting_date = $row['meeting_date']; + + // Kandidaten holen + $sql_candidates = " 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 FROM meeting_teilnehmer mt JOIN users u ON mt.user_id = u.id WHERE mt.meeting_id = ? AND mt.rsvp_status = 'accepted' - ORDER BY paid_count ASC + ORDER BY u.username "; - $stmt_next_payer = mysqli_prepare($conn, $sql_next_payer); - mysqli_stmt_bind_param($stmt_next_payer, "i", $meeting_id); - mysqli_stmt_execute($stmt_next_payer); - $result_next_payer = mysqli_stmt_get_result($stmt_next_payer); - $payer_candidates = []; - $min_paid_count = -1; - while ($row_payer = mysqli_fetch_assoc($result_next_payer)) { - if ($min_paid_count == -1 || $row_payer['paid_count'] < $min_paid_count) { - $min_paid_count = $row_payer['paid_count']; - $payer_candidates = [$row_payer['username']]; - } elseif ($row_payer['paid_count'] == $min_paid_count) { - $payer_candidates[] = $row_payer['username']; + + $stmt_candidates = mysqli_prepare($conn, $sql_candidates); + mysqli_stmt_bind_param($stmt_candidates, "i", $meeting_id); + mysqli_stmt_execute($stmt_candidates); + $candidates = mysqli_fetch_all(mysqli_stmt_get_result($stmt_candidates), MYSQLI_ASSOC); + mysqli_stmt_close($stmt_candidates); + + $birthday_payers = []; + $regular_payers = []; + + foreach ($candidates as $c) { + if (empty($c['birthday'])) { + $regular_payers[] = $c; + continue; + } + + $birth_month = (int)date('m', strtotime($c['birthday'])); + $birth_day = (int)date('d', strtotime($c['birthday'])); + $birthday_this_year = "$current_year-$birth_month-$birth_day"; + + $already_paid_this_year = ($c['last_birthday_year'] == $current_year); + $birthday_passed = (strtotime($birthday_this_year) <= strtotime($meeting_date)); + + if (!$already_paid_this_year && $birthday_passed) { + $birthday_payers[] = $c; + } else { + $regular_payers[] = $c; } } - mysqli_stmt_close($stmt_next_payer); - if (!empty($payer_candidates)) { - sort($payer_candidates); - $next_payer_username = $payer_candidates[0]; + + // Priorität: Geburtstagskinder zuerst + if (!empty($birthday_payers)) { + usort($birthday_payers, function ($a, $b) { + return $a['paid_count'] <=> $b['paid_count']; + }); + $next_payer_username = $birthday_payers[0]['username']; + } elseif (!empty($regular_payers)) { + usort($regular_payers, function ($a, $b) { + return $a['paid_count'] <=> $b['paid_count']; + }); + $next_payer_username = $regular_payers[0]['username']; } } @@ -283,7 +311,6 @@ $german_weekdays = [
nächster Termin:
= $german_weekday . ' ' . date('d.m.Y H:i', strtotime($row['meeting_date'])) ?>
- -Du hast zugesagt!