/** * Ejecuta los 3 nuevos buckets de drill-down (ncs_emitidas, ncs_recibidas, * no_deducibles_efectivo) directamente contra una BD tenant para verificar * que cada uno produce resultados distintos. Sirve para descartar hipótesis * de bug en frontend / cache / dev server stale. */ import { prisma, tenantDb } from '../src/config/database.js'; async function main() { const rfc = process.argv[2] || 'DESPACHO_MO7JE8BZ_VDOPR'; const fi = process.argv[3] || '2025-08-01'; const ff = process.argv[4] || '2025-08-31'; const t = await prisma.tenant.findFirst({ where: { rfc } }); if (!t) { console.log('Tenant', rfc, 'no encontrado'); return; } const pool = await tenantDb.getPool(t.id, t.databaseName); console.log(`Tenant: ${rfc} — Período: ${fi} → ${ff}\n`); const buckets = [ { name: 'ncs_emitidas', sql: ` SELECT COUNT(*)::int as n, COALESCE(SUM(total_mxn),0)::numeric(14,2) as total FROM cfdis WHERE type = 'EMITIDO' AND tipo_comprobante = 'E' AND metodo_pago = 'PUE' AND status NOT IN ('Cancelado','0') AND fecha_emision >= $1::date AND fecha_emision < ($2::date + interval '1 day') AND regimen_fiscal_emisor IS NOT NULL `, }, { name: 'ncs_recibidas', sql: ` SELECT COUNT(*)::int as n, COALESCE(SUM(total_mxn),0)::numeric(14,2) as total FROM cfdis WHERE type = 'RECIBIDO' AND tipo_comprobante = 'E' AND metodo_pago = 'PUE' AND status NOT IN ('Cancelado','0') AND fecha_emision >= $1::date AND fecha_emision < ($2::date + interval '1 day') AND regimen_fiscal_receptor IS NOT NULL `, }, { name: 'no_deducibles_efectivo', sql: ` SELECT COUNT(*)::int as n, COALESCE(SUM(total_mxn),0)::numeric(14,2) as total FROM cfdis WHERE type = 'RECIBIDO' AND forma_pago = '01' AND ( (tipo_comprobante = 'I' AND metodo_pago = 'PUE' AND COALESCE(total_mxn, 0) > 2000) OR (tipo_comprobante = 'P' AND COALESCE(monto_pago_mxn, 0) > 2000) ) AND status NOT IN ('Cancelado','0') AND fecha_emision >= $1::date AND fecha_emision < ($2::date + interval '1 day') AND regimen_fiscal_receptor IS NOT NULL `, }, ]; for (const b of buckets) { const { rows: [r] } = await pool.query(b.sql, [fi, ff]); console.log(`${b.name.padEnd(28)} → ${r.n} fila(s), total = $${Number(r.total).toLocaleString('es-MX')}`); } await prisma.$disconnect(); } main().catch(e => { console.error(e); process.exit(1); });