Commit c8afd4ad176bbacfd025d590917fbfa52723a342
1 parent
041b572f22
Exists in
master
and in
1 other branch
se quita código de prueba
Showing
1 changed file
with
0 additions
and
1 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 { APP_SETTINGS } from 'src/etc/AppSettings'; | 3 | import { APP_SETTINGS } 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 = APP_SETTINGS.apiImagenes; | 24 | private apiImagenes: string = APP_SETTINGS.apiImagenes; |
25 | private mostrarPantalla : boolean = false; | 25 | private mostrarPantalla : boolean = false; |
26 | 26 | ||
27 | @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective; | 27 | @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective; |
28 | 28 | ||
29 | @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) { | 29 | @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) { |
30 | 30 | ||
31 | if (e.keyCode == 13) { | 31 | if (e.keyCode == 13) { |
32 | this.buscarByCodigoBarras(this.busqueda); | 32 | this.buscarByCodigoBarras(this.busqueda); |
33 | this.busqueda = ''; | 33 | this.busqueda = ''; |
34 | } else { | 34 | } else { |
35 | this.busqueda += e.key; | 35 | this.busqueda += e.key; |
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 | localStorage.removeItem('pve') | ||
47 | this.productoAcargar = this.productoService.productoAcargar; | 46 | this.productoAcargar = this.productoService.productoAcargar; |
48 | this.getProductos(); | 47 | this.getProductos(); |
49 | this.mostrarPantalla = localStorage.getItem('impresoraPVE') ? true : false; | 48 | this.mostrarPantalla = localStorage.getItem('impresoraPVE') ? true : false; |
50 | } | 49 | } |
51 | 50 | ||
52 | ngAfterViewInit() { | 51 | ngAfterViewInit() { |
53 | 52 | ||
54 | setTimeout(() => { | 53 | setTimeout(() => { |
55 | if (!this.productoAcargar) return; | 54 | if (!this.productoAcargar) return; |
56 | 55 | ||
57 | if (this.productoAcargar.PRO) { | 56 | if (this.productoAcargar.PRO) { |
58 | this.promociones.push(this.productoAcargar); | 57 | this.promociones.push(this.productoAcargar); |
59 | this.promoSeleccionada(this.productoAcargar, true); | 58 | this.promoSeleccionada(this.productoAcargar, true); |
60 | } | 59 | } |
61 | else { | 60 | else { |
62 | this.getPromociones(); | 61 | this.getPromociones(); |
63 | } | 62 | } |
64 | }) | 63 | }) |
65 | } | 64 | } |
66 | 65 | ||
67 | getProductos() { | 66 | getProductos() { |
68 | 67 | ||
69 | this.productoService.getAll() | 68 | this.productoService.getAll() |
70 | .subscribe((productos: Producto[]) => { | 69 | .subscribe((productos: Producto[]) => { |
71 | this.productos = productos; | 70 | this.productos = productos; |
72 | }); | 71 | }); |
73 | } | 72 | } |
74 | 73 | ||
75 | getPromociones() { | 74 | getPromociones() { |
76 | 75 | ||
77 | var sector = this.productoAcargar.CodSec; | 76 | var sector = this.productoAcargar.CodSec; |
78 | var codigo = this.productoAcargar.CodArt; | 77 | var codigo = this.productoAcargar.CodArt; |
79 | this.productoService.getPromociones(sector, codigo) | 78 | this.productoService.getPromociones(sector, codigo) |
80 | .subscribe((res: Producto[]) => { | 79 | .subscribe((res: Producto[]) => { |
81 | 80 | ||
82 | if (res.length === 0) { | 81 | if (res.length === 0) { |
83 | 82 | ||
84 | this.productoAcargar.cantidad ? false : this.productoAcargar.cantidad = 1; | 83 | this.productoAcargar.cantidad ? false : this.productoAcargar.cantidad = 1; |
85 | this.productoService.setProductos(this.productoAcargar); | 84 | this.productoService.setProductos(this.productoAcargar); |
86 | this.productoAcargar = this.productoService.productoAcargar = undefined; | 85 | this.productoAcargar = this.productoService.productoAcargar = undefined; |
87 | } else { | 86 | } else { |
88 | 87 | ||
89 | this.promociones = res; | 88 | this.promociones = res; |
90 | this.popoverDirective.show(); | 89 | this.popoverDirective.show(); |
91 | } | 90 | } |
92 | }, error => { console.error(error); }) | 91 | }, error => { console.error(error); }) |
93 | } | 92 | } |
94 | 93 | ||
95 | confirmarProducto(cantidad?: number) { | 94 | confirmarProducto(cantidad?: number) { |
96 | 95 | ||
97 | let producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar; | 96 | let producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar; |
98 | 97 | ||
99 | if (cantidad) { | 98 | if (cantidad) { |
100 | producto.cantidad = cantidad; | 99 | producto.cantidad = cantidad; |
101 | } else { | 100 | } else { |
102 | producto.cantidad = producto.cantidad ? producto.cantidad : 1; | 101 | producto.cantidad = producto.cantidad ? producto.cantidad : 1; |
103 | } | 102 | } |
104 | 103 | ||
105 | this.productoService.setProductos(producto); | 104 | this.productoService.setProductos(producto); |
106 | this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; | 105 | this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; |
107 | this.productoService.esPromoPersonalizada = false; | 106 | this.productoService.esPromoPersonalizada = false; |
108 | this.promociones = []; | 107 | this.promociones = []; |
109 | this.popoverDirective.hide(); | 108 | this.popoverDirective.hide(); |
110 | } | 109 | } |
111 | 110 | ||
112 | promoSeleccionada($event: Producto, showPopover: boolean) { | 111 | promoSeleccionada($event: Producto, showPopover: boolean) { |
113 | 112 | ||
114 | this.productoService.getProductoById($event.id) | 113 | this.productoService.getProductoById($event.id) |
115 | .subscribe((res: Producto) => { | 114 | .subscribe((res: Producto) => { |
116 | 115 | ||
117 | $event.imagenes = res.imagenes.length == 0 ? [{ imagen: 'noImage.jpg' }] : res.imagenes; | 116 | $event.imagenes = res.imagenes.length == 0 ? [{ imagen: 'noImage.jpg' }] : res.imagenes; |
118 | this.promoAcargar = $event; | 117 | this.promoAcargar = $event; |
119 | 118 | ||
120 | if ($event.tieneSinonimos) { | 119 | if ($event.tieneSinonimos) { |
121 | this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt); | 120 | this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt); |
122 | } else if (showPopover) { | 121 | } else if (showPopover) { |
123 | this.popoverDirective.show(); | 122 | this.popoverDirective.show(); |
124 | } else { | 123 | } else { |
125 | this.popoverDirective.hide(); | 124 | this.popoverDirective.hide(); |
126 | } | 125 | } |
127 | 126 | ||
128 | }, | 127 | }, |
129 | error => { console.log(error); }) | 128 | error => { console.log(error); }) |
130 | } | 129 | } |
131 | 130 | ||
132 | getSinonimos(sector, codigo) { | 131 | getSinonimos(sector, codigo) { |
133 | 132 | ||
134 | this.productoService.getPromocionSinonimos(sector, codigo) | 133 | this.productoService.getPromocionSinonimos(sector, codigo) |
135 | .subscribe((res: Sinonimo[]) => { | 134 | .subscribe((res: Sinonimo[]) => { |
136 | 135 | ||
137 | res.forEach(sinonimo => { | 136 | res.forEach(sinonimo => { |
138 | 137 | ||
139 | sinonimo.cantidad = 0; | 138 | sinonimo.cantidad = 0; |
140 | this.promoAcargar.productos.forEach(productoPromo => { | 139 | this.promoAcargar.productos.forEach(productoPromo => { |
141 | 140 | ||
142 | sinonimo.productos.forEach(productoSinonimo => { | 141 | sinonimo.productos.forEach(productoSinonimo => { |
143 | 142 | ||
144 | if (productoSinonimo.id === productoPromo.id) { | 143 | if (productoSinonimo.id === productoPromo.id) { |
145 | productoSinonimo.cantidad = productoPromo.cantidad; | 144 | productoSinonimo.cantidad = productoPromo.cantidad; |
146 | sinonimo.cantidad += productoPromo.cantidad; | 145 | sinonimo.cantidad += productoPromo.cantidad; |
147 | } | 146 | } |
148 | }) | 147 | }) |
149 | }) | 148 | }) |
150 | }) | 149 | }) |
151 | this.sinonimos = res; | 150 | this.sinonimos = res; |
152 | (this.sinonimos.length > 0) ? this.popoverDirective.show() : this.popoverDirective.hide(); | 151 | (this.sinonimos.length > 0) ? this.popoverDirective.show() : this.popoverDirective.hide(); |
153 | }) | 152 | }) |
154 | } | 153 | } |
155 | 154 | ||
156 | productosPersonalizados($event: any) { | 155 | productosPersonalizados($event: any) { |
157 | 156 | ||
158 | let productosPersonalizados = $event.productosAenviar; | 157 | let productosPersonalizados = $event.productosAenviar; |
159 | 158 | ||
160 | productosPersonalizados.forEach(productoElegido => { | 159 | productosPersonalizados.forEach(productoElegido => { |
161 | 160 | ||
162 | this.promoAcargar.productos = this.promoAcargar.productos.filter(productoPromo => { | 161 | this.promoAcargar.productos = this.promoAcargar.productos.filter(productoPromo => { |
163 | return productoPromo.idSinonimo != productoElegido.idSinonimo; | 162 | return productoPromo.idSinonimo != productoElegido.idSinonimo; |
164 | }); | 163 | }); |
165 | }); | 164 | }); |
166 | 165 | ||
167 | this.promoAcargar.productos = this.promoAcargar.productos.concat(productosPersonalizados); | 166 | this.promoAcargar.productos = this.promoAcargar.productos.concat(productosPersonalizados); |
168 | this.confirmarProducto($event.cantidadPromo); | 167 | this.confirmarProducto($event.cantidadPromo); |
169 | } | 168 | } |
170 | 169 | ||
171 | buscarByCodigoBarras(busqueda) { | 170 | buscarByCodigoBarras(busqueda) { |
172 | 171 | ||
173 | let producto = this.productos.filter(producto => { | 172 | let producto = this.productos.filter(producto => { |
174 | return producto.codigoBarra == busqueda; | 173 | return producto.codigoBarra == busqueda; |
175 | }); | 174 | }); |
176 | 175 | ||
177 | if (producto.length) { | 176 | if (producto.length) { |
178 | 177 | ||
179 | this.productoAcargar = producto[0]; | 178 | this.productoAcargar = producto[0]; |
180 | this.getPromociones(); | 179 | this.getPromociones(); |
181 | 180 | ||
182 | } else { | 181 | } else { |
183 | alert('No se encuentra el producto'); | 182 | alert('No se encuentra el producto'); |
184 | } | 183 | } |
185 | 184 | ||
186 | } | 185 | } |
187 | 186 | ||
188 | irBusquedaProductos(value) { | 187 | irBusquedaProductos(value) { |
189 | 188 | ||
190 | this.productoService.mostrar = value; | 189 | this.productoService.mostrar = value; |
191 | this.router.navigate(['busqueda-productos']); | 190 | this.router.navigate(['busqueda-productos']); |
192 | } | 191 | } |
193 | 192 | ||
194 | deshacerCarga() { | 193 | deshacerCarga() { |
195 | 194 | ||
196 | if (this.sinonimos.length > 0) { | 195 | if (this.sinonimos.length > 0) { |
197 | this.sinonimos = []; | 196 | this.sinonimos = []; |
198 | this.productoService.esPromoPersonalizada = false; | 197 | this.productoService.esPromoPersonalizada = false; |
199 | this.popoverDirective.hide(); | 198 | this.popoverDirective.hide(); |
200 | } | 199 | } |
201 | 200 | ||
202 | if (this.promoAcargar) { | 201 | if (this.promoAcargar) { |
203 | this.promoAcargar = undefined; | 202 | this.promoAcargar = undefined; |
204 | if (this.productoAcargar.PRO) { | 203 | if (this.productoAcargar.PRO) { |
205 | this.productoAcargar = undefined; | 204 | this.productoAcargar = undefined; |
206 | this.promociones = []; | 205 | this.promociones = []; |
207 | this.popoverDirective.hide(); | 206 | this.popoverDirective.hide(); |
208 | } else { | 207 | } else { |
209 | this.popoverDirective.show(); | 208 | this.popoverDirective.show(); |
210 | } | 209 | } |
211 | } else { | 210 | } else { |
212 | this.productoAcargar = undefined; | 211 | this.productoAcargar = undefined; |
213 | this.promociones = []; | 212 | this.promociones = []; |
214 | this.popoverDirective.hide(); | 213 | this.popoverDirective.hide(); |
215 | } | 214 | } |
216 | } | 215 | } |
217 | 216 | ||
218 | } | 217 | } |
219 | 218 |