Commit 37266dcb05218f306dc226df7f6f07de8843fc45

Authored by Eric Fernandez
1 parent da1dfc07cb
Exists in master

+console log

Showing 2 changed files with 8 additions and 5 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');
29 console.log(body);
30
28 return new Promise((resolve, reject) => { 31 return new Promise((resolve, reject) => {
29 32
30 delete body.cabecera.idTransaccion; 33 delete body.cabecera.idTransaccion;
31 delete body.cabecera.GLN; 34 delete body.cabecera.GLN;
32 delete body.cabecera.C_HD2; 35 delete body.cabecera.C_HD2;
33 delete body.cabecera.E_HD2; 36 delete body.cabecera.E_HD2;
34 37
35 //smalldatetime 38 //smalldatetime
36 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', ' ');
37 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', ' ');
38 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', ' ');
39 //datetime 42 //datetime
40 body.cabecera.FEV = moment(body.cabecera.FEV).format('YYYYMMDD'); 43 body.cabecera.FEV = moment(body.cabecera.FEV).format('YYYYMMDD');
41 body.cabecera.FEP = moment(body.cabecera.FEP).format('YYYYMMDD'); 44 body.cabecera.FEP = moment(body.cabecera.FEP).format('YYYYMMDD');
42 45
43 var promesas = [ 46 var promesas = [
44 knex('PMAEFACT').insert(body.cabecera), 47 knex('PMAEFACT').insert(body.cabecera),
45 knex('PCOBYPAG').insert({ 48 knex('PCOBYPAG').insert({
46 COD: body.cabecera.COD, 49 COD: body.cabecera.COD,
47 FEP: body.cabecera.FEC, 50 FEP: body.cabecera.FEC,
48 TIP: body.cabecera.TIP, 51 TIP: body.cabecera.TIP,
49 TCO: body.cabecera.TCO, 52 TCO: body.cabecera.TCO,
50 SUC: body.cabecera.SUC, 53 SUC: body.cabecera.SUC,
51 NCO: body.cabecera.NCO, 54 NCO: body.cabecera.NCO,
52 IPA: body.cabecera.TOT, 55 IPA: body.cabecera.TOT,
53 SAL: 0, 56 SAL: 0,
54 TCA: 1, 57 TCA: 1,
55 ZONA: body.cabecera.ZON, 58 ZONA: body.cabecera.ZON,
56 FPA: body.cabecera.FPA, 59 FPA: body.cabecera.FPA,
57 REC: 0, 60 REC: 0,
58 FER: '19000101 00:00', 61 FER: '19000101 00:00',
59 PRO: '', 62 PRO: '',
60 FEV: body.cabecera.FEV, 63 FEV: body.cabecera.FEV,
61 ANU: '', 64 ANU: '',
62 PLA: 0, 65 PLA: 0,
63 LUG: 0, 66 LUG: 0,
64 RES: 0, 67 RES: 0,
65 CCU: 0, 68 CCU: 0,
66 UCU: 0, 69 UCU: 0,
67 HOS: '', 70 HOS: '',
68 E_HD: '', 71 E_HD: '',
69 C_HD: '' 72 C_HD: ''
70 }), 73 }),
71 ]; 74 ];
72 75
73 body.cuerpo.forEach(cuerpo => { 76 body.cuerpo.forEach(cuerpo => {
74 77
75 if (cuerpo.estado != 'rechazado' && !(cuerpo.COD == 0 && cuerpo.ART == 0)) { 78 if (cuerpo.estado != 'rechazado' && !(cuerpo.COD == 0 && cuerpo.ART == 0)) {
76 79
77 promesas.push(knex('AMOVSTOC').insert({ 80 promesas.push(knex('AMOVSTOC').insert({
78 SEC: cuerpo.COD, 81 SEC: cuerpo.COD,
79 ART: cuerpo.ART, 82 ART: cuerpo.ART,
80 FEC: body.cabecera.FEC, 83 FEC: body.cabecera.FEC,
81 CYV: 'C', 84 CYV: 'C',
82 TIP: body.cabecera.TIP, 85 TIP: body.cabecera.TIP,
83 TCO: body.cabecera.TCO, 86 TCO: body.cabecera.TCO,
84 PVE: body.cabecera.SUC, 87 PVE: body.cabecera.SUC,
85 NCO: body.cabecera.NCO, 88 NCO: body.cabecera.NCO,
86 ORD: cuerpo.ORD, 89 ORD: cuerpo.ORD,
87 CAN: cuerpo.cantidadRecibida, 90 CAN: cuerpo.cantidadRecibida,
88 PUN: cuerpo.PUN, 91 PUN: cuerpo.PUN,
89 COD: body.cabecera.COD, 92 COD: body.cabecera.COD,
90 DTO: cuerpo.DTO, 93 DTO: cuerpo.DTO,
91 IMI: cuerpo.IMI, 94 IMI: cuerpo.IMI,
92 PLA: 0, 95 PLA: 0,
93 LUG: 0, 96 LUG: 0,
94 ANU: '', 97 ANU: '',
95 TIM: 'Co', 98 TIM: 'Co',
96 OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario 99 OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario
97 IMI2: cuerpo.IMI2, 100 IMI2: cuerpo.IMI2,
98 E_HD: '', 101 E_HD: '',
99 C_HD: '', 102 C_HD: '',
100 JUS: 'PLANEX', 103 JUS: 'PLANEX',
101 NLC: 0, 104 NLC: 0,
102 IMI3: cuerpo.IMI3, 105 IMI3: cuerpo.IMI3,
103 JJN: '', 106 JJN: '',
104 JDJ: '', 107 JDJ: '',
105 ID_MOTIVO_NANB: 0 108 ID_MOTIVO_NANB: 0
106 })); 109 }));
107 }; 110 };
108 111
109 cuerpo.estado == 'rechazado' ? cuerpo.cantidadRecibida = 0 : false; 112 cuerpo.estado == 'rechazado' ? cuerpo.cantidadRecibida = 0 : false;
110 113
111 delete cuerpo.GLN; 114 delete cuerpo.GLN;
112 delete cuerpo.C_HD2; 115 delete cuerpo.C_HD2;
113 delete cuerpo.E_HD2; 116 delete cuerpo.E_HD2;
114 delete cuerpo.codigoBarras; 117 delete cuerpo.codigoBarras;
115 delete cuerpo.estado; 118 delete cuerpo.estado;
116 delete cuerpo.recibido; 119 delete cuerpo.recibido;
117 delete cuerpo.input; 120 delete cuerpo.input;
118 delete cuerpo.tempRecibido; 121 delete cuerpo.tempRecibido;
119 122
120 cuerpo.LI0 = cuerpo.LI0.slice(0, 29); 123 cuerpo.LI0 = cuerpo.LI0.slice(0, 29);
121 cuerpo.TIO = cuerpo.TIO.slice(0, 29); 124 cuerpo.TIO = cuerpo.TIO.slice(0, 29);
122 125
123 promesas.push(knex('PMOVFACT').insert(cuerpo)); 126 promesas.push(knex('PMOVFACT').insert(cuerpo));
124 127
125 }); 128 });
126 129
127 Promise.all(promesas) 130 Promise.all(promesas)
128 .then(function() { 131 .then(function() {
129 132
130 console.log('comprobantes guardados con éxito'); 133 console.log('comprobantes guardados con éxito');
131 resolve(); 134 resolve();
132 135
133 }) 136 })
134 .catch(function(e) { 137 .catch(function(e) {
135 console.log(e); 138 console.log(e);
136 reject('Ocurrió un error'); 139 reject('Ocurrió un error');
137 }); 140 });
138 }); 141 });
139 } 142 }
140 } 143 }
141 } 144 }
142 145
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 = knex = require('knex')(config.bo); 4 module.exports = knex = require('knex')(config.bo);
5 5
6 const client = new WebSocketClient(config.urlHO); 6 const client = new WebSocketClient(config.urlHO);
7 7
8 client.on('open', function open() { 8 client.on('open', function open() {
9 console.log('conection to socket ho is open'); 9 console.log('conection to socket ho is open');
10 10
11 client.send(JSON.stringify({ gln: config.gln, action: 'gln'})); 11 client.send(JSON.stringify({ gln: config.gln, action: 'gln'}));
12 12
13 client.on('message', function incoming(message) { 13 client.on('message', function incoming(message) {
14 14
15 message = JSON.parse(message.toString('utf8')); 15 message = JSON.parse(message.toString('utf8'));
16 console.log(message); 16
17 switch (message.action) { 17 switch (message.action) {
18 case 'getEntity': 18 case 'getEntity':
19 19
20 data.getEntity(message).then(function(message) { 20 data.getEntity(message).then(function(message) {
21 21
22 client.send(JSON.stringify(message)); 22 client.send(JSON.stringify(message));
23 }); 23 });
24 break; 24 break;
25 25
26 case 'comprobante': 26 case 'comprobante':
27 console.log('recibiendo comprobante') 27 console.log('recibiendo comprobante')
28 data.guardarComprobante(message.req).then(() => { 28 data.guardarComprobante(message.req).then(() => {
29 console.log('se guardó'); 29 console.log('se guardó');
30 client.send(JSON.stringify('ok')); 30 client.send(JSON.stringify('ok'));
31 }).catch(() => { 31 }).catch((ee) => {
32 console.log('no se guardó'); 32 console.log('no se guardó' + ee);
33 client.send(JSON.stringify('bad')); 33 client.send(JSON.stringify('bad'));
34 }); 34 });
35 35
36 break; 36 break;
37 37
38 default: 38 default:
39 break; 39 break;
40 } 40 }
41 }); 41 });
42 }); 42 });
43 43
44 client.on('error', function(e) { 44 client.on('error', function(e) {
45 console.log(e); 45 console.log(e);
46 }) 46 })
47 47
48 console.log('Sevice connected to wsServer: ' + config.urlHO); 48 console.log('Sevice connected to wsServer: ' + config.urlHO);
49 49