@facturacr/atv-sdk
Version:
Librería (SDK) de Javascript/NodeJS para acceder al API de Administración Tributaria Virtual (ATV) del Ministerio de Hacienda.
135 lines • 4.88 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setLinesDefaults = void 0;
const genXML_1 = require("../genXML");
// Default XML Values
const DEFAULT_VALUES = {
key: 0,
message: 'Default msj',
detailsMessage: 'Default details msj',
tipoIdentificacion: '01'
};
function sumLines(lines) {
return lines.reduce((accumulator, currentValue) => {
const prevTotal = accumulator.MontoTotal || 0;
const prevTax = accumulator.Impuesto || { Impuesto: { Monto: 0 } };
return {
total: prevTotal + currentValue.MontoTotal,
taxes: prevTax.Impuesto.Monto + currentValue.Impuesto.Monto
};
}, 0);
}
function getBillResum(lines) {
const sum = sumLines(lines);
return {
CodigoTipoMoneda: {
CodigoMoneda: 'CRC',
TipoCambio: '585.69'
},
TotalServGravados: 0,
TotalServExentos: 0,
// TotalServExonerado: 0,
TotalMercanciasGravadas: sum.total,
TotalMercanciasExentas: 0,
TotalGravado: sum.total,
TotalExento: 0,
TotalExonerado: 0,
TotalVenta: sum.total,
TotalDescuentos: 0,
TotalVentaNeta: sum.total,
TotalImpuesto: sum.taxes,
TotalComprobante: sum.total + sum.taxes
};
}
function getSender(frontEndRequest) {
const sender = frontEndRequest.Emisor;
return {
Nombre: sender.Nombre,
Identificacion: {
Tipo: sender.Identificacion.Tipo || DEFAULT_VALUES.tipoIdentificacion,
Numero: sender.Identificacion.Numero
},
NombreComercial: sender.NombreComercial,
Ubicacion: sender.Ubicacion,
Telefono: sender.Telefono,
Fax: sender.Fax,
CorreoElectronico: sender.CorreoElectronico
};
}
function setTaxObj(subtotal, taxObj) {
const tax = typeof taxObj.Tarifa === 'number' ? taxObj.Tarifa : 13;
return {
Codigo: taxObj.Codigo,
CodigoTarifa: taxObj.CodigoTarifa,
Tarifa: tax,
Monto: subtotal * (tax / 100)
};
}
function setLinesDefaults(lines) {
return lines.map((line, index) => {
const quantity = line.Cantidad || 1;
const total = line.PrecioUnitario * quantity;
const subtotal = total; // restar descuentos
const taxObj = setTaxObj(subtotal, line.Impuesto);
return {
NumeroLinea: (index + 1).toString(),
Codigo: line.Codigo,
Cantidad: quantity,
UnidadMedida: line.UnidadMedida || 'Sp',
Detalle: line.Detalle,
PrecioUnitario: line.PrecioUnitario,
MontoTotal: total,
SubTotal: subtotal,
Impuesto: taxObj,
MontoTotalLinea: subtotal + taxObj.Monto
};
});
}
exports.setLinesDefaults = setLinesDefaults;
function getReceiver(frontEndRequest) {
const receiver = frontEndRequest.Receptor;
return {
Nombre: receiver.Nombre,
Identificacion: {
Tipo: receiver.Identificacion.Tipo || DEFAULT_VALUES.tipoIdentificacion,
Numero: receiver.Identificacion.Numero
},
NombreComercial: receiver.NombreComercial,
Ubicacion: receiver.Ubicacion
};
}
exports.default = (frontEndRequest, date, clave, consecutivo, options) => __awaiter(void 0, void 0, void 0, function* () {
const receiver = getReceiver(frontEndRequest);
const sender = getSender(frontEndRequest);
const lines = setLinesDefaults(frontEndRequest.LineasDetalle);
const key = frontEndRequest.facturaElectronicaType || 'FacturaElectronica';
const body = {
Clave: clave,
CodigoActividad: frontEndRequest.actividad.padStart(6, '0'),
NumeroConsecutivo: consecutivo,
FechaEmision: date,
Emisor: sender,
Receptor: receiver,
CondicionVenta: '01',
MedioPago: '03',
DetalleServicio: {
LineaDetalle: lines
},
ResumenFactura: getBillResum(lines)
};
const factura = {
[key]: body
};
const XML = yield (0, genXML_1.genXML)(key, factura, options);
return XML;
});
//# sourceMappingURL=index.js.map