diff --git a/participant.php b/participant.php index 472eae8..499e425 100755 --- a/participant.php +++ b/participant.php @@ -85,41 +85,49 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { } mysqli_stmt_close($stmt_insert); - // 🔹 GEBURTSTAGS-ZAHLUNG BEHANDELN + // 🔹 GEBURTSTAGS-ZAHLUNG BEHANDELN – MIT last_birthday_year + $meeting_year = (int)date('Y', strtotime($meeting['meeting_date'])); + $meeting_month = (int)date('n', strtotime($meeting['meeting_date'])); + $meeting_day = (int)date('j', strtotime($meeting['meeting_date'])); + 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; - // Hole aktuellen Status - $user_stmt = mysqli_prepare($conn, "SELECT birthday_payer_pending FROM users WHERE id = ?"); + $user_stmt = mysqli_prepare($conn, "SELECT birthday, last_birthday_year 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)); mysqli_stmt_close($user_stmt); - if (!$user_row) continue; - - $was_birthday_payer = (bool)$user_row['birthday_payer_pending']; - - if ($was_birthday_payer) { - // 🎂 Geburtstags-Zahlung: NICHT zählen, aber Flag zurücksetzen - $update = mysqli_prepare($conn, " - UPDATE users - SET birthday_payer_pending = 0 - WHERE id = ? - "); + if (!$user_row || !$user_row['birthday'] || $user_row['birthday'] === '0000-00-00') { + // Kein Geburtstag → normale Zahlung + $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); + continue; + } + + $last_bday_year = (int)($user_row['last_birthday_year'] ?? 0); + $bday_month = (int)date('n', strtotime($user_row['birthday'])); + $bday_day = (int)date('j', strtotime($user_row['birthday'])); + + $birthday_passed = ($bday_month < $meeting_month || + ($bday_month == $meeting_month && $bday_day <= $meeting_day)); + $birthday_not_paid_yet = ($last_bday_year < $meeting_year); + + if ($birthday_passed && $birthday_not_paid_yet) { + // 🎂 Geburtstags-Zahlung + $update = mysqli_prepare($conn, "UPDATE users SET last_birthday_year = ? WHERE id = ?"); + mysqli_stmt_bind_param($update, "ii", $meeting_year, $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 = ? - "); + // 🔢 Normale Zahlung + $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); diff --git a/zahler.php b/zahler.php index 5ce57f2..a919003 100755 --- a/zahler.php +++ b/zahler.php @@ -18,15 +18,18 @@ function get_next_payer_info($conn, $meeting_id) } $meeting_date = $meeting['meeting_date']; - $current_year = (int)date('Y', strtotime($meeting_date)); + $meeting_ts = strtotime($meeting_date); + $meeting_year = (int)date('Y', $meeting_ts); + $meeting_month = (int)date('n', $meeting_ts); + $meeting_day = (int)date('j', $meeting_ts); - // Alle ZUSAGENDEN Teilnehmer dieses Meetings + // Alle ZUSAGENDEN Teilnehmer – mit last_birthday_year $sql = " SELECT u.id, u.username, u.birthday, - u.birthday_payer_pending, + u.last_birthday_year, u.regular_paid_count FROM meeting_teilnehmer mt JOIN users u ON mt.user_id = u.id @@ -44,8 +47,25 @@ function get_next_payer_info($conn, $meeting_id) return null; } - // 🔹 PRIORITÄT 1: Geburtstags-Zahler (birthday_payer_pending = 1) - $birthday_candidates = array_filter($candidates, fn($c) => $c['birthday_payer_pending'] == 1); + // 🔹 Geburtstagskandidaten: Geburtstag war + noch nicht in diesem Jahr gezahlt + $birthday_candidates = array_filter($candidates, function ($c) use ($meeting_year, $meeting_month, $meeting_day) { + if (!$c['birthday'] || $c['birthday'] === '0000-00-00') { + return false; + } + + $last_bday_year = (int)($c['last_birthday_year'] ?? 0); + if ($last_bday_year >= $meeting_year) { + return false; // Bereits in diesem Jahr gezahlt + } + + $bday_month = (int)date('n', strtotime($c['birthday'])); + $bday_day = (int)date('j', strtotime($c['birthday'])); + + // War der Geburtstag in diesem Jahr bereits? + return ($bday_month < $meeting_month || + ($bday_month == $meeting_month && $bday_day <= $meeting_day)); + }); + if (!empty($birthday_candidates)) { usort($birthday_candidates, fn($a, $b) => strcmp($a['username'], $b['username'])); $first = $birthday_candidates[0]; @@ -55,7 +75,7 @@ function get_next_payer_info($conn, $meeting_id) ]; } - // 🔹 PRIORITÄT 2: Normale Rotation (nach regular_paid_count) + // 🔹 Normale Rotation $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'])); @@ -65,9 +85,3 @@ function get_next_payer_info($conn, $meeting_id) '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; -}