Commit e528eadf15749ca48786bf5c60697078e5b9cc07

Authored by Eric Fernandez
1 parent c2a1db4112
Exists in master

guardo pmae antes de todo

Showing 2 changed files with 94 additions and 89 deletions   Show diff stats
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 const client = new WebSocketClient(config.urlHO); 8 const client = new WebSocketClient(config.urlHO);
9 var pingTimeout; 9 var pingTimeout;
10 var tiempoVivo = new Date(); 10 var tiempoVivo = new Date();
11 11
12 client.on('open', function open() { 12 client.on('open', function open() {
13 console.log('conection to socket ho is open'); 13 console.log('conection to socket ho is open');
14 tiempoVivo = new Date(); 14 tiempoVivo = new Date();
15 15
16 client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); 16 client.send(JSON.stringify({ gln: config.gln, action: 'gln'}));
17 17
18 client.on('message', function incoming(message) { 18 client.on('message', function incoming(message) {
19 19
20 message = JSON.parse(message.toString('utf8')); 20 message = JSON.parse(message.toString('utf8'));
21 21
22 switch (message.action) { 22 switch (message.action) {
23 case 'getEntity': 23 case 'getEntity':
24 24
25 data.getEntity(message).then(function(message) { 25 data.getEntity(message).then(function(message) {
26 26
27 client.send(JSON.stringify(message)); 27 client.send(JSON.stringify(message));
28 }); 28 });
29 break; 29 break;
30 30
31 case 'comprobante': 31 case 'comprobante':
32 console.log('recibiendo comprobante') 32 console.log('recibiendo comprobante')
33 data.guardarComprobante(message.req).then(() => { 33 data.guardarComprobante(message.req).then(() => {
34 console.log('se guardó'); 34 console.log('se guardó');
35 message.ok = 1; 35 message.ok = 1;
36 client.send(JSON.stringify(message)); 36 client.send(JSON.stringify(message));
37 }).catch((ee) => { 37 }).catch((ee) => {
38 console.log('no se guardó' + ee); 38 console.log('no se guardó' + ee);
39 message.ok = 0; 39 message.ok = 0;
40 client.send(JSON.stringify(message)); 40 client.send(JSON.stringify(message));
41 }); 41 });
42 42
43 break; 43 break;
44 44
45 default: 45 default:
46 break; 46 break;
47 } 47 }
48 }); 48 });
49 49
50 heartbeat(); 50 heartbeat();
51 }); 51 });
52 52
53 client.on('error', function(e) { 53 client.on('error', function(e) {
54 console.log(e); 54 console.log(e);
55 }); 55 });
56 56
57 client.on('ping', heartbeat); 57 client.on('ping', heartbeat);
58 58
59 client.on('close', function clear() { 59 client.on('close', function clear() {
60 clearTimeout(pingTimeout); 60 clearTimeout(pingTimeout);
61 }); 61 });
62 62
63 function heartbeat() { 63 function heartbeat() {
64 console.log('recibiendo ping'); 64 console.log('recibiendo ping');
65 console.log(`tiempo vivo desde ${tiempoVivo}`); 65 console.log(`tiempo vivo desde ${tiempoVivo}, ${new Date()}`);
66 66
67 clearTimeout(pingTimeout); 67 clearTimeout(pingTimeout);
68 68
69 // Use `WebSocket#terminate()`, which immediately destroys the connection, 69 // Use `WebSocket#terminate()`, which immediately destroys the connection,
70 // instead of `WebSocket#close()`, which waits for the close timer. 70 // instead of `WebSocket#close()`, which waits for the close timer.
71 // Delay should be equal to the interval at which your server 71 // Delay should be equal to the interval at which your server
72 // sends out pings plus a conservative assumption of the latency. 72 // sends out pings plus a conservative assumption of the latency.
73 pingTimeout = setTimeout(() => { 73 pingTimeout = setTimeout(() => {
74 client.terminate(); 74 client.terminate();
75 }, 10000 + 1000); 75 }, 10000 + 1000);
76 } 76 }
77 console.log('Sevice connected to wsServer: ' + config.urlHO); 77 console.log('Sevice connected to wsServer: ' + config.urlHO);
78 78