diff --git a/app/Http/Controllers/Dashboard/AtencionController.php b/app/Http/Controllers/Dashboard/AtencionController.php index 4c38771..7e08bc6 100644 --- a/app/Http/Controllers/Dashboard/AtencionController.php +++ b/app/Http/Controllers/Dashboard/AtencionController.php @@ -143,76 +143,39 @@ private function getFullData($startDate, $endDate, $period) } - 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 + // Obtener parámetros necesarios para la API $params = $request->only([ 'start_date', 'end_date', - 'type_id', - 'support_id' + 'department' ]); // Validaciones básicas - if (!$params['start_date'] || !$params['end_date']) { + if (!isset($params['start_date']) || !isset($params['end_date'])) { return response()->json([ 'error' => 'Las fechas son requeridas' ], 400); } - if (!$params['type_id']) { + if (!isset($params['department']) || $params['department'] === '') { return response()->json([ - 'error' => 'El type_id es requerido' + 'error' => 'El departamento es requerido' ], 400); } - $url = 'https://apoyos.comalcalco.gob.mx/beneficiaries/export/excel'; + // Validar que department sea 1 o 3 + if (!in_array((int)$params['department'], [1, 3])) { + return response()->json([ + 'error' => 'El departamento debe ser Atención Ciudadana o DIF' + ], 400); + } - // Log de parámetros para debug - Log::info('Exportando Excel con parámetros:', $params); + $url = 'https://apoyos.comalcalco.gob.mx/api/beneficiaries/export-excel'; - // IMPORTANTE: Hacer la petición sin timeout muy alto y con withoutVerifying para SSL + // Hacer la petición a la API externa $response = Http::withoutVerifying() ->timeout(60) ->get($url, $params); @@ -229,24 +192,30 @@ public function exportExcel(Request $request) ], $response->status()); } - // CORREGIR: Verificar que la respuesta sea realmente un Excel + // Verificar que la respuesta sea realmente un Excel $contentType = $response->header('Content-Type'); - if (!str_contains($contentType, 'spreadsheet') && !str_contains($contentType, 'excel')) { + if (!str_contains($contentType, 'spreadsheet') && !str_contains($contentType, 'excel') && !str_contains($contentType, 'octet-stream')) { Log::error('Respuesta no es un archivo Excel', [ + 'url' => $url, + 'params' => $params, + 'status' => $response->status(), 'content_type' => $contentType, - 'body_preview' => substr($response->body(), 0, 200) + 'body_preview' => substr($response->body(), 0, 500) ]); return response()->json([ - 'error' => 'El servidor no devolvió un archivo Excel válido' + 'error' => 'La API externa no devolvió un archivo Excel válido. URL: ' . $url . ' | Status: ' . $response->status() ], 400); } + // Nombre del departamento para el archivo + $departmentName = $params['department'] == 1 ? 'AtencionCiudadana' : 'DIF'; + // 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_' . + 'Content-Disposition' => 'attachment; filename="beneficiarios_' . $departmentName . '_' . $params['start_date'] . '_' . $params['end_date'] . '.xlsx"', 'Content-Length' => strlen($response->body()), 'Cache-Control' => 'no-cache, no-store, must-revalidate', diff --git a/resources/js/Components/Dashboard/Modal/ExportModal.vue b/resources/js/Components/Dashboard/Modal/ExportModal.vue index 732991e..28149bf 100644 --- a/resources/js/Components/Dashboard/Modal/ExportModal.vue +++ b/resources/js/Components/Dashboard/Modal/ExportModal.vue @@ -1,5 +1,5 @@ @@ -299,65 +179,21 @@ const closeModal = () => { /> - +
- Seleccionados: {{ form.supportOptions.length }} de {{ supportOptionsData.length }} + Se exportarán todos los registros del departamento seleccionado