diff --git a/debo/cabeceraDebo.js b/debo/cabeceraDebo.js index e9cf146..79c9720 100644 --- a/debo/cabeceraDebo.js +++ b/debo/cabeceraDebo.js @@ -87,5 +87,6 @@ module.exports = function(comprobante, afipTablas, entities) { CABA_PER: 0, // [money] NOT NULL, IMPORTE PERCEPCION CABA PERMUN: comprobante.importesTotales.importeImpuestosMunicipales, // [money] NOT NULL, IMPORTE PERCEPCIONES MUNICIPALES IMI2: 0, // [money] NOT NULL, FIJO 0 + gln: comprobante.identificaxSucursalReceptorFactura.glnSucursal }; } diff --git a/debo/cuerpoDebo.js b/debo/cuerpoDebo.js index b4fb59c..b04e979 100644 --- a/debo/cuerpoDebo.js +++ b/debo/cuerpoDebo.js @@ -37,5 +37,7 @@ module.exports = function(comprobante, afipTablas, entities, item) { COSTO_PMOV: comprobante.itemsFactura.precioUnitario, // [money] NOT NULL, IGUAL AL PUN SAL_ITEM: 0, // [money] NOT NULL, fijo 0 es el que se llenara al mostrar en la tablet o telefono para que llene operador TASIVA: comprobante.itemsFactura.alicuotaIVAAplicable, // [money] NOT NULL, TASA DE IVA DEL ITEM = 21.00 / 10.50 / ETC. + gln: comprobante.identificaxSucursalReceptorFactura.glnSucursal, //gln sucursal + codigoBarras: item.codigoUPCEAN13 }; } diff --git a/debo/index.js b/debo/index.js index 3502f4e..fb290bd 100644 --- a/debo/index.js +++ b/debo/index.js @@ -7,7 +7,7 @@ module.exports = function(comprobante, entities) { cuerpo: [] } - comprobante.itemsFactura.forEach((item, index) => { + comprobante.itemsFactura.forEach((item) => { objReturn.cuerpo.push(require('./cuerpoDebo')(comprobante, afipTablas, entities, item)); }); diff --git a/debo/relaciones.js b/debo/relaciones.js index 0485afa..2859a80 100644 --- a/debo/relaciones.js +++ b/debo/relaciones.js @@ -28,7 +28,7 @@ module.exports = function(planex, wsServer) { planex.itemsFactura.forEach((item, index) => { - if (!values[index]) console.error('No se encontrĂ³ el articulo'); + if (!values[index]) return; console.error('No se encontrĂ³ el articulo'); item.CodArt = values[index].CodArt; item.CodSec = values[index].CodSec; diff --git a/debo/validacion.js b/debo/validacion.js index a25889c..bfa88b3 100644 --- a/debo/validacion.js +++ b/debo/validacion.js @@ -18,4 +18,4 @@ module.exports = function(debo, wsServer, gln) { } }); }); -} \ No newline at end of file +} diff --git a/index.js b/index.js index 98689a4..d537b37 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,31 @@ config = require('./config/config.json'); -module.exports = knex = require('knex')(config.db) + +module.exports = + knex = require('knex')(config.db), + express = require('express'); + app = express(); + wsServer = require('./webSocketServer/index')(); + moment = require('moment'); + +app.listen(config.portWebService); +app.use(express.json({ limit: '50mb' })); require('./watch/index')(config.dir, onGetFile); -const wsServer = require('./webSocketServer/index')(); -var planexArr = [] +app.use(function(req, res, next) { + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader( + 'Access-Control-Allow-Headers', + 'Origin, X-Requested-With, Content-Type, Accept, X-Terminal-Key, X-Nombre-Usuario, X-Punto-Venta' + ); + res.setHeader('Access-Control-Allow-Methods', 'POST, GET, DELETE, OPTIONS'); + next(); +}); + +app.use('/gateway-debo', require('./rutas/comprobantes')); +app.use('/gateway-debo', require('./rutas/login')); + +var planexArr = []; function onGetFile(fileString) { @@ -17,9 +38,7 @@ function onGetFile(fileString) { // Traigo proveedor, empresa require('./debo/relaciones')(planex, wsServer).then(function(result) { - var planex = planexArr.filter(function(p) { - return p.identificaxMensaje.idMensaje == result.planex.identificaxMensaje.idMensaje - })[0]; + var planex = result.planex; var entities = {}; @@ -28,7 +47,7 @@ function onGetFile(fileString) { var debo = require('./debo/index')(planex, entities); - require('./debo/validacion')(debo, wsServer, planex.identificaxReceptorFactura.glnReceptor).then(function() { + require('./debo/validacion')(debo, wsServer, planex.identificaxSucursalReceptorFactura.glnSucursal).then(function() { require('./db/index')(debo); }).catch(function(err) { @@ -36,9 +55,9 @@ function onGetFile(fileString) { console.log(err); }); - }); } console.log('listen websocket port ' + config.port); +console.log('listen webservice port ' + config.portWebService); console.log('Ejecutar programa como administrador...'); diff --git a/package.json b/package.json index e53e307..310d5bb 100644 --- a/package.json +++ b/package.json @@ -4,15 +4,19 @@ "description": "WebSocket Head Office", "main": "index.js", "dependencies": { - "chokidar": "^3.0.0", - "express": "^4.16.4", + "chokidar": "^3.0.1", + "express": "^4.17.1", "knex": "^0.16.5", + "moment": "^2.24.0", "mssql": "^5.1.0", "ws": "^7.0.0" }, - "devDependencies": {}, + "devDependencies": { + "nodemon": "^1.19.1" + }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "dev": "nodemon index.js" }, "author": "Foca Software", "license": "ISC" diff --git a/webSocketServer/index.js b/webSocketServer/index.js index 2b0501d..bd92c90 100644 --- a/webSocketServer/index.js +++ b/webSocketServer/index.js @@ -1,73 +1,79 @@ module.exports = function() { -const webSocketServer = require('ws').Server; -var clients = []; + const webSocketServer = require('ws').Server; + var clients = []; -const objWs = {}; + const objWs = {}; -objWs.wsServer = new webSocketServer({ - port: config.port -}); + objWs.wsServer = new webSocketServer({ + port: config.port + }); -objWs.wsServer.on('connection', function connection(ws) { + objWs.wsServer.on('connection', function connection(ws) { - console.log('open socket server'); + console.log('open socket server'); - ws.on('message', function incoming(message) { + ws.on('message', function incoming(message) { - message = JSON.parse(message.toString('utf8')); + message = JSON.parse(message.toString('utf8')); - switch (message.action) { - case 'gln': - clients.push({ws: ws, gln: message.gln}); - break; - default: - // console.log(message.action); - } + switch (message.action) { + case 'gln': + clients.push({ws: ws, gln: message.gln}); + break; + default: + // console.log(message.action); + } + }); }); -}); -objWs.getEntidad = function(gln, tableName, where, queryString) { + objWs.getEntidad = function(gln, tableName, where, queryString) { - return new Promise(function(resolve, reject) { + return new Promise(function(resolve, reject) { - var client = clients.filter(function(client) { - return client.gln == gln - })[0]; + var client = clients.filter(function(client) { + return client.gln == gln + })[0]; + + if (!client) { + reject('No se encuentra el webSocket client'); + } - if (!client) { - reject('No se encuentra el webSocket client'); - } + var idSolicitud = Math.round(Math.random() * 1000); - var idSolicitud = Math.round(Math.random() * 1000); + var enviar = { + action: 'getEntity', + idSolicitud: idSolicitud + }; - var enviar = { - action: 'getEntity', - idSolicitud: idSolicitud - }; + if (queryString) { - if (queryString) { + enviar.queryString = queryString; + } else { + enviar.tableName = tableName; + enviar.where = where || {}; + } - enviar.queryString = queryString; - } else { - enviar.tableName = tableName; - enviar.where = where || {}; - } + client.ws.send(JSON.stringify(enviar)); - client.ws.send(JSON.stringify(enviar)); + client.ws.on('message', function(message) { - client.ws.on('message', function(message) { + message = JSON.parse(message); - message = JSON.parse(message); + if (idSolicitud == message.idSolicitud) { - if (idSolicitud == message.idSolicitud) { + resolve(message.data); + } - resolve(message.data); - } + }); + }); + } + objWs.getClientGln = function(gln) { + return clients.filter(function(client) { + return client.gln = gln; }); - }); -} + } -return objWs; + return objWs; }