UNPKG

@facturacr/atv-sdk

Version:

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

138 lines 6.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapReceptorMessageToAtvFormat = exports.mapDocumentToAtvFormat = void 0; const parseAtvMoneyFormat = (amount) => { return parseFloat(amount.toFixed(5)); }; const mapOrderLinesToAtvFormat = (orderLines) => { const LineaDetalle = orderLines.map((orderLine) => { return { NumeroLinea: orderLine.lineNumber, Codigo: orderLine.code, // CodigoComercial Cantidad: orderLine.quantity, UnidadMedida: orderLine.measureUnit, // UnidadMedidaComercial Detalle: orderLine.detail, PrecioUnitario: orderLine.unitaryPrice, MontoTotal: orderLine.totalAmount, // Descuento SubTotal: orderLine.subTotal, // BaseImponible Impuesto: { Codigo: orderLine.tax.code, CodigoTarifa: orderLine.tax.rateCode, Tarifa: orderLine.tax.rate, Monto: parseAtvMoneyFormat(orderLine.tax.amount) }, // ImpuestoNeto MontoTotalLinea: parseAtvMoneyFormat(orderLine.totalOrderLineAmount) }; }); return { LineaDetalle }; }; const mapSummaryInvoice = (summaryInvoice) => { return { CodigoTipoMoneda: { CodigoMoneda: summaryInvoice.currency.code, TipoCambio: summaryInvoice.currency.exchangeRate }, TotalServGravados: parseAtvMoneyFormat(summaryInvoice.totalEncumberedServices), TotalServExentos: parseAtvMoneyFormat(summaryInvoice.totalExemptServices), TotalMercanciasGravadas: parseAtvMoneyFormat(summaryInvoice.totalEncumberedMerchandise), TotalMercanciasExentas: parseAtvMoneyFormat(summaryInvoice.totalExemptMerchandise), TotalGravado: parseAtvMoneyFormat(summaryInvoice.totalEncumbered), TotalExento: parseAtvMoneyFormat(summaryInvoice.totalExempt), TotalExonerado: parseAtvMoneyFormat(summaryInvoice.totalExonerated), TotalVenta: parseAtvMoneyFormat(summaryInvoice.totalSale), TotalDescuentos: parseAtvMoneyFormat(summaryInvoice.totalDiscounts), TotalVentaNeta: parseAtvMoneyFormat(summaryInvoice.totalNetSale), TotalImpuesto: parseAtvMoneyFormat(summaryInvoice.totalTaxes), TotalComprobante: parseAtvMoneyFormat(summaryInvoice.totalVoucher) }; }; const mapPerson = (person) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; const atvPerson = { Nombre: person.fullName, Identificacion: { Tipo: person.identifierType, Numero: person.identifierId }, NombreComercial: person.commercialName, Ubicacion: undefined, Telefono: undefined, Fax: undefined, CorreoElectronico: undefined }; 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(2, '0'), OtrasSenas: (_h = person.location) === null || _h === void 0 ? void 0 : _h.details } : undefined; 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; atvPerson.Fax = person.fax ? { CodigoPais: (_l = person.fax) === null || _l === void 0 ? void 0 : _l.countryCode, NumTelefono: (_m = person.fax) === null || _m === void 0 ? void 0 : _m.number } : undefined; atvPerson.CorreoElectronico = person.email; return atvPerson; }; const mapReferenceInformation = (referenceInfo) => { return { TipoDoc: referenceInfo.docType, Numero: referenceInfo.refNumber, FechaEmision: referenceInfo.issueDate.toISOString(), Codigo: referenceInfo.code, Razon: referenceInfo.reason }; }; const mapDocumentToAtvFormat = (docName, document) => { const key = docName; const doc = { Clave: document.clave, CodigoActividad: document.activityCode.padStart(6, '0'), NumeroConsecutivo: document.fullConsecutive, FechaEmision: document.issueDate.toISOString(), Emisor: mapPerson(document.emitter), Receptor: mapPerson(document.receiver), CondicionVenta: document.conditionSale, PlazoCredito: document.deadlineCredit, MedioPago: document.paymentMethod, DetalleServicio: mapOrderLinesToAtvFormat(document.orderLines), ResumenFactura: mapSummaryInvoice(document.summaryInvoice), 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: props.documentIssueDate.toISOString(), 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