From e3d9aac26d0330efe7b922f371fd14fa971174ed Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Tue, 28 Jul 2026 21:59:16 -0500 Subject: [PATCH] feat(bot): add last_7 date_mode y next_endpoint_key para texto+PDF en cadena MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - last_7: fecha_desde = hoy-7 días, fecha_hasta = hoy (para ausentismos semana) - next_endpoint_key en executeApiReport: si la primera llamada retorna JSON texto con status=1, envía el texto al usuario y luego ejecuta el endpoint siguiente (ej: produccion_total → texto inmediato + PDF a continuación) Co-Authored-By: Claude Sonnet 4.6 --- services/NormalBot.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/services/NormalBot.php b/services/NormalBot.php index 6decfc6..67f8e39 100644 --- a/services/NormalBot.php +++ b/services/NormalBot.php @@ -1227,6 +1227,9 @@ class NormalBot } elseif ($dateMode === 'last_month') { $extraQuery['fecha_desde'] = date('Y-m-01', strtotime('first day of last month')); $extraQuery['fecha_hasta'] = date('Y-m-t', strtotime('last day of last month')); + } elseif ($dateMode === 'last_7') { + $extraQuery['fecha_desde'] = date('Y-m-d', strtotime('-7 days')); + $extraQuery['fecha_hasta'] = date('Y-m-d'); } $extraQuery['telefono'] = $context['from']; $extraQuery['nombre'] = $context['name'] ?? ''; @@ -1268,10 +1271,22 @@ class NormalBot return self::sendText('Error al obtener el reporte. Intenta de nuevo.' . $nav, $context['from'], $company); } - // Si el ERP devuelve JSON (ej: sin_datos), informar al usuario en texto + // Si el ERP devuelve JSON (ej: sin_datos o texto), informar al usuario en texto if (str_starts_with($contentType ?? '', 'application/json')) { $json = json_decode($content, true); $msg = $json['mensaje'] ?? ($json['message'] ?? 'No hay datos disponibles para el período solicitado.'); + $nextKey = $params['next_endpoint_key'] ?? ''; + if ($nextKey !== '' && ($json['status'] ?? '0') === '1') { + // Enviar texto primero, luego solicitar el reporte complementario (PDF/Excel) + self::sendText($msg, $context['from'], $company); + $nextParams = array_merge($params, [ + 'endpoint_key' => $nextKey, + 'caption' => $params['next_caption'] ?? '', + 'filename' => $params['next_filename'] ?? 'reporte.pdf', + ]); + unset($nextParams['next_endpoint_key'], $nextParams['next_caption'], $nextParams['next_filename']); + return self::executeApiReport($nextParams, $context, $company, $ctxId); + } ConversationContext::reset($ctxId); return self::sendText('ℹ️ ' . $msg . $nav, $context['from'], $company); }