154 lines
4.9 KiB
PHP
154 lines
4.9 KiB
PHP
<?php
|
|
|
|
function update_teilnehmende($ids_json = '', $old_status, $new_status, bool $ids_are_array = false) {
|
|
global $mysqli;
|
|
global $db_tabelle_teilnehmende;
|
|
global $db_tabelle_kategorien;
|
|
|
|
// 1. Quick exits
|
|
if (intval($old_status) === intval($new_status)) return false;
|
|
|
|
if (!$ids_are_array) {
|
|
$programm_ids = json_decode($ids_json, true) ?? [];
|
|
} else {
|
|
if (!is_array($ids_json)) return false;
|
|
$programm_ids = $ids_json;
|
|
}
|
|
|
|
if (count($programm_ids) < 1) return false;
|
|
|
|
// 2. Extract IDs (which are the keys of the associative array)
|
|
$ids = array_keys($programm_ids);
|
|
$tparams = implode(", ", array_fill(0, count($ids), "?"));
|
|
|
|
// 3. Fetch current paid amounts
|
|
$all_teilnehmede_stmt = $mysqli->prepare("SELECT `id`, `betrag_bezahlt`, `programm` FROM $db_tabelle_teilnehmende WHERE `id` IN ($tparams)");
|
|
$all_teilnehmede_stmt->bind_param(str_repeat("i", count($ids)), ...$ids);
|
|
$all_teilnehmede_stmt->execute();
|
|
$all_teilnehmede_res = $all_teilnehmede_stmt->get_result();
|
|
$all_teilnehmede = $all_teilnehmede_res->fetch_all(MYSQLI_ASSOC);
|
|
$all_teilnehmede_stmt->close();
|
|
|
|
$all_preise_db = db_select($mysqli, $db_tabelle_kategorien, "`programm`, `preis`");
|
|
|
|
$all_preise = array_column($all_preise_db, 'preis', 'programm');
|
|
|
|
// Creates a clean map of [id => betrag_bezahlt]
|
|
$all_teilnehmede_indexed = array_column($all_teilnehmede, 'betrag_bezahlt', 'id');
|
|
$all_teilnehmede_indexed_kat = array_column($all_teilnehmede, 'programm', 'id');
|
|
|
|
// 4. Prepare the update statement ONCE
|
|
$update_teiln_stmt = $mysqli->prepare("UPDATE $db_tabelle_teilnehmende SET `betrag_bezahlt` = ?, `bezahlt` = ? WHERE `id` = ?");
|
|
|
|
// Bind variables by reference once
|
|
$n_preis = 0.0;
|
|
|
|
$normal_order_status = match ($new_status) {
|
|
1 => 3,
|
|
2 => 4,
|
|
3 => 1,
|
|
default => 1
|
|
};
|
|
|
|
$status_to_set = $normal_order_status;
|
|
|
|
$s_teiln_id = 0;
|
|
$update_teiln_stmt->bind_param("dii", $n_preis, $status_to_set, $s_teiln_id);
|
|
|
|
// 5. Loop through and execute updates
|
|
foreach ($programm_ids as $id_key => $preis) {
|
|
$s_teiln_id = (int) $id_key;
|
|
|
|
// FIX: Check directly against the flat ID key map
|
|
if (!isset($all_teilnehmede_indexed[$s_teiln_id])) continue;
|
|
|
|
$eff_preis = 0.0;
|
|
if (intval($new_status) === 2) {
|
|
$eff_preis = (float) $preis;
|
|
} elseif (intval($new_status) !== 2 && intval($old_status) === 2) {
|
|
$eff_preis = ((float) $preis) * -1;
|
|
}
|
|
|
|
// Calculate the new absolute value
|
|
$n_preis = max(((float) $all_teilnehmede_indexed[$s_teiln_id]) + $eff_preis, 0);
|
|
|
|
if ($normal_order_status === 1) {
|
|
$kat = $all_teilnehmede_indexed_kat[$s_teiln_id];
|
|
|
|
$preis_kat = (float) $all_preise[$kat] ?? PHP_INT_MAX;
|
|
|
|
if ($n_preis >= $preis_kat) {
|
|
$status_to_set = 4;
|
|
} elseif ($n_preis !== 0.00) {
|
|
$status_to_set = 2;
|
|
} else {
|
|
$status_to_set = 1;
|
|
}
|
|
} else {
|
|
$status_to_set = $normal_order_status;
|
|
}
|
|
|
|
// Execute using the bound references
|
|
$update_teiln_stmt->execute();
|
|
}
|
|
|
|
$update_teiln_stmt->close();
|
|
return true;
|
|
}
|
|
|
|
function update_konten_vereine($benutzte_konten = '', $old_status, $new_status, bool $ids_are_array = false): bool {
|
|
global $mysqli, $db_tabelle_vereine;
|
|
|
|
$int_old_status = (int) $old_status;
|
|
$int_new_status = (int) $new_status;
|
|
|
|
// 1. Quick exit: No status change
|
|
if ($int_old_status === $int_new_status) {
|
|
return false;
|
|
}
|
|
|
|
// 2. Validate status transitions (Fixed $old_status === 0 || $old_status === 1 bug)
|
|
$isValidTransition =
|
|
(in_array($int_old_status, [0, 1, 3]) && $int_new_status === 2) ||
|
|
(in_array($int_new_status, [0, 1, 3]) && $int_old_status === 2) ||
|
|
($int_old_status === 0 && $int_new_status === 1);
|
|
|
|
if (!$isValidTransition) {
|
|
return false;
|
|
}
|
|
|
|
if (!$ids_are_array) {
|
|
$benutzte_konten_array = json_decode($benutzte_konten, true) ?? [];
|
|
} else {
|
|
if (!is_array($benutzte_konten)) {
|
|
return false;
|
|
}
|
|
$benutzte_konten_array = $benutzte_konten;
|
|
}
|
|
|
|
if (empty($benutzte_konten_array)) {
|
|
return false;
|
|
}
|
|
|
|
$faktor = ($int_new_status === 3) ? 1 : -1;
|
|
|
|
$stmt = $mysqli->prepare("UPDATE `$db_tabelle_vereine` SET `konto` = `konto` + ? WHERE `verein` = ?");
|
|
|
|
$konto_change = 0.0;
|
|
$verein = '';
|
|
$stmt->bind_param("ds", $konto_change, $verein);
|
|
|
|
foreach ($benutzte_konten_array as $verein_key => $konto_change_absolute) {
|
|
$verein = (string) $verein_key;
|
|
$konto_change = (float) $konto_change_absolute * $faktor;
|
|
|
|
if ($konto_change == 0) {
|
|
continue;
|
|
}
|
|
|
|
$stmt->execute();
|
|
}
|
|
|
|
$stmt->close();
|
|
return true;
|
|
} |