Commit b36cbe778b4d997612fb830487cacc69c904fb3f

Authored by Eric Fernandez
1 parent 14139f376a
Exists in master

fix cantidad recibida, gln de sucursal

1 module.exports = function(comprobante, nameFile) { 1 module.exports = function(comprobante, nameFile) {
2 2
3 var nameFileDest = nameFile.split('\\')[nameFile.split('\\').length - 1]; 3 var nameFileDest = nameFile.split('\\')[nameFile.split('\\').length - 1];
4 4
5 knex('planex_transacciones') 5 knex('planex_transacciones')
6 .insert({ 6 .insert({
7 archivoRecibido: nameFileDest, 7 archivoRecibido: nameFileDest,
8 estado: 0, // Ingresada 8 estado: 0, // Ingresada
9 GLN: comprobante.cabecera.GLN 9 GLN: comprobante.cabecera.GLN
10 }) 10 })
11 .returning('id') 11 .returning('id')
12 .then(saveDebo); 12 .then(saveDebo);
13 13
14 function saveDebo(id) { 14 function saveDebo(id) {
15 15
16 comprobante.cabecera.idTransaccion = id; 16 comprobante.cabecera.idTransaccion = id;
17 var promesas = [knex('PMAEFACT_TEMP').insert(comprobante.cabecera)]; 17 var promesas = [knex('PMAEFACT_TEMP').insert(comprobante.cabecera)];
18 18
19 comprobante.cuerpo.forEach(cuerpo => { 19 comprobante.cuerpo.forEach(cuerpo => {
20 promesas.push(knex('PMOVFACT_TEMP').insert(cuerpo)); 20 promesas.push(knex('PMOVFACT_TEMP').insert(cuerpo));
21 }); 21 });
22 22
23 Promise.all(promesas).then(function() { 23 Promise.all(promesas).then(function() {
24 24
25 var pathDest = config.dirDestino + '/' + nameFileDest; 25 var pathDest = config.dirDestino + '/' + nameFileDest;
26 26
27 fs.rename(nameFile, pathDest, function(err) { 27 fs.rename(nameFile, pathDest, function(err) {
28 28
29 if(err) console.log(err); 29 if(err) console.log(err);
30 console.log('COMPROBANTE GUARDADO CON ÉXITO'); 30 console.log('COMPROBANTE PROCESADO Y GUARDADO EN PMAEFACT_TEMP CON ÉXITO');
31 }); 31 });
32 }).catch(function(e) { 32 }).catch(function(e) {
33 console.log(e); 33 console.log(e);
34 console.log('ERROR AL GUARDAR EL COMPROBANTE'); 34 console.log('ERROR AL GUARDAR EL COMPROBANTE');
35 }); 35 });
36 } 36 }
37 } 37 }
38 38
1 module.exports = function(planex, wsServer) { 1 module.exports = function(planex, wsServer) {
2 2
3 return new Promise(function(resolve, reject) { 3 return new Promise(function(resolve, reject) {
4 4
5 var promesasArticulo = []; 5 var promesasArticulo = [];
6 6
7 var tipoCuit = planex.identificaxEmisor.cuit.slice(0, 2); 7 var tipoCuit = planex.identificaxEmisor.cuit.slice(0, 2);
8 var digitoVerificador = planex.identificaxEmisor.cuit[planex.identificaxEmisor.cuit.length - 1]; 8 var digitoVerificador = planex.identificaxEmisor.cuit[planex.identificaxEmisor.cuit.length - 1];
9 var cuitToDebo = tipoCuit + '-' + planex.identificaxEmisor.cuit.slice(2, 10) + '-' + digitoVerificador; 9 var cuitToDebo = tipoCuit + '-' + planex.identificaxEmisor.cuit.slice(2, 10) + '-' + digitoVerificador;
10 10
11 var promiseProveedores = wsServer.getEntidad(planex.identificaxReceptorFactura.glnReceptor, 'PROVEED', {CUIT: cuitToDebo}); 11 var promiseProveedores = wsServer.getEntidad(planex.identificaxSucursalReceptorFactura.glnSucursal, 'PROVEED', {CUIT: cuitToDebo});
12 var promiseEmp = wsServer.getEntidad(planex.identificaxReceptorFactura.glnReceptor, 'APAREMP'); 12 var promiseEmp = wsServer.getEntidad(planex.identificaxSucursalReceptorFactura.glnSucursal, 'APAREMP');
13 13
14 planex.itemsFactura.forEach(item => { 14 planex.itemsFactura.forEach(item => {
15 15
16 var queryString = 'select cb.*, art.CodRub, art.DET_LAR from CODBAR cb ' + 16 var queryString = 'select cb.*, art.CodRub, art.DET_LAR from CODBAR cb ' +
17 'join ARTICULOS art on cb.CodArt = art.CodArt and cb.CodSec = art.CodSec where cb.CodBar = \'' + 17 'join ARTICULOS art on cb.CodArt = art.CodArt and cb.CodSec = art.CodSec where cb.CodBar = \'' +
18 item.codigoUPCEAN13 + '\''; 18 item.codigoUPCEAN13 + '\'';
19 19
20 promesasArticulo.push( 20 promesasArticulo.push(
21 wsServer.getEntidad(planex.identificaxReceptorFactura.glnReceptor, null, {}, queryString) 21 wsServer.getEntidad(planex.identificaxSucursalReceptorFactura.glnSucursal, null, {}, queryString)
22 ); 22 );
23 }); 23 });
24 24
25 promesas = promesasArticulo.concat([promiseProveedores, promiseEmp]); 25 promesas = promesasArticulo.concat([promiseProveedores, promiseEmp]);
26 26
27 Promise.all(promesas).then(function(values) { 27 Promise.all(promesas).then(function(values) {
28 28
29 planex.itemsFactura.forEach((item, index) => { 29 planex.itemsFactura.forEach((item, index) => {
30 30
31 if (!values[index]) { 31 if (!values[index]) {
32 32
33 console.error('No se encontró el articulo Código de barras = ' + item.codigoUPCEAN13); 33 console.error('No se encontró el articulo Código de barras = ' + item.codigoUPCEAN13);
34 34
35 } 35 }
36 36
37 item.CodArt = values[index] ? values[index].CodArt : 0; 37 item.CodArt = values[index] ? values[index].CodArt : 0;
38 item.CodSec = values[index] ? values[index].CodSec : 0; 38 item.CodSec = values[index] ? values[index].CodSec : 0;
39 item.CodRub = values[index] ? values[index].CodRub : 0; 39 item.CodRub = values[index] ? values[index].CodRub : 0;
40 item.DET_LAR = values[index] ? values[index].DET_LAR : item.descripcion; 40 item.DET_LAR = values[index] ? values[index].DET_LAR : item.descripcion;
41 41
42 delete values[index]; 42 delete values[index];
43 }); 43 });
44 44
45 values = values.filter(function(p) { 45 values = values.filter(function(p) {
46 return p; 46 return p;
47 }); 47 });
48 48
49 resolve({planex, values}); 49 resolve({planex, values});
50 50
51 }).catch(function(e) { 51 }).catch(reject);
52 reject(e);
53 });
54 52
55 }); 53 });
56 } 54 }
57 55
1 module.exports = 1 module.exports =
2 config = require('./config/config.json'); 2 config = require('./config/config.json');
3 knex = require('knex')(config.db), 3 knex = require('knex')(config.db),
4 express = require('express'); 4 express = require('express');
5 app = express(); 5 app = express();
6 wsServer = require('./webSocketServer/index')(); 6 wsServer = require('./webSocketServer/index')();
7 moment = require('moment'); 7 moment = require('moment');
8 fs = require('fs'); 8 fs = require('fs');
9 9
10 10
11 app.listen(config.portWebService); 11 app.listen(config.portWebService);
12 app.use(express.json({ limit: '50mb' })); 12 app.use(express.json({ limit: '50mb' }));
13 13
14 require('./watch/index')(config.dir, onGetFile); 14 require('./watch/index')(config.dir, onGetFile);
15 15
16 app.use(function(req, res, next) { 16 app.use(function(req, res, next) {
17 res.setHeader('Access-Control-Allow-Origin', '*'); 17 res.setHeader('Access-Control-Allow-Origin', '*');
18 res.setHeader( 18 res.setHeader(
19 'Access-Control-Allow-Headers', 19 'Access-Control-Allow-Headers',
20 'Origin, X-Requested-With, Content-Type, Accept, X-Terminal-Key, X-Nombre-Usuario, X-Punto-Venta' 20 'Origin, X-Requested-With, Content-Type, Accept, X-Terminal-Key, X-Nombre-Usuario, X-Punto-Venta'
21 ); 21 );
22 res.setHeader('Access-Control-Allow-Methods', 'POST, GET, DELETE, OPTIONS'); 22 res.setHeader('Access-Control-Allow-Methods', 'POST, GET, DELETE, OPTIONS');
23 next(); 23 next();
24 }); 24 });
25 25
26 app.use('/gateway-debo', require('./rutas/comprobantes')); 26 app.use('/gateway-debo', require('./rutas/comprobantes'));
27 app.use('/gateway-debo', require('./rutas/login')); 27 app.use('/gateway-debo', require('./rutas/login'));
28 28
29 function onGetFile(fileString, nameFile) { 29 function onGetFile(fileString, nameFile) {
30 30
31 if (!fileString) { 31 if (!fileString) {
32 console.log(`no se pueden los datos del archivo ${nameFile}`); 32 console.log(`no se pueden los datos del archivo ${nameFile}`);
33 }; 33 };
34 34
35 require('./debo/validacion')(nameFile).then(function() { 35 require('./debo/validacion')(nameFile).then(function() {
36 36
37 console.log(`se valida que no exista... ${nameFile}`); 37 console.log(`se valida que no exista... ${nameFile}`);
38 38
39 var planex = require('./planex/index')(fileString); 39 var planex = require('./planex/index')(fileString);
40 40
41 console.log(`obtengo objeto planex de ${nameFile}`); 41 console.log(`obtengo objeto planex de ${nameFile}`);
42 // Traigo proveedor, empresa 42 // Traigo proveedor, empresa
43 require('./debo/relaciones')(planex, wsServer).then(function(result) { 43 require('./debo/relaciones')(planex, wsServer).then(function(result) {
44 44
45 console.log(`obtengo relaciones debo ${nameFile}`); 45 console.log(`obtengo relaciones debo ${nameFile}`);
46 var planex = result.planex; 46 var planex = result.planex;
47 47
48 var entities = {}; 48 var entities = {};
49 49
50 entities.proveedores = result.values[0]; 50 entities.proveedores = result.values[0];
51 entities.empresa = result.values[1]; 51 entities.empresa = result.values[1];
52 52
53 var debo = require('./debo/index')(planex, entities); 53 var debo = require('./debo/index')(planex, entities);
54 54
55 require('./db/index')(debo, nameFile); 55 require('./db/index')(debo, nameFile);
56 56
57 }).catch(function(e) { 57 }).catch(function(e) {
58
58 console.log(e); 59 console.log(e);
59 }); 60 });
60 }).catch(function(err) { 61 }).catch(function(err) {
61 62
62 console.log(err); 63 console.log(err);
63 }); 64 });
64 } 65 }
65 66
66 console.log('listen websocket port ' + config.port); 67 console.log('listen websocket port ' + config.port);
67 console.log('listen webservice port ' + config.portWebService); 68 console.log('listen webservice port ' + config.portWebService);
68 console.log('Ejecutar programa como administrador...'); 69 console.log('Ejecutar programa como administrador...');
69 70
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 delete req.body.cabecera.idTransaccion;
49 delete req.body.cabecera.GLN; 49 delete req.body.cabecera.GLN;
50 delete req.body.cabecera.C_HD2; 50 delete req.body.cabecera.C_HD2;
51 delete req.body.cabecera.E_HD2; 51 delete req.body.cabecera.E_HD2;
52 52
53 //smalldatetime 53 //smalldatetime
54 req.body.cabecera.FEC = new Date(req.body.cabecera.FEC).toISOString().slice(0, 19).replace('T', ' '); 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', ' '); 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', ' '); 56 req.body.cabecera.FECVEN = new Date(req.body.cabecera.FECVEN).toISOString().slice(0, 19).replace('T', ' ');
57 //datetime 57 //datetime
58 req.body.cabecera.FEV = moment(req.body.cabecera.FEV).format('YYYYMMDD'); 58 req.body.cabecera.FEV = moment(req.body.cabecera.FEV).format('YYYYMMDD');
59 req.body.cabecera.FEP = moment(req.body.cabecera.FEP).format('YYYYMMDD'); 59 req.body.cabecera.FEP = moment(req.body.cabecera.FEP).format('YYYYMMDD');
60 60
61 var promesas = [ 61 var promesas = [
62 recadv, 62 recadv,
63 knex('PMAEFACT').insert(req.body.cabecera), 63 knex('PMAEFACT').insert(req.body.cabecera),
64 knex('PCOBYPAG').insert({ 64 knex('PCOBYPAG').insert({
65 COD: req.body.cabecera.COD, 65 COD: req.body.cabecera.COD,
66 FEP: req.body.cabecera.FEC, 66 FEP: req.body.cabecera.FEC,
67 TIP: req.body.cabecera.TIP, 67 TIP: req.body.cabecera.TIP,
68 TCO: req.body.cabecera.TCO, 68 TCO: req.body.cabecera.TCO,
69 SUC: req.body.cabecera.SUC, 69 SUC: req.body.cabecera.SUC,
70 NCO: req.body.cabecera.NCO, 70 NCO: req.body.cabecera.NCO,
71 IPA: req.body.cabecera.TOT, 71 IPA: req.body.cabecera.TOT,
72 SAL: 0, 72 SAL: 0,
73 TCA: 1, 73 TCA: 1,
74 ZONA: req.body.cabecera.ZON, 74 ZONA: req.body.cabecera.ZON,
75 FPA: req.body.cabecera.FPA, 75 FPA: req.body.cabecera.FPA,
76 REC: 0, 76 REC: 0,
77 FER: '19000101 00:00', 77 FER: '19000101 00:00',
78 PRO: '', 78 PRO: '',
79 FEV: req.body.cabecera.FEV, 79 FEV: req.body.cabecera.FEV,
80 ANU: '', 80 ANU: '',
81 PLA: 0, 81 PLA: 0,
82 LUG: 0, 82 LUG: 0,
83 RES: 0, 83 RES: 0,
84 CCU: 0, 84 CCU: 0,
85 UCU: 0, 85 UCU: 0,
86 HOS: '', 86 HOS: '',
87 E_HD: '', 87 E_HD: '',
88 C_HD: '' 88 C_HD: ''
89 }), 89 }),
90 ]; 90 ];
91 91
92 req.body.cuerpo.forEach(cuerpo => { 92 req.body.cuerpo.forEach(cuerpo => {
93 93
94 if (cuerpo.estado != 'rechazado' && !(cuerpo.COD == 0 && cuerpo.ART == 0)) { 94 if (cuerpo.estado != 'rechazado' && !(cuerpo.COD == 0 && cuerpo.ART == 0)) {
95 95
96 promesas.push(knex('AMOVSTOC').insert({ 96 promesas.push(knex('AMOVSTOC').insert({
97 SEC: cuerpo.COD, 97 SEC: cuerpo.COD,
98 ART: cuerpo.ART, 98 ART: cuerpo.ART,
99 FEC: req.body.cabecera.FEC, 99 FEC: req.body.cabecera.FEC,
100 CYV: 'C', 100 CYV: 'C',
101 TIP: req.body.cabecera.TIP, 101 TIP: req.body.cabecera.TIP,
102 TCO: req.body.cabecera.TCO, 102 TCO: req.body.cabecera.TCO,
103 PVE: req.body.cabecera.SUC, 103 PVE: req.body.cabecera.SUC,
104 NCO: req.body.cabecera.NCO, 104 NCO: req.body.cabecera.NCO,
105 ORD: cuerpo.ORD, 105 ORD: cuerpo.ORD,
106 CAN: cuerpo.cantidadRecibida, 106 CAN: cuerpo.cantidadRecibida,
107 PUN: cuerpo.PUN, 107 PUN: cuerpo.PUN,
108 COD: req.body.cabecera.COD, 108 COD: req.body.cabecera.COD,
109 DTO: cuerpo.DTO, 109 DTO: cuerpo.DTO,
110 IMI: cuerpo.IMI, 110 IMI: cuerpo.IMI,
111 PLA: 0, 111 PLA: 0,
112 LUG: 0, 112 LUG: 0,
113 ANU: '', 113 ANU: '',
114 TIM: 'Co', 114 TIM: 'Co',
115 OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario 115 OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario
116 IMI2: cuerpo.IMI2, 116 IMI2: cuerpo.IMI2,
117 E_HD: '', 117 E_HD: '',
118 C_HD: '', 118 C_HD: '',
119 JUS: 'PLANEX', 119 JUS: 'PLANEX',
120 NLC: 0, 120 NLC: 0,
121 IMI3: cuerpo.IMI3, 121 IMI3: cuerpo.IMI3,
122 JJN: '', 122 JJN: '',
123 JDJ: '', 123 JDJ: '',
124 ID_MOTIVO_NANB: 0 124 ID_MOTIVO_NANB: 0
125 })); 125 }));
126 }; 126 };
127 127
128 cuerpo.estado == 'rechazado' ? cuerpo.CAN = 0 : false; 128 cuerpo.estado == 'rechazado' ? cuerpo.cantidadRecibida = 0 : false;
129 129
130 delete cuerpo.GLN; 130 delete cuerpo.GLN;
131 delete cuerpo.C_HD2; 131 delete cuerpo.C_HD2;
132 delete cuerpo.E_HD2; 132 delete cuerpo.E_HD2;
133 delete cuerpo.codigoBarras; 133 delete cuerpo.codigoBarras;
134 delete cuerpo.estado; 134 delete cuerpo.estado;
135 delete cuerpo.recibido; 135 delete cuerpo.recibido;
136 delete cuerpo.input; 136 delete cuerpo.input;
137 delete cuerpo.tempRecibido; 137 delete cuerpo.tempRecibido;
138 138
139 cuerpo.LI0 = cuerpo.LI0.slice(0, 29); 139 cuerpo.LI0 = cuerpo.LI0.slice(0, 29);
140 cuerpo.TIO = cuerpo.TIO.slice(0, 29); 140 cuerpo.TIO = cuerpo.TIO.slice(0, 29);
141 141
142 promesas.push(knex('PMOVFACT').insert(cuerpo)); 142 promesas.push(knex('PMOVFACT').insert(cuerpo));
143 143
144 }); 144 });
145 145
146 Promise.all(promesas) 146 Promise.all(promesas)
147 .then(function() { 147 .then(function() {
148 148
149 Promise.all([ 149 Promise.all([
150 knex('PMAEFACT_TEMP').where({ 150 knex('PMAEFACT_TEMP').where({
151 TIP: req.body.cabecera.TIP, 151 TIP: req.body.cabecera.TIP,
152 TCO: req.body.cabecera.TCO, 152 TCO: req.body.cabecera.TCO,
153 SUC: req.body.cabecera.SUC, 153 SUC: req.body.cabecera.SUC,
154 NCO: req.body.cabecera.NCO, 154 NCO: req.body.cabecera.NCO,
155 COD: req.body.cabecera.COD 155 COD: req.body.cabecera.COD
156 }) 156 })
157 .del(), 157 .del(),
158 knex('PMOVFACT_TEMP').where({ 158 knex('PMOVFACT_TEMP').where({
159 TIP: req.body.cabecera.TIP, 159 TIP: req.body.cabecera.TIP,
160 TCO: req.body.cabecera.TCO, 160 TCO: req.body.cabecera.TCO,
161 SUC: req.body.cabecera.SUC, 161 SUC: req.body.cabecera.SUC,
162 NCO: req.body.cabecera.NCO 162 NCO: req.body.cabecera.NCO
163 }) 163 })
164 .del() 164 .del()
165 ]).then(function() { 165 ]).then(function() {
166 166
167 console.log('comprobantes guardados con éxito'); 167 console.log('comprobantes guardados con éxito');
168 res.status(201).send(); 168 res.status(201).send();
169 }) 169 })
170 170
171 }) 171 })
172 .catch(function(e) { 172 .catch(function(e) {
173 console.log(e); 173 console.log(e);
174 res.status(500).send(e); 174 res.status(500).send(e);
175 }); 175 });
176 176
177 }); 177 });
178 178