Commit a486afa8baa18a2cb7f232b983c300d8d191b6d3

Authored by Marcelo Puebla
1 parent 2715885bfa
Exists in develop

Change

Import sin uso
Showing 1 changed file with 0 additions and 1 deletions   Show diff stats
src/app/shared/sinonimo/sinonimo.component.ts
1 import { Component, OnInit } from '@angular/core'; 1 import { Component, OnInit } from '@angular/core';
2 import { ISinonimo } from 'src/app/interfaces/ISinonimo'; 2 import { ISinonimo } from 'src/app/interfaces/ISinonimo';
3 import { IArticulo } from 'src/app/interfaces/IArticulo'; 3 import { IArticulo } from 'src/app/interfaces/IArticulo';
4 import { BsModalRef } from 'ngx-bootstrap/modal'; 4 import { BsModalRef } from 'ngx-bootstrap/modal';
5 import { Subject, forkJoin } from 'rxjs'; 5 import { Subject, forkJoin } from 'rxjs';
6 import { ArticuloService } from 'src/app/services/articulo/articulo.service'; 6 import { ArticuloService } from 'src/app/services/articulo/articulo.service';
7 import { element } from 'protractor';
8 7
9 @Component({ 8 @Component({
10 selector: 'app-sinonimo', 9 selector: 'app-sinonimo',
11 templateUrl: './sinonimo.component.html', 10 templateUrl: './sinonimo.component.html',
12 styleUrls: ['./sinonimo.component.scss'] 11 styleUrls: ['./sinonimo.component.scss']
13 }) 12 })
14 export class SinonimoComponent implements OnInit { 13 export class SinonimoComponent implements OnInit {
15 sinonimos: ISinonimo[] = []; 14 sinonimos: ISinonimo[] = [];
16 isValid: boolean; 15 isValid: boolean;
17 onClose: Subject<any>; 16 onClose: Subject<any>;
18 articulo: IArticulo; 17 articulo: IArticulo;
19 isSinonimoSelected = false; 18 isSinonimoSelected = false;
20 currentIndex = 0; 19 currentIndex = 0;
21 20
22 constructor( 21 constructor(
23 private modalRef: BsModalRef, 22 private modalRef: BsModalRef,
24 private articuloService: ArticuloService, 23 private articuloService: ArticuloService,
25 ) { 24 ) {
26 this.onClose = new Subject(); 25 this.onClose = new Subject();
27 } 26 }
28 27
29 ngOnInit() { 28 ngOnInit() {
30 for (const s of this.sinonimos) { 29 for (const s of this.sinonimos) {
31 for (const a of s.productos) { 30 for (const a of s.productos) {
32 a.cantidad = 0; 31 a.cantidad = 0;
33 } 32 }
34 } 33 }
35 } 34 }
36 35
37 validate() { 36 validate() {
38 this.isValid = true; 37 this.isValid = true;
39 for (const s of this.sinonimos) { 38 for (const s of this.sinonimos) {
40 if (s.cantidadRestante > 0) { 39 if (s.cantidadRestante > 0) {
41 this.isValid = false; 40 this.isValid = false;
42 break; 41 break;
43 } 42 }
44 } 43 }
45 return !this.isValid ? 'disabled' : 'btn-effect'; 44 return !this.isValid ? 'disabled' : 'btn-effect';
46 } 45 }
47 46
48 validateNext() { 47 validateNext() {
49 const sinonimo = this.sinonimos[this.currentIndex] 48 const sinonimo = this.sinonimos[this.currentIndex]
50 sinonimo.selected = (sinonimo.cantidadRestante > 0) ? false : true; 49 sinonimo.selected = (sinonimo.cantidadRestante > 0) ? false : true;
51 return !sinonimo.selected ? 'disabled' : 'btn-effect'; 50 return !sinonimo.selected ? 'disabled' : 'btn-effect';
52 } 51 }
53 52
54 goNext() { 53 goNext() {
55 if (!this.sinonimos[this.currentIndex].selected) return; 54 if (!this.sinonimos[this.currentIndex].selected) return;
56 this.currentIndex++; 55 this.currentIndex++;
57 } 56 }
58 57
59 continue() { 58 continue() {
60 if (!this.isValid) return; 59 if (!this.isValid) return;
61 if (this.isSinonimoSelected) return; 60 if (this.isSinonimoSelected) return;
62 this.isSinonimoSelected = true; 61 this.isSinonimoSelected = true;
63 const observables = []; 62 const observables = [];
64 const cantidades = []; 63 const cantidades = [];
65 64
66 for (const s of this.sinonimos) { 65 for (const s of this.sinonimos) {
67 for (const articulo of s.productos) { 66 for (const articulo of s.productos) {
68 if (articulo.cantidad === 0) continue; 67 if (articulo.cantidad === 0) continue;
69 cantidades.push(articulo.cantidad); 68 cantidades.push(articulo.cantidad);
70 observables.push(this.articuloService.getById(articulo.id)); 69 observables.push(this.articuloService.getById(articulo.id));
71 } 70 }
72 } 71 }
73 forkJoin(observables) 72 forkJoin(observables)
74 .subscribe((res: IArticulo[]) => { 73 .subscribe((res: IArticulo[]) => {
75 res.forEach((articulo, i) => { 74 res.forEach((articulo, i) => {
76 articulo.cantidad = cantidades[i]; 75 articulo.cantidad = cantidades[i];
77 }); 76 });
78 this.modalRef.hide(); 77 this.modalRef.hide();
79 this.onClose.next({ 78 this.onClose.next({
80 articulos: res, 79 articulos: res,
81 }); 80 });
82 }, err => console.error(err)); 81 }, err => console.error(err));
83 } 82 }
84 83
85 sumarCantidadSinonimo(articulo: IArticulo, i: number) { 84 sumarCantidadSinonimo(articulo: IArticulo, i: number) {
86 if (this.sinonimos[i].cantidadRestante === 0) return; 85 if (this.sinonimos[i].cantidadRestante === 0) return;
87 articulo.cantidad++; 86 articulo.cantidad++;
88 this.sinonimos[i].cantidadRestante--; 87 this.sinonimos[i].cantidadRestante--;
89 } 88 }
90 89
91 restarCantidadSinonimo(articulo: IArticulo, i: number) { 90 restarCantidadSinonimo(articulo: IArticulo, i: number) {
92 if (this.sinonimos[i].cantidadRestante === this.sinonimos[i].cantidad) return; 91 if (this.sinonimos[i].cantidadRestante === this.sinonimos[i].cantidad) return;
93 if (articulo.cantidad === 0) return; 92 if (articulo.cantidad === 0) return;
94 articulo.cantidad--; 93 articulo.cantidad--;
95 this.sinonimos[i].cantidadRestante++; 94 this.sinonimos[i].cantidadRestante++;
96 } 95 }
97 96
98 scrollTo(index: number) { 97 scrollTo(index: number) {
99 const el = document.getElementById(index.toString()); 98 const el = document.getElementById(index.toString());
100 el.scrollIntoView({ behavior: 'smooth' }); 99 el.scrollIntoView({ behavior: 'smooth' });
101 } 100 }
102 101
103 close() { 102 close() {
104 this.modalRef.hide(); 103 this.modalRef.hide();
105 this.onClose.next(); 104 this.onClose.next();
106 } 105 }
107 106
108 } 107 }
109 108