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