From b695e77f11fd45d9cf23e911ee6ab1e47bb8cb0d Mon Sep 17 00:00:00 2001 From: mpuebla Date: Tue, 20 Aug 2019 17:54:43 -0300 Subject: [PATCH] Logica para cargar productos al carrito. --- src/app/components/inicio/inicio.component.html | 26 +++-- src/app/components/inicio/inicio.component.ts | 130 +++++++++++++-------- .../popover-promos/popover-promos.component.html | 4 +- .../popover-promos/popover-promos.component.ts | 15 ++- .../popover-sinonimos.component.html | 13 ++- .../popover-sinonimos.component.ts | 37 ++++-- 6 files changed, 140 insertions(+), 85 deletions(-) diff --git a/src/app/components/inicio/inicio.component.html b/src/app/components/inicio/inicio.component.html index abcfd40..a9ceefc 100644 --- a/src/app/components/inicio/inicio.component.html +++ b/src/app/components/inicio/inicio.component.html @@ -29,13 +29,16 @@ @@ -75,7 +78,7 @@ *ngIf="sinonimos.length > 0" [popover]="popoverDirective" [popoverContent]="sinonimos" - (sinonimoSeleccionado)="sinonimoSeleccionado($event)" + (productosPersonalizados)="productosPersonalizados($event)" class="text-white rounded-sm border-0"> @@ -126,16 +129,16 @@
- + src="{{apiUrl}}/imagenes/{{promoAcargar. imagenes[0].imagen}}"> -->
-

{{promoAcargar.nombrePromo}}

+

{{promoAcargar.DetArt}}

-

{{promoAcargar.precioTotal | currency}}

+

{{promoAcargar.PreVen | currency}}

