From 3f9f0e6ab3a54e07a79402c84197d48e6c1e5b70 Mon Sep 17 00:00:00 2001 From: mpuebla Date: Mon, 2 Sep 2019 13:12:13 -0300 Subject: [PATCH] Arreglo en servicio de articulos. --- rutas/articulos.js | 70 +++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/rutas/articulos.js b/rutas/articulos.js index d28a0c9..853ee2d 100644 --- a/rutas/articulos.js +++ b/rutas/articulos.js @@ -1,5 +1,6 @@ var bookshelf = require('bookshelf')(knex); +bookshelf.plugin('pagination'); const ArticuloImagen = bookshelf.Model.extend({ tableName: 'ARTICULOS_IMAGEN' @@ -7,44 +8,29 @@ const ArticuloImagen = bookshelf.Model.extend({ const Articulo = bookshelf.Model.extend({ tableName: 'ARTICULOS', - imagenes: function() { + imagenes: function () { return this.hasMany(ArticuloImagen, 'id_articulo', 'id'); } }); -router.get('/articulos', (req , res) => { +router.get('/articulos/:page', (req, res) => { - let cantidadesVendidas = knex.schema - .raw(`select stock.SEC, stock.ART, SUM(stock.can) as cantidadVendida - from AMOVSTOC stock - join ARTICULOS art on art.CodSec = stock.SEC and art.CodArt = stock.ART and - art.categoria_selfservice > 0 and art.PreVen > 0 - where stock.FEC >= dateadd(month, -2, getdate()) and stock.CYV = 'V' and TCO = 'FT' - group by stock.sec, stock.art`) - - var articulos = Articulo - .where((query) => { - query.where('NHA', 0); - query.where('PreVen', '>', 0); - query.where('categoria_selfservice', '>', 0); + Articulo + .query((qb) => { + qb.where('NHA', 0); + qb.where('PreVen', '>', 0); }) - .fetchAll({withRelated: ['imagenes']}) - - Promise.all([articulos, cantidadesVendidas]) - .then(values => { - - let articulos = values[0]; - let cantidadesVendidas = values[1]; + .orderBy('id', 'asc') + .fetchPage({ + pageSize: 150, + page: req.params.page, + withRelated: ['imagenes'] + }) + .then(data => { var promisesCodBar = []; - articulos.forEach(articulo => { - - let cantidadVendida = cantidadesVendidas.filter(art => { - return articulo.get('CodArt') == art.ART && articulo.get('CodSec') == art.SEC; - }); - - articulo.set('cantidadVendida', cantidadVendida[0] ? cantidadVendida[0].cantidadVendida : 0); + data.forEach(articulo => { promisesCodBar.push( knex('CODBAR').where({ @@ -59,25 +45,23 @@ router.get('/articulos', (req , res) => { codigoBarra.forEach(codigo => { codigo = codigo[0]; - let articulo = articulos.filter(art => { - return art.get('CodArt') == codigo.CodArt; - }); + if (codigo) { - articulo[0].set('codigoBarra', codigo.CodBar); - }); + let articulo = data.filter(art => { + return art.get('CodArt') == codigo.CodArt; + }); - res.status(200).send(articulos); + articulo[0].set('codigoBarra', codigo.CodBar); + } + }); + res.status(200).send( + { + data: data, + pagination: data.pagination, + }); }); }); }); -router.get('/articulos/:id', (req, res) => { - Articulo - .where({id: req.params.id}) - .fetch({withRelated: 'imagenes'}) - .then(articulo => res.status(200).send(articulo)) - .catch(err => { res.status(500).send(err)}) -}); - module.exports = router; -- 1.9.1