Commit 41ed6251851c520a30bacf61be1eaca8f82e1cc0

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

arreglo del producto PEBETE.

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