@@ -154,14 +157,17 @@ - diff --git a/src/app/components/inicio/inicio.component.ts b/src/app/components/inicio/inicio.component.ts index ccab297..3a01863 100644 --- a/src/app/components/inicio/inicio.component.ts +++ b/src/app/components/inicio/inicio.component.ts @@ -1,12 +1,10 @@ -import { Component, OnInit, ViewChild } from '@angular/core'; +import { Component, OnInit, ViewChild, HostListener } from '@angular/core'; import { PopoverDirective } from 'ngx-bootstrap'; import { appSettings } from 'src/etc/AppSettings'; -import { Producto } from 'src/app/wrappers/producto'; -import { ProductoService } from 'src/app/services/producto.service'; import { Router } from '@angular/router'; -import { Promocion } from 'src/app/wrappers/promocion'; +import { ProductoService } from 'src/app/services/producto.service'; +import { Producto } from 'src/app/wrappers/producto'; import { Sinonimo } from 'src/app/wrappers/sinonimo'; -import { HostListener } from '@angular/core'; @Component({ selector: 'app-inicio', @@ -27,15 +25,15 @@ export class InicioComponent implements OnInit { }; @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective; - private productoAcargar: Producto; - private productos: Producto[]; - private promoAcargar: Promocion; private tienePromo = false; private productoEsPromo = false; private busqueda: string = ''; - private sinonimoAcargar: Sinonimo; - promociones: Promocion[] = []; + private productoAcargar: Producto; + private promoAcargar: Producto; + + productos: Producto[] = []; + promociones: Producto[] = []; sinonimos: Sinonimo[] = []; apiUrl: string = appSettings.apiUrl @@ -46,30 +44,36 @@ export class InicioComponent implements OnInit { ngOnInit() { this.productoAcargar = this.productoService.productoAcargar; - this.getPromociones(); + if (this.productoAcargar) { + if (this.productoAcargar.PRO) { + this.promociones.push(this.productoAcargar); + this.promoSeleccionada(this.productoAcargar); + } + else + this.getPromociones(); + } this.getProductos(); } getPromociones() { - if (this.productoAcargar) { - var sector = this.productoAcargar.CodSec; - var codigo = this.productoAcargar.CodArt; - this.productoService.getPromocion(sector, codigo) - .subscribe((res: Promocion[]) => { - - if (res.length === 0) { - //Si no tiene promociones la cargará al carrito despues de un tiempo - setTimeout(() => { - this.productoService.productos.push(this.productoAcargar); - this.productoAcargar = this.productoService.productoAcargar = undefined; - }, 2000) - } else { - - this.promociones = res; - this.popoverDirective.show(); - } - }, error => { console.error(error); }) - } + + var sector = this.productoAcargar.CodSec; + var codigo = this.productoAcargar.CodArt; + this.productoService.getPromociones(sector, codigo) + .subscribe((res: Producto[]) => { + + if (res.length === 0) { + //Si no tiene promociones la cargará al carrito despues de un tiempo + setTimeout(() => { + this.productoService.productos.push(this.productoAcargar); + this.productoAcargar = this.productoService.productoAcargar = undefined; + }, 2000) + } else { + + this.promociones = res; + this.popoverDirective.show(); + } + }, error => { console.error(error); }) } showPopover() { @@ -84,23 +88,37 @@ export class InicioComponent implements OnInit { }); } - private irBusquedaProductos(verPromociones) { + irBusquedaProductos(verPromociones) { this.productoService.verCategoriasProductos = verPromociones; this.router.navigate(['busqueda-productos']); } + confirmarProducto() { + + this.productoService.productos.push(this.promoAcargar); + this.promoAcargar = undefined; + this.productoAcargar = undefined; + this.promociones = []; + this.popoverDirective.hide(); + } + deshacerCarga() { - if (this.sinonimoAcargar || this.sinonimos.length > 0) { + if (this.sinonimos.length > 0) { this.sinonimos = []; - this.sinonimoAcargar = undefined; this.popoverDirective.hide(); } if (this.promoAcargar) { this.promoAcargar = undefined; - this.popoverDirective.show(); + if(this.productoAcargar.PRO){ + this.productoAcargar = undefined; + this.promociones = []; + this.popoverDirective.hide(); + }else{ + this.popoverDirective.show(); + } } else { this.productoAcargar = undefined; this.promociones = []; @@ -108,37 +126,49 @@ export class InicioComponent implements OnInit { } } - promoSeleccionada($event: Promocion) { + promoSeleccionada($event: Producto) { this.promoAcargar = $event; - this.popoverDirective.hide(); - if (this.promoAcargar.sinonimos) { - var sector = this.promoAcargar.sector; - var codigo = this.promoAcargar.codigo; + this.promoAcargar.tieneSinonimos = true; + if (this.promoAcargar.tieneSinonimos) { + var sector = this.promoAcargar.CodSec; + var codigo = this.promoAcargar.CodArt; this.productoService.getPromocionSinonimos(sector, codigo) .subscribe((res: Sinonimo[]) => { - res.forEach(resSinonimo => { + res.forEach(sinonimo => { - this.promoAcargar.productos.forEach(productoPromo => { + sinonimo.productos.forEach(productoSinonimo => { - if (productoPromo.idSinonimo === resSinonimo.ID_SIN) { - resSinonimo.cantidad = productoPromo.cantidad; - resSinonimo.productoPadre = productoPromo.id; - } - }); + this.promoAcargar.productos.forEach(productoPromo => { + if (productoSinonimo.id === productoPromo.id) { + sinonimo.cantidad = productoPromo.cantidad; + sinonimo.productoPadre = productoPromo.id; + } + }) + }) }) + this.sinonimos = res; - this.showPopover(); + if (res.length > 0) + this.showPopover(); }) + } else { + this.popoverDirective.hide(); } } - sinonimoSeleccionado($event: Sinonimo) { + productosPersonalizados($event: Producto[]) { + + var productosPersonalizados = $event; + + this.promoAcargar.productos.forEach(productoPromo => { + + if (!productoPromo.idSinonimo) productosPersonalizados.push(productoPromo); + }) - console.log($event); - this.sinonimoAcargar = $event; + this.promoAcargar.productos = productosPersonalizados; } buscarByCodigoBarras(busqueda) { diff --git a/src/app/components/popover-promos/popover-promos.component.html b/src/app/components/popover-promos/popover-promos.component.html index eb9536a..63cef7d 100644 --- a/src/app/components/popover-promos/popover-promos.component.html +++ b/src/app/components/popover-promos/popover-promos.component.html @@ -19,12 +19,12 @@

- {{promo.nombrePromo}} + {{promo.DetArt}}

- {{calcularPrecioDePromo(promo) | currency}} + {{promo.PreVen | currency}}

diff --git a/src/app/components/popover-promos/popover-promos.component.ts b/src/app/components/popover-promos/popover-promos.component.ts index 00cba2b..52fe0d1 100644 --- a/src/app/components/popover-promos/popover-promos.component.ts +++ b/src/app/components/popover-promos/popover-promos.component.ts @@ -1,6 +1,5 @@ import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { PopoverDirective } from 'ngx-bootstrap/popover'; -import { Promocion } from 'src/app/wrappers/promocion'; import { Producto } from 'src/app/wrappers/producto'; @Component({ @@ -11,8 +10,8 @@ import { Producto } from 'src/app/wrappers/producto'; export class PopoverPromosComponent implements OnInit { @Input() popover: PopoverDirective; - @Input() popoverContent: Promocion[]; - @Output() promoSeleccionada = new EventEmitter(); + @Input() popoverContent: Producto[]; + @Output() promoSeleccionada = new EventEmitter(); constructor() { } @@ -23,18 +22,18 @@ export class PopoverPromosComponent implements OnInit { this.popover.hide(); } - seleccionarPromo(promo: Promocion) { + seleccionarPromo(promo: Producto) { this.promoSeleccionada.emit(promo); } - calcularPrecioDePromo(promo: Promocion) { + calcularPrecioDePromo(promo: Producto) { - promo.precioTotal = 0; + promo.PreVen = 0; promo.productos.forEach(producto => { - promo.precioTotal += producto.PreVen; + promo.PreVen += producto.PreVen; }) - return promo.precioTotal; + return promo.PreVen; } } diff --git a/src/app/components/popover-sinonimos/popover-sinonimos.component.html b/src/app/components/popover-sinonimos/popover-sinonimos.component.html index 859c8db..87fad83 100644 --- a/src/app/components/popover-sinonimos/popover-sinonimos.component.html +++ b/src/app/components/popover-sinonimos/popover-sinonimos.component.html @@ -22,16 +22,16 @@
-
+

{{producto.DetArt}}

-
+
@@ -43,7 +43,7 @@
@@ -58,7 +58,10 @@
- diff --git a/src/app/components/popover-sinonimos/popover-sinonimos.component.ts b/src/app/components/popover-sinonimos/popover-sinonimos.component.ts index cb169d3..2349d76 100644 --- a/src/app/components/popover-sinonimos/popover-sinonimos.component.ts +++ b/src/app/components/popover-sinonimos/popover-sinonimos.component.ts @@ -2,6 +2,7 @@ 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', @@ -13,12 +14,12 @@ export class PopoverSinonimosComponent implements OnInit { //Directiva del popover, para poder cerrarlo desde este componente @Input() popover: PopoverDirective; @Input() popoverContent: Sinonimo[]; - @Output() sinonimoSeleccionado = new EventEmitter(); - sinonimo: Sinonimo; + @Output() productosPersonalizados = new EventEmitter(); + // sinonimo: Sinonimo; private cantidadRestanteSinonimos: number[] = []; - constructor() { } + constructor(private productoService: ProductoService) { } ngOnInit() { @@ -26,7 +27,8 @@ export class PopoverSinonimosComponent implements OnInit { this.popoverContent.forEach(sinonimo => { this.cantidadRestanteSinonimos.push(0); - this.sinonimo = sinonimo.productoPadre ? sinonimo : undefined; + // this.sinonimo = sinonimo.productoPadre ? sinonimo : undefined; + sinonimo.productos.forEach(producto => { producto.cantidad = (producto.id === sinonimo.productoPadre) ? sinonimo.cantidad : 0; @@ -34,15 +36,30 @@ export class PopoverSinonimosComponent implements OnInit { }) } - hidePopover() { + continuar() { - this.sinonimoSeleccionado.emit(this.sinonimo); - this.popover.hide(); - } + //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; + } - setSinonimo(sinonimo: Sinonimo) { + var productosAenviar: Producto[] = []; - this.sinonimo = sinonimo; + 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) { -- 1.9.1