Commit b27c39603141354a59353795fdc1d984f0a996ad

Authored by Eric Fernandez
Exists in master

Merge branch 'master' into 'master'

Master(efernandez)

See merge request !6
... ... @@ -0,0 +1,18 @@
  1 +module.exports = function(comprobante) {
  2 +
  3 + var promesas = [];
  4 +
  5 + promesas.push(knex('PMAEFACT_TEMP').insert(comprobante.cabecera));
  6 +
  7 + comprobante.cuerpo.forEach(cuerpo => {
  8 + promesas.push(knex('PMOVFACT_TEMP').insert(cuerpo));
  9 + });
  10 +
  11 + Promise.all(promesas).then(function() {
  12 +
  13 + console.log('COMPROBANTE GUARDADO CON ÉXITO');
  14 + }).catch(function() {
  15 +
  16 + console.log('ERROR AL GUARDAR EL COMPROBANTE');
  17 + });
  18 +}
debo/cabeceraDebo.js
... ... @@ -87,5 +87,6 @@ module.exports = function(comprobante, afipTablas, entities) {
87 87 CABA_PER: 0, // [money] NOT NULL, IMPORTE PERCEPCION CABA
88 88 PERMUN: comprobante.importesTotales.importeImpuestosMunicipales, // [money] NOT NULL, IMPORTE PERCEPCIONES MUNICIPALES
89 89 IMI2: 0, // [money] NOT NULL, FIJO 0
  90 + gln: comprobante.identificaxSucursalReceptorFactura.glnSucursal
90 91 };
91 92 }
... ... @@ -37,5 +37,7 @@ module.exports = function(comprobante, afipTablas, entities, item) {
37 37 COSTO_PMOV: comprobante.itemsFactura.precioUnitario, // [money] NOT NULL, IGUAL AL PUN
38 38 SAL_ITEM: 0, // [money] NOT NULL, fijo 0 es el que se llenara al mostrar en la tablet o telefono para que llene operador
39 39 TASIVA: comprobante.itemsFactura.alicuotaIVAAplicable, // [money] NOT NULL, TASA DE IVA DEL ITEM = 21.00 / 10.50 / ETC.
  40 + gln: comprobante.identificaxSucursalReceptorFactura.glnSucursal, //gln sucursal
  41 + codigoBarras: item.codigoUPCEAN13
40 42 };
41 43 }
... ... @@ -7,7 +7,7 @@ module.exports = function(comprobante, entities) {
7 7 cuerpo: []
8 8 }
9 9  
10   - comprobante.itemsFactura.forEach((item, index) => {
  10 + comprobante.itemsFactura.forEach((item) => {
11 11  
12 12 objReturn.cuerpo.push(require('./cuerpoDebo')(comprobante, afipTablas, entities, item));
13 13 });
... ... @@ -0,0 +1,50 @@
  1 +module.exports = function(planex, wsServer) {
  2 +
  3 + return new Promise(function(resolve, reject) {
  4 +
  5 + var promesasArticulo = [];
  6 +
  7 + var tipoCuit = planex.identificaxEmisor.cuit.slice(0, 2);
  8 + var digitoVerificador = planex.identificaxEmisor.cuit[planex.identificaxEmisor.cuit.length - 1];
  9 + var cuitToDebo = tipoCuit + '-' + planex.identificaxEmisor.cuit.slice(2, 10) + '-' + digitoVerificador;
  10 +
  11 + var promiseProveedores = wsServer.getEntidad(planex.identificaxReceptorFactura.glnReceptor, 'PROVEED', {CUIT: cuitToDebo});
  12 + var promiseEmp = wsServer.getEntidad(planex.identificaxReceptorFactura.glnReceptor, 'APAREMP');
  13 +
  14 + planex.itemsFactura.forEach(item => {
  15 +
  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 = \'' +
  18 + item.codigoUPCEAN13 + '\'';
  19 +
  20 + promesasArticulo.push(
  21 + wsServer.getEntidad(planex.identificaxReceptorFactura.glnReceptor, null, {}, queryString)
  22 + );
  23 + });
  24 +
  25 + promesas = promesasArticulo.concat([promiseProveedores, promiseEmp]);
  26 +
  27 + Promise.all(promesas).then(function(values) {
  28 +
  29 + planex.itemsFactura.forEach((item, index) => {
  30 +
  31 + if (!values[index]) return; console.error('No se encontró el articulo');
  32 +
  33 + item.CodArt = values[index].CodArt;
  34 + item.CodSec = values[index].CodSec;
  35 + item.CodRub = values[index].CodRub;
  36 + item.DET_LAR = values[index].DET_LAR;
  37 +
  38 + delete values[index];
  39 + });
  40 +
  41 + values = values.filter(function(p) {
  42 + return p;
  43 + });
  44 +
  45 + resolve({planex, values});
  46 +
  47 + });
  48 +
  49 + });
  50 +}
... ... @@ -0,0 +1,21 @@
  1 +module.exports = function(debo, wsServer, gln) {
  2 +
  3 + return new Promise(function(resolve, reject) {
  4 +
  5 + wsServer
  6 + .getEntidad(gln, 'PMAEFACT_TEMP', {
  7 + TIP: debo.cabecera.TIP,
  8 + TCO: debo.cabecera.TCO,
  9 + SUC: debo.cabecera.SUC,
  10 + NCO: debo.cabecera.NCO,
  11 + COD: debo.cabecera.COD
  12 + }).then(function(values) {
  13 +
  14 + if (values) {
  15 + reject('Ya existe el comprobante', values);
  16 + } else {
  17 + resolve();
  18 + }
  19 + });
  20 + });
  21 +}
1 1 config = require('./config/config.json');
2   -module.exports = knex = require('knex')(config.db)
3 2  
4   -require('./watch/index')(config.dir, onGetFile);
  3 +module.exports =
  4 + knex = require('knex')(config.db),
  5 + express = require('express');
  6 + app = express();
  7 + wsServer = require('./webSocketServer/index')();
  8 + moment = require('moment');
5 9  
6   -const wsServer = require('./webSocketServer/index')();
  10 +app.listen(config.portWebService);
  11 +app.use(express.json({ limit: '50mb' }));
7 12  
8   -function onGetFile(event, fileString) {
  13 +require('./watch/index')(config.dir, onGetFile);
9 14  
10   - if (!fileString) return;
  15 +app.use(function(req, res, next) {
  16 + res.setHeader('Access-Control-Allow-Origin', '*');
  17 + res.setHeader(
  18 + 'Access-Control-Allow-Headers',
  19 + 'Origin, X-Requested-With, Content-Type, Accept, X-Terminal-Key, X-Nombre-Usuario, X-Punto-Venta'
  20 + );
  21 + res.setHeader('Access-Control-Allow-Methods', 'POST, GET, DELETE, OPTIONS');
  22 + next();
  23 +});
11 24  
12   - var planex = require('./planex/index')(fileString);
  25 +app.use('/gateway-debo', require('./rutas/comprobantes'));
  26 +app.use('/gateway-debo', require('./rutas/login'));
13 27  
14   - console.log(planex);
  28 +var planexArr = [];
15 29  
16   - var entities = {};
  30 +function onGetFile(fileString) {
17 31  
18   - var tipoCuit = planex.identificaxEmisor.cuit.slice(0, 2);
19   - var digitoVerificador = planex.identificaxEmisor.cuit[planex.identificaxEmisor.cuit.length - 1];
  32 + if (!fileString) return;
20 33  
21   - var cuitToDebo = tipoCuit + '-' + planex.identificaxEmisor.cuit.slice(2, 10) + '-' + digitoVerificador;
  34 + var planex = require('./planex/index')(fileString);
22 35  
23   - var promiseProveedores = wsServer.getEntidad('7790968003283', 'PROVEED', {CUIT: cuitToDebo});
24   - var promiseEmp = wsServer.getEntidad('7790968003283', 'APAREMP');
  36 + planexArr.push(planex);
25 37  
26   - var promesasArticulo = [];
  38 + // Traigo proveedor, empresa
  39 + require('./debo/relaciones')(planex, wsServer).then(function(result) {
27 40  
28   - planex.itemsFactura.forEach(item => {
  41 + var planex = result.planex;
29 42  
30   - var queryString = 'select cb.*, art.CodRub, art.DET_LAR from CODBAR cb ' +
31   - 'join ARTICULOS art on cb.CodArt = art.CodArt and cb.CodSec = art.CodSec where cb.CodBar = \'' +
32   - item.codigoUPCEAN13 + '\'';
  43 + var entities = {};
33 44  
34   - promesasArticulo.push(wsServer.getEntidad('7790968003283', null, {}, queryString));
35   - });
  45 + entities.proveedores = result.values[0];
  46 + entities.empresa = result.values[1];
36 47  
37   - Promise.all(promesasArticulo).then(function(values) {
  48 + var debo = require('./debo/index')(planex, entities);
38 49  
39   - planex.itemsFactura.forEach((item, index) => {
  50 + require('./debo/validacion')(debo, wsServer, planex.identificaxSucursalReceptorFactura.glnSucursal).then(function() {
40 51  
41   - if (!values[index]) console.error('No se encontró el articulo');
  52 + require('./db/index')(debo);
  53 + }).catch(function(err) {
42 54  
43   - item.CodArt = values[index].CodArt;
44   - item.CodSec = values[index].CodSec;
45   - item.CodRub = values[index].CodRub;
46   - item.DET_LAR = values[index].DET_LAR;
  55 + console.log(err);
47 56 });
48 57  
49   - Promise.all([promiseProveedores, promiseEmp]).then(function(data) {
50   -
51   - entities.proveedores = data[0];
52   - entities.empresa = data[1];
53   -
54   - var debo = require('./debo/index')(planex, entities);
55   -
56   - require('./db/index')(debo);
57   - });
58 58 });
59 59 }
60 60  
61 61 console.log('listen websocket port ' + config.port);
  62 +console.log('listen webservice port ' + config.portWebService);
62 63 console.log('Ejecutar programa como administrador...');
... ... @@ -4,14 +4,19 @@
4 4 "description": "WebSocket Head Office",
5 5 "main": "index.js",
6 6 "dependencies": {
7   - "express": "^4.16.4",
  7 + "chokidar": "^3.0.1",
  8 + "express": "^4.17.1",
8 9 "knex": "^0.16.5",
  10 + "moment": "^2.24.0",
9 11 "mssql": "^5.1.0",
10 12 "ws": "^7.0.0"
11 13 },
12   - "devDependencies": {},
  14 + "devDependencies": {
  15 + "nodemon": "^1.19.1"
  16 + },
13 17 "scripts": {
14   - "test": "echo \"Error: no test specified\" && exit 1"
  18 + "test": "echo \"Error: no test specified\" && exit 1",
  19 + "dev": "nodemon index.js"
15 20 },
16 21 "author": "Foca Software",
17 22 "license": "ISC"
rutas/comprobantes.js
... ... @@ -0,0 +1,145 @@
  1 +module.exports = router = express.Router();
  2 +
  3 +router.get('/comprobantes/:gln*?', function(req, res) {
  4 +
  5 + var cabecera = knex('PMAEFACT_TEMP')
  6 + .where(function(qb) {
  7 + if (req.params.gln) {
  8 + qb.where({gln: req.params.gln});
  9 + }
  10 + })
  11 + .select('*');
  12 +
  13 + var cuerpo = knex('PMOVFACT_TEMP')
  14 + .where(function(qb) {
  15 + if (req.params.gln) {
  16 + qb.where({gln: req.params.gln});
  17 + }
  18 + })
  19 + .select('*');
  20 +
  21 + Promise.all([cabecera, cuerpo]).then(function(data) {
  22 +
  23 + var result = [];
  24 + data[0].forEach((cabecera) => {
  25 +
  26 + let idCabecera = cabecera.TIP + cabecera.TCO + cabecera.SUC + cabecera.NCO;
  27 +
  28 + var cuerpos = data[1].filter((cuerpo) => {
  29 + let idCuerpo = cuerpo.TIP + cuerpo.TCO + cuerpo.SUC + cuerpo.NCO;
  30 + return idCuerpo == idCabecera
  31 + });
  32 +
  33 + result.push({
  34 + cabecera: cabecera,
  35 + cuerpo: cuerpos
  36 + });
  37 + });
  38 +
  39 + res.status(200).send(result);
  40 + });
  41 +});
  42 +
  43 +router.post('/comprobante', function(req, res) {
  44 +
  45 + console.log(req.body);
  46 + delete req.body.cabecera.gln;
  47 + delete req.body.cabecera.C_HD2;
  48 + delete req.body.cabecera.E_HD2;
  49 +
  50 + //smalldatetime
  51 + req.body.cabecera.FEC = new Date(req.body.cabecera.FEC).toISOString().slice(0, 19).replace('T', ' ');
  52 + req.body.cabecera.FECCAI = new Date(req.body.cabecera.FECCAI).toISOString().slice(0, 19).replace('T', ' ');
  53 + req.body.cabecera.FECVEN = new Date(req.body.cabecera.FECVEN).toISOString().slice(0, 19).replace('T', ' ');
  54 +
  55 + //datetime
  56 + req.body.cabecera.FEV = moment(req.body.cabecera.FEV).format('YYYYMMDD');
  57 + req.body.cabecera.FEP = moment(req.body.cabecera.FEP).format('YYYYMMDD');
  58 +
  59 + var promesas = [
  60 + knex('PMAEFACT').insert(req.body.cabecera),
  61 + knex('PCOBYPAG').insert({
  62 + COD: req.body.cabecera.COD,
  63 + FEP: req.body.cabecera.FEC,
  64 + TIP: req.body.cabecera.TIP,
  65 + TCO: req.body.cabecera.TCO,
  66 + SUC: req.body.cabecera.SUC,
  67 + NCO: req.body.cabecera.NCO,
  68 + IPA: req.body.cabecera.TOT,
  69 + SAL: 0,
  70 + TCA: 1,
  71 + ZONA: req.body.cabecera.ZON,
  72 + FPA: req.body.cabecera.FPA,
  73 + REC: 0,
  74 + FER: '19000101 00:00',
  75 + PRO: '',
  76 + FEV: req.body.cabecera.FEV,
  77 + ANU: '',
  78 + PLA: 0,
  79 + LUG: 0,
  80 + RES: 0,
  81 + CCU: 0,
  82 + UCU: 0,
  83 + HOS: '',
  84 + E_HD: '',
  85 + C_HD: ''
  86 + })
  87 + ];
  88 +
  89 + req.body.cuerpo.forEach(cuerpo => {
  90 +
  91 +
  92 + promesas.push(knex('AMOVSTOC').insert({
  93 + SEC: cuerpo.COD,
  94 + ART: cuerpo.ART,
  95 + FEC: req.body.cabecera.FEC,
  96 + CYV: 'C',
  97 + TIP: req.body.cabecera.TIP,
  98 + TCO: req.body.cabecera.TCO,
  99 + PVE: req.body.cabecera.SUC,
  100 + NCO: req.body.cabecera.NCO,
  101 + ORD: cuerpo.ORD,
  102 + CAN: cuerpo.recibido || cuerpo.CAN,
  103 + PUN: cuerpo.PUN,
  104 + COD: req.body.cabecera.COD,
  105 + DTO: cuerpo.DTO,
  106 + IMI: cuerpo.IMI,
  107 + PLA: 0,
  108 + LUG: 0,
  109 + ANU: '',
  110 + TIM: 'Co',
  111 + OPE: -888, // <= TODO: Cuando se haga el login poner codigo operario
  112 + IMI2: cuerpo.IMI2,
  113 + E_HD: '',
  114 + C_HD: '',
  115 + JUS: 'PLANEX',
  116 + NLC: 0,
  117 + IMI3: cuerpo.IMI3,
  118 + JJN: '',
  119 + JDJ: '',
  120 + ID_MOTIVO_NANB: 0
  121 + }));
  122 +
  123 + delete cuerpo.gln;
  124 + delete cuerpo.C_HD2;
  125 + delete cuerpo.E_HD2;
  126 + delete cuerpo.codigoBarras;
  127 + delete cuerpo.estado;
  128 + delete cuerpo.recibido;
  129 + delete cuerpo.input;
  130 +
  131 + promesas.push(knex('PMOVFACT').insert(cuerpo));
  132 +
  133 + });
  134 +
  135 + Promise.all(promesas)
  136 + .then(function() {
  137 +
  138 + console.log('comprobantes guardados con éxito');
  139 + })
  140 + .catch(function(e) {
  141 + console.log(e);
  142 + });
  143 +
  144 + res.status(201).send('ok');
  145 +});
... ... @@ -0,0 +1,11 @@
  1 +module.exports = router = express.Router();
  2 +
  3 +router.post('/login', function(req, res) {
  4 +
  5 + if (wsServer.getClientGln(req.body.gln).length) {
  6 +
  7 + res.status(200).send({data: 'ok'});
  8 + } else {
  9 + res.status(200).send({data: 'No existe el cliente'});
  10 + }
  11 +});
1   -module.exports = function(dir, callback) {
  1 +module.exports = function (dir, callback) {
2 2  
3 3 const fs = require('fs');
  4 + const chokidar = require('chokidar');
4 5  
5   - fs.watch(dir, function(event, file) {
  6 + const watcher = chokidar.watch(dir);
  7 +
  8 + watcher.on('add', function(file) {
6 9  
7 10 var extencion = file.split('.')[1];
8 11  
9   - if (extencion == 'txt' && event == 'change') {
  12 + if (extencion == 'txt') {
10 13  
11   - fs.readFile(dir + '\\' + file, 'utf8', function(err , fileString) {
  14 + fs.readFile(file, 'utf8', function (err, fileString) {
12 15  
13   - callback(event, fileString);
  16 + callback(fileString);
14 17 });
15 18 }
16 19  
webSocketServer/index.js
1 1 module.exports = function() {
2 2  
3   -const webSocketServer = require('ws').Server;
4   -var clients = [];
  3 + const webSocketServer = require('ws').Server;
  4 + var clients = [];
5 5  
6   -const objWs = {};
  6 + const objWs = {};
7 7  
8   -objWs.wsServer = new webSocketServer({
9   - port: config.port
10   -});
  8 + objWs.wsServer = new webSocketServer({
  9 + port: config.port
  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) {
21   - case 'gln':
22   - clients.push({ws: ws, gln: message.gln});
23   - break;
24   - default:
25   - // console.log(message.action);
26   - }
  20 + switch (message.action) {
  21 + case 'gln':
  22 + clients.push({ws: ws, gln: message.gln});
  23 + break;
  24 + default:
  25 + // console.log(message.action);
  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) {
35   - return client.gln == gln
36   - })[0];
  34 + var client = clients.filter(function(client) {
  35 + return client.gln == gln
  36 + })[0];
  37 +
  38 + if (!client) {
  39 + reject('No se encuentra el webSocket client');
  40 + }
37 41  
38   - if (!client) {
39   - reject('No se encuentra el webSocket client');
40   - }
  42 + var idSolicitud = Math.round(Math.random() * 1000);
41 43  
42   - var idSolicitud = Math.round(Math.random() * 1000);
  44 + var enviar = {
  45 + action: 'getEntity',
  46 + idSolicitud: idSolicitud
  47 + };
43 48  
44   - var enviar = {
45   - action: 'getEntity',
46   - idSolicitud: idSolicitud
47   - };
  49 + if (queryString) {
48 50  
49   - if (queryString) {
  51 + enviar.queryString = queryString;
  52 + } else {
  53 + enviar.tableName = tableName;
  54 + enviar.where = where || {};
  55 + }
50 56  
51   - enviar.queryString = queryString;
52   - } else {
53   - enviar.tableName = tableName;
54   - enviar.where = where || {};
55   - }
  57 + client.ws.send(JSON.stringify(enviar));
56 58  
57   - client.ws.send(JSON.stringify(enviar));
  59 + client.ws.on('message', function(message) {
58 60  
59   - client.ws.on('message', function(message) {
  61 + message = JSON.parse(message);
60 62  
61   - message = JSON.parse(message);
  63 + if (idSolicitud == message.idSolicitud) {
62 64  
63   - if (idSolicitud == message.idSolicitud) {
  65 + resolve(message.data);
  66 + }
64 67  
65   - resolve(message.data);
66   - }
  68 + });
  69 + });
  70 + }
67 71  
  72 + objWs.getClientGln = function(gln) {
  73 + return clients.filter(function(client) {
  74 + return client.gln = gln;
68 75 });
69   - });
70   -}
  76 + }
71 77  
72   -return objWs;
  78 + return objWs;
73 79 }