feat(facturacion): seccion CFDIs Relacionados para tipos I y E
This commit is contained in:
@@ -182,6 +182,13 @@ export async function emitir(req: Request, res: Response, next: NextFunction) {
|
||||
[parsed.rfcReceptor, parsed.nombreReceptor || null, parsed.regimenFiscalReceptor || null, req.body.customer?.zip || null],
|
||||
);
|
||||
|
||||
// Extraer relaciones para persistencia en BD
|
||||
const relatedDocs: Array<{ relationship: string; uuids: string[] }> = req.body.relatedDocuments || [];
|
||||
const cfdiTipoRelacion = relatedDocs.length > 0 ? relatedDocs[0].relationship : null;
|
||||
const cfdisRelacionados = relatedDocs.length > 0
|
||||
? relatedDocs.flatMap(r => r.uuids).join('|')
|
||||
: null;
|
||||
|
||||
await pool.query(`
|
||||
INSERT INTO cfdis (
|
||||
year, month, type, uuid, serie, folio, status, fecha_emision, fecha_cert_sat,
|
||||
@@ -192,7 +199,8 @@ export async function emitir(req: Request, res: Response, next: NextFunction) {
|
||||
iva_traslado, iva_traslado_mxn,
|
||||
iva_retencion, iva_retencion_mxn,
|
||||
source, facturapi_id,
|
||||
contribuyente_id, xml_original
|
||||
contribuyente_id, xml_original,
|
||||
cfdi_tipo_relacion, cfdis_relacionados
|
||||
) VALUES (
|
||||
$1, $2, 'EMITIDO', $3, $4, $5, 'Vigente', $6, $7,
|
||||
$8, $9, $10, $11,
|
||||
@@ -202,7 +210,8 @@ export async function emitir(req: Request, res: Response, next: NextFunction) {
|
||||
$23, $23,
|
||||
$24, $24,
|
||||
'facturapi', $25,
|
||||
$26, $27
|
||||
$26, $27,
|
||||
$28, $29
|
||||
)
|
||||
`, [
|
||||
year, month, parsed.uuid, parsed.serie, parsed.folio, fecha, parsed.fechaCertSat,
|
||||
@@ -214,6 +223,7 @@ export async function emitir(req: Request, res: Response, next: NextFunction) {
|
||||
parsed.ivaRetencion,
|
||||
invoice.id,
|
||||
contribuyenteId ?? null, xmlString,
|
||||
cfdiTipoRelacion, cfdisRelacionados,
|
||||
]);
|
||||
|
||||
// Enviar por email si el receptor tiene email — ruteado a la org correcta
|
||||
|
||||
@@ -308,10 +308,11 @@ export async function createInvoiceContribuyente(
|
||||
if (data.series) invoicePayload.series = data.series;
|
||||
if (data.folioNumber) invoicePayload.folio_number = data.folioNumber;
|
||||
|
||||
// Documentos relacionados (Ingreso / Egreso)
|
||||
if (data.relatedDocuments?.length) {
|
||||
invoicePayload.related_documents = data.relatedDocuments.map((r: any) => ({
|
||||
relationship: r.relationship,
|
||||
documents: [r.uuid],
|
||||
documents: r.uuids || [r.uuid],
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
@@ -247,7 +247,7 @@ export interface FacturapiInvoiceData {
|
||||
series?: string;
|
||||
folioNumber?: number;
|
||||
conditions?: string;
|
||||
relatedDocuments?: Array<{ uuid: string; relationship: string }>;
|
||||
relatedDocuments?: Array<{ relationship: string; uuids: string[] }>;
|
||||
/**
|
||||
* Régimen fiscal del emisor (override del default de la organización).
|
||||
* Requerido cuando el contribuyente tiene múltiples régimenes y Facturapi
|
||||
@@ -308,11 +308,11 @@ export async function createInvoice(
|
||||
if (data.series) invoiceData.series = data.series;
|
||||
if (data.folioNumber) invoiceData.folio_number = data.folioNumber;
|
||||
|
||||
// Documentos relacionados (Egreso)
|
||||
// Documentos relacionados (Ingreso / Egreso)
|
||||
if (data.relatedDocuments?.length) {
|
||||
invoiceData.related_documents = data.relatedDocuments.map(r => ({
|
||||
relationship: r.relationship,
|
||||
documents: [r.uuid],
|
||||
documents: r.uuids,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user