diff --git a/src/bot/handler.js b/src/bot/handler.js index 4d0853b..2afe571 100644 --- a/src/bot/handler.js +++ b/src/bot/handler.js @@ -1495,6 +1495,9 @@ Digite /help para ver todos os comandos disponíveis! } else if (data.startsWith('faturas_cliente_')) { console.log(`🔍 Condição faturas_cliente_ atendida`); await this.handleFaturasClienteCallback(callbackQuery, data); + } else if (data.startsWith('faturas_servico_')) { + console.log(`🔍 Condição faturas_servico_ atendida`); + await this.handleFaturasServicoCallback(callbackQuery, data); } else if (data.startsWith('servicos_cliente_')) { console.log(`🔍 Condição servicos_cliente_ atendida`); await this.handleServicosClienteCallback(callbackQuery, data); @@ -4062,7 +4065,7 @@ Digite /help para ver todos os comandos disponíveis! /** * Ver faturas do cliente */ - async handleVerFaturas(chatId, targetOrId, location = '') { + async handleVerFaturas(chatId, targetOrId, location = '', idServicoFilter = null) { try { let idCliente = targetOrId; @@ -4098,7 +4101,7 @@ Digite /help para ver todos os comandos disponíveis! return; } - const faturasPorServico = resultado.faturasPorServico || []; + let faturasPorServico = resultado.faturasPorServico || []; if (faturasPorServico.length === 0) { await this.bot.sendMessage(chatId, @@ -4109,6 +4112,33 @@ Digite /help para ver todos os comandos disponíveis! return; } + // Se há mais de um serviço com faturas e nenhum filtro foi aplicado, + // perguntar qual serviço o usuário quer ver + if (faturasPorServico.length > 1 && !idServicoFilter) { + let message = `📄 **SELECIONE O SERVIÇO**\n\n`; + message += `🆔 **ID Cliente**: ${idCliente}\n`; + message += `📊 Este cliente possui **${faturasPorServico.length} serviços** com faturas.\n\n`; + message += `Escolha qual serviço deseja ver as faturas:`; + + const keyboard = faturasPorServico.map((servicoInfo) => [{ + text: `📋 ${servicoInfo.servico.nome || servicoInfo.servico.descricao || 'Serviço'} — ${servicoInfo.faturas.length} fatura(s)`, + callback_data: `faturas_servico_${idCliente}_${servicoInfo.servico.id}_${location}` + }]); + + await this.bot.sendMessage(chatId, message, { + parse_mode: 'Markdown', + reply_markup: { inline_keyboard: keyboard } + }); + return; + } + + // Filtrar pelo serviço selecionado, se houver + if (idServicoFilter) { + faturasPorServico = faturasPorServico.filter( + s => String(s.servico.id) === String(idServicoFilter) + ); + } + // Contar total de faturas let totalFaturas = 0; faturasPorServico.forEach(s => { @@ -4121,7 +4151,7 @@ Digite /help para ver todos os comandos disponíveis! faturasPorServico.forEach((servicoInfo, sIndex) => { if (servicoInfo.faturas.length > 0) { - message += `📋 **Serviço**: ${servicoInfo.servico.descricao || 'Serviço'}\n`; + message += `📋 **Serviço**: ${servicoInfo.servico.nome || servicoInfo.servico.descricao || 'Serviço'}\n`; message += `🆔 **ID**: ${servicoInfo.servico.id}\n\n`; servicoInfo.faturas.forEach((fatura, fIndex) => { @@ -4157,23 +4187,25 @@ Digite /help para ver todos os comandos disponíveis! }); // Criar botões para cada fatura com linha digitável/PIX + // Inclui a data de vencimento para melhor identificação const keyboard = []; faturasPorServico.forEach((servicoInfo) => { servicoInfo.faturas.forEach((fatura) => { if (fatura.status !== 'Pago') { + const vencLabel = fatura.vencimento ? ` (${fatura.vencimento})` : ''; const row = []; if (fatura.linhaDigitavel) { row.push({ - text: `📋 Linha Digitável #${fatura.id}`, + text: `📋 Linha Digitável #${fatura.id}${vencLabel}`, callback_data: `linha_${fatura.id}_${location}` }); } if (fatura.pixCopiaCola) { row.push({ - text: `💳 PIX #${fatura.id}`, + text: `💳 PIX #${fatura.id}${vencLabel}`, callback_data: `pix_${fatura.id}_${location}` }); } @@ -4511,6 +4543,20 @@ Digite /help para ver todos os comandos disponíveis! await this.handleVerFaturas(chatId, idCliente, location); } + /** + * Callback para selecionar serviço específico ao ver faturas + */ + async handleFaturasServicoCallback(callbackQuery, data) { + const chatId = callbackQuery.message.chat.id; + const parts = data.replace('faturas_servico_', '').split('_'); + const idCliente = parts[0]; + const idServico = parts[1]; + const location = parts.slice(2).join('_'); + + await this.bot.answerCallbackQuery(callbackQuery.id); + await this.handleVerFaturas(chatId, idCliente, location, idServico); + } + /** * Callback para serviços do cliente */ diff --git a/src/nlp/processor.js b/src/nlp/processor.js index 5a10585..a8c7ed0 100644 --- a/src/nlp/processor.js +++ b/src/nlp/processor.js @@ -189,10 +189,17 @@ class NLPProcessor { // Detectar comandos TopSapp - ver faturas if (/ver|mostrar/i.test(action) && /faturas?|boletos?|contas?/i.test(fullMatch)) { let query = match[3] || match[2]; // ID/CPF do cliente + if (query) { + query = query.trim(); + // Converter CPF sem formatação (11 dígitos) para formato XXX.XXX.XXX-XX + if (/^\d{11}$/.test(query)) { + query = `${query.slice(0,3)}.${query.slice(3,6)}.${query.slice(6,9)}-${query.slice(9,11)}`; + } + } return { action: 'ver_faturas', - target: query ? query.trim() : null, + target: query || null, confidence: 0.95, type: 'topsapp_faturas_pattern' };