Commit 7c801ad76d436f01e8421d353820c0c26c1c118d
1 parent
35cfb8e487
Exists in
master
filtro sólo productos que tienen sinónimo
Showing
1 changed file
with
8 additions
and
2 deletions
Show diff stats
src/app/components/inicio/inicio.component.ts
1 | import { Component, OnInit, ViewChild, HostListener, AfterViewInit } from '@angular/core'; | 1 | import { Component, OnInit, ViewChild, HostListener, AfterViewInit } from '@angular/core'; |
2 | import { PopoverDirective } from 'ngx-bootstrap'; | 2 | import { PopoverDirective } from 'ngx-bootstrap'; |
3 | import { appSettings } from 'src/etc/AppSettings'; | 3 | import { appSettings } from 'src/etc/AppSettings'; |
4 | import { Router } from '@angular/router'; | 4 | import { Router } from '@angular/router'; |
5 | import { ProductoService } from 'src/app/services/producto.service'; | 5 | import { ProductoService } from 'src/app/services/producto.service'; |
6 | import { Producto } from 'src/app/wrappers/producto'; | 6 | import { Producto } from 'src/app/wrappers/producto'; |
7 | import { Sinonimo } from 'src/app/wrappers/sinonimo'; | 7 | import { Sinonimo } from 'src/app/wrappers/sinonimo'; |
8 | 8 | ||
9 | @Component({ | 9 | @Component({ |
10 | selector: 'app-inicio', | 10 | selector: 'app-inicio', |
11 | templateUrl: './inicio.component.html', | 11 | templateUrl: './inicio.component.html', |
12 | styleUrls: ['./inicio.component.scss'] | 12 | styleUrls: ['./inicio.component.scss'] |
13 | }) | 13 | }) |
14 | export class InicioComponent implements OnInit, AfterViewInit { | 14 | export class InicioComponent implements OnInit, AfterViewInit { |
15 | 15 | ||
16 | private tienePromo = false; | 16 | private tienePromo = false; |
17 | private productoEsPromo = false; | 17 | private productoEsPromo = false; |
18 | private busqueda: string = ''; | 18 | private busqueda: string = ''; |
19 | private productoAcargar: Producto; | 19 | private productoAcargar: Producto; |
20 | private promoAcargar: Producto; | 20 | private promoAcargar: Producto; |
21 | private productos: Producto[] = []; | 21 | private productos: Producto[] = []; |
22 | private promociones: Producto[] = []; | 22 | private promociones: Producto[] = []; |
23 | private sinonimos: Sinonimo[] = []; | 23 | private sinonimos: Sinonimo[] = []; |
24 | private apiImagenes: string = appSettings.apiImagenes; | 24 | private apiImagenes: string = appSettings.apiImagenes; |
25 | 25 | ||
26 | @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective; | 26 | @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective; |
27 | 27 | ||
28 | @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) { | 28 | @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) { |
29 | 29 | ||
30 | if (e.keyCode == 13) { | 30 | if (e.keyCode == 13) { |
31 | this.buscarByCodigoBarras(this.busqueda); | 31 | this.buscarByCodigoBarras(this.busqueda); |
32 | this.busqueda = ''; | 32 | this.busqueda = ''; |
33 | } else { | 33 | } else { |
34 | this.busqueda += e.key; | 34 | this.busqueda += e.key; |
35 | } | 35 | } |
36 | 36 | ||
37 | }; | 37 | }; |
38 | 38 | ||
39 | constructor( | 39 | constructor( |
40 | private router: Router, | 40 | private router: Router, |
41 | private productoService: ProductoService | 41 | private productoService: ProductoService |
42 | ) { } | 42 | ) { } |
43 | 43 | ||
44 | ngOnInit() { | 44 | ngOnInit() { |
45 | 45 | ||
46 | this.productoAcargar = this.productoService.productoAcargar; | 46 | this.productoAcargar = this.productoService.productoAcargar; |
47 | this.getProductos(); | 47 | this.getProductos(); |
48 | } | 48 | } |
49 | 49 | ||
50 | ngAfterViewInit() { | 50 | ngAfterViewInit() { |
51 | 51 | ||
52 | setTimeout(() => { | 52 | setTimeout(() => { |
53 | if (!this.productoAcargar) return; | 53 | if (!this.productoAcargar) return; |
54 | 54 | ||
55 | if (this.productoAcargar.PRO) { | 55 | if (this.productoAcargar.PRO) { |
56 | this.promociones.push(this.productoAcargar); | 56 | this.promociones.push(this.productoAcargar); |
57 | this.promoSeleccionada(this.productoAcargar, true); | 57 | this.promoSeleccionada(this.productoAcargar, true); |
58 | } | 58 | } |
59 | else { | 59 | else { |
60 | this.getPromociones(); | 60 | this.getPromociones(); |
61 | } | 61 | } |
62 | }) | 62 | }) |
63 | } | 63 | } |
64 | 64 | ||
65 | getProductos() { | 65 | getProductos() { |
66 | 66 | ||
67 | this.productoService.getAll() | 67 | this.productoService.getAll() |
68 | .subscribe((productos: Producto[]) => { | 68 | .subscribe((productos: Producto[]) => { |
69 | this.productos = productos; | 69 | this.productos = productos; |
70 | }); | 70 | }); |
71 | } | 71 | } |
72 | 72 | ||
73 | getPromociones() { | 73 | getPromociones() { |
74 | 74 | ||
75 | var sector = this.productoAcargar.CodSec; | 75 | var sector = this.productoAcargar.CodSec; |
76 | var codigo = this.productoAcargar.CodArt; | 76 | var codigo = this.productoAcargar.CodArt; |
77 | this.productoService.getPromociones(sector, codigo) | 77 | this.productoService.getPromociones(sector, codigo) |
78 | .subscribe((res: Producto[]) => { | 78 | .subscribe((res: Producto[]) => { |
79 | 79 | ||
80 | if (res.length === 0) { | 80 | if (res.length === 0) { |
81 | 81 | ||
82 | this.productoAcargar.cantidad = 1; | 82 | this.productoAcargar.cantidad = 1; |
83 | this.productoService.setProductos(this.productoAcargar); | 83 | this.productoService.setProductos(this.productoAcargar); |
84 | this.productoAcargar = this.productoService.productoAcargar = undefined; | 84 | this.productoAcargar = this.productoService.productoAcargar = undefined; |
85 | } else { | 85 | } else { |
86 | 86 | ||
87 | this.promociones = res; | 87 | this.promociones = res; |
88 | this.popoverDirective.show(); | 88 | this.popoverDirective.show(); |
89 | } | 89 | } |
90 | }, error => { console.error(error); }) | 90 | }, error => { console.error(error); }) |
91 | } | 91 | } |
92 | 92 | ||
93 | confirmarProducto() { | 93 | confirmarProducto() { |
94 | 94 | ||
95 | var producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar; | 95 | let producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar; |
96 | |||
97 | producto.productos = producto.productos.filter(producto => { | ||
98 | return producto.idSinonimo > 0; | ||
99 | }); | ||
100 | |||
96 | producto.cantidad = producto.cantidad ? producto.cantidad : 1; | 101 | producto.cantidad = producto.cantidad ? producto.cantidad : 1; |
102 | |||
97 | this.productoService.setProductos(producto); | 103 | this.productoService.setProductos(producto); |
98 | this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; | 104 | this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; |
99 | this.productoService.esPromoPersonalizada = false; | 105 | this.productoService.esPromoPersonalizada = false; |
100 | this.promociones = []; | 106 | this.promociones = []; |
101 | this.popoverDirective.hide(); | 107 | this.popoverDirective.hide(); |
102 | } | 108 | } |
103 | 109 | ||
104 | promoSeleccionada($event: Producto, showPopover: boolean) { | 110 | promoSeleccionada($event: Producto, showPopover: boolean) { |
105 | 111 | ||
106 | this.productoService.getProductoById($event.id) | 112 | this.productoService.getProductoById($event.id) |
107 | .subscribe((res: Producto) => { | 113 | .subscribe((res: Producto) => { |
108 | 114 | ||
109 | $event.imagenes = res.imagenes.length == 0 ? [{ imagen: 'noImage.jpg' }] : res.imagenes; | 115 | $event.imagenes = res.imagenes.length == 0 ? [{ imagen: 'noImage.jpg' }] : res.imagenes; |
110 | this.promoAcargar = $event; | 116 | this.promoAcargar = $event; |
111 | 117 | ||
112 | if ($event.tieneSinonimos) { | 118 | if ($event.tieneSinonimos) { |
113 | this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt); | 119 | this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt); |
114 | } else if (showPopover) { | 120 | } else if (showPopover) { |
115 | this.popoverDirective.show(); | 121 | this.popoverDirective.show(); |
116 | } else { | 122 | } else { |
117 | this.popoverDirective.hide(); | 123 | this.popoverDirective.hide(); |
118 | } | 124 | } |
119 | 125 | ||
120 | }, | 126 | }, |
121 | error => { console.log(error); }) | 127 | error => { console.log(error); }) |
122 | } | 128 | } |
123 | 129 | ||
124 | getSinonimos(sector, codigo) { | 130 | getSinonimos(sector, codigo) { |
125 | 131 | ||
126 | this.productoService.getPromocionSinonimos(sector, codigo) | 132 | this.productoService.getPromocionSinonimos(sector, codigo) |
127 | .subscribe((res: Sinonimo[]) => { | 133 | .subscribe((res: Sinonimo[]) => { |
128 | 134 | ||
129 | res.forEach(sinonimo => { | 135 | res.forEach(sinonimo => { |
130 | 136 | ||
131 | sinonimo.cantidad = 0; | 137 | sinonimo.cantidad = 0; |
132 | this.promoAcargar.productos.forEach(productoPromo => { | 138 | this.promoAcargar.productos.forEach(productoPromo => { |
133 | 139 | ||
134 | sinonimo.productos.forEach(productoSinonimo => { | 140 | sinonimo.productos.forEach(productoSinonimo => { |
135 | 141 | ||
136 | if (productoSinonimo.id === productoPromo.id) { | 142 | if (productoSinonimo.id === productoPromo.id) { |
137 | productoSinonimo.cantidad = productoPromo.cantidad; | 143 | productoSinonimo.cantidad = productoPromo.cantidad; |
138 | sinonimo.cantidad += productoPromo.cantidad; | 144 | sinonimo.cantidad += productoPromo.cantidad; |
139 | } | 145 | } |
140 | }) | 146 | }) |
141 | }) | 147 | }) |
142 | }) | 148 | }) |
143 | this.sinonimos = res; | 149 | this.sinonimos = res; |
144 | (this.sinonimos.length > 0) ? this.popoverDirective.show() : this.popoverDirective.hide(); | 150 | (this.sinonimos.length > 0) ? this.popoverDirective.show() : this.popoverDirective.hide(); |
145 | }) | 151 | }) |
146 | } | 152 | } |
147 | 153 | ||
148 | productosPersonalizados($event: Producto[]) { | 154 | productosPersonalizados($event: Producto[]) { |
149 | 155 | ||
150 | var productosPersonalizados = $event; | 156 | let productosPersonalizados = $event; |
151 | this.promoAcargar.productos = productosPersonalizados; | 157 | this.promoAcargar.productos = productosPersonalizados; |
152 | this.confirmarProducto(); | 158 | this.confirmarProducto(); |
153 | } | 159 | } |
154 | 160 | ||
155 | buscarByCodigoBarras(busqueda) { | 161 | buscarByCodigoBarras(busqueda) { |
156 | 162 | ||
157 | let producto = this.productos.filter(producto => { | 163 | let producto = this.productos.filter(producto => { |
158 | return producto.codigoBarra == busqueda; | 164 | return producto.codigoBarra == busqueda; |
159 | }); | 165 | }); |
160 | 166 | ||
161 | if (producto.length) { | 167 | if (producto.length) { |
162 | 168 | ||
163 | this.productoAcargar = producto[0]; | 169 | this.productoAcargar = producto[0]; |
164 | this.getPromociones(); | 170 | this.getPromociones(); |
165 | 171 | ||
166 | } else { | 172 | } else { |
167 | alert('No se encuentra el producto'); | 173 | alert('No se encuentra el producto'); |
168 | } | 174 | } |
169 | 175 | ||
170 | } | 176 | } |
171 | 177 | ||
172 | irBusquedaProductos(value) { | 178 | irBusquedaProductos(value) { |
173 | 179 | ||
174 | this.productoService.mostrar = value; | 180 | this.productoService.mostrar = value; |
175 | this.router.navigate(['busqueda-productos']); | 181 | this.router.navigate(['busqueda-productos']); |
176 | } | 182 | } |
177 | 183 | ||
178 | deshacerCarga() { | 184 | deshacerCarga() { |
179 | 185 | ||
180 | if (this.sinonimos.length > 0) { | 186 | if (this.sinonimos.length > 0) { |
181 | this.sinonimos = []; | 187 | this.sinonimos = []; |
182 | this.productoService.esPromoPersonalizada = false; | 188 | this.productoService.esPromoPersonalizada = false; |
183 | this.popoverDirective.hide(); | 189 | this.popoverDirective.hide(); |
184 | } | 190 | } |
185 | 191 | ||
186 | if (this.promoAcargar) { | 192 | if (this.promoAcargar) { |
187 | this.promoAcargar = undefined; | 193 | this.promoAcargar = undefined; |
188 | if (this.productoAcargar.PRO) { | 194 | if (this.productoAcargar.PRO) { |
189 | this.productoAcargar = undefined; | 195 | this.productoAcargar = undefined; |
190 | this.promociones = []; | 196 | this.promociones = []; |
191 | this.popoverDirective.hide(); | 197 | this.popoverDirective.hide(); |
192 | } else { | 198 | } else { |
193 | this.popoverDirective.show(); | 199 | this.popoverDirective.show(); |
194 | } | 200 | } |
195 | } else { | 201 | } else { |
196 | this.productoAcargar = undefined; | 202 | this.productoAcargar = undefined; |
197 | this.promociones = []; | 203 | this.promociones = []; |
198 | this.popoverDirective.hide(); | 204 | this.popoverDirective.hide(); |
199 | } | 205 | } |
200 | } | 206 | } |
201 | 207 | ||
202 | } | 208 | } |
203 | 209 |