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); }