popover-sinonimos.component.ts
2.84 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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<any>();
private cantMin: number = 1;
private cantMax: number = 50;
private cantidadRestanteSinonimos: number = 0;
private cantidadOriginal: number = 0;
private cantidadPromo: number = 1;
constructor(private productoService: ProductoService) { }
ngOnInit() {
this.popoverContent.forEach(sinonimo => {
sinonimo.productos.forEach((producto, index) => {
if (this.productoService.esPromoPersonalizada) {
producto.cantidad = producto.cantidad ? producto.cantidad : 0;
this.cantidadRestanteSinonimos = 0;
this.cantidadOriginal = 0;
} else {
this.cantidadRestanteSinonimos += producto.cantidad ? producto.cantidad : 0;
this.cantidadOriginal += producto.cantidad ? producto.cantidad : 0;
producto.cantidad = 0;
}
})
})
}
continuar() {
//Si aún quedan articulos que agregar no deja continuar.
if (this.cantidadRestanteSinonimos > 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: productosAenviar,cantidadPromo: this.cantidadPromo});
this.popover.hide();
}
sumarCantidad(producto: Producto, i: number) {
if (this.cantidadRestanteSinonimos === 0) return;
producto.cantidad++;
this.cantidadRestanteSinonimos--;
}
restarCantidad(producto: Producto, i: number) {
if (this.cantidadRestanteSinonimos === this.popoverContent[i].cantidad) return;
if (producto.cantidad === 0) return;
producto.cantidad--;
this.cantidadRestanteSinonimos++;
}
sumarCantidadPromo() {
if (this.cantidadPromo < this.cantMax) {
this.cantidadPromo++;
this.cantidadRestanteSinonimos += this.cantidadOriginal;
}
}
restarCantidadPromo() {
if (this.cantidadPromo > this.cantMin) {
this.cantidadPromo--;
this.cantidadRestanteSinonimos -= this.cantidadOriginal;
}
}
}