Commit 94ee7a762a2e8fd92fa7fdc32bc0f5c79850e552

Authored by Eric Fernandez
Exists in master

Merge branch 'master' into 'master'

Master(efernandez)

See merge request !16
1 module.exports = function(config) { 1 module.exports = function(config) {
2 2
3 return { 3 return {
4 getEntity: function (message) { 4 getEntity: function (message) {
5 5
6 return new Promise(function(resolve, reject) { 6 return new Promise(function(resolve, reject) {
7 7
8 var promise; 8 var promise;
9 9
10 if (message.queryString) { 10 if (message.queryString) {
11 11
12 promise = knex.schema.raw(message.queryString); 12 promise = knex.schema.raw(message.queryString);
13 } else { 13 } else {
14 14
15 promise = knex(message.tableName).where(message.where).select('*'); 15 promise = knex(message.tableName).where(message.where).select('*');
16 } 16 }
17 17
18 promise.then(function (data) { 18 promise.then(function (data) {
19 19
20 message.data = data[0]; 20 message.data = data[0];
21 resolve(message); 21 resolve(message);
22 }); 22 });
23 }); 23 });
24 }, 24 },
25 25
26 guardarComprobante: function (body) { 26 guardarComprobante: function (body) {
27 27
28 console.log('recibo datos guardarComprobante en estación'); 28 console.log('recibo datos guardarComprobante en estación');
29 console.log(body); 29 console.log(body);
30 30
31 return new Promise((resolve, reject) => { 31 return new Promise((resolve, reject) => {
32 32
33 delete body.cabecera.idTransaccion; 33 delete body.cabecera.idTransaccion;
34 delete body.cabecera.GLN; 34 delete body.cabecera.GLN;
35 delete body.cabecera.C_HD2; 35 delete body.cabecera.C_HD2;
36 delete body.cabecera.E_HD2; 36 delete body.cabecera.E_HD2;
37 37
38 //smalldatetime 38 //smalldatetime
39 body.cabecera.FEC = new Date(body.cabecera.FEC).toISOString().slice(0, 19).replace('T', ' '); 39 body.cabecera.FEC = new Date(body.cabecera.FEC).toISOString().slice(0, 19).replace('T', ' ');
40 body.cabecera.FECCAI = new Date(body.cabecera.FECCAI).toISOString().slice(0, 19).replace('T', ' '); 40 body.cabecera.FECCAI = new Date(body.cabecera.FECCAI).toISOString().slice(0, 19).replace('T', ' ');
41 body.cabecera.FECVEN = new Date(body.cabecera.FECVEN).toISOString().slice(0, 19).replace('T', ' '); 41 body.cabecera.FECVEN = new Date(body.cabecera.FECVEN).toISOString().slice(0, 19).replace('T', ' ');
42 //datetime 42 //datetime
43 body.cabecera.FEV = moment(body.cabecera.FEV).format('YYYYMMDD'); 43 body.cabecera.FEV = moment(body.cabecera.FEV).format('YYYYMMDD');
44 body.cabecera.FEP = moment(body.cabecera.FEP).format('YYYYMMDD'); 44 body.cabecera.FEP = moment(body.cabecera.FEP).format('YYYYMMDD');
45 45
46 var promesas = [ 46 knex('PMAEFACT').insert(body.cabecera).then(() => {
47 knex('PMAEFACT').insert(body.cabecera), 47
48 knex('PCOBYPAG').insert({ 48 var promesas = [
49 COD: body.cabecera.COD, 49 knex('PCOBYPAG').insert({
50 FEP: body.cabecera.FEC, 50 COD: body.cabecera.COD,
51 TIP: body.cabecera.TIP, 51 FEP: body.cabecera.FEC,
52 TCO: body.cabecera.TCO,
53 SUC: body.cabecera.SUC,
54 NCO: body.cabecera.NCO,
55 IPA: body.cabecera.TOT,
56 SAL: 0,
57 TCA: 1,
58 ZONA: body.cabecera.ZON,
59 FPA: body.cabecera.FPA,
60 REC: 0,
61 FER: '19000101 00:00',
62 PRO: '',
63 FEV: body.cabecera.FEV,
64 ANU: '',
65 PLA: 0,
66 LUG: 0,
67 RES: 0,
68 CCU: 0,
69 UCU: 0,
70 HOS: '',
71 E_HD: '',
72 C_HD: ''
73 }),
74 ];
75
76 body.cuerpo.forEach(cuerpo => {
77
78 if (cuerpo.estado != 'rechazado' && !(cuerpo.COD == 0 && cuerpo.ART == 0)) {
79
80 promesas.push(knex('AMOVSTOC').insert({
81 SEC: cuerpo.COD,
82 ART: cuerpo.ART,
83 FEC: body.cabecera.FEC,
84 CYV: 'C',
85 TIP: body.cabecera.TIP, 52 TIP: body.cabecera.TIP,
86 TCO: body.cabecera.TCO, 53 TCO: body.cabecera.TCO,
87 PVE: body.cabecera.SUC, 54 SUC: body.cabecera.SUC,
88 NCO: body.cabecera.NCO, 55 NCO: body.cabecera.NCO,
89 ORD: cuerpo.ORD, 56 IPA: body.cabecera.TOT,
90 CAN: cuerpo.cantidadRecibida, 57 SAL: 0,
91 PUN: cuerpo.PUN, 58 TCA: 1,
92 COD: body.cabecera.COD, 59 ZONA: body.cabecera.ZON,
93 DTO: cuerpo.DTO, 60 FPA: body.cabecera.FPA,
94 IMI: cuerpo.IMI, 61 REC: 0,
62 FER: '19000101 00:00',
63 PRO: '',
64 FEV: body.cabecera.FEV,
65 ANU: '',
95 PLA: 0, 66 PLA: 0,
96 LUG: 0, 67 LUG: 0,
97 ANU: '', 68 RES: 0,
98 TIM: 'Co', 69 CCU: 0,
99 OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario 70 UCU: 0,
100 IMI2: cuerpo.IMI2, 71 HOS: '',
101 E_HD: '', 72 E_HD: '',
102 C_HD: '', 73 C_HD: ''
103 JUS: 'PLANEX', 74 }),
104 NLC: 0, 75 ];
105 IMI3: cuerpo.IMI3, 76
106 JJN: '', 77 body.cuerpo.forEach(cuerpo => {
107 JDJ: '', 78
108 ID_MOTIVO_NANB: 0 79 if (cuerpo.estado != 'rechazado' && !(cuerpo.COD == 0 && cuerpo.ART == 0)) {
109 })); 80
110 }; 81 promesas.push(knex('AMOVSTOC').insert({
111 82 SEC: cuerpo.COD,
112 cuerpo.estado == 'rechazado' ? cuerpo.cantidadRecibida = 0 : false; 83 ART: cuerpo.ART,
113 84 FEC: body.cabecera.FEC,
114 delete cuerpo.GLN; 85 CYV: 'C',
115 delete cuerpo.C_HD2; 86 TIP: body.cabecera.TIP,
116 delete cuerpo.E_HD2; 87 TCO: body.cabecera.TCO,
117 delete cuerpo.codigoBarras; 88 PVE: body.cabecera.SUC,
118 delete cuerpo.estado; 89 NCO: body.cabecera.NCO,
119 delete cuerpo.recibido; 90 ORD: cuerpo.ORD,
120 delete cuerpo.input; 91 CAN: cuerpo.cantidadRecibida,
121 delete cuerpo.tempRecibido; 92 PUN: cuerpo.PUN,
122 93 COD: body.cabecera.COD,
123 cuerpo.LI0 = cuerpo.LI0.slice(0, 29); 94 DTO: cuerpo.DTO,
124 cuerpo.TIO = cuerpo.TIO.slice(0, 29); 95 IMI: cuerpo.IMI,
125 96 PLA: 0,
126 promesas.push(knex('PMOVFACT').insert(cuerpo)); 97 LUG: 0,
98 ANU: '',
99 TIM: 'Co',
100 OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario
101 IMI2: cuerpo.IMI2,
102 E_HD: '',
103 C_HD: '',
104 JUS: 'PLANEX',
105 NLC: 0,
106 IMI3: cuerpo.IMI3,
107 JJN: '',
108 JDJ: '',
109 ID_MOTIVO_NANB: 0
110 }));
111 };
112
113 cuerpo.estado == 'rechazado' ? cuerpo.cantidadRecibida = 0 : false;
114
115 delete cuerpo.GLN;
116 delete cuerpo.C_HD2;
117 delete cuerpo.E_HD2;
1 const WebSocketClient = require('ws'); 1 const WebSocketClient = require('ws');
2 const config = require('./config/config.json'); 2 const config = require('./config/config.json');
3 const data = require('./data/index')(config.bo); 3 const data = require('./data/index')(config.bo);
4 module.exports = 4 module.exports =
5 knex = require('knex')(config.bo), 5 knex = require('knex')(config.bo),
6 moment = require('moment'); 6 moment = require('moment');
7 7
8
9 const client = new WebSocketClient(config.urlHO); 8 const client = new WebSocketClient(config.urlHO);
9 var pingTimeout;
10 var tiempoVivo = new Date();
10 11
11 client.on('open', function open() { 12 client.on('open', function open() {
12 console.log('conection to socket ho is open'); 13 console.log('conection to socket ho is open');
14 tiempoVivo = new Date();
13 15
14 client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); 16 client.send(JSON.stringify({ gln: config.gln, action: 'gln'}));
15 17
16 client.on('message', function incoming(message) { 18 client.on('message', function incoming(message) {
17 19
18 message = JSON.parse(message.toString('utf8')); 20 message = JSON.parse(message.toString('utf8'));
19 21
20 switch (message.action) { 22 switch (message.action) {
21 case 'getEntity': 23 case 'getEntity':
22 24
23 data.getEntity(message).then(function(message) { 25 data.getEntity(message).then(function(message) {
24 26
25 client.send(JSON.stringify(message)); 27 client.send(JSON.stringify(message));
26 }); 28 });
27 break; 29 break;
28 30
29 case 'comprobante': 31 case 'comprobante':
30 console.log('recibiendo comprobante') 32 console.log('recibiendo comprobante')
31 data.guardarComprobante(message.req).then(() => { 33 data.guardarComprobante(message.req).then(() => {
32 console.log('se guardó'); 34 console.log('se guardó');
33 message.ok = 1; 35 message.ok = 1;
34 client.send(JSON.stringify(message)); 36 client.send(JSON.stringify(message));
35 }).catch((ee) => { 37 }).catch((ee) => {
36 console.log('no se guardó' + ee); 38 console.log('no se guardó' + ee);
37 message.ok = 0; 39 message.ok = 0;
38 client.send(JSON.stringify(message)); 40 client.send(JSON.stringify(message));
39 }); 41 });
40 42
41 break; 43 break;
42 44
43 default: 45 default:
44 break; 46 break;
45 } 47 }
46 }); 48 });
49
50 heartbeat();
47 }); 51 });
48 52
49 client.on('error', function(e) { 53 client.on('error', function(e) {
50 console.log(e); 54 console.log(e);
51 }) 55 });
56
57 client.on('ping', heartbeat);
58
59 client.on('close', function clear() {
60 clearTimeout(pingTimeout);
61 });
62
63 function heartbeat() {
64 console.log('recibiendo ping');
65 console.log(`tiempo vivo desde ${tiempoVivo}, ${new Date()}`);
66
67 clearTimeout(pingTimeout);
52 68
69 // Use `WebSocket#terminate()`, which immediately destroys the connection,
70 // instead of `WebSocket#close()`, which waits for the close timer.
71 // Delay should be equal to the interval at which your server
72 // sends out pings plus a conservative assumption of the latency.
73 pingTimeout = setTimeout(() => {
74 client.terminate();
75 }, 10000 + 1000);
76 }
53 console.log('Sevice connected to wsServer: ' + config.urlHO); 77 console.log('Sevice connected to wsServer: ' + config.urlHO);
1 { 1 {
2 "name": "websocketbo", 2 "name": "websocketbo",
3 "version": "0.0.1", 3 "version": "0.0.1",
4 "description": "WebSocket Back Office", 4 "description": "WebSocket Back Office",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1" 7 "test": "echo \"Error: no test specified\" && exit 1"
8 }, 8 },
9 "author": "Foca Software", 9 "author": "Foca Software",
10 "license": "ISC", 10 "license": "ISC",
11 "dependencies": { 11 "dependencies": {
12 "express": "^4.16.4", 12 "express": "^4.16.4",
13 "knex": "^0.16.5", 13 "knex": "^0.16.5",
14 "moment": "^2.24.0",
14 "mssql": "^5.1.0", 15 "mssql": "^5.1.0",
15 "ws": "^7.0.0" 16 "ws": "^7.0.0"
16 } 17 }
17 } 18 }
18 19