From 00655694caaf6b01977b7e0bc53ac611ffd87602 Mon Sep 17 00:00:00 2001 From: Sidney Date: Fri, 8 May 2026 10:11:15 -0300 Subject: [PATCH] =?UTF-8?q?refactor:=20automa=C3=A7=C3=A3o=20de=20cobran?= =?UTF-8?q?=C3=A7as=20agora=20utiliza=20JSON=20(school=5Fdata)=20como=20fo?= =?UTF-8?q?nte=20prim=C3=A1ria=20para=20garantir=20sucesso?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manager/server.selfhosted.js | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/manager/server.selfhosted.js b/manager/server.selfhosted.js index 365742c..999b668 100644 --- a/manager/server.selfhosted.js +++ b/manager/server.selfhosted.js @@ -1154,22 +1154,23 @@ async function executarRotinaCobrancas(tipo = 'ambos') { const rules = appData?.messageTemplates?.automationRules || {}; const sendDaysBefore = parseInt(rules.sendDaysBefore) || 3; const maxPreWarnings = parseInt(rules.maxPreWarnings) || 1; - const sendDaysAfter = parseInt(rules.sendDaysAfter) || 1; const repeatEveryDays = parseInt(rules.repeatEveryDays) || 3; - let enviadasAtraso = 0; - let enviadasAviso = 0; + const allPayments = appData.payments || []; + const hoje = getLocalSafeDate(new Date()); // 1. Processar Atrasados if (tipo === 'atrasado' || tipo === 'ambos') { - const atrasados = await getCobrancasAtrasadas(); - const hoje = new Date(); - hoje.setHours(0,0,0,0); + const atrasados = allPayments.filter(p => (p.status || '').toUpperCase() === 'ATRASADO'); - for (const cob of atrasados) { - if (!cob.asaas_payment_id || !cob.vencimento) continue; + for (const pJSON of atrasados) { + if (!pJSON.asaasPaymentId || !pJSON.dueDate) continue; + + // Pegamos os contadores do SQL para este boleto específico + const cob = await getCobrancaByPaymentId(pJSON.asaasPaymentId); + if (!cob) continue; // Sincronização cuidará de criar no SQL depois - const vencimento = getLocalSafeDate(cob.vencimento); + const vencimento = getLocalSafeDate(pJSON.dueDate); if (!vencimento) continue; const diffDiasAtraso = Math.floor((hoje.getTime() - vencimento.getTime()) / (1000 * 60 * 60 * 24)); @@ -1202,14 +1203,16 @@ async function executarRotinaCobrancas(tipo = 'ambos') { // 2. Processar A Vencer (Lembretes Preventivos) if (tipo === 'preventivo' || tipo === 'ambos') { - const pendentes = await getCobrancasPendentes(); - const hoje = new Date(); - hoje.setHours(0,0,0,0); + const pendentes = allPayments.filter(p => ['PENDENTE', 'PENDING'].includes((p.status || '').toUpperCase())); - for (const cob of pendentes) { - if (!cob.asaas_payment_id || !cob.vencimento) continue; + for (const pJSON of pendentes) { + if (!pJSON.asaasPaymentId || !pJSON.dueDate) continue; - const vencimento = getLocalSafeDate(cob.vencimento); + // Pegamos os contadores do SQL para este boleto específico + const cob = await getCobrancaByPaymentId(pJSON.asaasPaymentId); + if (!cob) continue; + + const vencimento = getLocalSafeDate(pJSON.dueDate); if (!vencimento) continue; const diffDias = Math.ceil((vencimento.getTime() - hoje.getTime()) / (1000 * 60 * 60 * 24));