v1.3.1 - Zahlerlogik verbessert

This commit is contained in:
Borgal
2025-11-19 20:40:09 +01:00
parent 2046d80c9b
commit 654157f174
6 changed files with 136 additions and 77 deletions

View File

@@ -23,14 +23,12 @@ function get_next_payer_info($conn, $meeting_id)
$meeting_month = (int)date('n', $meeting_ts);
$meeting_day = (int)date('j', $meeting_ts);
// Alle ZUSAGENDEN Teilnehmer mit last_birthday_year
// Alle ZUSAGENDEN Teilnehmer
$sql = "
SELECT
u.id,
u.username,
u.birthday,
u.last_birthday_year,
u.regular_paid_count
u.birthday
FROM meeting_teilnehmer mt
JOIN users u ON mt.user_id = u.id
WHERE mt.meeting_id = ? AND mt.rsvp_status = 'accepted'
@@ -47,23 +45,36 @@ function get_next_payer_info($conn, $meeting_id)
return null;
}
// 🔹 Geburtstagskandidaten: Geburtstag war + noch nicht in diesem Jahr gezahlt
$birthday_candidates = array_filter($candidates, function ($c) use ($meeting_year, $meeting_month, $meeting_day) {
// 🔹 Geburtstagskandidaten: Geburtstag war + noch nicht in diesem Jahr als Geburtstagszahler gezahlt
$birthday_candidates = array_filter($candidates, function ($c) use ($conn, $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));
// War Geburtstag in diesem Jahr bereits?
if (!($bday_month < $meeting_month ||
($bday_month == $meeting_month && $bday_day <= $meeting_day))) {
return false;
}
// Hat er in diesem Jahr schon als Geburtstagszahler gezahlt?
$check_stmt = mysqli_prepare($conn, "
SELECT 1 FROM meeting_teilnehmer mt
JOIN meetings m ON mt.meeting_id = m.id
WHERE mt.user_id = ?
AND mt.birthday_pay = 1
AND YEAR(m.meeting_date) = ?
LIMIT 1
");
mysqli_stmt_bind_param($check_stmt, "ii", $c['id'], $meeting_year);
mysqli_stmt_execute($check_stmt);
$already_paid = mysqli_num_rows(mysqli_stmt_get_result($check_stmt)) > 0;
mysqli_stmt_close($check_stmt);
return !$already_paid;
});
if (!empty($birthday_candidates)) {
@@ -75,11 +86,28 @@ function get_next_payer_info($conn, $meeting_id)
];
}
// 🔹 Normale Rotation
$min_paid = min(array_column($candidates, 'regular_paid_count'));
$regular_candidates = array_filter($candidates, fn($c) => $c['regular_paid_count'] == $min_paid);
// 🔹 Normale Rotation: Zähle paid=1 UND birthday_pay=0
$user_paid_counts = [];
foreach ($candidates as $c) {
$count_stmt = mysqli_prepare($conn, "
SELECT COUNT(*)
FROM meeting_teilnehmer
WHERE user_id = ? AND paid = 1 AND birthday_pay = 0
");
mysqli_stmt_bind_param($count_stmt, "i", $c['id']);
mysqli_stmt_execute($count_stmt);
$count = (int)mysqli_fetch_row(mysqli_stmt_get_result($count_stmt))[0];
mysqli_stmt_close($count_stmt);
$user_paid_counts[$c['id']] = $count;
}
$min_paid = min($user_paid_counts);
$regular_candidates = array_filter($candidates, function ($c) use ($user_paid_counts, $min_paid) {
return $user_paid_counts[$c['id']] === $min_paid;
});
usort($regular_candidates, fn($a, $b) => strcmp($a['username'], $b['username']));
$first = $regular_candidates[0];
$first = reset($regular_candidates);
return [
'username' => $first['username'],
'is_birthday_payer' => false