['TIO', 'NOEL', 'ROIS'], 'anniversary' => ['ANNIV', 'SANT'], ]; // Vue courante $currentView = strtolower($_GET['view'] ?? ($_SESSION['gift_view'] ?? 'nadal')); if (!isset($VIEWS[$currentView])) $currentView = 'nadal'; $_SESSION['gift_view'] = $currentView; $allowedOccasions = $VIEWS[$currentView]; // Logique spécifique : Adultes supplémentaires pour Anniversaires $extraAdults = ['Pauline', 'Papy JC', 'Mamy Caro']; $adultsByChildForAnniv = [ 'Pol' => array_merge($baseAdults, $extraAdults), 'Pep' => array_merge($baseAdults, $extraAdults), 'Elna' => $baseAdults, 'Bru' => $baseAdults, 'Guim' => $baseAdults, ]; // Labels & Icônes $allOccasionLabels = [ 'TIO' => 'Tió', 'NOEL' => 'Nadal', 'ROIS' => 'Reis', 'ANNIV' => 'Anniversary', 'SANT' => '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', ]; $tableGifts = 'pf_gifts'; // --- 2. RÉCUPÉRATION DES DONNÉES --- // Préparation requête $inMarks = implode(',', array_fill(0, count($allowedOccasions), '?')); $sql = "SELECT * FROM {$tableGifts} WHERE year = ? AND occasion IN ($inMarks) ORDER BY adult_name, child_name, occasion, created_at"; $stmt = $pdo->prepare($sql); $params = array_merge([$year], $allowedOccasions); $stmt->execute($params); $gifts = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: []; // Indexation [occasion][child][adult] pour la matrice $byOccasion = []; foreach ($gifts as $gift) { $byOccasion[$gift['occasion']][$gift['child_name']][$gift['adult_name']][] = $gift; } // Occasions à afficher $occasionsToShow = array_values(array_intersect(array_keys($allOccasionLabels), $allowedOccasions)); // --- 3. CALCUL TRICOUNT (Backend) --- // On pré-calcule ici pour alléger la vue HTML $people = $baseAdults; $adultsInDb = array_column($gifts, 'adult_name'); $payersInDb = array_column($gifts, 'payer_name'); // Peut contenir des NULL si colonne vide, mais array_unique gère // Nettoyage et fusion des participants $people = array_values(array_unique(array_merge($people, $adultsInDb, $payersInDb))); $people = array_filter($people); // Enlève les vides éventuels // Matrice de dettes $matrix = []; 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']; // Si payé par quelqu'un d'autre que le bénéficiaire (l'adulte responsable) if ($amt > 0 && $adult && $payer && $adult !== $payer) { if (isset($matrix[$adult][$payer])) { $matrix[$adult][$payer] += $amt; } } } // Résolution des dettes (Liquidations) $settlements = []; $countPeople = count($people); for ($i = 0; $i < $countPeople; $i++) { for ($j = $i + 1; $j < $countPeople; $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]; } } } // --- 4. DÉBUT DU RENDU HTML --- require __DIR__ . '/header.php'; ?>
No hi ha cap regal registrat per a = htmlspecialchars($year) ?> en aquesta vista.
|
= htmlspecialchars($adultName) ?>
= number_format($totals[$adultName], 0, ',', ' ') ?> €
|
|---|
| — |
|
(pagat per = htmlspecialchars($payer) ?>)
—
|
| Adult | Infant | Festa | Total |
|---|---|---|---|
| = htmlspecialchars($row['adult_name']) ?> | = htmlspecialchars($row['child_name']) ?> | = htmlspecialchars($allOccasionLabels[$row['occasion']] ?? $row['occasion']) ?> | = number_format($row['total'], 0, ',', ' ') ?> € |
| Deutor ↓Creditor → | = htmlspecialchars($p) ?> |
|---|---|
| = htmlspecialchars($debtor) ?> | 0 ? 'cl-mtx-owe' : 'cl-mtx-empty'); $display = $isDiag || $val == 0 ? '—' : number_format($val, 0, ',', ' ') . ' €'; ?>= $display ?> |
Cap deute pendent.
| Adult | Infant | Festa | Regal | € | Enllaç |
|---|---|---|---|---|---|
| = htmlspecialchars($g['adult_name']) ?> | = htmlspecialchars($g['child_name']) ?> | = htmlspecialchars($allOccasionLabels[$g['occasion']] ?? $g['occasion']) ?> | = htmlspecialchars($g['gift_description']) ?> | = number_format($g['amount'], 0, ',', ' ') ?> | 🔗 |