Commit 536bb85311eafbbcd34ec905eb19b05f2f6dc967
1 parent
2be4954708
Exists in
master
Agregada condicion en else.
Showing
1 changed file
with
2 additions
and
2 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 | this.productoService.getAll() | 81 | this.productoService.getAll() |
82 | .subscribe((productos: Producto[]) => { | 82 | .subscribe((productos: Producto[]) => { |
83 | this.productos = productos; | 83 | this.productos = productos; |
84 | }); | 84 | }); |
85 | } | 85 | } |
86 | 86 | ||
87 | irBusquedaProductos(verPromociones) { | 87 | irBusquedaProductos(verPromociones) { |
88 | 88 | ||
89 | this.productoService.verCategoriasProductos = verPromociones; | 89 | this.productoService.verCategoriasProductos = verPromociones; |
90 | this.router.navigate(['busqueda-productos']); | 90 | this.router.navigate(['busqueda-productos']); |
91 | } | 91 | } |
92 | 92 | ||
93 | confirmarProducto() { | 93 | confirmarProducto() { |
94 | this.promoAcargar.cantidad = 1; | 94 | this.promoAcargar.cantidad = 1; |
95 | this.productoService.productos.push(this.promoAcargar); | 95 | this.productoService.productos.push(this.promoAcargar); |
96 | this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; | 96 | this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; |
97 | this.promociones = []; | 97 | this.promociones = []; |
98 | this.popoverDirective.hide(); | 98 | this.popoverDirective.hide(); |
99 | } | 99 | } |
100 | 100 | ||
101 | deshacerCarga() { | 101 | deshacerCarga() { |
102 | 102 | ||
103 | if (this.sinonimos.length > 0) { | 103 | if (this.sinonimos.length > 0) { |
104 | this.sinonimos = []; | 104 | this.sinonimos = []; |
105 | this.popoverDirective.hide(); | 105 | this.popoverDirective.hide(); |
106 | } | 106 | } |
107 | 107 | ||
108 | if (this.promoAcargar) { | 108 | if (this.promoAcargar) { |
109 | this.promoAcargar = undefined; | 109 | this.promoAcargar = undefined; |
110 | if (this.productoAcargar.PRO) { | 110 | if (this.productoAcargar.PRO) { |
111 | this.productoAcargar = undefined; | 111 | this.productoAcargar = undefined; |
112 | this.promociones = []; | 112 | this.promociones = []; |
113 | this.popoverDirective.hide(); | 113 | this.popoverDirective.hide(); |
114 | } else { | 114 | } else { |
115 | this.popoverDirective.show(); | 115 | this.popoverDirective.show(); |
116 | } | 116 | } |
117 | } else { | 117 | } else { |
118 | this.productoAcargar = undefined; | 118 | this.productoAcargar = undefined; |
119 | this.promociones = []; | 119 | this.promociones = []; |
120 | this.popoverDirective.hide(); | 120 | this.popoverDirective.hide(); |
121 | } | 121 | } |
122 | } | 122 | } |
123 | 123 | ||
124 | promoSeleccionada($event: Producto) { | 124 | promoSeleccionada($event: Producto) { |
125 | 125 | ||
126 | this.promoAcargar = $event; | 126 | this.promoAcargar = $event; |
127 | if (this.promoAcargar.tieneSinonimos) { | 127 | if (this.promoAcargar.tieneSinonimos) { |
128 | var sector = this.promoAcargar.CodSec; | 128 | var sector = this.promoAcargar.CodSec; |
129 | var codigo = this.promoAcargar.CodArt; | 129 | var codigo = this.promoAcargar.CodArt; |
130 | this.productoService.getPromocionSinonimos(sector, codigo) | 130 | this.productoService.getPromocionSinonimos(sector, codigo) |
131 | .subscribe((res: Sinonimo[]) => { | 131 | .subscribe((res: Sinonimo[]) => { |
132 | 132 | ||
133 | res.forEach(sinonimo => { | 133 | res.forEach(sinonimo => { |
134 | 134 | ||
135 | sinonimo.productos.forEach(productoSinonimo => { | 135 | sinonimo.productos.forEach(productoSinonimo => { |
136 | 136 | ||
137 | this.promoAcargar.productos.forEach(productoPromo => { | 137 | this.promoAcargar.productos.forEach(productoPromo => { |
138 | 138 | ||
139 | if (productoSinonimo.id === productoPromo.id) { | 139 | if (productoSinonimo.id === productoPromo.id) { |
140 | sinonimo.cantidad = productoPromo.cantidad; | 140 | sinonimo.cantidad = productoPromo.cantidad; |
141 | sinonimo.productoPadre = productoPromo.id; | 141 | sinonimo.productoPadre = productoPromo.id; |
142 | } | 142 | } |
143 | }) | 143 | }) |
144 | }) | 144 | }) |
145 | }) | 145 | }) |
146 | 146 | ||
147 | this.sinonimos = res; | 147 | this.sinonimos = res; |
148 | if (res.length > 0) | 148 | if (res.length > 0) |
149 | this.showPopover(); | 149 | this.popoverDirective.show(); |
150 | else | 150 | else |
151 | this.popoverDirective.hide(); | 151 | this.popoverDirective.hide(); |
152 | }) | 152 | }) |
153 | } else { | 153 | } else if (this.popoverDirective) { |
154 | this.popoverDirective.hide(); | 154 | this.popoverDirective.hide(); |
155 | } | 155 | } |
156 | } | 156 | } |
157 | 157 | ||
158 | productosPersonalizados($event: Producto[]) { | 158 | productosPersonalizados($event: Producto[]) { |
159 | 159 | ||
160 | var productosPersonalizados = $event; | 160 | var productosPersonalizados = $event; |
161 | 161 | ||
162 | this.promoAcargar.productos.forEach(productoPromo => { | 162 | this.promoAcargar.productos.forEach(productoPromo => { |
163 | 163 | ||
164 | if (!productoPromo.idSinonimo) productosPersonalizados.push(productoPromo); | 164 | if (!productoPromo.idSinonimo) productosPersonalizados.push(productoPromo); |
165 | }) | 165 | }) |
166 | 166 | ||
167 | this.promoAcargar.productos = productosPersonalizados; | 167 | this.promoAcargar.productos = productosPersonalizados; |
168 | this.confirmarProducto(); | 168 | this.confirmarProducto(); |
169 | } | 169 | } |
170 | 170 | ||
171 | buscarByCodigoBarras(busqueda) { | 171 | buscarByCodigoBarras(busqueda) { |
172 | 172 | ||
173 | let producto = this.productos.filter(producto => { | 173 | let producto = this.productos.filter(producto => { |
174 | return producto.codigoBarra == busqueda; | 174 | return producto.codigoBarra == busqueda; |
175 | }); | 175 | }); |
176 | 176 | ||
177 | if (producto.length) { | 177 | if (producto.length) { |
178 | 178 | ||
179 | this.productoAcargar = producto[0]; | 179 | this.productoAcargar = producto[0]; |
180 | this.getPromociones(); | 180 | this.getPromociones(); |
181 | 181 | ||
182 | } else { | 182 | } else { |
183 | alert('No se encuentra el producto'); | 183 | alert('No se encuentra el producto'); |
184 | } | 184 | } |
185 | 185 | ||
186 | } | 186 | } |
187 | 187 | ||
188 | } | 188 | } |
189 | 189 |