From 5eda16ee29cf853020a1fb92901f204d06259ab7 Mon Sep 17 00:00:00 2001 From: Eric Fernandez Date: Tue, 21 May 2019 18:28:45 -0300 Subject: [PATCH] avances debo --- afip/index.js | 11 +++- index.js | 44 ++++++---------- planex/comprobantesReferencia.js | 2 - planex/detallePercepcionesIIBB.js | 2 - planex/detallesImportesIVA.js | 2 - planex/identificaxComprobante.js | 2 - planex/identificaxEmisor.js | 2 - planex/identificaxReceptorFactura.js | 2 - planex/identificaxSucursalReceptorFactura.js | 2 - planex/importesTotales.js | 2 - planex/index.js | 79 +++++++++++++++++++++------- planex/itemsFactura.js | 2 - watch/index.js | 19 +++++++ webSocketServer/index.js | 58 ++++++++++++++++++++ 14 files changed, 162 insertions(+), 67 deletions(-) create mode 100644 watch/index.js create mode 100644 webSocketServer/index.js diff --git a/afip/index.js b/afip/index.js index 013480a..4dd309e 100644 --- a/afip/index.js +++ b/afip/index.js @@ -33,8 +33,17 @@ module.exports = function(codigo) { } ] - return tablaAfip.filter(function(comprobante) { + var result = tablaAfip.filter(function(comprobante) { return comprobante.codigo == parseInt(codigo) })[0]; + if (!result) { + result = { + letraComprobante: 'CIB', + tipoComprobante: 'CI' + } + } + + return result; + } diff --git a/index.js b/index.js index 9116555..b42f29c 100644 --- a/index.js +++ b/index.js @@ -1,40 +1,28 @@ -const webSocketServer = require('ws').Server; -const fs = require('fs'); -const config = require('./config/config.json'); +config = require('./config/config.json'); -const planex = require('./planex/index')(''); +require('./watch/index')(config.dir, onGetFile); -const debo = require('./debo/index')(planex); +const wsServer = require('./webSocketServer/index')(); -console.log(planex); -console.log(debo); +function onGetFile(event, fileString) { -fs.watch(__dirname, function (event, file) { - console.log(event); - console.log(file); -}); + if (!fileString) return; -const wsServer = new webSocketServer({ - port: config.port -}); + var planex = require('./planex/index')(fileString); -wsServer.on('connection', function connection(ws) { + var entidades = []; - console.log('open socket server'); + wsServer.getEntidad('7790968003283', 'PROVEED').then(function(data) { + entidades.push({ entidad:'proveedores', data: data}) + }); - ws.on('message', function incoming(message) { + console.log(entidades.length); - message = message.toString('utf8'); - console.log(message); + var debo = require('./debo/index')(planex, entidades); - switch (message) { - case message.action == 'gln': - ws.gln = message.gln; - break; - default: - break; - } - }); -}); + // console.log(debo); + +} console.log('listen websocket port ' + config.port); +console.log('Ejecutar programa como administrador...'); diff --git a/planex/comprobantesReferencia.js b/planex/comprobantesReferencia.js index 16cadc3..d8861c5 100644 --- a/planex/comprobantesReferencia.js +++ b/planex/comprobantesReferencia.js @@ -1,6 +1,4 @@ module.exports = function(comprobante) { - - comprobante = '020;RE;0009;00093491;;' var comprobanteArr = comprobante.split(';'); diff --git a/planex/detallePercepcionesIIBB.js b/planex/detallePercepcionesIIBB.js index b3217e8..3fc6553 100644 --- a/planex/detallePercepcionesIIBB.js +++ b/planex/detallePercepcionesIIBB.js @@ -1,6 +1,4 @@ module.exports = function(comprobante, returnFloatByDecimals) { - - comprobante = '070;01;000000000000411;00;000000000000000;PERC .I.B. BS. A.S;00250;000000000016436;00000;000000000000000 '; var comprobanteArr = comprobante.split(';'); diff --git a/planex/detallesImportesIVA.js b/planex/detallesImportesIVA.js index 5a0b786..f90cfc5 100644 --- a/planex/detallesImportesIVA.js +++ b/planex/detallesImportesIVA.js @@ -1,7 +1,5 @@ module.exports = function(comprobante, returnFloatByDecimals) { - comprobante = '060;02100;000000000024349;000000000115946;000000000000000'; - var comprobanteArr = comprobante.split(';'); return { diff --git a/planex/identificaxComprobante.js b/planex/identificaxComprobante.js index c2fce9d..aebd4f1 100644 --- a/planex/identificaxComprobante.js +++ b/planex/identificaxComprobante.js @@ -1,7 +1,5 @@ module.exports = function(comprobante) { - var comprobante = '010;01;00081929;00081929;0002;20120927;20120927;;;14 DIAS;;;32012114736927;20140716;;;;;;;;;;;20120927'; - var comprobanteArr = comprobante.split(';'); return { diff --git a/planex/identificaxEmisor.js b/planex/identificaxEmisor.js index 8c73296..389a171 100644 --- a/planex/identificaxEmisor.js +++ b/planex/identificaxEmisor.js @@ -1,7 +1,5 @@ module.exports = function(comprobante) { - comprobante = '030;EMPRESA EMISORA S.A.;7790522012340;01;902-108324-2;19690727;;Maximo M 885;;;;;;;Lanus Oeste;Buenos Aires;1824;;;30123654736;;;;;;' - comprobanteArr = comprobante.split(';'); return { diff --git a/planex/identificaxReceptorFactura.js b/planex/identificaxReceptorFactura.js index 460586b..da9fb56 100644 --- a/planex/identificaxReceptorFactura.js +++ b/planex/identificaxReceptorFactura.js @@ -1,6 +1,4 @@ module.exports = function(comprobante) { - - comprobante = '040;CADENARECEPTORA;7798032710006;401000;01;;9011890556;;CALLE 401 6TO PISO;;;;;;;Cap. federal;1640;;;80;30123654434;;;;; ' var comprobanteArr = comprobante.split(';'); diff --git a/planex/identificaxSucursalReceptorFactura.js b/planex/identificaxSucursalReceptorFactura.js index 7625a8e..c8daf79 100644 --- a/planex/identificaxSucursalReceptorFactura.js +++ b/planex/identificaxSucursalReceptorFactura.js @@ -1,7 +1,5 @@ module.exports = function(comprobante) { - comprobante = '045;SUCURSAL CADENA RECEPTORA;7791236854;;;;;;;;;;;;;;;;;;80;30123610434;;;;;;; '; - var comprobanteArr = comprobante.split(';'); return { diff --git a/planex/importesTotales.js b/planex/importesTotales.js index 3d83d72..59f6211 100644 --- a/planex/importesTotales.js +++ b/planex/importesTotales.js @@ -1,7 +1,5 @@ module.exports = function(comprobante, returnFloatByDecimals) { - comprobante = '050;000000000000000;000000000000000;000000000000000;000000000140295;000000000000000;000000000115946;000000 000024349;000000000000000;000000000000000;000000000000000;000000000000000;000000000000000;000000000000000;;PES;00001000000;1;000000000000000;;;' - var comprobanteArr = comprobante.split(';'); return { diff --git a/planex/index.js b/planex/index.js index 455c9b2..b592b3a 100644 --- a/planex/index.js +++ b/planex/index.js @@ -1,32 +1,71 @@ module.exports = function(comprobante) { + // split por salto de línea + comprobante = comprobante.split(/\r?\n/); + + var objReturn = { + identificaxComprobante: {}, + identificaxMensaje: {}, + comprobantesReferencia: [], + identificaxEmisor: {}, + informaxRepresentanteEmisor: {}, + identificaxReceptorFactura: {}, + identificaxSucursalReceptorFactura: {}, + importesTotales: {}, + detallesImportesIVA: {}, + detallePercepcionesIIBB: {}, + descuentosGlobalesFactura: {}, + detalleOtrosImpuestosComprobante: {}, + itemsFactura: [], + detalleDescuentosItemFactura: {}, + detalleImpuestosItemFactura: {} + }; function returnFloatByDecimals(parameter, cantDecimal) { if (!parameter) return; - var antesDeLaComa = parseFloat(parameter.slice(0, parameter.length - cantDecimal)); + var beforeSemiColon = parseFloat(parameter.slice(0, parameter.length - cantDecimal)); - var despuesDeLaComa = parameter.slice(parameter.length - cantDecimal, parameter.length); + var afterSemicolon = parameter.slice(parameter.length - cantDecimal, parameter.length); - return parseFloat(antesDeLaComa + '.' + despuesDeLaComa); + return parseFloat(beforeSemiColon + '.' + afterSemicolon); } - return { - identificaxComprobante: require('./identificaxComprobante')(comprobante),// 010 - identificaxMensaje: require('./identificaxMensaje')(comprobante),// 012 - comprobantesReferencia: require('./comprobantesReferencia')(comprobante), // 020 - identificaxEmisor: require('./identificaxEmisor')(comprobante), // 030 - informaxRepresentanteEmisor: require('./informaxRepresentanteEmisor')(comprobante), // 035 - identificaxReceptorFactura: require('./identificaxReceptorFactura')(comprobante), // 040 - identificaxSucursalReceptorFactura: require('./identificaxSucursalReceptorFactura')(comprobante), // 045 - importesTotales: require('./importesTotales')(comprobante, returnFloatByDecimals), // 050 - detallesImportesIVA: require('./detallesImportesIVA')(comprobante, returnFloatByDecimals), // 060 - detallePercepcionesIIBB: require('./detallePercepcionesIIBB')(comprobante, returnFloatByDecimals), // 070 - descuentosGlobalesFactura: require('./descuentosGlobalesFactura')(comprobante, returnFloatByDecimals), // 080 - detalleOtrosImpuestosComprobante: require('./detalleOtrosImpuestosComprobante')(comprobante, returnFloatByDecimals), // 090 - itemsFactura: require('./itemsFactura')(comprobante, returnFloatByDecimals), // 100 - detalleDescuentosItemFactura: require('./detalleDescuentosItemFactura')(comprobante, returnFloatByDecimals), // 110 - detalleImpuestosItemFactura: require('./detalleImpuestosItemFactura')(comprobante, returnFloatByDecimals), // 120 - }; + comprobante.forEach(sector => { + + if (sector.slice(0, 3) == '010') { + objReturn.identificaxComprobante = require('./identificaxComprobante')(sector)// 010 + } else if (sector.slice(0, 3) == '012') { + objReturn.identificaxMensaje = require('./identificaxMensaje')(sector)// 012 + } else if (sector.slice(0, 3) == '020') { + objReturn.comprobantesReferencia.push(require('./comprobantesReferencia')(sector)) // 020 + } else if (sector.slice(0, 3) == '030') { + objReturn.identificaxEmisor = require('./identificaxEmisor')(sector) // 030 + } else if (sector.slice(0, 3) == '035') { + objReturn.informaxRepresentanteEmisor = require('./informaxRepresentanteEmisor')(sector) // 035 + } else if (sector.slice(0, 3) == '040') { + objReturn.identificaxReceptorFactura = require('./identificaxReceptorFactura')(sector) // 040 + } else if (sector.slice(0, 3) == '045') { + objReturn.identificaxSucursalReceptorFactura = require('./identificaxSucursalReceptorFactura')(sector) // 045 + } else if (sector.slice(0, 3) == '050') { + objReturn.importesTotales = require('./importesTotales')(sector, returnFloatByDecimals) // 050 + } else if (sector.slice(0, 3) == '060') { + objReturn.detallesImportesIVA = require('./detallesImportesIVA')(sector, returnFloatByDecimals) // 060 + } else if (sector.slice(0, 3) == '070') { + objReturn.detallePercepcionesIIBB = require('./detallePercepcionesIIBB')(sector, returnFloatByDecimals) // 070 + } else if (sector.slice(0, 3) == '080') { + objReturn.descuentosGlobalesFactura = require('./descuentosGlobalesFactura')(sector, returnFloatByDecimals) // 080 + } else if (sector.slice(0, 3) == '090') { + objReturn.detalleOtrosImpuestosComprobante = require('./detalleOtrosImpuestosComprobante')(sector, returnFloatByDecimals) // 090 + } else if (sector.slice(0, 3) == '100') { + objReturn.itemsFactura.push(require('./itemsFactura')(sector, returnFloatByDecimals)); // 100 + } else if (sector.slice(0, 3) == '110') { + objReturn.detalleDescuentosItemFactura = require('./detalleDescuentosItemFactura')(sector, returnFloatByDecimals); // 110 + } else if (sector.slice(0, 3) == '120') { + objReturn.detalleImpuestosItemFactura = require('./detalleImpuestosItemFactura')(sector, returnFloatByDecimals); // 120 + } + }); + + return objReturn; } diff --git a/planex/itemsFactura.js b/planex/itemsFactura.js index 099b426..d42b253 100644 --- a/planex/itemsFactura.js +++ b/planex/itemsFactura.js @@ -1,7 +1,5 @@ module.exports = function(comprobante, returnFloatByDecimals) { - comprobante = '100;000001;74300;2087455000005;DescripciónArticulo;000000000500000;;000000000035500;02100;0000 00000003452;000000000016437;000000000000000;000000000000000;G; ;000000000000100;000000000000100;CR;CU;000000000032873;7798569874125;; '; - var comprobanteArr = comprobante.split(';'); return { diff --git a/watch/index.js b/watch/index.js new file mode 100644 index 0000000..f37d243 --- /dev/null +++ b/watch/index.js @@ -0,0 +1,19 @@ +module.exports = function(dir, callback) { + + const fs = require('fs'); + + fs.watch(dir, function(event, file) { + + var extencion = file.split('.')[1]; + + if (extencion == 'plx') { + + fs.readFile(dir + '\\' + file, 'utf8', function(err , fileString) { + + callback(event, fileString); + }) + } + + }); + +}; diff --git a/webSocketServer/index.js b/webSocketServer/index.js new file mode 100644 index 0000000..7c96774 --- /dev/null +++ b/webSocketServer/index.js @@ -0,0 +1,58 @@ +module.exports = function() { + +const webSocketServer = require('ws').Server; +var clients = []; + +const objWs = {}; + +objWs.wsServer = new webSocketServer({ + port: config.port +}); + +objWs.wsServer.on('connection', function connection(ws) { + + console.log('open socket server'); + + ws.on('message', function incoming(message) { + + message = JSON.parse(message.toString('utf8')); + + switch (message.action) { + case 'gln': + clients.push({ws: ws, gln: message.gln}); + break; + default: + console.log(message.action); + } + }); +}); + +objWs.getEntidad = function(gln, tableName, where) { + + return new Promise(function(resolve, reject) { + + clients.forEach(client => { + + if (client.gln == gln) { + + var enviar = { + action: 'getEntity', + tableName: tableName, + where: where + } + + client.ws.send(JSON.stringify(enviar)); + + client.ws.on('message', function(message) { + + console.log(JSON.parse(message)); + + resolve(message.data); + }); + } + }); + }); +} + +return objWs; +} \ No newline at end of file -- 1.9.1