1.3.0
This commit is contained in:
58
zahler.php
58
zahler.php
@@ -1,35 +1,59 @@
|
||||
<?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 paid_count ASC
|
||||
ORDER BY u.username
|
||||
";
|
||||
$stmt = mysqli_prepare($conn, $sql);
|
||||
if (!$stmt) return null;
|
||||
|
||||
mysqli_stmt_bind_param($stmt, "i", $meeting_id);
|
||||
mysqli_stmt_execute($stmt);
|
||||
$result = mysqli_stmt_get_result($stmt);
|
||||
|
||||
$candidates = [];
|
||||
$min_count = -1;
|
||||
while ($row = mysqli_fetch_assoc($result)) {
|
||||
if ($min_count == -1 || $row['paid_count'] < $min_count) {
|
||||
$min_count = $row['paid_count'];
|
||||
$candidates = [$row['username']];
|
||||
} elseif ($row['paid_count'] == $min_count) {
|
||||
$candidates[] = $row['username'];
|
||||
}
|
||||
}
|
||||
$candidates = mysqli_fetch_all(mysqli_stmt_get_result($stmt), MYSQLI_ASSOC);
|
||||
mysqli_stmt_close($stmt);
|
||||
|
||||
if (empty($candidates)) return null;
|
||||
sort($candidates);
|
||||
return $candidates[0];
|
||||
|
||||
// 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'];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user