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