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