Commit b160476d2e17e3ebfd4eea986ba8ec9af2fcd10b

Authored by Eric Fernandez
1 parent 7c801ad76d
Exists in master

fix productos no promo2

Showing 1 changed file with 6 additions and 3 deletions   Show diff stats
src/app/components/inicio/inicio.component.ts
1 import { Component, OnInit, ViewChild, HostListener, AfterViewInit } from '@angular/core'; 1 import { Component, OnInit, ViewChild, HostListener, AfterViewInit } 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, AfterViewInit { 14 export class InicioComponent implements OnInit, AfterViewInit {
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 apiImagenes: string = appSettings.apiImagenes; 24 private apiImagenes: string = appSettings.apiImagenes;
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 43
44 ngOnInit() { 44 ngOnInit() {
45 45
46 this.productoAcargar = this.productoService.productoAcargar; 46 this.productoAcargar = this.productoService.productoAcargar;
47 this.getProductos(); 47 this.getProductos();
48 } 48 }
49 49
50 ngAfterViewInit() { 50 ngAfterViewInit() {
51 51
52 setTimeout(() => { 52 setTimeout(() => {
53 if (!this.productoAcargar) return; 53 if (!this.productoAcargar) return;
54 54
55 if (this.productoAcargar.PRO) { 55 if (this.productoAcargar.PRO) {
56 this.promociones.push(this.productoAcargar); 56 this.promociones.push(this.productoAcargar);
57 this.promoSeleccionada(this.productoAcargar, true); 57 this.promoSeleccionada(this.productoAcargar, true);
58 } 58 }
59 else { 59 else {
60 this.getPromociones(); 60 this.getPromociones();
61 } 61 }
62 }) 62 })
63 } 63 }
64 64
65 getProductos() { 65 getProductos() {
66 66
67 this.productoService.getAll() 67 this.productoService.getAll()
68 .subscribe((productos: Producto[]) => { 68 .subscribe((productos: Producto[]) => {
69 this.productos = productos; 69 this.productos = productos;
70 }); 70 });
71 } 71 }
72 72
73 getPromociones() { 73 getPromociones() {
74 74
75 var sector = this.productoAcargar.CodSec; 75 var sector = this.productoAcargar.CodSec;
76 var codigo = this.productoAcargar.CodArt; 76 var codigo = this.productoAcargar.CodArt;
77 this.productoService.getPromociones(sector, codigo) 77 this.productoService.getPromociones(sector, codigo)
78 .subscribe((res: Producto[]) => { 78 .subscribe((res: Producto[]) => {
79 79
80 if (res.length === 0) { 80 if (res.length === 0) {
81 81
82 this.productoAcargar.cantidad = 1; 82 this.productoAcargar.cantidad = 1;
83 this.productoService.setProductos(this.productoAcargar); 83 this.productoService.setProductos(this.productoAcargar);
84 this.productoAcargar = this.productoService.productoAcargar = undefined; 84 this.productoAcargar = this.productoService.productoAcargar = undefined;
85 } else { 85 } else {
86 86
87 this.promociones = res; 87 this.promociones = res;
88 this.popoverDirective.show(); 88 this.popoverDirective.show();
89 } 89 }
90 }, error => { console.error(error); }) 90 }, error => { console.error(error); })
91 } 91 }
92 92
93 confirmarProducto() { 93 confirmarProducto() {
94 94
95 let producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar; 95 let producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar;
96 96
97 producto.productos = producto.productos.filter(producto => { 97 if (producto.productos) {
98 return producto.idSinonimo > 0; 98
99 }); 99 producto.productos = producto.productos.filter(producto => {
100 return producto.idSinonimo > 0;
101 });
102 }
100 103
101 producto.cantidad = producto.cantidad ? producto.cantidad : 1; 104 producto.cantidad = producto.cantidad ? producto.cantidad : 1;
102 105
103 this.productoService.setProductos(producto); 106 this.productoService.setProductos(producto);
104 this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; 107 this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined;
105 this.productoService.esPromoPersonalizada = false; 108 this.productoService.esPromoPersonalizada = false;
106 this.promociones = []; 109 this.promociones = [];
107 this.popoverDirective.hide(); 110 this.popoverDirective.hide();
108 } 111 }
109 112
110 promoSeleccionada($event: Producto, showPopover: boolean) { 113 promoSeleccionada($event: Producto, showPopover: boolean) {
111 114
112 this.productoService.getProductoById($event.id) 115 this.productoService.getProductoById($event.id)
113 .subscribe((res: Producto) => { 116 .subscribe((res: Producto) => {
114 117
115 $event.imagenes = res.imagenes.length == 0 ? [{ imagen: 'noImage.jpg' }] : res.imagenes; 118 $event.imagenes = res.imagenes.length == 0 ? [{ imagen: 'noImage.jpg' }] : res.imagenes;
116 this.promoAcargar = $event; 119 this.promoAcargar = $event;
117 120
118 if ($event.tieneSinonimos) { 121 if ($event.tieneSinonimos) {
119 this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt); 122 this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt);
120 } else if (showPopover) { 123 } else if (showPopover) {
121 this.popoverDirective.show(); 124 this.popoverDirective.show();
122 } else { 125 } else {
123 this.popoverDirective.hide(); 126 this.popoverDirective.hide();
124 } 127 }
125 128
126 }, 129 },
127 error => { console.log(error); }) 130 error => { console.log(error); })
128 } 131 }
129 132
130 getSinonimos(sector, codigo) { 133 getSinonimos(sector, codigo) {
131 134
132 this.productoService.getPromocionSinonimos(sector, codigo) 135 this.productoService.getPromocionSinonimos(sector, codigo)
133 .subscribe((res: Sinonimo[]) => { 136 .subscribe((res: Sinonimo[]) => {
134 137
135 res.forEach(sinonimo => { 138 res.forEach(sinonimo => {
136 139
137 sinonimo.cantidad = 0; 140 sinonimo.cantidad = 0;
138 this.promoAcargar.productos.forEach(productoPromo => { 141 this.promoAcargar.productos.forEach(productoPromo => {
139 142
140 sinonimo.productos.forEach(productoSinonimo => { 143 sinonimo.productos.forEach(productoSinonimo => {
141 144
142 if (productoSinonimo.id === productoPromo.id) { 145 if (productoSinonimo.id === productoPromo.id) {
143 productoSinonimo.cantidad = productoPromo.cantidad; 146 productoSinonimo.cantidad = productoPromo.cantidad;
144 sinonimo.cantidad += productoPromo.cantidad; 147 sinonimo.cantidad += productoPromo.cantidad;
145 } 148 }
146 }) 149 })
147 }) 150 })
148 }) 151 })
149 this.sinonimos = res; 152 this.sinonimos = res;
150 (this.sinonimos.length > 0) ? this.popoverDirective.show() : this.popoverDirective.hide(); 153 (this.sinonimos.length > 0) ? this.popoverDirective.show() : this.popoverDirective.hide();
151 }) 154 })
152 } 155 }
153 156
154 productosPersonalizados($event: Producto[]) { 157 productosPersonalizados($event: Producto[]) {
155 158
156 let productosPersonalizados = $event; 159 let productosPersonalizados = $event;
157 this.promoAcargar.productos = productosPersonalizados; 160 this.promoAcargar.productos = productosPersonalizados;
158 this.confirmarProducto(); 161 this.confirmarProducto();
159 } 162 }
160 163
161 buscarByCodigoBarras(busqueda) { 164 buscarByCodigoBarras(busqueda) {
162 165
163 let producto = this.productos.filter(producto => { 166 let producto = this.productos.filter(producto => {
164 return producto.codigoBarra == busqueda; 167 return producto.codigoBarra == busqueda;
165 }); 168 });
166 169
167 if (producto.length) { 170 if (producto.length) {
168 171
169 this.productoAcargar = producto[0]; 172 this.productoAcargar = producto[0];
170 this.getPromociones(); 173 this.getPromociones();
171 174
172 } else { 175 } else {
173 alert('No se encuentra el producto'); 176 alert('No se encuentra el producto');
174 } 177 }
175 178
176 } 179 }
177 180
178 irBusquedaProductos(value) { 181 irBusquedaProductos(value) {
179 182
180 this.productoService.mostrar = value; 183 this.productoService.mostrar = value;
181 this.router.navigate(['busqueda-productos']); 184 this.router.navigate(['busqueda-productos']);
182 } 185 }
183 186
184 deshacerCarga() { 187 deshacerCarga() {
185 188
186 if (this.sinonimos.length > 0) { 189 if (this.sinonimos.length > 0) {
187 this.sinonimos = []; 190 this.sinonimos = [];
188 this.productoService.esPromoPersonalizada = false; 191 this.productoService.esPromoPersonalizada = false;
189 this.popoverDirective.hide(); 192 this.popoverDirective.hide();
190 } 193 }
191 194
192 if (this.promoAcargar) { 195 if (this.promoAcargar) {
193 this.promoAcargar = undefined; 196 this.promoAcargar = undefined;
194 if (this.productoAcargar.PRO) { 197 if (this.productoAcargar.PRO) {
195 this.productoAcargar = undefined; 198 this.productoAcargar = undefined;
196 this.promociones = []; 199 this.promociones = [];
197 this.popoverDirective.hide(); 200 this.popoverDirective.hide();
198 } else { 201 } else {
199 this.popoverDirective.show(); 202 this.popoverDirective.show();
200 } 203 }
201 } else { 204 } else {
202 this.productoAcargar = undefined; 205 this.productoAcargar = undefined;
203 this.promociones = []; 206 this.promociones = [];
204 this.popoverDirective.hide(); 207 this.popoverDirective.hide();
205 } 208 }
206 } 209 }
207 210
208 } 211 }
209 212