index.js 3.22 KB
module.exports = function(comprobante) {

  // split por salto de línea
  comprobante = comprobante.split(/\r?\n/);

  var objReturn = {
    identificaxComprobante: {},
    identificaxMensaje: {},
    comprobantesReferencia: {},
    identificaxEmisor: {},
    informaxRepresentanteEmisor: {},
    identificaxReceptorFactura: {},
    identificaxSucursalReceptorFactura: {},
    importesTotales: {},
    detallesImportesIVA: {},
    detallePercepcionesIIBB: {},
    descuentosGlobalesFactura: {},
    detalleOtrosImpuestosComprobante: {},
    itemsFactura: [],
    detalleDescuentosItemFactura: [],
    detalleImpuestosItemFactura: []
  };

  function returnFloatByDecimals(parameter, cantDecimal) {

    if (!parameter) return;

    var beforeSemiColon = parseFloat(parameter.slice(0, parameter.length - cantDecimal));

    var afterSemicolon = parameter.slice(parameter.length - cantDecimal, parameter.length);

    return parseFloat(beforeSemiColon + '.' + afterSemicolon);
  }

  comprobante.forEach(sector => {

    if (sector.slice(0, 3) == '010') {
      objReturn.identificaxComprobante = require('./identificaxComprobante')(sector)// 010
    } else if (sector.slice(0, 3) == '012') {
      objReturn.identificaxMensaje = require('./identificaxMensaje')(sector)// 012
    } else if (sector.slice(0, 3) == '020') {
      require('./comprobantesReferencia')(sector, objReturn) // 020
    } else if (sector.slice(0, 3) == '030') {
      objReturn.identificaxEmisor = require('./identificaxEmisor')(sector) // 030
    } else if (sector.slice(0, 3) == '035') {
      objReturn.informaxRepresentanteEmisor = require('./informaxRepresentanteEmisor')(sector) // 035
    } else if (sector.slice(0, 3) == '040') {
      objReturn.identificaxReceptorFactura = require('./identificaxReceptorFactura')(sector) // 040
    } else if (sector.slice(0, 3) == '045') {
      objReturn.identificaxSucursalReceptorFactura = require('./identificaxSucursalReceptorFactura')(sector) // 045
    } else if (sector.slice(0, 3) == '050') {
      objReturn.importesTotales = require('./importesTotales')(sector, returnFloatByDecimals) // 050
    } else if (sector.slice(0, 3) == '060') {
      objReturn.detallesImportesIVA = require('./detallesImportesIVA')(sector, returnFloatByDecimals) // 060
    } else if (sector.slice(0, 3) == '070') {
      objReturn.detallePercepcionesIIBB = require('./detallePercepcionesIIBB')(sector, returnFloatByDecimals) // 070
    } else if (sector.slice(0, 3) == '080') {
      objReturn.descuentosGlobalesFactura = require('./descuentosGlobalesFactura')(sector, returnFloatByDecimals) // 080
    } else if (sector.slice(0, 3) == '090') {
      require('./detalleOtrosImpuestosComprobante')(sector, returnFloatByDecimals, objReturn) // 090
    } else if (sector.slice(0, 3) == '100') {
      objReturn.itemsFactura.push(require('./itemsFactura')(sector, returnFloatByDecimals)); // 100
    } else if (sector.slice(0, 3) == '110') {
      objReturn.detalleDescuentosItemFactura.push(require('./detalleDescuentosItemFactura')(sector, returnFloatByDecimals)); // 110
    } else if (sector.slice(0, 3) == '120') {
      objReturn.detalleImpuestosItemFactura.push(require('./detalleImpuestosItemFactura')(sector, returnFloatByDecimals, objReturn)); // 120
    }
  });

  return objReturn;
}