Commit 3f9f0e6ab3a54e07a79402c84197d48e6c1e5b70

Authored by Marcelo Puebla
1 parent b8b527453e
Exists in master

Arreglo en servicio de articulos.

Showing 1 changed file with 27 additions and 43 deletions   Show diff stats
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) => {