@php $fmt = function($v) { return ($v != 0) ? \App\Helpers\TimeHelper::decimalToHours($v) : ''; }; // 0. RÉCUPÉRATION DES JOURS FÉRIÉS DU MOIS POUR LA FILIÈRE $joursFeries = $filiere->joursFeries() ->whereBetween('date_ferie', [ $activeMois->copy()->startOfMonth(), $activeMois->copy()->endOfMonth() ]) ->pluck('date_ferie') ->map(fn($d) => $d->format('Y-m-d')) ->toArray(); // 0.B. CALCUL DU NOMBRE DE COLONNES DYNAMIQUES $countRecupCols = $filiere->activer_dimanche_ferie ? 5 : 6; if (!$filiere->dimanche_ferie_recuperable) { $countRecupCols -= ($filiere->activer_dimanche_ferie ? 1 : 2); } $countPayeCols = $filiere->activer_dimanche_ferie ? 4 : 5; if ($filiere->appliquer_seuil_paie) { $countPayeCols += 1; } $detailCols = ($countRecupCols - 1) + ($countPayeCols - 1); if ($detailCols >= 11) { $divisor = 60; } elseif ($detailCols == 10) { $divisor = 65; } elseif ($detailCols == 9) { $divisor = 70; } elseif ($detailCols == 8) { $divisor = 80; } else { $divisor = 90; } // 1. CALCUL DES POIDS LOGIQUES DES LIGNES $hsWithWeights = $lines->map(function($l) use ($divisor) { $l->logical_weight = floor(strlen($l->travail_effectue) / $divisor) + floor(strlen($l->motif->libelle_motif ?? '') / 16) + 1; return $l; }); // PADDING A 20 LIGNES MINIMUM $totalNbLignes = $hsWithWeights->sum('logical_weight'); while ($totalNbLignes < 20) { $emptyLine = new \stdClass(); $emptyLine->is_empty = true; $emptyLine->logical_weight = 1; $hsWithWeights->push($emptyLine); $totalNbLignes++; } // Recalcul après padding $totalNbLignes = $hsWithWeights->sum('logical_weight'); // 2. DÉTERMINATION DES PAGES $pages = []; if ($totalNbLignes < 24) { $pages[] = [ 'lines' => $hsWithWeights, 'show_header' => true, 'show_footer' => true, 'show_astreintes' => true, 'break_after_recup' => false ]; } elseif ($totalNbLignes <= 30) { $pages[] = [ 'lines' => $hsWithWeights, 'show_header' => true, 'show_footer' => true, 'show_astreintes' => false, 'break_after_recup' => true ]; $pages[] = [ 'lines' => collect(), 'show_header' => true, 'show_footer' => true, 'show_astreintes' => true, 'break_after_recup' => false ]; } else { $tempLines = $hsWithWeights->all(); while (count($tempLines) > 0) { $currentChunk = collect(); $currentWeight = 0; while (count($tempLines) > 0) { $next = $tempLines[0]; if ($currentWeight + $next->logical_weight <= 30 || $currentChunk->isEmpty()) { $currentChunk->push(array_shift($tempLines)); $currentWeight += $next->logical_weight; } else { break; } } $pages[] = [ 'lines' => $currentChunk, 'show_header' => true, 'show_footer' => false, 'show_astreintes' => false, 'break_after_recup' => false ]; } $lastPageIndex = count($pages) - 1; $reste = $pages[$lastPageIndex]['lines']->sum('logical_weight'); if ($reste <= 30) { $pages[$lastPageIndex]['show_footer'] = true; $pages[$lastPageIndex]['show_astreintes'] = true; } else { $pages[] = [ 'lines' => collect(), 'show_header' => true, 'show_footer' => true, 'show_astreintes' => true, 'break_after_recup' => false ]; } } $srv = $refService; $nStartNorm = \Carbon\Carbon::parse($srv->heure_nuit_fin)->format('H\h'); $nEndNorm = \Carbon\Carbon::parse($srv->heure_nuit_debut)->format('H\h'); $mentionNormales = "(heures normales de $nStartNorm à $nEndNorm)"; $nStartNuit = \Carbon\Carbon::parse($srv->heure_nuit_debut)->format('H\h'); $nEndNuit = \Carbon\Carbon::parse($srv->heure_nuit_fin)->format('H\h'); $mentionNuitLabel = "Heures de nuit $nStartNuit-$nEndNuit"; $isStandard = $filiere->activer_dimanche_ferie; $cellStyle = "width: " . ($isStandard ? '40px' : '30px') . "; font-size: " . ($isStandard ? '6pt' : '5pt') . "; line-height: 1.1;"; $bodyStyle = "width: " . ($isStandard ? '40px' : '30px') . ";"; @endphp @foreach($pages as $pageIndex => $p) @endforeach