diff --git a/gift-list.php b/gift-list.php index 7ec0637..704c6fa 100644 --- a/gift-list.php +++ b/gift-list.php @@ -8,15 +8,15 @@ require_once __DIR__ . '/includes/i18n.php'; if (session_status() === PHP_SESSION_NONE) { session_start(); } -// --- 1. CONFIGURATION & DONNÉES --- +// --- 1. CONFIGURATION --- $year = (int)date('Y'); $pageTitle = tr('gift_page_title'); $activePage = "gift-list"; $bodyClass = "pf-gift-list"; $pageCss = "/modules/gift-list/gift-list.css"; +$children = ['Pol', 'Pep', 'Elna', 'Bru', 'Guim']; $baseAdults = ['Laia', 'Laura', 'Avi Iaia']; -$children = ['Pol', 'Pep', 'Elna', 'Bru', 'Guim']; $extraAdults = ['Pauline', 'Papy JC', 'Mamy Caro']; $adultsByChildForAnniv = [ @@ -35,71 +35,46 @@ $VIEWS = [ $currentView = strtolower($_GET['view'] ?? ($_SESSION['gift_view'] ?? 'nadal')); if (!isset($VIEWS[$currentView])) $currentView = 'nadal'; $_SESSION['gift_view'] = $currentView; - $allowedOccasions = $VIEWS[$currentView]; $allOccasionLabels = [ - 'TIO' => tr('gift_occ_tio'), - 'NOEL' => tr('gift_occ_noel'), - 'ROIS' => tr('gift_occ_rois'), - 'ANNIV' => tr('gift_occ_anniv'), - 'SANT' => tr('gift_occ_sant'), + 'TIO' => tr('gift_occ_tio'), 'NOEL' => tr('gift_occ_noel'), 'ROIS' => tr('gift_occ_rois'), + 'ANNIV' => tr('gift_occ_anniv'), 'SANT' => tr('gift_occ_sant') ]; - $occasionIcons = [ - 'TIO' => '/modules/gift-list/assets/img/tio.png', - 'NOEL' => '/modules/gift-list/assets/img/santa.png', - 'ROIS' => '/modules/gift-list/assets/img/reis.png', - 'ANNIV' => '/modules/gift-list/assets/img/corona.png', - 'SANT' => '/modules/gift-list/assets/img/sant.png', + 'TIO' => '/modules/gift-list/assets/img/tio.png', 'NOEL' => '/modules/gift-list/assets/img/santa.png', + 'ROIS' => '/modules/gift-list/assets/img/reis.png', 'ANNIV' => '/modules/gift-list/assets/img/corona.png', + 'SANT' => '/modules/gift-list/assets/img/sant.png' ]; -// --- 2. RÉCUPÉRATION DES DONNÉES --- +// --- 2. DONNÉES --- $inMarks = implode(',', array_fill(0, count($allowedOccasions), '?')); -// Le tri SQL regroupe par Occasion, puis par Enfant, puis par Adulte -$sql = "SELECT * FROM pf_gifts WHERE year = ? AND occasion IN ($inMarks) ORDER BY occasion ASC, child_name ASC, adult_name ASC, created_at DESC"; +// Tri: Par Fête, puis Enfant, puis Adulte +$sql = "SELECT * FROM pf_gifts WHERE year = ? AND occasion IN ($inMarks) ORDER BY occasion ASC, child_name ASC, adult_name ASC"; $stmt = $pdo->prepare($sql); $stmt->execute(array_merge([$year], $allowedOccasions)); -$gifts = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: []; +$gifts = $stmt->fetchAll(PDO::FETCH_ASSOC); -$dataByOccasion = []; +$data = []; $adultsInView = []; foreach ($gifts as $g) { - $occ = $g['occasion']; - $child = $g['child_name']; - $adult = $g['adult_name']; - - // Groupement structuré : Occasion -> Enfant -> Cadeaux - $dataByOccasion[$occ][$child]['gifts'][] = $g; - - // Totaux pour les petites pills sous le nom de l'enfant - if (!isset($dataByOccasion[$occ][$child]['totals'][$adult])) { - $dataByOccasion[$occ][$child]['totals'][$adult] = 0; - } - $dataByOccasion[$occ][$child]['totals'][$adult] += (float)$g['amount']; - - $adultsInView[$adult] = true; + $data[$g['occasion']][$g['child_name']]['gifts'][] = $g; + $data[$g['occasion']][$g['child_name']]['totals'][$g['adult_name']] = ($data[$g['occasion']][$g['child_name']]['totals'][$g['adult_name']] ?? 0) + $g['amount']; + $adultsInView[$g['adult_name']] = true; } +$allAdultsList = array_keys($adultsInView); sort($allAdultsList); -$allAdultsList = array_keys($adultsInView); -sort($allAdultsList); -$occasionsToShow = array_values(array_intersect(array_keys($allOccasionLabels), $allowedOccasions)); - -// --- 3. CALCUL TRICOUNT (Bilan financier global de la vue) --- +// --- 3. TRICOUNT --- $people = array_values(array_unique(array_merge($baseAdults, array_column($gifts, 'adult_name'), array_column($gifts, 'payer_name')))); $people = array_filter($people); - $matrix = []; -foreach ($people as $p1) { - foreach ($people as $p2) $matrix[$p1][$p2] = 0.0; -} +foreach ($people as $p1) { foreach ($people as $p2) $matrix[$p1][$p2] = 0.0; } foreach ($gifts as $g) { $adult = $g['adult_name']; $payer = $g['payer_name'] ?? $g['adult_name']; $amt = (float)$g['amount']; - if ($amt > 0 && $adult && $payer && $adult !== $payer) { $matrix[$adult][$payer] += $amt; } @@ -109,8 +84,7 @@ $settlements = []; $countPeople = count($people); for ($i = 0; $i < $countPeople; $i++) { for ($j = $i + 1; $j < $countPeople; $j++) { - $a = $people[$i]; - $b = $people[$j]; + $a = $people[$i]; $b = $people[$j]; $net = $matrix[$a][$b] - $matrix[$b][$a]; if ($net > 0.01) { $settlements[] = ['from' => $a, 'to' => $b, 'amount' => $net]; } elseif ($net < -0.01) { $settlements[] = ['from' => $b, 'to' => $a, 'amount' => -$net]; } @@ -123,116 +97,105 @@ require __DIR__ . '/header.php';