89 lines
4.1 KiB
TypeScript
89 lines
4.1 KiB
TypeScript
/**
|
|
* Desglosa cada I/07 recibida de un contribuyente en un rango, mostrando:
|
|
* - NETO_CUSTOM(I/07)
|
|
* - UUIDs en cfdis_relacionados
|
|
* - NETO_CUSTOM de cada relacionada vigente
|
|
* - Contribución neta de la I/07 al gasto
|
|
*
|
|
* Útil para detectar:
|
|
* - Múltiples I/07 que referencian el mismo anticipo (doble-resta)
|
|
* - Anticipos fuera del periodo que dominan la compensación
|
|
* - UUIDs relacionados incorrectos (apuntan a CFDIs enormes no-anticipo)
|
|
*/
|
|
import { prisma, tenantDb } from '../src/config/database.js';
|
|
|
|
const tenantRfc = process.argv[2] || 'DESPACHO_MO3NI6U8_B9VGG';
|
|
const contribuyenteId = process.argv[3] || 'd745a915-6a23-4818-944b-a7e1e18e536a';
|
|
const yearMonth = process.argv[4] || '2025-07';
|
|
|
|
async function main() {
|
|
const tenant = await prisma.tenant.findFirst({ where: { rfc: tenantRfc }, select: { id: true, databaseName: true } });
|
|
if (!tenant) return;
|
|
const pool = await tenantDb.getPool(tenant.id, tenant.databaseName);
|
|
|
|
const [anio, mes] = yearMonth.split('-').map(Number);
|
|
const lastDay = new Date(anio, mes, 0).getDate();
|
|
const fi = `${yearMonth}-01`;
|
|
const ff = `${yearMonth}-${String(lastDay).padStart(2, '0')}`;
|
|
|
|
const NETO = (a: string) => `(
|
|
COALESCE(${a}.total_mxn,0) - COALESCE(${a}.iva_traslado_mxn,0) + COALESCE(${a}.iva_retencion_mxn,0)
|
|
+ COALESCE(${a}.isr_retencion_mxn,0)
|
|
- COALESCE(${a}.ieps_traslado_mxn,0) + COALESCE(${a}.ieps_retencion_mxn,0)
|
|
- COALESCE(${a}.impuestos_locales_trasladado_mxn,0) + COALESCE(${a}.impuestos_locales_retenidos_mxn,0)
|
|
)`;
|
|
|
|
const { rows } = await pool.query(
|
|
`SELECT c.uuid, c.fecha_emision, c.total_mxn, c.rfc_emisor, c.cfdis_relacionados,
|
|
${NETO('c')} AS neto_i07
|
|
FROM cfdis c
|
|
WHERE c.type='RECIBIDO' AND c.tipo_comprobante='I' AND c.metodo_pago='PUE'
|
|
AND c.cfdi_tipo_relacion='07'
|
|
AND c.status NOT IN ('Cancelado','0')
|
|
AND c.fecha_emision >= $1::date AND c.fecha_emision < ($2::date + interval '1 day')
|
|
AND c.contribuyente_id = $3
|
|
ORDER BY c.fecha_emision`,
|
|
[fi, ff, contribuyenteId],
|
|
);
|
|
|
|
console.log(`\n=== I/07 RECIBIDAS en ${fi} a ${ff} ===`);
|
|
console.log(`Total I/07: ${rows.length}`);
|
|
|
|
let sumContrib = 0;
|
|
for (const r of rows) {
|
|
const relsUuids = (r.cfdis_relacionados || '').split('|').filter(Boolean).map((u: string) => u.toLowerCase());
|
|
console.log(`\n I/07 ${r.uuid.substring(0,8)} — fecha=${r.fecha_emision.toISOString().slice(0,10)} — emisor=${r.rfc_emisor}`);
|
|
console.log(` total_mxn: ${Number(r.total_mxn).toFixed(2)}`);
|
|
console.log(` NETO(I/07): ${Number(r.neto_i07).toFixed(2)}`);
|
|
console.log(` relacionados (${relsUuids.length}):`);
|
|
|
|
let sumRel = 0;
|
|
if (relsUuids.length > 0) {
|
|
const { rows: rels } = await pool.query(
|
|
`SELECT uuid, fecha_emision, total_mxn, tipo_comprobante, metodo_pago, status, ${NETO('a')} AS neto_rel
|
|
FROM cfdis a
|
|
WHERE LOWER(a.uuid) = ANY($1::text[])`,
|
|
[relsUuids],
|
|
);
|
|
for (const rel of rels) {
|
|
const vig = rel.status === 'Vigente' ? '✓' : '✗';
|
|
console.log(` ${vig} ${rel.uuid.substring(0,8)} ${rel.tipo_comprobante} ${rel.metodo_pago || '-'} fecha=${rel.fecha_emision?.toISOString?.().slice(0,10) || '-'} total=${Number(rel.total_mxn).toFixed(2)} NETO=${Number(rel.neto_rel).toFixed(2)}`);
|
|
if (rel.status === 'Vigente') sumRel += Number(rel.neto_rel);
|
|
}
|
|
const missing = relsUuids.filter((u: string) => !rels.find((x: any) => x.uuid.toLowerCase() === u));
|
|
if (missing.length > 0) {
|
|
console.log(` ⚠️ ${missing.length} UUID(s) relacionados NO están en BD:`);
|
|
for (const m of missing) console.log(` ${m}`);
|
|
}
|
|
}
|
|
const contrib = Number(r.neto_i07) - sumRel;
|
|
sumContrib += contrib;
|
|
console.log(` Σ NETO(rel vigentes): ${sumRel.toFixed(2)}`);
|
|
console.log(` CONTRIB: ${contrib.toFixed(2)} ${contrib < 0 ? '⚠️ NEGATIVA' : ''}`);
|
|
}
|
|
|
|
console.log(`\nSuma total contribuciones I/07: ${sumContrib.toFixed(2)}`);
|
|
await prisma.$disconnect();
|
|
}
|
|
main().catch(async e => { console.error(e); await prisma.$disconnect().catch(() => {}); process.exit(1); });
|