only(['start', 'end', 'type', 'action', 'period', 'charts_only']); $input = array_merge(['action' => 'index', 'type' => 'api'], $query); $startDate = $input['start'] ?? date('Y-01-01'); $endDate = $input['end'] ?? date('Y-12-31'); $period = $input['period'] ?? null; $chartsOnly = $input['charts_only'] ?? false; // Cache key para diferentes tipos de peticiones $cacheKey = 'atencion_' . md5(serialize([ 'start' => $startDate, 'end' => $endDate, 'period' => $period, 'charts_only' => $chartsOnly ])); // Intentar obtener desde cache (2 minutos) if (Cache::has($cacheKey)) { return response()->json(Cache::get($cacheKey), 200) ->header('Content-Type', 'application/json'); } try { // Si solo necesitamos gráficas, hacer menos peticiones if ($chartsOnly) { $result = $this->getChartsData($startDate, $endDate); } else { $result = $this->getFullData($startDate, $endDate, $period); } // Cachear resultado por 2 minutos Cache::put($cacheKey, $result, 120); return response()->json($result, 200) ->header('Content-Type', 'application/json'); } catch (\Exception $e) { Log::error('Error en la consulta de atención', ['exception' => $e->getMessage()]); return response()->json(['error' => 'Error en la consulta de atención'], 500); } } private function getChartsData($startDate, $endDate) { $baseParams = [ 'start_date' => $startDate, 'end_date' => $endDate, 'type' => 'api', 'action' => 'index', ]; $baseUrl = 'https://apoyos.comalcalco.gob.mx/beneficiaries/stats-by-date-range'; // Solo hacer la petición principal para gráficas $response = Http::timeout(15)->get($baseUrl, $baseParams); if (!$response->successful()) { throw new \Exception('Error del servicio principal'); } return $response->json(); } private function getFullData($startDate, $endDate, $period) { $baseParams = [ 'start_date' => $startDate, 'end_date' => $endDate, 'type' => 'api', 'action' => 'index', ]; $urls = [ 'main' => 'https://apoyos.comalcalco.gob.mx/beneficiaries/stats-by-date-range', 'counts' => 'https://apoyos.comalcalco.gob.mx/beneficiaries/counts-by-date', 'dashboard' => 'https://apoyos.comalcalco.gob.mx/beneficiaries/dashboard/api', 'dashboard_servicio' => 'https://apoyos.comalcalco.gob.mx/beneficiaries/dashboard/api?type=servicio' ]; // Hacer peticiones en paralelo usando HTTP Pool $responses = Http::pool(fn($pool) => [ $pool->timeout(15)->get($urls['main'], $baseParams), $pool->timeout(15)->get($urls['counts'], [ 'start_date' => $startDate, 'end_date' => $endDate, ]), $pool->timeout(15)->get($urls['dashboard']), $pool->timeout(15)->get($urls['dashboard_servicio']) ]); $mainData = []; $countsAc = []; $dashboardData = []; $dashboardServicioData = []; // Procesar respuestas if ($responses[0]->successful()) { $mainData = $responses[0]->json(); } else { Log::error('Error en petición principal', ['status' => $responses[0]->status()]); } if ($responses[1]->successful()) { $countsAc = $responses[1]->json(); } else { Log::error('Error en petición counts', ['status' => $responses[1]->status()]); } if ($responses[2]->successful()) { $dashboardData = $responses[2]->json(); } else { Log::error('Error en petición dashboard', ['status' => $responses[2]->status()]); } if ($responses[3]->successful()) { $dashboardServicioData = $responses[3]->json(); } else { Log::error('Error en petición dashboard servicio', ['status' => $responses[3]->status()]); } // Combinar todos los resultados return array_merge( is_array($mainData) ? $mainData : [], [ 'counts' => $countsAc, 'dashboard' => $dashboardData, 'dashboard_servicio' => $dashboardServicioData, ] ); } public function getSupportOptions(Request $request) { try { $typeId = $request->query('type_id'); if (!$typeId) { return response()->json(['error' => 'type_id es requerido'], 400); } $url = 'https://apoyos.comalcalco.gob.mx/beneficiaries/export/support-options'; $response = Http::timeout(30)->get($url, [ 'type_id' => $typeId ]); if (!$response->successful()) { Log::error('Error al obtener opciones de apoyo', [ 'status' => $response->status(), 'body' => $response->body(), 'type_id' => $typeId ]); return response()->json([ 'error' => 'Error al obtener opciones de apoyo' ], $response->status()); } return response()->json($response->json()); } catch (\Exception $e) { Log::error('Error en getSupportOptions', [ 'exception' => $e->getMessage(), 'type_id' => $request->query('type_id') ]); return response()->json([ 'error' => 'Error interno del servidor' ], 500); } } public function exportExcel(Request $request) { try { // CAMBIO: Usar los parámetros correctos que espera la API $params = $request->only([ 'start_date', 'end_date', 'type_id', 'support_id' ]); // Validaciones básicas if (!$params['start_date'] || !$params['end_date']) { return response()->json([ 'error' => 'Las fechas son requeridas' ], 400); } if (!$params['type_id']) { return response()->json([ 'error' => 'El type_id es requerido' ], 400); } $url = 'https://apoyos.comalcalco.gob.mx/beneficiaries/export/excel'; // Log de parámetros para debug Log::info('Exportando Excel con parámetros:', $params); // IMPORTANTE: Hacer la petición sin timeout muy alto y con withoutVerifying para SSL $response = Http::withoutVerifying() ->timeout(60) ->get($url, $params); if (!$response->successful()) { Log::error('Error al exportar Excel', [ 'status' => $response->status(), 'body' => $response->body(), 'params' => $params ]); return response()->json([ 'error' => 'Error al generar el archivo Excel: ' . $response->body() ], $response->status()); } // CORREGIR: Verificar que la respuesta sea realmente un Excel $contentType = $response->header('Content-Type'); if (!str_contains($contentType, 'spreadsheet') && !str_contains($contentType, 'excel')) { Log::error('Respuesta no es un archivo Excel', [ 'content_type' => $contentType, 'body_preview' => substr($response->body(), 0, 200) ]); return response()->json([ 'error' => 'El servidor no devolvió un archivo Excel válido' ], 400); } // Retornar el archivo Excel directamente con headers correctos return response($response->body(), 200, [ 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Content-Disposition' => 'attachment; filename="beneficiarios_' . $params['start_date'] . '_' . $params['end_date'] . '.xlsx"', 'Content-Length' => strlen($response->body()), 'Cache-Control' => 'no-cache, no-store, must-revalidate', 'Pragma' => 'no-cache', 'Expires' => '0' ]); } catch (\Exception $e) { Log::error('Error en exportExcel', [ 'exception' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'params' => $request->all() ]); return response()->json([ 'error' => 'Error interno del servidor: ' . $e->getMessage() ], 500); } } }