60 lines
1.7 KiB
PHP
Executable File
60 lines
1.7 KiB
PHP
Executable File
<?php
|
|
function get_next_payer_username($conn, $meeting_id)
|
|
{
|
|
$current_year = (int)date('Y');
|
|
|
|
// Alle zugesagten Nutzer mit paid_count
|
|
$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
|
|
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;
|
|
}
|
|
}
|
|
|
|
// Sortiere alphabetisch
|
|
$sort = fn($a, $b) => strcmp($a['username'], $b['username']);
|
|
|
|
if (!empty($birthday_eligible)) {
|
|
usort($birthday_eligible, $sort);
|
|
return $birthday_eligible[0]['username'];
|
|
}
|
|
|
|
$all = array_merge($birthday_eligible, $others);
|
|
usort($all, $sort);
|
|
return $all[0]['username'];
|
|
}
|