popover-sinonimos.component.ts
2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { PopoverDirective } from 'ngx-bootstrap';
import { Producto } from 'src/app/wrappers/producto';
import { Sinonimo } from 'src/app/wrappers/sinonimo';
import { ProductoService } from 'src/app/services/producto.service';
@Component({
selector: 'app-popover-sinonimos',
templateUrl: './popover-sinonimos.component.html',
styleUrls: ['./popover-sinonimos.component.scss']
})
export class PopoverSinonimosComponent implements OnInit {
//Directiva del popover, para poder cerrarlo desde este componente
@Input() popover: PopoverDirective;
@Input() popoverContent: Sinonimo[];
@Output() productosPersonalizados = new EventEmitter<Producto[]>();
// sinonimo: Sinonimo;
private cantidadRestanteSinonimos: number[] = [];
constructor(private productoService: ProductoService) { }
ngOnInit() {
//Seteo en la variable a emitir el sinonimo que sea padre
this.popoverContent.forEach(sinonimo => {
this.cantidadRestanteSinonimos.push(0);
// this.sinonimo = sinonimo.productoPadre ? sinonimo : undefined;
sinonimo.productos.forEach(producto => {
producto.cantidad = (producto.id === sinonimo.productoPadre) ? sinonimo.cantidad : 0;
})
})
}
continuar() {
//Si aún quedan articulos que agregar no deja continuar.
for (let i = 0; i < this.cantidadRestanteSinonimos.length; i++) {
if (this.cantidadRestanteSinonimos[i] > 0) return;
}
var productosAenviar: Producto[] = [];
this.popoverContent.forEach(sinonimo => {
sinonimo.productos.forEach(producto => {
if (producto.cantidad > 0) {
producto.idSinonimo = sinonimo.ID_SIN;
productosAenviar.push(producto);
}
})
})
this.productosPersonalizados.emit(productosAenviar);
this.popover.hide();
}
sumarCantidad(producto: Producto, i: number) {
if (this.cantidadRestanteSinonimos[i] === 0) return;
producto.cantidad++;
this.cantidadRestanteSinonimos[i]--;
}
restarCantidad(producto: Producto, i: number) {
if (this.cantidadRestanteSinonimos[i] === this.popoverContent[i].cantidad) return;
if (producto.cantidad === 0) return;
producto.cantidad--;
this.cantidadRestanteSinonimos[i]++;
}
}