Commit 3f9f0e6ab3a54e07a79402c84197d48e6c1e5b70

Authored by Marcelo Puebla
1 parent b8b527453e
Exists in master and in 2 other branches develop, lab

Arreglo en servicio de articulos.

Showing 1 changed file with 27 additions and 43 deletions   Show diff stats
1 1  
2 2 var bookshelf = require('bookshelf')(knex);
  3 +bookshelf.plugin('pagination');
3 4  
4 5 const ArticuloImagen = bookshelf.Model.extend({
5 6 tableName: 'ARTICULOS_IMAGEN'
... ... @@ -7,44 +8,29 @@ const ArticuloImagen = bookshelf.Model.extend({
7 8  
8 9 const Articulo = bookshelf.Model.extend({
9 10 tableName: 'ARTICULOS',
10   - imagenes: function() {
  11 + imagenes: function () {
11 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   - .raw(`select stock.SEC, stock.ART, SUM(stock.can) as cantidadVendida
19   - from AMOVSTOC stock
20   - join ARTICULOS art on art.CodSec = stock.SEC and art.CodArt = stock.ART and
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);
  18 + Articulo
  19 + .query((qb) => {
  20 + qb.where('NHA', 0);
  21 + qb.where('PreVen', '>', 0);
30 22 })
31   - .fetchAll({withRelated: ['imagenes']})
32   -
33   - Promise.all([articulos, cantidadesVendidas])
34   - .then(values => {
35   -
36   - let articulos = values[0];
37   - let cantidadesVendidas = values[1];
  23 + .orderBy('id', 'asc')
  24 + .fetchPage({
  25 + pageSize: 150,
  26 + page: req.params.page,
  27 + withRelated: ['imagenes']
  28 + })
  29 + .then(data => {
38 30  
39 31 var promisesCodBar = [];
40 32  
41   - articulos.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);
  33 + data.forEach(articulo => {
48 34  
49 35 promisesCodBar.push(
50 36 knex('CODBAR').where({
... ... @@ -59,25 +45,23 @@ router.get('/articulos', (req , res) => {
59 45 codigoBarra.forEach(codigo => {
60 46 codigo = codigo[0];
61 47  
62   - let articulo = articulos.filter(art => {
63   - return art.get('CodArt') == codigo.CodArt;
64   - });
  48 + if (codigo) {
65 49  
66   - articulo[0].set('codigoBarra', codigo.CodBar);
67   - });
  50 + let articulo = data.filter(art => {
  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) => {
76   - Articulo
77   - .where({id: req.params.id})
78   - .fetch({withRelated: 'imagenes'})
79   - .then(articulo => res.status(200).send(articulo))
80   - .catch(err => { res.status(500).send(err)})
81   -});
82   -
83 67 module.exports = router;