Commit 5288585325afa8baf03270cdbdf96aaf2ba4dea5
1 parent
59960b7897
Exists in
master
refactor última hora
Showing
2 changed files
with
40 additions
and
126 deletions
Show diff stats
rutas/comprobantes.js
| 1 | module.exports = router = express.Router(); | 1 | module.exports = router = express.Router(); |
| 2 | 2 | ||
| 3 | router.get('/comprobantes/:gln*?', function(req, res) { | 3 | router.get('/comprobantes/:gln*?', function(req, res) { |
| 4 | 4 | ||
| 5 | var cabecera = knex('PMAEFACT_TEMP') | 5 | var cabecera = knex('PMAEFACT_TEMP') |
| 6 | .where(function(qb) { | 6 | .where(function(qb) { |
| 7 | if (req.params.gln) { | 7 | if (req.params.gln) { |
| 8 | qb.where({GLN: req.params.gln}); | 8 | qb.where({GLN: req.params.gln}); |
| 9 | } | 9 | } |
| 10 | }) | 10 | }) |
| 11 | .select('*'); | 11 | .select('*'); |
| 12 | 12 | ||
| 13 | var cuerpo = knex('PMOVFACT_TEMP') | 13 | var cuerpo = knex('PMOVFACT_TEMP') |
| 14 | .where(function(qb) { | 14 | .where(function(qb) { |
| 15 | if (req.params.gln) { | 15 | if (req.params.gln) { |
| 16 | qb.where({GLN: req.params.gln}); | 16 | qb.where({GLN: req.params.gln}); |
| 17 | } | 17 | } |
| 18 | }) | 18 | }) |
| 19 | .select('*'); | 19 | .select('*'); |
| 20 | 20 | ||
| 21 | Promise.all([cabecera, cuerpo]).then(function(data) { | 21 | Promise.all([cabecera, cuerpo]).then(function(data) { |
| 22 | 22 | ||
| 23 | var result = []; | 23 | var result = []; |
| 24 | data[0].forEach((cabecera) => { | 24 | data[0].forEach((cabecera) => { |
| 25 | 25 | ||
| 26 | let idCabecera = cabecera.TIP + cabecera.TCO + cabecera.SUC + cabecera.NCO; | 26 | let idCabecera = cabecera.TIP + cabecera.TCO + cabecera.SUC + cabecera.NCO; |
| 27 | 27 | ||
| 28 | var cuerpos = data[1].filter((cuerpo) => { | 28 | var cuerpos = data[1].filter((cuerpo) => { |
| 29 | let idCuerpo = cuerpo.TIP + cuerpo.TCO + cuerpo.SUC + cuerpo.NCO; | 29 | let idCuerpo = cuerpo.TIP + cuerpo.TCO + cuerpo.SUC + cuerpo.NCO; |
| 30 | return idCuerpo == idCabecera; | 30 | return idCuerpo == idCabecera; |
| 31 | }); | 31 | }); |
| 32 | 32 | ||
| 33 | result.push({ | 33 | result.push({ |
| 34 | cabecera: cabecera, | 34 | cabecera: cabecera, |
| 35 | cuerpo: cuerpos | 35 | cuerpo: cuerpos |
| 36 | }); | 36 | }); |
| 37 | }); | 37 | }); |
| 38 | 38 | ||
| 39 | res.status(200).send(result); | 39 | res.status(200).send(result); |
| 40 | }); | 40 | }); |
| 41 | }); | 41 | }); |
| 42 | 42 | ||
| 43 | router.post('/comprobante', function(req, res) { | 43 | router.post('/comprobante', function(req, res) { |
| 44 | 44 | ||
| 45 | var recadv = require('../recadv')(JSON.parse(JSON.stringify(req.body.cabecera)), | 45 | var recadv = require('../recadv')(JSON.parse(JSON.stringify(req.body.cabecera)), |
| 46 | JSON.parse(JSON.stringify(req.body.cuerpo))); | 46 | JSON.parse(JSON.stringify(req.body.cuerpo))); |
| 47 | 47 | ||
| 48 | delete req.body.cabecera.idTransaccion; | 48 | var guardar = wsServer.guardarComprobante(req, req.body.cabecera.GLN); |
| 49 | delete req.body.cabecera.GLN; | ||
| 50 | delete req.body.cabecera.C_HD2; | ||
| 51 | delete req.body.cabecera.E_HD2; | ||
| 52 | |||
| 53 | //smalldatetime | ||
| 54 | req.body.cabecera.FEC = new Date(req.body.cabecera.FEC).toISOString().slice(0, 19).replace('T', ' '); | ||
| 55 | req.body.cabecera.FECCAI = new Date(req.body.cabecera.FECCAI).toISOString().slice(0, 19).replace('T', ' '); | ||
| 56 | req.body.cabecera.FECVEN = new Date(req.body.cabecera.FECVEN).toISOString().slice(0, 19).replace('T', ' '); | ||
| 57 | //datetime | ||
| 58 | req.body.cabecera.FEV = moment(req.body.cabecera.FEV).format('YYYYMMDD'); | ||
| 59 | req.body.cabecera.FEP = moment(req.body.cabecera.FEP).format('YYYYMMDD'); | ||
| 60 | |||
| 61 | var promesas = [ | ||
| 62 | recadv, | ||
| 63 | knex('PMAEFACT').insert(req.body.cabecera), | ||
| 64 | knex('PCOBYPAG').insert({ | ||
| 65 | COD: req.body.cabecera.COD, | ||
| 66 | FEP: req.body.cabecera.FEC, | ||
| 67 | TIP: req.body.cabecera.TIP, | ||
| 68 | TCO: req.body.cabecera.TCO, | ||
| 69 | SUC: req.body.cabecera.SUC, | ||
| 70 | NCO: req.body.cabecera.NCO, | ||
| 71 | IPA: req.body.cabecera.TOT, | ||
| 72 | SAL: 0, | ||
| 73 | TCA: 1, | ||
| 74 | ZONA: req.body.cabecera.ZON, | ||
| 75 | FPA: req.body.cabecera.FPA, | ||
| 76 | REC: 0, | ||
| 77 | FER: '19000101 00:00', | ||
| 78 | PRO: '', | ||
| 79 | FEV: req.body.cabecera.FEV, | ||
| 80 | ANU: '', | ||
| 81 | PLA: 0, | ||
| 82 | LUG: 0, | ||
| 83 | RES: 0, | ||
| 84 | CCU: 0, | ||
| 85 | UCU: 0, | ||
| 86 | HOS: '', | ||
| 87 | E_HD: '', | ||
| 88 | C_HD: '' | ||
| 89 | }), | ||
| 90 | ]; | ||
| 91 | |||
| 92 | req.body.cuerpo.forEach(cuerpo => { | ||
| 93 | |||
| 94 | if (cuerpo.estado != 'rechazado' && !(cuerpo.COD == 0 && cuerpo.ART == 0)) { | ||
| 95 | |||
| 96 | promesas.push(knex('AMOVSTOC').insert({ | ||
| 97 | SEC: cuerpo.COD, | ||
| 98 | ART: cuerpo.ART, | ||
| 99 | FEC: req.body.cabecera.FEC, | ||
| 100 | CYV: 'C', | ||
| 101 | TIP: req.body.cabecera.TIP, | ||
| 102 | TCO: req.body.cabecera.TCO, | ||
| 103 | PVE: req.body.cabecera.SUC, | ||
| 104 | NCO: req.body.cabecera.NCO, | ||
| 105 | ORD: cuerpo.ORD, | ||
| 106 | CAN: cuerpo.cantidadRecibida, | ||
| 107 | PUN: cuerpo.PUN, | ||
| 108 | COD: req.body.cabecera.COD, | ||
| 109 | DTO: cuerpo.DTO, | ||
| 110 | IMI: cuerpo.IMI, | ||
| 111 | PLA: 0, | ||
| 112 | LUG: 0, | ||
| 113 | ANU: '', | ||
| 114 | TIM: 'Co', | ||
| 115 | OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario | ||
| 116 | IMI2: cuerpo.IMI2, | ||
| 117 | E_HD: '', | ||
| 118 | C_HD: '', | ||
| 119 | JUS: 'PLANEX', | ||
| 120 | NLC: 0, | ||
| 121 | IMI3: cuerpo.IMI3, | ||
| 122 | JJN: '', | ||
| 123 | JDJ: '', | ||
| 124 | ID_MOTIVO_NANB: 0 | ||
| 125 | })); | ||
| 126 | }; | ||
| 127 | |||
| 128 | cuerpo.estado == 'rechazado' ? cuerpo.cantidadRecibida = 0 : false; | ||
| 129 | |||
| 130 | delete cuerpo.GLN; | ||
| 131 | delete cuerpo.C_HD2; | ||
| 132 | delete cuerpo.E_HD2; | ||
| 133 | delete cuerpo.codigoBarras; | ||
| 134 | delete cuerpo.estado; | ||
| 135 | delete cuerpo.recibido; | ||
| 136 | delete cuerpo.input; | ||
| 137 | delete cuerpo.tempRecibido; | ||
| 138 | |||
| 139 | cuerpo.LI0 = cuerpo.LI0.slice(0, 29); | ||
| 140 | cuerpo.TIO = cuerpo.TIO.slice(0, 29); | ||
| 141 | |||
| 142 | promesas.push(knex('PMOVFACT').insert(cuerpo)); | ||
| 143 | |||
| 144 | }); | ||
| 145 | |||
| 146 | Promise.all(promesas) | ||
| 147 | .then(function() { | ||
| 148 | |||
| 149 | Promise.all([ | ||
| 150 | knex('PMAEFACT_TEMP').where({ | ||
| 151 | TIP: req.body.cabecera.TIP, | ||
| 152 | TCO: req.body.cabecera.TCO, | ||
| 153 | SUC: req.body.cabecera.SUC, | ||
| 154 | NCO: req.body.cabecera.NCO, | ||
| 155 | COD: req.body.cabecera.COD | ||
| 156 | }) | ||
| 157 | .del(), | ||
| 158 | knex('PMOVFACT_TEMP').where({ | ||
| 159 | TIP: req.body.cabecera.TIP, | ||
| 160 | TCO: req.body.cabecera.TCO, | ||
| 161 | SUC: req.body.cabecera.SUC, | ||
| 162 | NCO: req.body.cabecera.NCO | ||
| 163 | }) | ||
| 164 | .del() | ||
| 165 | ]).then(function() { | ||
| 166 | |||
| 167 | console.log('comprobantes guardados con éxito'); | ||
| 168 | res.status(201).send(); | ||
| 169 | }) | ||
| 170 | 49 | ||
| 50 | Promise.all([recadv, guardar]) | ||
| 51 | .then(() => { | ||
| 52 | res.status(200).send('Comprobante guardado con éxito'); | ||
| 171 | }) | 53 | }) |
| 172 | .catch(function(e) { | 54 | .catch(() => res.status(500).send('Algo salió mal')); |
| 173 | console.log(e); | ||
| 174 | res.status(500).send(e); | ||
| 175 | }); |
webSocketServer/index.js
| 1 | module.exports = function() { | 1 | module.exports = function() { |
| 2 | 2 | ||
| 3 | const webSocketServer = require('ws').Server; | 3 | const webSocketServer = require('ws').Server; |
| 4 | var clients = []; | 4 | var clients = []; |
| 5 | 5 | ||
| 6 | const objWs = {}; | 6 | const objWs = {}; |
| 7 | 7 | ||
| 8 | objWs.wsServer = new webSocketServer({ | 8 | objWs.wsServer = new webSocketServer({ |
| 9 | port: config.port | 9 | port: config.port |
| 10 | }); | 10 | }); |
| 11 | 11 | ||
| 12 | objWs.wsServer.on('connection', function connection(ws) { | 12 | objWs.wsServer.on('connection', function connection(ws) { |
| 13 | 13 | ||
| 14 | console.log('open socket server'); | 14 | console.log('open socket server'); |
| 15 | 15 | ||
| 16 | ws.on('message', function incoming(message) { | 16 | ws.on('message', function incoming(message) { |
| 17 | 17 | ||
| 18 | message = JSON.parse(message.toString('utf8')); | 18 | message = JSON.parse(message.toString('utf8')); |
| 19 | 19 | ||
| 20 | switch (message.action) { | 20 | switch (message.action) { |
| 21 | case 'gln': | 21 | case 'gln': |
| 22 | clients.push({ws: ws, gln: message.gln}); | 22 | clients.push({ws: ws, gln: message.gln}); |
| 23 | break; | 23 | break; |
| 24 | default: | 24 | default: |
| 25 | // console.log(message.action); | 25 | // console.log(message.action); |
| 26 | } | 26 | } |
| 27 | }); | 27 | }); |
| 28 | }); | 28 | }); |
| 29 | 29 | ||
| 30 | objWs.getEntidad = function(gln, tableName, where, queryString) { | 30 | objWs.getEntidad = function(gln, tableName, where, queryString) { |
| 31 | 31 | ||
| 32 | return new Promise(function(resolve, reject) { | 32 | return new Promise(function(resolve, reject) { |
| 33 | 33 | ||
| 34 | var client = clients.filter(function(client) { | 34 | var client = clients.filter(function(client) { |
| 35 | return client.gln == gln | 35 | return client.gln == gln |
| 36 | })[0]; | 36 | })[0]; |
| 37 | 37 | ||
| 38 | if (!client) { | 38 | if (!client) { |
| 39 | reject('No se encuentra el webSocket client'); | 39 | reject('No se encuentra el webSocket client'); |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | var idSolicitud = Math.round(Math.random() * 1000); | 42 | var idSolicitud = Math.round(Math.random() * 1000); |
| 43 | 43 | ||
| 44 | var enviar = { | 44 | var enviar = { |
| 45 | action: 'getEntity', | 45 | action: 'getEntity', |
| 46 | idSolicitud: idSolicitud | 46 | idSolicitud: idSolicitud |
| 47 | }; | 47 | }; |
| 48 | 48 | ||
| 49 | if (queryString) { | 49 | if (queryString) { |
| 50 | 50 | ||
| 51 | enviar.queryString = queryString; | 51 | enviar.queryString = queryString; |
| 52 | } else { | 52 | } else { |
| 53 | enviar.tableName = tableName; | 53 | enviar.tableName = tableName; |
| 54 | enviar.where = where || {}; | 54 | enviar.where = where || {}; |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | client.ws.send(JSON.stringify(enviar)); | 57 | client.ws.send(JSON.stringify(enviar)); |
| 58 | 58 | ||
| 59 | client.ws.on('message', function(message) { | 59 | client.ws.on('message', function(message) { |
| 60 | 60 | ||
| 61 | message = JSON.parse(message); | 61 | message = JSON.parse(message); |
| 62 | 62 | ||
| 63 | if (idSolicitud == message.idSolicitud) { | 63 | if (idSolicitud == message.idSolicitud) { |
| 64 | 64 | ||
| 65 | resolve(message.data); | 65 | resolve(message.data); |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | }); | 68 | }); |
| 69 | }); | 69 | }); |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | objWs.getClientGln = function(gln) { | 72 | objWs.getClientGln = function(gln) { |
| 73 | return clients.filter(function(client) { | 73 | return clients.filter(function(client) { |
| 74 | return client.gln = gln; | 74 | return client.gln = gln; |
| 75 | }); | 75 | }); |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | objWs.guardarComprobante = function (cuerpo, gln) { | ||
| 79 | |||
| 80 | return new Promise((resolve, reject) => { | ||
| 81 | |||
| 82 | var client = clients.filter(function(client) { | ||
| 83 | return client.gln == gln | ||
| 84 | })[0]; | ||
| 85 | |||
| 86 | if (!client) { | ||
| 87 | reject('No se encuentra el webSocket client'); | ||
| 88 | } | ||
| 89 | |||
| 90 | var idSolicitud = Math.round(Math.random() * 1000); | ||
| 91 | |||
| 92 | var enviar = { | ||
| 93 | action: 'comprobante', | ||
| 94 | req: cuerpo | ||
| 95 | }; | ||
| 96 | |||
| 97 | client.ws.send(JSON.stringify(enviar)); | ||
| 98 | |||
| 99 | client.ws.on('message', function(message) { | ||
| 100 | |||
| 101 | message = JSON.parse(message); | ||
| 102 | |||
| 103 | if (idSolicitud == message.idSolicitud) { | ||
| 104 | |||
| 105 | resolve(message.data); | ||
| 106 | } | ||
| 107 | |||
| 108 | }); | ||
| 109 | |||
| 110 | }); | ||
| 111 | } | ||
| 112 | |||
| 78 | return objWs; | 113 | return objWs; |
| 79 | } | 114 | } |
| 80 | 115 |