Commit b9afdd7cddd401683b084ff249fe2cb3b389ed20

Authored by Marcelo Puebla
Exists in master

Merge branch 'master' into 'master'

Master(efernandez)

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