Commit f410bbf1ed8ec632191f887ece26e5fba8e9d937
Exists in
master
and in
2 other branches
Merge branch 'master' into 'master'
Master(mpuebla) See merge request !1
Showing
3 changed files
Show diff stats
index.js
| 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 | router = express.Router(); | 5 | router = express.Router(); |
| 6 | 6 | ||
| 7 | var app = express(); | 7 | var app = express(); |
| 8 | 8 | ||
| 9 | app.use(express.json({limit: '550mb'})); | 9 | app.use(express.json({limit: '550mb'})); |
| 10 | app.use((req, res, next) => { | 10 | app.use((req, res, next) => { |
| 11 | res.setHeader('Access-Control-Allow-Origin', '*'); | 11 | res.setHeader('Access-Control-Allow-Origin', '*'); |
| 12 | res.setHeader( | 12 | res.setHeader( |
| 13 | 'Access-Control-Allow-Headers', | 13 | 'Access-Control-Allow-Headers', |
| 14 | 'Origin, X-Requested-With, Content-Type, Accept, X-Terminal-Key, X-Nombre-Usuario, X-Punto-Venta' | 14 | 'Origin, X-Requested-With, Content-Type, Accept, X-Terminal-Key, X-Nombre-Usuario, X-Punto-Venta' |
| 15 | ); | 15 | ); |
| 16 | res.setHeader('Access-Control-Allow-Methods', 'POST, GET, DELETE, OPTIONS'); | 16 | res.setHeader('Access-Control-Allow-Methods', 'POST, GET, DELETE, OPTIONS'); |
| 17 | next(); | 17 | next(); |
| 18 | }); | 18 | }); |
| 19 | 19 | ||
| 20 | app.listen(config.port, '0.0.0.0', () => { | 20 | app.listen(config.port, '0.0.0.0', () => { |
| 21 | console.log(`Escuchando puerto ${config.port}`); | 21 | console.log(`Escuchando puerto ${config.port}`); |
| 22 | }); | 22 | }); |
| 23 | 23 | ||
| 24 | app.use('/', [ | 24 | app.use('/', [ |
| 25 | require('./rutas/articulos'), | 25 | require('./rutas/articulos'), |
| 26 | require('./rutas/imagenes.js'), | 26 | require('./rutas/imagenes'), |
| 27 | ]); | 27 | ]); |
| 28 | 28 |
rutas/articulos.js
| 1 | 1 | ||
| 2 | var bookshelf = require('bookshelf')(knex); | 2 | var bookshelf = require('bookshelf')(knex); |
| 3 | bookshelf.plugin('pagination'); | ||
| 3 | 4 | ||
| 4 | const ArticuloImagen = bookshelf.Model.extend({ | 5 | const ArticuloImagen = bookshelf.Model.extend({ |
| 5 | tableName: 'ARTICULOS_IMAGEN' | 6 | tableName: 'ARTICULOS_IMAGEN' |
| 6 | }); | 7 | }); |
| 7 | 8 | ||
| 8 | const Articulo = bookshelf.Model.extend({ | 9 | const Articulo = bookshelf.Model.extend({ |
| 9 | tableName: 'ARTICULOS', | 10 | tableName: 'ARTICULOS', |
| 10 | imagenes: function() { | 11 | imagenes: function () { |
| 11 | return this.hasMany(ArticuloImagen, 'id_articulo', 'id'); | 12 | return this.hasMany(ArticuloImagen, 'id_articulo', 'id'); |
| 12 | } | 13 | } |
| 13 | }); | 14 | }); |
| 14 | 15 | ||
| 15 | router.get('/articulos', (req , res) => { | 16 | router.get('/articulos/:page', (req, res) => { |
| 16 | 17 | ||
| 17 | let cantidadesVendidas = knex.schema | 18 | Articulo |
| 18 | .raw(`select stock.SEC, stock.ART, SUM(stock.can) as cantidadVendida | 19 | .query((qb) => { |
| 19 | from AMOVSTOC stock | 20 | qb.where('NHA', 0); |
| 20 | join ARTICULOS art on art.CodSec = stock.SEC and art.CodArt = stock.ART and | 21 | qb.where('PreVen', '>', 0); |
| 21 | art.categoria_selfservice > 0 and art.PreVen > 0 | ||
| 22 | where stock.FEC >= dateadd(month, -2, getdate()) and stock.CYV = 'V' and TCO = 'FT' | ||
| 23 | group by stock.sec, stock.art`) | ||
| 24 | |||
| 25 | var articulos = Articulo | ||
| 26 | .where((query) => { | ||
| 27 | query.where('NHA', 0); | ||
| 28 | query.where('PreVen', '>', 0); | ||
| 29 | query.where('categoria_selfservice', '>', 0); | ||
| 30 | }) | 22 | }) |
| 31 | .fetchAll({withRelated: ['imagenes']}) | 23 | .orderBy('id', 'asc') |
| 32 | 24 | .fetchPage({ | |
| 33 | Promise.all([articulos, cantidadesVendidas]) | 25 | pageSize: 150, |
| 34 | .then(values => { | 26 | page: req.params.page, |
| 35 | 27 | withRelated: ['imagenes'] | |
| 36 | let articulos = values[0]; | 28 | }) |
| 37 | let cantidadesVendidas = values[1]; | 29 | .then(data => { |
| 38 | 30 | ||
| 39 | var promisesCodBar = []; | 31 | var promisesCodBar = []; |
| 40 | 32 | ||
| 41 | articulos.forEach(articulo => { | 33 | data.forEach(articulo => { |
| 42 | |||
| 43 | let cantidadVendida = cantidadesVendidas.filter(art => { | ||
| 44 | return articulo.get('CodArt') == art.ART && articulo.get('CodSec') == art.SEC; | ||
| 45 | }); | ||
| 46 | |||
| 47 | articulo.set('cantidadVendida', cantidadVendida[0] ? cantidadVendida[0].cantidadVendida : 0); | ||
| 48 | 34 | ||
| 49 | promisesCodBar.push( | 35 | promisesCodBar.push( |
| 50 | knex('CODBAR').where({ | 36 | knex('CODBAR').where({ |
| 51 | CodSec: articulo.get('CodSec'), | 37 | CodSec: articulo.get('CodSec'), |
| 52 | CodArt: articulo.get('CodArt') | 38 | CodArt: articulo.get('CodArt') |
| 53 | }) | 39 | }) |
| 54 | ); | 40 | ); |
| 55 | }); | 41 | }); |
| 56 | 42 | ||
| 57 | Promise.all(promisesCodBar).then((codigoBarra) => { | 43 | Promise.all(promisesCodBar).then((codigoBarra) => { |
| 58 | 44 | ||
| 59 | codigoBarra.forEach(codigo => { | 45 | codigoBarra.forEach(codigo => { |
| 60 | codigo = codigo[0]; | 46 | codigo = codigo[0]; |
| 61 | 47 | ||
| 62 | let articulo = articulos.filter(art => { | 48 | if (codigo) { |
| 63 | return art.get('CodArt') == codigo.CodArt; | ||
| 64 | }); | ||
| 65 | 49 | ||
| 66 | articulo[0].set('codigoBarra', codigo.CodBar); | 50 | let articulo = data.filter(art => { |
| 67 | }); | 51 | return art.get('CodArt') == codigo.CodArt; |
| 52 | }); | ||
| 68 | 53 | ||
| 69 | res.status(200).send(articulos); | 54 | articulo[0].set('codigoBarra', codigo.CodBar); |
| 55 | } | ||
| 56 | }); | ||
| 57 | res.status(200).send( | ||
| 58 | { | ||
| 59 | data: data, | ||
| 60 | pagination: data.pagination, | ||
| 61 | }); | ||
| 70 | }); | 62 | }); |
| 71 | 63 | ||
| 72 | }); | 64 | }); |
| 73 | }); | 65 | }); |
| 74 | 66 | ||
| 75 | router.get('/articulos/:id', (req, res) => { |
rutas/imagenes.js
| 1 | const path = require('path') | 1 | const path = require('path') |
| 2 | 2 | ||
| 3 | router.get('/imagenes/:nombre', (req, res) => { | 3 | router.get('/imagenes/:nombre', (req, res) => { |
| 4 | 4 | ||
| 5 | var options = { | 5 | var options = { |
| 6 | root: path.join(__dirname, '/../img'), | 6 | root: path.join(__dirname, '/../img'), |
| 7 | dotfiles: 'deny', | 7 | dotfiles: 'deny', |
| 8 | headers: { | 8 | headers: { |
| 9 | 'x-timestamp': Date.now(), | 9 | 'x-timestamp': Date.now(), |
| 10 | 'x-sent': true | 10 | 'x-sent': true |
| 11 | } | 11 | } |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | res.sendFile(req.params.nombre, options, err => { | 14 | res.sendFile(req.params.nombre, options, err => { |
| 15 | if (err) console.log(err); | 15 | if (err) console.log(err); |
| 16 | }); | 16 | }); |
| 17 | }); | 17 | }); |
| 18 | 18 | ||
| 19 | router.post('/imagenes/guardar', (req, res) => { | 19 | router.post('/imagenes/guardar', (req, res) => { |
| 20 | 20 | ||
| 21 | let base64 = req.body.base64.split(';base64,').pop(); | 21 | let base64 = req.body.base64.split(';base64,').pop(); |
| 22 | let path = `${__dirname}/../img/${req.body.name}`; | 22 | let path = `${__dirname}/../img/${req.body.name}`; |
| 23 | 23 | ||
| 24 | require('fs').writeFile(path, base64, {encoding: 'base64'} , err => { | 24 | require('fs').writeFile(path, base64, {encoding: 'base64'} , err => { |
| 25 | 25 | ||
| 26 | if (err) { | 26 | if (err) { |
| 27 | 27 | ||
| 28 | console.log(err); | 28 | console.log(err); |
| 29 | res.status(500).send('Hubo un error'); | 29 | res.status(500).send('Hubo un error'); |
| 30 | } | 30 | } |
| 31 | }); | 31 | }); |
| 32 | 32 | ||
| 33 | knex('ARTICULOS') | 33 | knex('ARTICULOS') |
| 34 | .where({ | 34 | .where({ |
| 35 | CodArt: req.body.codigo, | 35 | CodArt: req.body.codigo, |
| 36 | CodSec: req.body.sector | 36 | CodSec: req.body.sector |
| 37 | }) | 37 | }) |
| 38 | .then(articulo => { | 38 | .then(articulo => { |
| 39 | 39 | ||
| 40 | let idArticulo = articulo[0].id; | 40 | let idArticulo = articulo[0].id; |
| 41 | 41 | ||
| 42 | knex('ARTICULOS_IMAGEN') | 42 | knex('ARTICULOS_IMAGEN') |
| 43 | .where('id_articulo', idArticulo) | 43 | .where('id_articulo', idArticulo) |
| 44 | .del(); | 44 | .del(); |
| 45 | 45 | ||
| 46 | knex('ARTICULOS_IMAGEN') | 46 | knex('ARTICULOS_IMAGEN') |
| 47 | .insert({ | 47 | .insert({ |
| 48 | id_articulo: idArticulo, | 48 | id_articulo: idArticulo, |
| 49 | imagen: req.body.name | 49 | imagen: req.body.name |
| 50 | }) | 50 | }) |
| 51 | .then(() => { | 51 | .then(() => { |
| 52 | res.status(200).send({}); | 52 | res.status(200).send({}); |
| 53 | }) | 53 | }) |
| 54 | 54 | ||
| 55 | }); | 55 | }); |
| 56 | 56 | ||
| 57 | }); | 57 | }); |
| 58 | 58 | ||
| 59 | router.post('/imagen/borrar', (req, res) => { | ||
| 60 | |||
| 61 | let deleted = false | ||
| 62 | let path = `${__dirname}/../img/${req.body.name}`; | ||
| 63 | |||
| 64 | knex('ARTICULOS_IMAGEN') | ||
| 65 | .where('id_articulo', req.body.id_articulo) | ||
| 66 | .andWhere('id', req.body.id) | ||
| 67 | .update({ | ||
| 68 | id_articulo: 0, | ||
| 69 | }) | ||
| 70 | .then(() => { | ||
| 71 | try { | ||
| 72 | require('fs').unlinkSync(path); | ||
| 73 | deleted = true; | ||
| 74 | res.status(200).send(deleted); | ||
| 75 | } catch (err) { } | ||
| 76 | }); | ||
| 77 | }); | ||
| 78 | |||
| 59 | module.exports = router; | 79 | module.exports = router; |
| 60 | 80 |