Commit eaf216e31119164baae1de3414b2cb5aea9a45a4

Authored by Marcelo Puebla
1 parent 416f7e8bb5
Exists in master and in 1 other branch validar_pve

Arreglo por si en el get de sinonimos viene un array vacio.

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