Commit f48ea92e29cb5f9c484459c2b8b21ad539e0a6b2

Authored by Eric Fernandez
Exists in master and in 1 other branch validar_pve

Merge branch 'master' into 'master'

Master(mpuebla)

See merge request !69
src/app/components/busqueda-productos/busqueda-productos.component.ts
1 import { Component, OnInit, EventEmitter } from '@angular/core'; 1 import { Component, OnInit, EventEmitter } from '@angular/core';
2 import { ProductoService } from 'src/app/services/producto.service'; 2 import { ProductoService } from 'src/app/services/producto.service';
3 import { Producto } from 'src/app/wrappers/producto'; 3 import { Producto } from 'src/app/wrappers/producto';
4 import { Categoria } from 'src/app/wrappers/categoria'; 4 import { Categoria } from 'src/app/wrappers/categoria';
5 import { appSettings } from 'src/etc/AppSettings'; 5 import { appSettings } from 'src/etc/AppSettings';
6 import { Router } from '@angular/router'; 6 import { Router } from '@angular/router';
7 7
8 @Component({ 8 @Component({
9 selector: 'app-busqueda-productos', 9 selector: 'app-busqueda-productos',
10 templateUrl: './busqueda-productos.component.html', 10 templateUrl: './busqueda-productos.component.html',
11 styleUrls: ['./busqueda-productos.component.scss'] 11 styleUrls: ['./busqueda-productos.component.scss']
12 }) 12 })
13 export class BusquedaProductosComponent implements OnInit { 13 export class BusquedaProductosComponent implements OnInit {
14 14
15 private productos: Producto[] = []; 15 private productos: Producto[] = [];
16 private auxProductos: Producto[] = []; 16 private auxProductos: Producto[] = [];
17 private searchTerm: string = ''; 17 private searchTerm: string = '';
18 private categoriaActive: number = null; 18 private categoriaActive: number = null;
19 private showSpinner: boolean = true; 19 private showSpinner: boolean = true;
20 private queMostrar: string = 'todos'; 20 private queMostrar: string = 'todos';
21 private apiImagenes: string = appSettings.apiImagenes; 21 private apiImagenes: string = appSettings.apiImagenes;
22 private categorias: Categoria[] = []; 22 private categorias: Categoria[] = [];
23 private blurFocus = new EventEmitter(); 23 private blurFocus = new EventEmitter();
24 private ordenandoByVendidos = true; 24 private ordenandoByVendidos = true;
25 private title: string = 'Búsqueda' 25 private title: string = 'Búsqueda'
26 26
27 constructor( 27 constructor(
28 private productoService: ProductoService, 28 private productoService: ProductoService,
29 private router: Router) { } 29 private router: Router) { }
30 30
31 ngOnInit() { 31 ngOnInit() {
32 32
33 this.queMostrar = this.productoService.mostrar; 33 this.queMostrar = this.productoService.mostrar;
34 34
35 this.productoService.getCategorias() 35 this.productoService.getCategorias()
36 .subscribe((categorias: Categoria[]) => { 36 .subscribe((categorias: Categoria[]) => {
37 37
38 switch (this.queMostrar) { 38 switch (this.queMostrar) {
39 case 'todos': 39 case 'todos':
40 this.categorias = categorias; 40 this.categorias = categorias;
41 this.categoriaActive = 0; 41 this.categoriaActive = 0;
42 this.title = 'Búsqueda'; 42 this.title = 'Búsqueda';
43 break; 43 break;
44 case 'promociones': 44 case 'promociones':
45 this.categorias = categorias; 45 this.categorias = categorias;
46 this.categoriaActive = 1; 46 this.categoriaActive = 1;
47 this.title = 'Promociones'; 47 this.title = 'Promociones';
48 break; 48 break;
49 case 'ordenar': 49 case 'ordenar':
50 50 this.categorias = categorias.filter((categoria: Categoria) => {
51 this.categorias = categorias.filter((categoria: Categoria) => { 51 return categoria.ES_PEDIDO;
52 return categoria.ES_PEDIDO; 52 });
53 }); 53 this.categoriaActive = 0;
54 54 this.title = 'Ordenar';
55 this.categoriaActive = 0;
56 this.title = 'Ordenar';
57
58 break; 55 break;
59 default: 56 default:
57 this.categorias = categorias;
58 this.categoriaActive = 0;
59 this.title = 'Búsqueda';
60 break; 60 break;
61 } 61 }
62
63 }); 62 });
64 63
65 this.productoService.productoAcargar = undefined; 64 this.productoService.productoAcargar = undefined;
66 this.productoService.getAll() 65 this.productoService.getAll()
67 .subscribe((data: Producto[]) => { 66 .subscribe((data: Producto[]) => {
68 67
69 this.setProductosSinImagen(data); 68 this.setProductosSinImagen(data);
70 69
71 if (this.queMostrar == 'ordenar') { 70 if (this.queMostrar == 'ordenar') {
72 71
73 this.categorias.forEach((categoria: Categoria) => { 72 this.categorias.forEach((categoria: Categoria) => {
74 73
75 let tempProductos = data.filter((producto: Producto) => { 74 let tempProductos = data.filter((producto: Producto) => {
76 return producto.categoria_selfservice == categoria.id; 75 return producto.categoria_selfservice == categoria.id;
77 }); 76 });
78 77
79 this.productos = this.productos.concat(tempProductos); 78 this.productos = this.productos.concat(tempProductos);
80 79
81 }); 80 });
82 } else { 81 } else {
83 this.productos = data; 82 this.productos = data;
84 } 83 }
85 this.filterItems(); 84 this.filterItems();
86 this.ordenar(); 85 this.ordenar();
87 }, (error) => { 86 }, (error) => {
88 this.showSpinner = false; 87 this.showSpinner = false;
89 console.error(error); 88 console.error(error);
90 }); 89 });
91 } 90 }
92 91
93 filterItems() { 92 filterItems() {
94 93
95 this.auxProductos = this.productos.filter(x => { 94 this.auxProductos = this.productos.filter(x => {
96 if (this.categoriaActive === 0) { 95 if (this.categoriaActive === 0) {
97 return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()); 96 return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase());
98 } 97 }
99 else { 98 else {
100 return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) && 99 return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) &&
101 x.categoria_selfservice === this.categoriaActive; 100 x.categoria_selfservice === this.categoriaActive;
102 } 101 }
103 }); 102 });
104 103
105 } 104 }
106 105
107 agregarAlCarrito(producto: Producto) { 106 agregarAlCarrito(producto: Producto) {
108 107
109 producto.cantidad = 1; 108 producto.cantidad = 1;
110 this.productoService.productos.push(producto); 109 this.productoService.productos.push(producto);
111 } 110 }
112 111
113 ordenar() { 112 ordenar() {
114 113
115 if (this.ordenandoByVendidos) { 114 if (this.ordenandoByVendidos) {
116 115
117 this.auxProductos.sort((a, b) => { 116 this.auxProductos.sort((a, b) => {
118 return b.cantidadVendida - a.cantidadVendida; 117 return b.cantidadVendida - a.cantidadVendida;
119 }); 118 });
120 } else { 119 } else {
121 this.filterItems(); 120 this.filterItems();
122 } 121 }
123 122
124 } 123 }
125 124
126 private elegirProducto(producto: Producto) { 125 private elegirProducto(producto: Producto) {
127 126
128 if (producto.PRO) { 127 if (producto.PRO) {
129 128
130 let imagenes = producto.imagenes; 129 let imagenes = producto.imagenes;
131 this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt) 130 this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt)
132 .subscribe(res => { 131 .subscribe(res => {
133 132
134 this.productoService.productoAcargar = res[0]; 133 this.productoService.productoAcargar = res[0];
135 this.productoService.productoAcargar.imagenes = imagenes; 134 this.productoService.productoAcargar.imagenes = imagenes;
136 this.router.navigate(['inicio']); 135 this.router.navigate(['inicio']);
137 }, 136 },
138 error => { console.error(error); } 137 error => { console.error(error); }
139 ); 138 );
140 } else { 139 } else {
141 140
142 this.productoService.productoAcargar = producto; 141 this.productoService.productoAcargar = producto;
143 this.router.navigate(['inicio']); 142 this.router.navigate(['inicio']);
144 } 143 }
145 144
146 } 145 }
147 146
148 private setProductosSinImagen(productos: Producto[]) { 147 private setProductosSinImagen(productos: Producto[]) {
149 148
150 productos.forEach((producto: Producto) => { 149 productos.forEach((producto: Producto) => {
151 producto.imagenes = producto.imagenes.length == 0 ? 150 producto.imagenes = producto.imagenes.length == 0 ?
152 [{ imagen: 'noImage.jpg' }] : producto.imagenes; 151 [{ imagen: 'noImage.jpg' }] : producto.imagenes;
153 }) 152 })