Commit 4301149e16b1c0d8444e4316a856c8ea105685e0

Authored by Marcelo Puebla
1 parent 02a11ddacf
Exists in master and in 1 other branch validar_pve

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 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 apiUrl: string = appSettings.apiUrl 24 private apiUrl: string = appSettings.apiUrl
25 25
26 @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective; 26 @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective;
27 27
28 @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) { 28 @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) {
29 29
30 if (e.keyCode == 13) { 30 if (e.keyCode == 13) {
31 this.buscarByCodigoBarras(this.busqueda); 31 this.buscarByCodigoBarras(this.busqueda);
32 this.busqueda = ''; 32 this.busqueda = '';
33 } else { 33 } else {
34 this.busqueda += e.key; 34 this.busqueda += e.key;
35 } 35 }
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 ngOnInit() { 43 ngOnInit() {
44 44
45 this.productoAcargar = this.productoService.productoAcargar; 45 this.productoAcargar = this.productoService.productoAcargar;
46 if (this.productoAcargar) { 46 if (this.productoAcargar) {
47 if (this.productoAcargar.PRO) { 47 if (this.productoAcargar.PRO) {
48 this.promociones.push(this.productoAcargar); 48 this.promociones.push(this.productoAcargar);
49 this.promoSeleccionada(this.productoAcargar); 49 this.promoSeleccionada(this.productoAcargar);
50 } 50 }
51 else 51 else
52 this.getPromociones(); 52 this.getPromociones();
53 } 53 }
54 this.getProductos(); 54 this.getProductos();
55 } 55 }
56 56
57 getPromociones() { 57 getPromociones() {
58 58
59 var sector = this.productoAcargar.CodSec; 59 var sector = this.productoAcargar.CodSec;
60 var codigo = this.productoAcargar.CodArt; 60 var codigo = this.productoAcargar.CodArt;
61 this.productoService.getPromociones(sector, codigo) 61 this.productoService.getPromociones(sector, codigo)
62 .subscribe((res: Producto[]) => { 62 .subscribe((res: Producto[]) => {
63 63
64 if (res.length === 0) { 64 if (res.length === 0) {
65 //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
66 // setTimeout(() => { 66 // setTimeout(() => {
67 this.productoAcargar.cantidad = 1; 67 this.productoAcargar.cantidad = 1;
68 this.productoService.productos.push(this.productoAcargar); 68 this.productoService.productos.push(this.productoAcargar);
69 this.productoAcargar = this.productoService.productoAcargar = undefined; 69 this.productoAcargar = this.productoService.productoAcargar = undefined;
70 // }, 2000) 70 // }, 2000)
71 } else { 71 } else {
72 72
73 this.promociones = res; 73 this.promociones = res;
74 this.popoverDirective.show(); 74 this.popoverDirective.show();
75 } 75 }
76 }, error => { console.error(error); }) 76 }, error => { console.error(error); })
77 } 77 }
78 78
79 getProductos() { 79 getProductos() {
80 80
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 var producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar; 94 var producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar;
95 producto.cantidad = 1; 95 producto.cantidad = 1;
96 this.productoService.productos.push(producto); 96 this.productoService.productos.push(producto);
97 this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; 97 this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined;
98 this.promociones = []; 98 this.promociones = [];
99 this.popoverDirective.hide(); 99 this.popoverDirective.hide();
100 } 100 }
101 101
102 deshacerCarga() { 102 deshacerCarga() {
103 103
104 if (this.sinonimos.length > 0) { 104 if (this.sinonimos.length > 0) {
105 this.sinonimos = []; 105 this.sinonimos = [];
106 this.popoverDirective.hide(); 106 this.popoverDirective.hide();
107 } 107 }
108 108
109 if (this.promoAcargar) { 109 if (this.promoAcargar) {
110 this.promoAcargar = undefined; 110 this.promoAcargar = undefined;
111 if (this.productoAcargar.PRO) { 111 if (this.productoAcargar.PRO) {
112 this.productoAcargar = undefined; 112 this.productoAcargar = undefined;
113 this.promociones = []; 113 this.promociones = [];
114 this.popoverDirective.hide(); 114 this.popoverDirective.hide();
115 } else { 115 } else {
116 this.popoverDirective.show(); 116 this.popoverDirective.show();
117 } 117 }
118 } else { 118 } else {
119 this.productoAcargar = undefined; 119 this.productoAcargar = undefined;
120 this.promociones = []; 120 this.promociones = [];
121 this.popoverDirective.hide(); 121 this.popoverDirective.hide();
122 } 122 }
123 } 123 }
124 124
125 promoSeleccionada($event: Producto) { 125 promoSeleccionada($event: Producto) {
126 126
127 if ($event.tieneSinonimos) { 127 if ($event.tieneSinonimos) {
128 128
129 this.productoService.getProductoById($event.id) 129 this.productoService.getProductoById($event.id)
130 .subscribe((res : Producto) => { 130 .subscribe((res: Producto) => {
131 131
132 $event.imagenes = res.imagenes; 132 $event.imagenes = res.imagenes;
133 this.promoAcargar = $event; 133 this.promoAcargar = $event;
134 this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt); 134 this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt);
135 }, 135 },
136 error => { console.log(error); }) 136 error => { console.log(error); })
137 137
138 138
139 } else if (this.popoverDirective) { 139 } else if (this.popoverDirective) {
140 this.popoverDirective.hide(); 140 this.popoverDirective.hide();
141 } 141 }
142 } 142 }
143 143
144 getSinonimos(sector, codigo) { 144 getSinonimos(sector, codigo) {
145 145
146 this.productoService.getPromocionSinonimos(sector, codigo) 146 this.productoService.getPromocionSinonimos(sector, codigo)
147 .subscribe((res: Sinonimo[]) => { 147 .subscribe((res: Sinonimo[]) => {
148 148
149 res.forEach(sinonimo => { 149 res.forEach(sinonimo => {
150 150
151 sinonimo.productos.forEach(productoSinonimo => { 151 sinonimo.productos.forEach(productoSinonimo => {
152 152
153 this.promoAcargar.productos.forEach(productoPromo => { 153 this.promoAcargar.productos.forEach(productoPromo => {
154 154
155 if (productoSinonimo.id === productoPromo.id) { 155 if (productoSinonimo.id === productoPromo.id) {
156 sinonimo.cantidad = productoPromo.cantidad; 156 sinonimo.cantidad = productoPromo.cantidad;
157 sinonimo.productoPadre = productoPromo.id; 157 sinonimo.productoPadre = productoPromo.id;
158 } 158 }
159 }) 159 })
160 }) 160 })
161 }) 161 })
162 162
163 this.sinonimos = res; 163 this.sinonimos = res;
164 if (res.length > 0) 164 if (res.length > 0)
165 this.popoverDirective.show(); 165 this.popoverDirective.show();
166 else 166 else
167 this.popoverDirective.hide(); 167 this.popoverDirective.hide();
168 }) 168 })
169 } 169 }
170 170
171 productosPersonalizados($event: Producto[]) { 171 productosPersonalizados($event: Producto[]) {
172 172
173 var productosPersonalizados = $event; 173 var productosPersonalizados = $event;
174 174
175 this.promoAcargar.productos.forEach(productoPromo => { 175 this.promoAcargar.productos.forEach(productoPromo => {
176 176
177 if (!productoPromo.idSinonimo) productosPersonalizados.push(productoPromo); 177 if (!productoPromo.idSinonimo) productosPersonalizados.push(productoPromo);
178 }) 178 })
179 179
180 this.promoAcargar.productos = productosPersonalizados; 180 this.promoAcargar.productos = productosPersonalizados;
181 this.confirmarProducto(); 181 this.confirmarProducto();
182 } 182 }
183 183
184 buscarByCodigoBarras(busqueda) { 184 buscarByCodigoBarras(busqueda) {
185 185
186 let producto = this.productos.filter(producto => { 186 let producto = this.productos.filter(producto => {
187 return producto.codigoBarra == busqueda; 187 return producto.codigoBarra == busqueda;
188 }); 188 });
189 189
190 if (producto.length) { 190 if (producto.length) {
191 191
192 this.productoAcargar = producto[0]; 192 this.productoAcargar = producto[0];
193 this.getPromociones(); 193 this.getPromociones();
194 194
195 } else { 195 } else {
196 alert('No se encuentra el producto'); 196 alert('No se encuentra el producto');
197 } 197 }
198 198
199 } 199 }
200 200
201 } 201 }
202 202