Commit 2c319982ac8b9a21870618e24355c196f293bf0a

Authored by Eric Fernandez
1 parent 103858e63e
Exists in master

busqueda by codigo de barras

src/app/components/inicio/inicio.component.ts
... ... @@ -16,11 +16,19 @@ import { HostListener } from '@angular/core';
16 16 export class InicioComponent implements OnInit {
17 17  
18 18 @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) {
19   - this.busqueda += e.key;
  19 +
  20 + if (e.keyCode == 13) {
  21 + this.buscarByCodigoBarras(this.busqueda);
  22 + this.busqueda = '';
  23 + } else {
  24 + this.busqueda += e.key;
  25 + }
  26 +
20 27 };
21 28  
22 29 @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective;
23 30 private productoAcargar: Producto;
  31 + private productos: Producto[];
24 32 private promoAcargar: Promocion;
25 33 private tienePromo = false;
26 34 private productoEsPromo = false;
... ... @@ -37,10 +45,9 @@ export class InicioComponent implements OnInit {
37 45  
38 46 ngOnInit() {
39 47  
40   - this.vaciarBusqueda();
41   -
42 48 this.productoAcargar = this.productoService.productoAcargar;
43 49 this.getPromociones();
  50 + this.getProductos();
44 51 }
45 52  
46 53 getPromociones() {
... ... @@ -70,6 +77,13 @@ export class InicioComponent implements OnInit {
70 77 this.popoverDirective.show();
71 78 }
72 79  
  80 + getProductos() {
  81 + this.productoService.getAll()
  82 + .subscribe((productos: Producto[]) => {
  83 + this.productos = productos;
  84 + });
  85 + }
  86 +
73 87 private goPage(pageUrl) {
74 88  
75 89 this.router.navigate([pageUrl]);
... ... @@ -118,17 +132,27 @@ export class InicioComponent implements OnInit {
118 132 }
119 133 }
120 134  
121   - vaciarBusqueda() {
122   -
123   - setTimeout(() => {
124   - this.busqueda = '';
125   - }, 2000);
126   - }
127   -
128 135 sinonimoSeleccionado($event: Sinonimo) {
129 136  
130 137 console.log($event);
131 138 this.sinonimoAcargar = $event;
132 139 }
133 140  
  141 + buscarByCodigoBarras(busqueda) {
  142 +
  143 + let producto = this.productos.filter(producto => {
  144 + return producto.codigoBarra == busqueda;
  145 + });
  146 +
  147 + if (producto.length) {
  148 +
  149 + this.productoAcargar = producto[0];
  150 + this.getPromociones();
  151 +
  152 + } else {
  153 + alert('No se encuentra el producto');
  154 + }
  155 +
  156 + }
  157 +
134 158 }
src/app/wrappers/producto.ts
... ... @@ -91,4 +91,5 @@ export interface Producto {
91 91 cantidad?: number;
92 92 showCargarProducto?: boolean;
93 93 esPadre?: boolean;
  94 + codigoBarra: string;
94 95 }