Commit f410bbf1ed8ec632191f887ece26e5fba8e9d937

Authored by Eric Fernandez
Exists in master and in 2 other branches develop, lab

Merge branch 'master' into 'master'

Master(mpuebla)

See merge request !1
... ... @@ -23,5 +23,5 @@ app.listen(config.port, '0.0.0.0', () => {
23 23  
24 24 app.use('/', [
25 25 require('./rutas/articulos'),
26   - require('./rutas/imagenes.js'),
  26 + require('./rutas/imagenes'),
27 27 ]);
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;
... ... @@ -56,4 +56,24 @@ router.post('/imagenes/guardar', (req, res) => {
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 79 module.exports = router;