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