diff --git a/db/index.js b/db/index.js new file mode 100644 index 0000000..c765324 --- /dev/null +++ b/db/index.js @@ -0,0 +1,18 @@ +module.exports = function(comprobante) { + + var promesas = []; + + promesas.push(knex('PMAEFACT_TEMP').insert(comprobante.cabecera)); + + comprobante.cuerpo.forEach(cuerpo => { + promesas.push(knex('PMOVFACT_TEMP').insert(cuerpo)); + }); + + Promise.all(promesas).then(function() { + + console.log('COMPROBANTE GUARDADO CON ÉXITO'); + }).catch(function() { + + console.log('ERROR AL GUARDAR EL COMPROBANTE'); + }); +} diff --git a/debo/relaciones.js b/debo/relaciones.js new file mode 100644 index 0000000..0485afa --- /dev/null +++ b/debo/relaciones.js @@ -0,0 +1,50 @@ +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'); + + 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}); + + }); + + }); +} \ No newline at end of file diff --git a/debo/validacion.js b/debo/validacion.js new file mode 100644 index 0000000..a25889c --- /dev/null +++ b/debo/validacion.js @@ -0,0 +1,21 @@ +module.exports = function(debo, wsServer, gln) { + + return new Promise(function(resolve, reject) { + + wsServer + .getEntidad(gln, 'PMAEFACT_TEMP', { + TIP: debo.cabecera.TIP, + TCO: debo.cabecera.TCO, + SUC: debo.cabecera.SUC, + NCO: debo.cabecera.NCO, + COD: debo.cabecera.COD + }).then(function(values) { + + if (values) { + reject('Ya existe el comprobante', values); + } else { + resolve(); + } + }); + }); +} \ No newline at end of file