diff --git a/scripts/apply-menus.php b/scripts/apply-menus.php index 7cef5c8..45c1688 100644 --- a/scripts/apply-menus.php +++ b/scripts/apply-menus.php @@ -88,13 +88,13 @@ $menuConfig = [ 'descargar_informes' => ['type' => 'menu', 'menu' => 'submenu_informes'], 'menu_mixto' => ['type' => 'menu', 'menu' => 'menu_mixto'], 'submenu_ciclos' => ['type' => 'menu', 'menu' => 'submenu_ciclos'], - 'submenu_produccion' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=produccion', 'filename' => 'reporte_produccion.xlsx', 'caption' => 'Reporte de producción']], - 'submenu_mantenimiento_fecha' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=mantenimiento_fecha', 'filename' => 'reporte_mantenimiento.xlsx', 'caption' => 'Ciclos de mantenimiento a la fecha']], - 'submenu_ciclos_cosecha' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=ciclos_cosecha', 'filename' => 'reporte_ciclos_cosecha.xlsx', 'caption' => 'Ciclos de cosecha']], - 'submenu_ciclos_sanidad' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=ciclos_sanidad', 'filename' => 'reporte_ciclos_sanidad.xlsx', 'caption' => 'Ciclos de sanidad']], + 'submenu_produccion' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'produccion_dn', 'filename' => 'reporte_produccion.xlsx', 'caption' => 'Reporte de producción']], + 'submenu_mantenimiento_fecha' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'mantenimiento_dn', 'filename' => 'reporte_mantenimiento.xlsx', 'caption' => 'Ciclos de mantenimiento a la fecha']], + 'submenu_ciclos_cosecha' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'cosecha_dn', 'filename' => 'reporte_ciclos_cosecha.xlsx', 'caption' => 'Ciclos de cosecha']], + 'submenu_ciclos_sanidad' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'sanidad_dn', 'filename' => 'reporte_ciclos_sanidad.xlsx', 'caption' => 'Ciclos de sanidad']], 'submenu_ciclo_cosecha' => ['type' => 'menu', 'menu' => 'submenu_ciclo_cosecha'], - 'ciclo_cosecha_dia' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=ciclos_cosecha_dia', 'filename' => 'ciclo_cosecha_hoy.xlsx', 'caption' => 'Ciclo de cosecha del día de hoy']], - 'ciclo_cosecha_historico' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=ciclos_cosecha_historico', 'filename' => 'ciclo_cosecha_30dias.xlsx', 'caption' => 'Histórico ciclos de cosecha últimos 30 días']], + 'ciclo_cosecha_dia' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'cosecha_dn', 'filename' => 'ciclo_cosecha_hoy.xlsx', 'caption' => 'Ciclo de cosecha del día de hoy', 'date_mode' => 'today']], + 'ciclo_cosecha_historico' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'cosecha_dn', 'filename' => 'ciclo_cosecha_30dias.xlsx', 'caption' => 'Histórico ciclos de cosecha últimos 30 días', 'date_mode' => 'last_30']], ], ]; diff --git a/services/NormalBot.php b/services/NormalBot.php index 098e7ee..cfd62ac 100644 --- a/services/NormalBot.php +++ b/services/NormalBot.php @@ -100,32 +100,54 @@ class NormalBot private static function executeApiReport(array $params, array $context, array $company, int $ctxId): ?array { - $endpoint = $params['endpoint'] ?? ''; - if ($endpoint === '') { + $endpointKey = $params['endpoint_key'] ?? ''; + if ($endpointKey === '') { return self::sendText('Error: endpoint no configurado.', $context['from'], $company); } - $baseUrl = rtrim($company['api_base_url'] ?? '', '/'); - $url = $baseUrl . '/' . ltrim($endpoint, '/'); - $apiKey = $company['api_key'] ?? ''; + $stmt = db()->prepare("SELECT url, method FROM company_endpoints WHERE company_id = ? AND endpoint_key = ? AND is_active = 1 LIMIT 1"); + $stmt->execute([(int)$company['id'], $endpointKey]); + $ep = $stmt->fetch(); - $query = $params['query'] ?? []; - if (!empty($query)) { - $url .= '?' . http_build_query($query); + if (!$ep || empty($ep['url'])) { + self::log("API report: endpoint_key '{$endpointKey}' no configurado para company {$company['id']}"); + return self::sendText('El informe solicitado no está configurado. Contacta al administrador.', $context['from'], $company); } - $url .= (str_contains($url, '?') ? '&' : '?') . 'telefono=' . urlencode($context['from']) . '&nombre=' . urlencode($context['name'] ?? ''); + $url = $ep['url']; + $method = strtoupper($ep['method'] ?? 'GET'); + $apiKey = $company['api_key'] ?? ''; + + $extraQuery = $params['query'] ?? []; + $dateMode = $params['date_mode'] ?? ''; + if ($dateMode === 'today') { + $extraQuery['fecha'] = date('Y-m-d'); + } elseif ($dateMode === 'last_30') { + $extraQuery['fecha_inicio'] = date('Y-m-d', strtotime('-30 days')); + $extraQuery['fecha_fin'] = date('Y-m-d'); + } + $extraQuery['telefono'] = $context['from']; + $extraQuery['nombre'] = $context['name'] ?? ''; + + $glue = str_contains($url, '?') ? '&' : '?'; + $url .= $glue . http_build_query($extraQuery); $ch = curl_init($url); - curl_setopt_array($ch, [ + $curlOpts = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 60, CURLOPT_HTTPHEADER => [ + 'Authorization: Bearer ' . $apiKey, 'X-API-Key: ' . $apiKey, 'User-Agent: bot-palmas360/1.0', ], - ]); + ]; + if ($method === 'POST') { + $curlOpts[CURLOPT_POST] = true; + $curlOpts[CURLOPT_POSTFIELDS] = '{}'; + } + curl_setopt_array($ch, $curlOpts); $content = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);