Commit 22a76c25a448e7de6a26b7f1ffcb944006d2effe

Authored by Eric Fernandez
1 parent 977726522b
Exists in master

fix, se pisan productos2

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