Commit 2695e89a60b71e876856882e606fdbad56fce8f5
Exists in
master
and in
1 other branch
Merge branch 'master' into 'master'
Master(efernandez) See merge request !115
Showing
2 changed files
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 ? false : 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 | 153 | ||
154 | productosPersonalizados.forEach(productoElegido => { | 154 | productosPersonalizados.forEach(productoElegido => { |
155 | 155 | ||
156 | this.promoAcargar.productos = this.promoAcargar.productos.filter(productoPromo => { | 156 | this.promoAcargar.productos = this.promoAcargar.productos.filter(productoPromo => { |
157 | return productoPromo.idSinonimo != productoElegido.idSinonimo; | 157 | return productoPromo.idSinonimo != productoElegido.idSinonimo; |
158 | }); | 158 | }); |
159 | }); | 159 | }); |
160 | 160 | ||
161 | this.promoAcargar.productos = this.promoAcargar.productos.concat(productosPersonalizados); | 161 | this.promoAcargar.productos = this.promoAcargar.productos.concat(productosPersonalizados); |
162 | 162 | ||
163 | this.confirmarProducto(); | 163 | this.confirmarProducto(); |
164 | } | 164 | } |
165 | 165 | ||
166 | buscarByCodigoBarras(busqueda) { | 166 | buscarByCodigoBarras(busqueda) { |
167 | 167 | ||
168 | let producto = this.productos.filter(producto => { | 168 | let producto = this.productos.filter(producto => { |
169 | return producto.codigoBarra == busqueda; | 169 | return producto.codigoBarra == busqueda; |
170 | }); | 170 | }); |
171 | 171 | ||
172 | if (producto.length) { | 172 | if (producto.length) { |
173 | 173 | ||
174 | this.productoAcargar = producto[0]; | 174 | this.productoAcargar = producto[0]; |
175 | this.getPromociones(); | 175 | this.getPromociones(); |
176 | 176 | ||
177 | } else { | 177 | } else { |
178 | alert('No se encuentra el producto'); | 178 | alert('No se encuentra el producto'); |
179 | } | 179 | } |
180 | 180 | ||
181 | } | 181 | } |
182 | 182 | ||
183 | irBusquedaProductos(value) { | 183 | irBusquedaProductos(value) { |
184 | 184 | ||
185 | this.productoService.mostrar = value; | 185 | this.productoService.mostrar = value; |
186 | this.router.navigate(['busqueda-productos']); | 186 | this.router.navigate(['busqueda-productos']); |
187 | } | 187 | } |
188 | 188 | ||
189 | deshacerCarga() { | 189 | deshacerCarga() { |
190 | 190 | ||
191 | if (this.sinonimos.length > 0) { | 191 | if (this.sinonimos.length > 0) { |
192 | this.sinonimos = []; | 192 | this.sinonimos = []; |
193 | this.productoService.esPromoPersonalizada = false; | 193 | this.productoService.esPromoPersonalizada = false; |
194 | this.popoverDirective.hide(); | 194 | this.popoverDirective.hide(); |
195 | } | 195 | } |
196 | 196 | ||
197 | if (this.promoAcargar) { | 197 | if (this.promoAcargar) { |
198 | this.promoAcargar = undefined; | 198 | this.promoAcargar = undefined; |
199 | if (this.productoAcargar.PRO) { | 199 | if (this.productoAcargar.PRO) { |
200 | this.productoAcargar = undefined; | 200 | this.productoAcargar = undefined; |
201 | this.promociones = []; | 201 | this.promociones = []; |
202 | this.popoverDirective.hide(); | 202 | this.popoverDirective.hide(); |
203 | } else { | 203 | } else { |
204 | this.popoverDirective.show(); | 204 | this.popoverDirective.show(); |
205 | } | 205 | } |
206 | } else { | 206 | } else { |
207 | this.productoAcargar = undefined; | 207 | this.productoAcargar = undefined; |
208 | this.promociones = []; | 208 | this.promociones = []; |
209 | this.popoverDirective.hide(); | 209 | this.popoverDirective.hide(); |
210 | } | 210 | } |
211 | } | 211 | } |
212 | 212 | ||
213 | } | 213 | } |
214 | 214 |
src/app/components/sidebar/sidebar.component.ts
1 | import { Component, OnInit } from '@angular/core'; | 1 | import { Component, OnInit } from '@angular/core'; |
2 | import { Producto } from 'src/app/wrappers/producto'; | 2 | import { Producto } from 'src/app/wrappers/producto'; |
3 | import { appSettings } from 'src/etc/AppSettings'; | 3 | import { appSettings } from 'src/etc/AppSettings'; |
4 | import { ProductoService } from 'src/app/services/producto.service'; | 4 | import { ProductoService } from 'src/app/services/producto.service'; |
5 | import { Router } from '@angular/router'; | 5 | import { Router } from '@angular/router'; |
6 | 6 | ||
7 | @Component({ | 7 | @Component({ |
8 | selector: 'app-sidebar', | 8 | selector: 'app-sidebar', |
9 | templateUrl: './sidebar.component.html', | 9 | templateUrl: './sidebar.component.html', |
10 | styleUrls: ['./sidebar.component.scss'], | 10 | styleUrls: ['./sidebar.component.scss'], |
11 | }) | 11 | }) |
12 | export class SidebarComponent implements OnInit { | 12 | export class SidebarComponent implements OnInit { |
13 | 13 | ||
14 | private cantTotal: number = 0; | 14 | private cantTotal: number = 0; |
15 | private cantMin: number = 1; | 15 | private cantMin: number = 1; |
16 | private cantMax: number = 50; | 16 | private cantMax: number = 50; |
17 | private total: number = 0; | 17 | private total: number = 0; |
18 | private apiImagenes: string = appSettings.apiImagenes; | 18 | private apiImagenes: string = appSettings.apiImagenes; |
19 | 19 | ||
20 | public productosCarrito: Producto[] = []; | 20 | public productosCarrito: Producto[] = []; |
21 | 21 | ||
22 | constructor( | 22 | constructor( |
23 | private productoService: ProductoService, | 23 | private productoService: ProductoService, |
24 | private router: Router) { | 24 | private router: Router) { |
25 | this.router.routeReuseStrategy.shouldReuseRoute = function () { | 25 | this.router.routeReuseStrategy.shouldReuseRoute = function () { |
26 | return false; | 26 | return false; |
27 | } | 27 | } |
28 | } | 28 | } |
29 | 29 | ||
30 | ngOnInit() { | 30 | ngOnInit() { |
31 | 31 | ||
32 | this.productosCarrito = this.productoService.productos; | 32 | this.productosCarrito = this.productoService.productos; |
33 | } | 33 | } |
34 | 34 | ||
35 | getCantidadProductos() { | 35 | getCantidadProductos() { |
36 | 36 | ||
37 | var cantTotalAux = 0; | 37 | var cantTotalAux = 0; |
38 | this.productosCarrito.forEach(producto => { | 38 | this.productosCarrito.forEach(producto => { |
39 | 39 | ||
40 | cantTotalAux += producto.cantidad; | 40 | cantTotalAux += producto.cantidad; |
41 | }); | 41 | }); |
42 | this.cantTotal = cantTotalAux; | 42 | this.cantTotal = cantTotalAux; |
43 | 43 | ||
44 | return this.cantTotal; | 44 | return this.cantTotal; |
45 | } | 45 | } |
46 | 46 | ||
47 | getTotal() { | 47 | getTotal() { |
48 | 48 | ||
49 | var subTotal = 0; | 49 | var subTotal = 0; |
50 | for (let i = 0; i < this.productosCarrito.length; i++) { | 50 | for (let i = 0; i < this.productosCarrito.length; i++) { |
51 | subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad); | 51 | subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad); |
52 | } | 52 | } |
53 | return this.total = subTotal; | 53 | return this.total = subTotal; |
54 | } | 54 | } |
55 | 55 | ||
56 | aumentarCantidad(producto: Producto) { | 56 | aumentarCantidad(producto: Producto) { |
57 | 57 | ||
58 | if (producto.cantidad < this.cantMax) { | 58 | if (producto.cantidad < this.cantMax) { |
59 | producto.cantidad++; | 59 | producto.cantidad++; |
60 | this.cantTotal++ | 60 | this.cantTotal++ |
61 | } | 61 | } |
62 | } | 62 | } |
63 | 63 | ||
64 | restarCantidad(producto: Producto) { | 64 | restarCantidad(producto: Producto) { |
65 | 65 | ||
66 | if (producto.cantidad > this.cantMin) { | 66 | if (producto.cantidad > this.cantMin) { |
67 | producto.cantidad--; | 67 | producto.cantidad--; |
68 | this.cantTotal--; | 68 | this.cantTotal--; |
69 | } | 69 | } |
70 | } | 70 | } |
71 | 71 | ||
72 | deleteProducto(producto: Producto, index: number) { | 72 | deleteProducto(producto: Producto, index: number) { |
73 | 73 | ||
74 | this.cantTotal -= producto.cantidad; | 74 | this.cantTotal -= producto.cantidad; |
75 | this.total -= producto.PreVen * producto.cantidad; | 75 | this.total -= producto.PreVen * producto.cantidad; |
76 | delete producto.cantidad; | ||
76 | this.productosCarrito.splice(index, 1); | 77 | this.productosCarrito.splice(index, 1); |
77 | return; | 78 | return; |
78 | } | 79 | } |
79 | 80 | ||
80 | personalizarPromo(producto: Producto, index) { | 81 | personalizarPromo(producto: Producto, index) { |
81 | 82 | ||
82 | this.productoService.productoAcargar = producto; | 83 | this.productoService.productoAcargar = producto; |
83 | this.productoService.esPromoPersonalizada = true; | 84 | this.productoService.esPromoPersonalizada = true; |
84 | this.deleteProducto(producto, index); | 85 | this.deleteProducto(producto, index); |
85 | this.router.navigate(['inicio']); | 86 | this.router.navigate(['inicio']); |
86 | } | 87 | } |
87 | 88 | ||
88 | } | 89 | } |
89 | 90 |