UNPKG

falcotura-atv-sdk

Version:

Librería (SDK) de Javascript/NodeJS para acceder al API de Administración Tributaria Virtual (ATV) del Ministerio de Hacienda.

167 lines 8.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapReceptorMessageToAtvFormat = exports.mapDocumentToAtvFormat = void 0; const dateUtils_1 = require("../../utils/dateUtils"); const parseAtvMoneyFormat = (amount) => { return parseFloat(amount.toFixed(5)); }; const mapOrderLinesToAtvFormat = (orderLines) => { const LineaDetalle = orderLines.map((orderLine) => { var _a; const impuestoMonto = orderLine.tax ? parseAtvMoneyFormat((_a = orderLine.tax.amount) !== null && _a !== void 0 ? _a : 0) : 0; return Object.assign(Object.assign({ NumeroLinea: orderLine.lineNumber, CodigoCABYS: orderLine.code, // CodigoComercial Cantidad: orderLine.quantity, UnidadMedida: orderLine.measureUnit, // UnidadMedidaComercial Detalle: orderLine.detail, PrecioUnitario: orderLine.unitaryPrice, MontoTotal: orderLine.totalAmount, // Descuento SubTotal: orderLine.subTotal, BaseImponible: orderLine.subTotal }, (orderLine.tax && { Impuesto: { Codigo: orderLine.tax.code, CodigoTarifaIVA: orderLine.tax.rateCode, Tarifa: orderLine.tax.rate, Monto: impuestoMonto // Exoneracion is explicitly excluded here } })), { ImpuestoAsumidoEmisorFabrica: 0, // @ts-expect-error pending-to-fix ImpuestoNeto: parseAtvMoneyFormat(orderLine.tax.amount), MontoTotalLinea: parseAtvMoneyFormat(orderLine.totalOrderLineAmount), exchangeRate: orderLine.exchangeRate, currency: orderLine.currency }); }); return { LineaDetalle }; }; const mapSummaryInvoice = (document) => { const summaryInvoice = document.summaryInvoice; const orderLines = document.orderLines; // Still need access to order lines for breakdown // --- Lógica para TotalDesgloseImpuesto (sin considerar exoneraciones) --- const taxBreakdownMap = new Map(); orderLines.forEach(orderLine => { var _a; if (((_a = orderLine.tax) === null || _a === void 0 ? void 0 : _a.amount) !== undefined && orderLine.tax.amount !== null && orderLine.tax.amount > 0) { const key = `${orderLine.tax.code}-${orderLine.tax.rateCode}`; const currentTotal = taxBreakdownMap.get(key) || 0; taxBreakdownMap.set(key, currentTotal + orderLine.tax.amount); } }); const TotalDesgloseImpuesto = Array.from(taxBreakdownMap.entries()).map(([key, totalMonto]) => { const [Codigo, CodigoTarifaIVA, Tarifa] = key.split('-'); return { Codigo, CodigoTarifaIVA, Tarifa, TotalMontoImpuesto: parseAtvMoneyFormat(totalMonto) }; }); return { CodigoTipoMoneda: { // @ts-expect-error pending-to-fix CodigoMoneda: summaryInvoice.currency.code, // @ts-expect-error pending-to-fix TipoCambio: summaryInvoice.currency.exchangeRate }, TotalServGravados: parseAtvMoneyFormat(summaryInvoice.totalEncumberedServices), TotalServExentos: parseAtvMoneyFormat(summaryInvoice.totalExemptServices), TotalServNoSujeto: parseAtvMoneyFormat(summaryInvoice.totalNonTaxableServices), // Moved here // @ts-expect-error pending-to-fix TotalMercanciasGravadas: parseAtvMoneyFormat(summaryInvoice.totalEncumberedMerchandise), // @ts-expect-error pending-to-fix TotalMercanciasExentas: parseAtvMoneyFormat(summaryInvoice.totalExemptMerchandise), TotalMercNoSujeta: parseAtvMoneyFormat(summaryInvoice.totalNonTaxableMerchandise), // Moved here TotalGravado: parseAtvMoneyFormat(summaryInvoice.totalEncumbered), TotalExento: parseAtvMoneyFormat(summaryInvoice.totalExempt), TotalExonerado: parseAtvMoneyFormat(summaryInvoice.totalExonerated), TotalNoSujeto: parseAtvMoneyFormat(summaryInvoice.totalNonTaxable), // Moved here TotalVenta: parseAtvMoneyFormat(summaryInvoice.totalSale), // @ts-expect-error pending-to-fix TotalDescuentos: parseAtvMoneyFormat(summaryInvoice.totalDiscounts), // @ts-expect-error pending-to-fix TotalVentaNeta: parseAtvMoneyFormat(summaryInvoice.totalNetSale), TotalDesgloseImpuesto, TotalImpuesto: parseAtvMoneyFormat(summaryInvoice.totalTaxes), TotalImpAsumEmisorFabrica: 0, TotalOtrosCargos: 0, MedioPago: { // @ts-expect-error pending-to-fix TipoMedioPago: document.paymentMethod }, TotalComprobante: parseAtvMoneyFormat(summaryInvoice.totalVoucher) }; }; const mapPerson = (person) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const atvPerson = { Nombre: person.fullName, Identificacion: { Tipo: person.identifierType, Numero: person.identifierId }, NombreComercial: person.commercialName, Ubicacion: undefined, Telefono: undefined, CorreoElectronico: undefined }; // @ts-expect-error pending-to-fix atvPerson.Ubicacion = person.location ? { Provincia: (_a = person.location) === null || _a === void 0 ? void 0 : _a.province, Canton: (_c = (_b = person.location) === null || _b === void 0 ? void 0 : _b.canton) === null || _c === void 0 ? void 0 : _c.padStart(2, '0'), Distrito: (_e = (_d = person.location) === null || _d === void 0 ? void 0 : _d.district) === null || _e === void 0 ? void 0 : _e.padStart(2, '0'), Barrio: (_g = (_f = person.location) === null || _f === void 0 ? void 0 : _f.neighborhood) === null || _g === void 0 ? void 0 : _g.padStart(5, '0'), OtrasSenas: (_h = person.location) === null || _h === void 0 ? void 0 : _h.details } : undefined; // @ts-expect-error pending-to-fix atvPerson.Telefono = person.phone ? { CodigoPais: (_j = person.phone) === null || _j === void 0 ? void 0 : _j.countryCode, NumTelefono: (_k = person.phone) === null || _k === void 0 ? void 0 : _k.number } : undefined; // @ts-expect-error pending-to-fix atvPerson.CorreoElectronico = person.email; return atvPerson; }; const mapReferenceInformation = (referenceInfo) => { return { TipoDocIR: referenceInfo.docType, Numero: referenceInfo.refNumber, FechaEmisionIR: (0, dateUtils_1.toCostaRicaISOString)(referenceInfo.issueDate), Codigo: referenceInfo.code, Razon: referenceInfo.reason }; }; const mapDocumentToAtvFormat = (docName, document) => { var _a, _b; const key = docName; const doc = Object.assign(Object.assign(Object.assign(Object.assign({ Clave: document.clave, ProveedorSistemas: document.providerId, CodigoActividadEmisor: document.emitter.activityCode.padStart(6, '0') }, (document.receiver && { CodigoActividadReceptor: (_b = (_a = document.receiver) === null || _a === void 0 ? void 0 : _a.activityCode) === null || _b === void 0 ? void 0 : _b.padStart(6, '0') })), { NumeroConsecutivo: document.fullConsecutive, FechaEmision: (0, dateUtils_1.toCostaRicaISOString)(document.issueDate), Emisor: mapPerson(document.emitter) }), (document.receiver && { Receptor: mapPerson(document.receiver) })), { CondicionVenta: document.conditionSale, PlazoCredito: document.deadlineCredit, DetalleServicio: mapOrderLinesToAtvFormat(document.orderLines), ResumenFactura: mapSummaryInvoice(document), Otros: document.others }); if (document.referenceInformation) { doc.InformacionReferencia = mapReferenceInformation(document.referenceInformation); } return { [key]: doc }; }; exports.mapDocumentToAtvFormat = mapDocumentToAtvFormat; const mapReceptorMessageToAtvFormat = (props) => { return { MensajeReceptor: { Clave: props.clave, NumeroCedulaEmisor: props.emitterIdentifier, FechaEmisionDoc: (0, dateUtils_1.toCostaRicaISOString)(props.documentIssueDate), Mensaje: props.aceptationState.toString(), // 1 Aceptado | 2 Aceptado Parcialmente | 3 Rechazado DetalleMensaje: props.aceptationDetailMessage, MontoTotalImpuesto: props.totalTaxes, CodigoActividad: props.activityCode, CondicionImpuesto: props.taxCondition, MontoTotalDeGastoAplicable: props.totalSale, // fullInvoice.ResumenFactura.TotalVenta, // TODO investigar casos de uso TotalFactura: props.totalSale, // fullInvoice.ResumenFactura.TotalVenta, NumeroCedulaReceptor: props.receptorIdentifier, NumeroConsecutivoReceptor: props.receptorConcecutive } }; }; exports.mapReceptorMessageToAtvFormat = mapReceptorMessageToAtvFormat; //# sourceMappingURL=billDocToAtv.js.map