Commit a2074bb5b4964aac9b002a0382265afb3eeff598

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

Arreglo cuando se carga un producto al carrito.

Showing 1 changed file with 1 additions and 1 deletions   Show diff stats
src/app/components/inicio/inicio.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'; 1 import { Component, OnInit, ViewChild } 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 { Producto } from 'src/app/wrappers/producto'; 4 import { Producto } from 'src/app/wrappers/producto';
5 import { ProductoService } from 'src/app/services/producto.service'; 5 import { ProductoService } from 'src/app/services/producto.service';
6 import { Router } from '@angular/router'; 6 import { Router } from '@angular/router';
7 import { Promocion } from 'src/app/wrappers/promocion'; 7 import { Promocion } from 'src/app/wrappers/promocion';
8 import { Sinonimo } from 'src/app/wrappers/sinonimo'; 8 import { Sinonimo } from 'src/app/wrappers/sinonimo';
9 import { HostListener } from '@angular/core'; 9 import { HostListener } from '@angular/core';
10 10
11 @Component({ 11 @Component({
12 selector: 'app-inicio', 12 selector: 'app-inicio',
13 templateUrl: './inicio.component.html', 13 templateUrl: './inicio.component.html',
14 styleUrls: ['./inicio.component.scss'] 14 styleUrls: ['./inicio.component.scss']
15 }) 15 })
16 export class InicioComponent implements OnInit { 16 export class InicioComponent implements OnInit {
17 17
18 @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) { 18 @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) {
19 19
20 if (e.keyCode == 13) { 20 if (e.keyCode == 13) {
21 this.buscarByCodigoBarras(this.busqueda); 21 this.buscarByCodigoBarras(this.busqueda);
22 this.busqueda = ''; 22 this.busqueda = '';
23 } else { 23 } else {
24 this.busqueda += e.key; 24 this.busqueda += e.key;
25 } 25 }
26 26
27 }; 27 };
28 28
29 @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective; 29 @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective;
30 private productoAcargar: Producto; 30 private productoAcargar: Producto;
31 private productos: Producto[]; 31 private productos: Producto[];
32 private promoAcargar: Promocion; 32 private promoAcargar: Promocion;
33 private tienePromo = false; 33 private tienePromo = false;
34 private productoEsPromo = false; 34 private productoEsPromo = false;
35 private busqueda: string = ''; 35 private busqueda: string = '';
36 private sinonimoAcargar: Sinonimo; 36 private sinonimoAcargar: Sinonimo;
37 37
38 promociones: Promocion[] = []; 38 promociones: Promocion[] = [];
39 sinonimos: Sinonimo[] = []; 39 sinonimos: Sinonimo[] = [];
40 apiUrl: string = appSettings.apiUrl 40 apiUrl: string = appSettings.apiUrl
41 41
42 constructor( 42 constructor(
43 private router: Router, 43 private router: Router,
44 private productoService: ProductoService) { } 44 private productoService: ProductoService) { }
45 45
46 ngOnInit() { 46 ngOnInit() {
47 47
48 this.productoAcargar = this.productoService.productoAcargar; 48 this.productoAcargar = this.productoService.productoAcargar;
49 this.getPromociones(); 49 this.getPromociones();
50 this.getProductos(); 50 this.getProductos();
51 } 51 }
52 52
53 getPromociones() { 53 getPromociones() {
54 if (this.productoAcargar) { 54 if (this.productoAcargar) {
55 var sector = this.productoAcargar.CodSec; 55 var sector = this.productoAcargar.CodSec;
56 var codigo = this.productoAcargar.CodArt; 56 var codigo = this.productoAcargar.CodArt;
57 this.productoService.getPromocion(sector, codigo) 57 this.productoService.getPromocion(sector, codigo)
58 .subscribe((res: Promocion[]) => { 58 .subscribe((res: Promocion[]) => {
59 59
60 if (res.length === 0) { 60 if (res.length === 0) {
61 //Si no tiene promociones la cargará al carrito despues de un tiempo 61 //Si no tiene promociones la cargará al carrito despues de un tiempo
62 setTimeout(() => { 62 setTimeout(() => {
63 this.productoService.productos.push(this.productoAcargar); 63 this.productoService.productos.push(this.productoAcargar);
64 this.productoAcargar = undefined; 64 this.productoAcargar = this.productoService.productoAcargar = undefined;
65 }, 2000) 65 }, 2000)
66 } else { 66 } else {
67 67
68 this.promociones = res; 68 this.promociones = res;
69 this.popoverDirective.show(); 69 this.popoverDirective.show();
70 } 70 }
71 }, error => { console.error(error); }) 71 }, error => { console.error(error); })
72 } 72 }
73 } 73 }
74 74
75 showPopover() { 75 showPopover() {
76 76
77 this.popoverDirective.show(); 77 this.popoverDirective.show();
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 private irBusquedaProductos(verPromociones) { 87 private 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 deshacerCarga() { 93 deshacerCarga() {
94 94
95 if (this.sinonimoAcargar || this.sinonimos.length > 0) { 95 if (this.sinonimoAcargar || this.sinonimos.length > 0) {
96 this.sinonimos = []; 96 this.sinonimos = [];
97 this.sinonimoAcargar = undefined; 97 this.sinonimoAcargar = undefined;
98 this.popoverDirective.hide(); 98 this.popoverDirective.hide();
99 } 99 }
100 100
101 if (this.promoAcargar) { 101 if (this.promoAcargar) {
102 this.promoAcargar = undefined; 102 this.promoAcargar = undefined;
103 this.popoverDirective.show(); 103 this.popoverDirective.show();
104 } else { 104 } else {
105 this.productoAcargar = undefined; 105 this.productoAcargar = undefined;
106 this.promociones = []; 106 this.promociones = [];
107 this.popoverDirective.hide(); 107 this.popoverDirective.hide();
108 } 108 }
109 } 109 }
110 110
111 promoSeleccionada($event: Promocion) { 111 promoSeleccionada($event: Promocion) {
112 112
113 this.promoAcargar = $event; 113 this.promoAcargar = $event;
114 this.popoverDirective.hide(); 114 this.popoverDirective.hide();
115 if (this.promoAcargar.sinonimos) { 115 if (this.promoAcargar.sinonimos) {
116 var sector = this.promoAcargar.sector; 116 var sector = this.promoAcargar.sector;
117 var codigo = this.promoAcargar.codigo; 117 var codigo = this.promoAcargar.codigo;
118 this.productoService.getPromocionSinonimos(sector, codigo) 118 this.productoService.getPromocionSinonimos(sector, codigo)
119 .subscribe((res: Sinonimo[]) => { 119 .subscribe((res: Sinonimo[]) => {
120 120
121 res.forEach(resSinonimo => { 121 res.forEach(resSinonimo => {
122 122
123 this.promoAcargar.productos.forEach(productoPromo => { 123 this.promoAcargar.productos.forEach(productoPromo => {
124 124
125 if (productoPromo.idSinonimo === resSinonimo.ID_SIN) { 125 if (productoPromo.idSinonimo === resSinonimo.ID_SIN) {
126 resSinonimo.cantidad = productoPromo.cantidad; 126 resSinonimo.cantidad = productoPromo.cantidad;
127 resSinonimo.productoPadre = productoPromo.id; 127 resSinonimo.productoPadre = productoPromo.id;
128 } 128 }
129 }); 129 });
130 130
131 }) 131 })
132 this.sinonimos = res; 132 this.sinonimos = res;
133 this.showPopover(); 133 this.showPopover();
134 }) 134 })
135 } 135 }
136 } 136 }
137 137
138 sinonimoSeleccionado($event: Sinonimo) { 138 sinonimoSeleccionado($event: Sinonimo) {
139 139
140 console.log($event); 140 console.log($event);
141 this.sinonimoAcargar = $event; 141 this.sinonimoAcargar = $event;
142 } 142 }
143 143
144 buscarByCodigoBarras(busqueda) { 144 buscarByCodigoBarras(busqueda) {
145 145
146 let producto = this.productos.filter(producto => { 146 let producto = this.productos.filter(producto => {
147 return producto.codigoBarra == busqueda; 147 return producto.codigoBarra == busqueda;
148 }); 148 });
149 149
150 if (producto.length) { 150 if (producto.length) {
151 151
152 this.productoAcargar = producto[0]; 152 this.productoAcargar = producto[0];
153 this.getPromociones(); 153 this.getPromociones();
154 154
155 } else { 155 } else {
156 alert('No se encuentra el producto'); 156 alert('No se encuentra el producto');
157 } 157 }
158 158
159 } 159 }
160 160
161 } 161 }
162 162