Commit 4f1673d9e01b14084bdac9ce68be34a2a8d93f8e
Exists in
master
and in
1 other branch
Merge branch 'master' of git.focasoftware.com:angular/autoservicio
# Conflicts: # src/app/components/busqueda-productos/busqueda-productos.component.ts
Showing
1 changed file
Show diff stats
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 = 'todo'; | 20 | private queMostrar: string = 'todo'; |
21 | private apiUrl: string = appSettings.apiUrl; | 21 | private apiUrl: string = appSettings.apiUrl; |
22 | private categorias: Categoria[] = []; | 22 | private categorias: Categoria[] = []; |
23 | private blurFocus = new EventEmitter(); | 23 | private blurFocus = new EventEmitter(); |
24 | 24 | ||
25 | constructor( | 25 | constructor( |
26 | private productoService: ProductoService, | 26 | private productoService: ProductoService, |
27 | private router: Router) { } | 27 | private router: Router) { } |
28 | 28 | ||
29 | ngOnInit() { | 29 | ngOnInit() { |
30 | 30 | ||
31 | this.queMostrar = this.productoService.mostrar; | 31 | this.queMostrar = this.productoService.mostrar; |
32 | 32 | ||
33 | this.productoService.getCategorias() | 33 | this.productoService.getCategorias() |
34 | .subscribe((categorias: Categoria[]) => { | 34 | .subscribe((categorias: Categoria[]) => { |
35 | this.categorias = categorias; | ||
36 | 35 | ||
37 | switch (this.queMostrar) { | 36 | switch (this.queMostrar) { |
38 | case 'todos': | 37 | case 'todos': |
39 | this.categoriaActive = 1; | 38 | this.categorias = categorias; |
39 | this.categoriaActive = 0; | ||
40 | break; | 40 | break; |
41 | case 'promociones': | 41 | case 'promociones': |
42 | this.categorias = categorias; | ||
42 | this.categoriaActive = 1; | 43 | this.categoriaActive = 1; |
43 | break; | 44 | break; |
44 | case 'ordenar': | 45 | case 'ordenar': |
45 | 46 | ||
46 | this.categorias = this.categorias.filter((categoria: Categoria) => { | 47 | this.categorias = categorias.filter((categoria: Categoria) => { |
47 | return categoria.ES_PEDIDO; | 48 | return categoria.ES_PEDIDO; |
48 | }); | 49 | }); |
49 | 50 | ||
50 | this.categoriaActive = this.categorias[0].id; | 51 | this.categoriaActive = 0; |
51 | 52 | ||
52 | break; | 53 | break; |
53 | default: | 54 | default: |
54 | break; | 55 | break; |
55 | } | 56 | } |
56 | 57 | ||
57 | }); | 58 | }); |
58 | 59 | ||
59 | this.productoService.productoAcargar = undefined; | 60 | this.productoService.productoAcargar = undefined; |
60 | this.productoService.getAll() | 61 | this.productoService.getAll() |
61 | .subscribe((data: Producto[]) => { | 62 | .subscribe((data: Producto[]) => { |
62 | 63 | ||
63 | this.productos = data; | 64 | if (this.queMostrar == 'ordenar') { |
65 | |||
66 | this.categorias.forEach((categoria: Categoria) => { | ||
67 | |||
68 | let tempProductos = data.filter((producto: Producto) => { | ||
69 | return producto.categoria_selfservice == categoria.id; | ||
70 | }); | ||
71 | |||
72 | this.productos = this.productos.concat(tempProductos); | ||
73 | |||
74 | }); | ||
75 | } else { | ||
76 | this.productos = data; | ||
77 | } | ||
64 | this.filterItems(); | 78 | this.filterItems(); |
65 | }, (error) => { | 79 | }, (error) => { |
66 | this.showSpinner = false; | 80 | this.showSpinner = false; |
67 | console.error(error); | 81 | console.error(error); |
68 | }); | 82 | }); |
69 | } | 83 | } |
70 | 84 | ||
71 | filterItems() { | 85 | filterItems() { |
72 | 86 | ||
73 | this.auxProductos = this.productos.filter(x => { | 87 | this.auxProductos = this.productos.filter(x => { |
74 | if (this.categoriaActive === 0) { | 88 | if (this.categoriaActive === 0) { |
75 | return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) | 89 | return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()); |
76 | } | 90 | } |
77 | else { | 91 | else { |
78 | return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) && | 92 | return x.DET_LAR.toLowerCase().includes(this.searchTerm.toLowerCase()) && |
79 | x.categoria_selfservice === this.categoriaActive; | 93 | x.categoria_selfservice === this.categoriaActive; |
80 | } | 94 | } |
81 | }); | 95 | }); |
96 | |||
82 | } | 97 | } |
83 | 98 | ||
84 | agregarAlCarrito(producto: Producto) { | 99 | agregarAlCarrito(producto: Producto) { |
85 | 100 | ||
86 | producto.cantidad = 1; | 101 | producto.cantidad = 1; |
87 | this.productoService.productos.push(producto); | 102 | this.productoService.productos.push(producto); |
88 | } | 103 | } |
89 | 104 | ||
90 | lostBlur() { | 105 | lostBlur() { |
91 | this.blurFocus.emit(); | 106 | this.blurFocus.emit(); |
92 | } | 107 | } |
93 | 108 | ||
94 | private elegirProducto(producto: Producto) { | 109 | private elegirProducto(producto: Producto) { |
95 | 110 | ||
96 | if (producto.PRO) { | 111 | if (producto.PRO) { |
97 | 112 | ||
98 | let imagenes = producto.imagenes; | 113 | let imagenes = producto.imagenes; |
99 | this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt) | 114 | this.productoService.getPromocionByCodigos(producto.CodSec, producto.CodArt) |
100 | .subscribe(res => { | 115 | .subscribe(res => { |
101 | 116 | ||
102 | this.productoService.productoAcargar = res[0]; | 117 | this.productoService.productoAcargar = res[0]; |
103 | this.productoService.productoAcargar.imagenes = imagenes; | 118 | this.productoService.productoAcargar.imagenes = imagenes; |
104 | this.router.navigate(['inicio']); | 119 | this.router.navigate(['inicio']); |
105 | }, | 120 | }, |
106 | error => { console.error(error); } | 121 | error => { console.error(error); } |
107 | ); | 122 | ); |
108 | } else { | 123 | } else { |
109 | 124 | ||
110 | this.productoService.productoAcargar = producto; | 125 | this.productoService.productoAcargar = producto; |
111 | this.router.navigate(['inicio']); | 126 | this.router.navigate(['inicio']); |
112 | } | 127 | } |
113 | 128 | ||
114 | } | 129 | } |
115 | } | 130 | } |