@php $establishment = $document->establishment; if (!$establishment && method_exists($document, 'relationLoaded') && $document->relationLoaded('establishment')) { $establishment = $document->getRelation('establishment'); } $customer = $document->customer; if (!$customer && method_exists($document, 'relationLoaded') && $document->relationLoaded('patient')) { $customer = $document->getRelation('patient'); } $tittle = ($document->series ?? $document->prefix ?? '').'-'.str_pad($document->number ?? 0, 8, '0', STR_PAD_LEFT); $legendsList = $document->legends; if (is_null($legendsList)) { $legendsList = []; } elseif (is_object($legendsList) && !($legendsList instanceof \Illuminate\Support\Collection)) { $legendsList = [$legendsList]; } else { $legendsList = is_array($legendsList) ? $legendsList : collect($legendsList)->all(); } // Logo: ruta segura para evitar errores al imprimir $logo_ticket_path = null; $logo_ticket_base64 = null; $logo_ticket_mime = 'image/jpeg'; $establishment_id = $establishment && isset($establishment->id) ? $establishment->id : (isset($document->establishment_id) ? $document->establishment_id : null); $candidates = []; if ($establishment_id) { $candidates[] = public_path("logos_de_sistema/logosuc_{$establishment_id}.jpg"); $candidates[] = public_path("logos_de_sistema/avatar_logosuc_{$establishment_id}.jpg"); $candidates[] = public_path("logos_de_sistema/avatar_logosuc_{$establishment_id}.png"); } $candidates[] = public_path('logos_de_sistema/logo_sistema.jpg'); foreach ($candidates as $path) { if (is_string($path) && $path !== '' && file_exists($path) && is_readable($path) && @filesize($path) > 0) { $logo_ticket_path = $path; if (strtolower(pathinfo($path, PATHINFO_EXTENSION)) === 'png') { $logo_ticket_mime = 'image/png'; } $raw = @file_get_contents($path); if ($raw !== false && $raw !== '') { $logo_ticket_base64 = base64_encode($raw); } break; } } // N° Expediente (Historia clínica), no N° documento $number_history = null; try { $patient_id = $document->customer_id ?? ($customer && isset($customer->id) ? $customer->id : null); $specialty_id = $document->specialty_id ?? null; $est_id = $document->establishment_id ?? ($establishment && isset($establishment->id) ? $establishment->id : null); if ($patient_id) { $q = \App\Models\History::where('patient_id', $patient_id)->whereNotNull('number_history'); if ($specialty_id) $q->where('specialty_id', $specialty_id); if ($est_id) $q->where('establishment_id', $est_id); $history = $q->latest('id')->first(); if ($history && $history->number_history) $number_history = $history->number_history; } } catch (\Throwable $e) { $number_history = null; } $currencySymbol = optional($document->currency_type)->symbol ?? ''; $dateIssueStr = ''; if (!empty($document->date_of_issue)) { try { $di = $document->date_of_issue; $dateIssueStr = $di instanceof \Carbon\Carbon ? $di->format('d-m-Y') : \Carbon\Carbon::parse($di)->format('d-m-Y'); } catch (\Throwable $e) { $dateIssueStr = is_string($document->date_of_issue) ? $document->date_of_issue : ''; } } @endphp {{--{{ $tittle }}--}} {{-- Estilos en style.css vía Mpdf HEADER_CSS (no