222 lines
7.1 KiB
PHP
222 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class AtencionController extends BaseController
|
|
{
|
|
public function Atencion(Request $request)
|
|
{
|
|
$query = $request->only(['start', 'end', 'type', 'action']);
|
|
$input = array_merge(['action' => 'index', 'type' => 'api'], $query);
|
|
|
|
$startDate = $input['start'] ?? date('Y-01-01');
|
|
$endDate = $input['end'] ?? date('Y-12-31');
|
|
|
|
$baseParams = [
|
|
'start_date' => $startDate,
|
|
'end_date' => $endDate,
|
|
'type' => $input['type'] ?? 'api',
|
|
'action' => $input['action'] ?? 'index',
|
|
];
|
|
|
|
$baseUrl = 'https://apoyos.comalcalco.gob.mx/beneficiaries/stats-by-date-range';
|
|
$countsUrl = 'https://apoyos.comalcalco.gob.mx/beneficiaries/counts-by-date';
|
|
$dashboardUrl = 'https://apoyos.comalcalco.gob.mx/beneficiaries/dashboard/api';
|
|
$dashboardUrl2 = 'https://apoyos.comalcalco.gob.mx/beneficiaries/dashboard/api?type=servicio';
|
|
|
|
try {
|
|
$response = Http::get($baseUrl, $baseParams);
|
|
|
|
if (!$response->successful()) {
|
|
return response()->json(['error' => 'Error del servicio'], $response->status());
|
|
}
|
|
|
|
$mainData = $response->json();
|
|
$countsAc = [];
|
|
try {
|
|
$countsResp = Http::get($countsUrl, [
|
|
'start_date' => $startDate,
|
|
'end_date' => $endDate,
|
|
]);
|
|
|
|
if ($countsResp->successful()) {
|
|
$countsAc = $countsResp->json();
|
|
} else {
|
|
Log::error('Error al obtener los conteos de atención', ['status' => $countsResp->status()]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('Error al obtener los conteos de atención', ['exception' => $e->getMessage()]);
|
|
}
|
|
|
|
$dashboardData = [];
|
|
try {
|
|
$dashboardResp = Http::get($dashboardUrl);
|
|
|
|
if ($dashboardResp->successful()) {
|
|
$dashboardData = $dashboardResp->json();
|
|
} else {
|
|
Log::error('Error al obtener datos del dashboard', ['status' => $dashboardResp->status()]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('Error al obtener datos del dashboard', ['exception' => $e->getMessage()]);
|
|
}
|
|
|
|
$dashboardServicioData = [];
|
|
try {
|
|
$dashboardResp2 = Http::get($dashboardUrl2);
|
|
|
|
if ($dashboardResp2->successful()) {
|
|
$dashboardServicioData = $dashboardResp2->json();
|
|
} else {
|
|
Log::error('Error al obtener datos del dashboard servicio', ['status' => $dashboardResp2->status()]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
Log::error('Error al obtener datos del dashboard servicio', ['exception' => $e->getMessage()]);
|
|
}
|
|
|
|
// Combina todos los resultados
|
|
$combinedData = array_merge(
|
|
is_array($mainData) ? $mainData : [],
|
|
[
|
|
'counts' => $countsAc,
|
|
'dashboard' => $dashboardData,
|
|
'dashboard_servicio' => $dashboardServicioData,
|
|
]
|
|
);
|
|
return response()->json($combinedData, 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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|