module.exports = function(planex, wsServer) { return new Promise(function(resolve, reject) { var promesasArticulo = []; var tipoCuit = planex.identificaxEmisor.cuit.slice(0, 2); var digitoVerificador = planex.identificaxEmisor.cuit[planex.identificaxEmisor.cuit.length - 1]; var cuitToDebo = tipoCuit + '-' + planex.identificaxEmisor.cuit.slice(2, 10) + '-' + digitoVerificador; var promiseProveedores = wsServer.getEntidad(planex.identificaxReceptorFactura.glnReceptor, 'PROVEED', {CUIT: cuitToDebo}); var promiseEmp = wsServer.getEntidad(planex.identificaxReceptorFactura.glnReceptor, 'APAREMP'); planex.itemsFactura.forEach(item => { var queryString = 'select cb.*, art.CodRub, art.DET_LAR from CODBAR cb ' + 'join ARTICULOS art on cb.CodArt = art.CodArt and cb.CodSec = art.CodSec where cb.CodBar = \'' + item.codigoUPCEAN13 + '\''; promesasArticulo.push( wsServer.getEntidad(planex.identificaxReceptorFactura.glnReceptor, null, {}, queryString) ); }); promesas = promesasArticulo.concat([promiseProveedores, promiseEmp]); Promise.all(promesas).then(function(values) { planex.itemsFactura.forEach((item, index) => { if (!values[index]) { console.error('No se encontró el articulo Código de barras = ' + item.codigoUPCEAN13); return; } item.CodArt = values[index].CodArt; item.CodSec = values[index].CodSec; item.CodRub = values[index].CodRub; item.DET_LAR = values[index].DET_LAR; delete values[index]; }); values = values.filter(function(p) { return p; }); resolve({planex, values}); }).catch(function(e) { reject(e); }); }); }