Commit 7809cdfb0c1591095075d90f3a78d837b7a75a20

Authored by Marcelo Puebla
1 parent c619abbde9
Exists in master

Muestreo el sinonimo padre al prinicipio.

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