Commit 2d442ac850fd678a5c16d80b6bd7d6323517e8ff

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

Logica para sumar cantidad en productos iguales.

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 apiUrl: string = appSettings.apiUrl; 24 private apiUrl: string = appSettings.apiUrl;
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.productos.push(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 var producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar; 95 var producto = this.promoAcargar ? this.promoAcargar : this.productoAcargar;
96 producto.cantidad = 1; 96 producto.cantidad = 1;
97 this.productoService.productos.push(producto); 97 this.productoService.setProductos(producto);
98 this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined; 98 this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined;
99 this.promociones = []; 99 this.promociones = [];
100 this.popoverDirective.hide(); 100 this.popoverDirective.hide();
101 } 101 }
102 102
103 promoSeleccionada($event: Producto, showPopover: boolean) { 103 promoSeleccionada($event: Producto, showPopover: boolean) {
104 104
105 this.productoService.getProductoById($event.id) 105 this.productoService.getProductoById($event.id)
106 .subscribe((res: Producto) => { 106 .subscribe((res: Producto) => {
107 107
108 $event.imagenes = res.imagenes.length == 0 ? [{ imagen: 'noImage.jpg' }] : res.imagenes; 108 $event.imagenes = res.imagenes.length == 0 ? [{ imagen: 'noImage.jpg' }] : res.imagenes;
109 this.promoAcargar = $event; 109 this.promoAcargar = $event;
110 110
111 if ($event.tieneSinonimos) { 111 if ($event.tieneSinonimos) {
112 this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt); 112 this.getSinonimos(this.promoAcargar.CodSec, this.promoAcargar.CodArt);
113 } else if (showPopover) { 113 } else if (showPopover) {
114 this.popoverDirective.show(); 114 this.popoverDirective.show();
115 } else { 115 } else {
116 this.popoverDirective.hide(); 116 this.popoverDirective.hide();
117 } 117 }
118 118
119 }, 119 },
120 error => { console.log(error); }) 120 error => { console.log(error); })
121 } 121 }
122 122
123 getSinonimos(sector, codigo) { 123 getSinonimos(sector, codigo) {
124 124
125 this.productoService.getPromocionSinonimos(sector, codigo) 125 this.productoService.getPromocionSinonimos(sector, codigo)
126 .subscribe((res: Sinonimo[]) => { 126 .subscribe((res: Sinonimo[]) => {
127 127
128 res.forEach(sinonimo => { 128 res.forEach(sinonimo => {
129 129
130 sinonimo.cantidad = 0; 130 sinonimo.cantidad = 0;
131 this.promoAcargar.productos.forEach(productoPromo => { 131 this.promoAcargar.productos.forEach(productoPromo => {
132 132
133 sinonimo.productos.forEach(productoSinonimo => { 133 sinonimo.productos.forEach(productoSinonimo => {
134 134
135 if (productoSinonimo.id === productoPromo.id) { 135 if (productoSinonimo.id === productoPromo.id) {
136 productoSinonimo.cantidad = productoPromo.cantidad; 136 productoSinonimo.cantidad = productoPromo.cantidad;
137 sinonimo.cantidad += productoPromo.cantidad; 137 sinonimo.cantidad += productoPromo.cantidad;
138 } 138 }
139 }) 139 })
140 }) 140 })
141 }) 141 })
142 this.sinonimos = res; 142 this.sinonimos = res;
143 (this.sinonimos.length > 0) ? this.popoverDirective.show() : this.popoverDirective.hide(); 143 (this.sinonimos.length > 0) ? this.popoverDirective.show() : this.popoverDirective.hide();
144 }) 144 })
145 } 145 }
146 146
147 productosPersonalizados($event: Producto[]) { 147 productosPersonalizados($event: Producto[]) {
148 148
149 var productosPersonalizados = $event; 149 var productosPersonalizados = $event;
150 150
151 this.promoAcargar.productos.forEach(productoPromo => { 151 this.promoAcargar.productos.forEach(productoPromo => {
152 152
153 if (!productoPromo.idSinonimo) productosPersonalizados.push(productoPromo); 153 if (!productoPromo.idSinonimo) productosPersonalizados.push(productoPromo);
154 }) 154 })
155 155
156 this.promoAcargar.productos = productosPersonalizados; 156 this.promoAcargar.productos = productosPersonalizados;
157 this.confirmarProducto(); 157 this.confirmarProducto();
158 } 158 }
159 159
160 buscarByCodigoBarras(busqueda) { 160 buscarByCodigoBarras(busqueda) {
161 161
162 let producto = this.productos.filter(producto => { 162 let producto = this.productos.filter(producto => {
163 return producto.codigoBarra == busqueda; 163 return producto.codigoBarra == busqueda;
164 }); 164 });
165 165
166 if (producto.length) { 166 if (producto.length) {
167 167
168 this.productoAcargar = producto[0]; 168 this.productoAcargar = producto[0];
169 this.getPromociones(); 169 this.getPromociones();
170 170
171 } else { 171 } else {
172 alert('No se encuentra el producto'); 172 alert('No se encuentra el producto');
173 } 173 }
174 174
175 } 175 }
176 176
177 irBusquedaProductos(value) { 177 irBusquedaProductos(value) {
178 178
179 this.productoService.mostrar = value; 179 this.productoService.mostrar = value;
180 this.router.navigate(['busqueda-productos']); 180 this.router.navigate(['busqueda-productos']);
181 } 181 }
182 182
183 deshacerCarga() { 183 deshacerCarga() {
184 184
185 if (this.sinonimos.length > 0) { 185 if (this.sinonimos.length > 0) {
186 this.sinonimos = []; 186 this.sinonimos = [];
187 this.popoverDirective.hide(); 187 this.popoverDirective.hide();
188 } 188 }
189 189
190 if (this.promoAcargar) { 190 if (this.promoAcargar) {
191 this.promoAcargar = undefined; 191 this.promoAcargar = undefined;
192 if (this.productoAcargar.PRO) { 192 if (this.productoAcargar.PRO) {
193 this.productoAcargar = undefined; 193 this.productoAcargar = undefined;
194 this.promociones = []; 194 this.promociones = [];
195 this.popoverDirective.hide(); 195 this.popoverDirective.hide();
196 } else { 196 } else {
197 this.popoverDirective.show(); 197 this.popoverDirective.show();
198 } 198 }
199 } else { 199 } else {
200 this.productoAcargar = undefined; 200 this.productoAcargar = undefined;
201 this.promociones = []; 201 this.promociones = [];
202 this.popoverDirective.hide(); 202 this.popoverDirective.hide();
203 } 203 }
204 } 204 }
205 205
206 } 206 }
207 207
src/app/services/producto.service.ts
1 import { Injectable } from '@angular/core'; 1 import { Injectable } from '@angular/core';
2 import { HttpClient } from '@angular/common/http'; 2 import { HttpClient } from '@angular/common/http';
3 import { Observable } from 'rxjs'; 3 import { Observable } from 'rxjs';
4 import { appSettings } from 'src/etc/AppSettings'; 4 import { appSettings } from 'src/etc/AppSettings';
5 import { Producto } from '../wrappers/producto'; 5 import { Producto } from '../wrappers/producto';
6 6
7 @Injectable({ 7 @Injectable({
8 providedIn: 'root' 8 providedIn: 'root'
9 }) 9 })
10 export class ProductoService { 10 export class ProductoService {
11 11
12 productos: Producto[] = []; 12 productos: Producto[] = [];
13 productoAcargar: Producto; 13 productoAcargar: Producto;
14 promoAcargar: Producto; 14 promoAcargar: Producto;
15 mostrar: string; 15 mostrar: string;
16 16
17 constructor(private http: HttpClient) { } 17 constructor(private http: HttpClient) { }
18 18
19 getProductoById(id): Observable<any> { 19 getProductoById(id): Observable<any> {
20 20
21 return this.http.get(`${appSettings.apiUrl}/articulos/${id}`); 21 return this.http.get(`${appSettings.apiUrl}/articulos/${id}`);
22 } 22 }
23 23
24 getAll(): Observable<any> { 24 getAll(): Observable<any> {
25 25
26 return this.http.get(`${appSettings.apiUrl}/articulos`); 26 return this.http.get(`${appSettings.apiUrl}/articulos`);
27 } 27 }
28 28
29 setProductos(producto: Producto) { 29 setProductos(producto: Producto) {
30 30
31 for (let i = 0; i < this.productos.length; i++) {
32
33 if (this.productos[i].id === producto.id) {
34
35 if (producto.PRO) {
36 if (this.promosIdenticas(this.productos[i], producto)) {
37 this.productos[i].cantidad++;
38 return;
39 } else {
40 break;
41 }
42 }
43 this.productos[i].cantidad++;
44 return;
45 }
46 }
47
31 this.productos.push(producto); 48 this.productos.push(producto);
32 } 49 }
33 50
34 getPromocionByCodigos(sector, codigo): Observable<any> { 51 getPromocionByCodigos(sector, codigo): Observable<any> {
35 52
36 var url = `${appSettings.apiUrl}/promociones/by-codigos/${sector}/${codigo}`; 53 var url = `${appSettings.apiUrl}/promociones/by-codigos/${sector}/${codigo}`;
37 // var url = `${appSettings.apiUrl}/promociones/by-codigos/${2}/${7}`; 54 // var url = `${appSettings.apiUrl}/promociones/by-codigos/${2}/${7}`;
38 return this.http.get(url); 55 return this.http.get(url);
39 } 56 }
40 57
41 getPromociones(sector, codigo): Observable<any> { 58 getPromociones(sector, codigo): Observable<any> {
42 59
43 var url = `${appSettings.apiUrl}/promociones/incluir-articulo/${sector}/${codigo}`; 60 var url = `${appSettings.apiUrl}/promociones/incluir-articulo/${sector}/${codigo}`;
44 // var url = `${appSettings.apiUrl}/promociones/incluir-articulo/${2}/${1306}`; 61 // var url = `${appSettings.apiUrl}/promociones/incluir-articulo/${2}/${1306}`;
45 return this.http.get(url); 62 return this.http.get(url);
46 } 63 }
47 64
48 getPromocionSinonimos(sector, codigo): Observable<any> { 65 getPromocionSinonimos(sector, codigo): Observable<any> {
49 66
50 var url = `${appSettings.apiUrl}/sinonimos/promo/${sector}/${codigo}`; 67 var url = `${appSettings.apiUrl}/sinonimos/promo/${sector}/${codigo}`;
51 // var url = `${appSettings.apiUrl}/sinonimos/promo/${2}/${7}`; 68 // var url = `${appSettings.apiUrl}/sinonimos/promo/${2}/${7}`;
52 return this.http.get(url); 69 return this.http.get(url);
53 } 70 }
54 71
55 updateImages(body): Observable<any> { 72 updateImages(body): Observable<any> {
56 return this.http.post(`${appSettings.apiUrl}/imagenes/guardar`, body); 73 return this.http.post(`${appSettings.apiUrl}/imagenes/guardar`, body);
57 } 74 }
58 75
59 getCategorias() { 76 getCategorias() {
60 return this.http.get(`${appSettings.apiUrl}/categorias`); 77 return this.http.get(`${appSettings.apiUrl}/categorias`);
61 } 78 }
62 79
63 pagar() { 80 pagar() {
64 return this.http.post(`${appSettings.apiUrl}/comprobante/guardar`, { 81 return this.http.post(`${appSettings.apiUrl}/comprobante/guardar`, {
65 productos: this.productos 82 productos: this.productos
66 }); 83 });
67 } 84 }
68 85
86 private promosIdenticas(promoEnCarrito: Producto, promo: Producto) {
87
88 var sonIdenticas = true;
89 var productosPromoCarrito = promoEnCarrito.productos;
90 var productosPromoAcargar = promo.productos;
91
92 if (productosPromoCarrito.length !== productosPromoAcargar.length) {
93 return false;
94 }
95
96 for (let i = 0; i < productosPromoCarrito.length; i++) {
97
98 if (productosPromoCarrito[i].id !== productosPromoAcargar[i].id) {
99 return false;
100 }
101 }
102
103 return sonIdenticas;
104 }
105
69 } 106 }
70 107