diff --git a/index.js b/index.js index 58dc9ff..98689a4 100644 --- a/index.js +++ b/index.js @@ -4,57 +4,39 @@ module.exports = knex = require('knex')(config.db) require('./watch/index')(config.dir, onGetFile); const wsServer = require('./webSocketServer/index')(); +var planexArr = [] -function onGetFile(event, fileString) { +function onGetFile(fileString) { if (!fileString) return; var planex = require('./planex/index')(fileString); - console.log(planex); + planexArr.push(planex); - var entities = {}; + // Traigo proveedor, empresa + require('./debo/relaciones')(planex, wsServer).then(function(result) { - var tipoCuit = planex.identificaxEmisor.cuit.slice(0, 2); - var digitoVerificador = planex.identificaxEmisor.cuit[planex.identificaxEmisor.cuit.length - 1]; + var planex = planexArr.filter(function(p) { + return p.identificaxMensaje.idMensaje == result.planex.identificaxMensaje.idMensaje + })[0]; - var cuitToDebo = tipoCuit + '-' + planex.identificaxEmisor.cuit.slice(2, 10) + '-' + digitoVerificador; + var entities = {}; - var promiseProveedores = wsServer.getEntidad('7790968003283', 'PROVEED', {CUIT: cuitToDebo}); - var promiseEmp = wsServer.getEntidad('7790968003283', 'APAREMP'); + entities.proveedores = result.values[0]; + entities.empresa = result.values[1]; - var promesasArticulo = []; + var debo = require('./debo/index')(planex, entities); - planex.itemsFactura.forEach(item => { + require('./debo/validacion')(debo, wsServer, planex.identificaxReceptorFactura.glnReceptor).then(function() { - 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('7790968003283', null, {}, queryString)); - }); - - Promise.all(promesasArticulo).then(function(values) { - - planex.itemsFactura.forEach((item, index) => { - - if (!values[index]) console.error('No se encontrĂ³ el articulo'); + require('./db/index')(debo); + }).catch(function(err) { - item.CodArt = values[index].CodArt; - item.CodSec = values[index].CodSec; - item.CodRub = values[index].CodRub; - item.DET_LAR = values[index].DET_LAR; + console.log(err); }); - Promise.all([promiseProveedores, promiseEmp]).then(function(data) { - - entities.proveedores = data[0]; - entities.empresa = data[1]; - - var debo = require('./debo/index')(planex, entities); - require('./db/index')(debo); - }); }); } diff --git a/package.json b/package.json index c0ff3e0..e53e307 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "WebSocket Head Office", "main": "index.js", "dependencies": { + "chokidar": "^3.0.0", "express": "^4.16.4", "knex": "^0.16.5", "mssql": "^5.1.0", diff --git a/watch/index.js b/watch/index.js index 3e316e7..ab60bcf 100644 --- a/watch/index.js +++ b/watch/index.js @@ -1,16 +1,19 @@ -module.exports = function(dir, callback) { +module.exports = function (dir, callback) { const fs = require('fs'); + const chokidar = require('chokidar'); - fs.watch(dir, function(event, file) { + const watcher = chokidar.watch(dir); + + watcher.on('add', function(file) { var extencion = file.split('.')[1]; - if (extencion == 'txt' && event == 'change') { + if (extencion == 'txt') { - fs.readFile(dir + '\\' + file, 'utf8', function(err , fileString) { + fs.readFile(file, 'utf8', function (err, fileString) { - callback(event, fileString); + callback(fileString); }); }