Commit 4142232fc24152814ffe8cbac85868d929b4432b
1 parent
994ed32ee7
Exists in
develop
Fix
Limpiar carrito
Showing
2 changed files
with
2 additions
and
2 deletions
Show diff stats
src/app/services/articulo/articulo.service.ts
| 1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
| 2 | import { HttpClient } from '@angular/common/http'; | 2 | import { HttpClient } from '@angular/common/http'; |
| 3 | import { APP_SETTINGS } from '../../../etc/AppSettings'; | 3 | import { APP_SETTINGS } from '../../../etc/AppSettings'; |
| 4 | import { IArticulo } from '../../interfaces/IArticulo'; | 4 | import { IArticulo } from '../../interfaces/IArticulo'; |
| 5 | import { ClienteService } from '../cliente/cliente.service'; | 5 | import { ClienteService } from '../cliente/cliente.service'; |
| 6 | import { Observable } from 'rxjs'; | 6 | import { Observable } from 'rxjs'; |
| 7 | 7 | ||
| 8 | @Injectable() | 8 | @Injectable() |
| 9 | export class ArticuloService { | 9 | export class ArticuloService { |
| 10 | carrito: IArticulo[] = []; | 10 | carrito: IArticulo[] = []; |
| 11 | articuloAcargar: IArticulo; | 11 | articuloAcargar: IArticulo; |
| 12 | promoAcargar: IArticulo; | 12 | promoAcargar: IArticulo; |
| 13 | mostrar: string; | 13 | mostrar: string; |
| 14 | esPromoPersonalizada = false; | 14 | esPromoPersonalizada = false; |
| 15 | urlDeboSuite = APP_SETTINGS.apiDeboSuite; | 15 | urlDeboSuite = APP_SETTINGS.apiDeboSuite; |
| 16 | medioPago: number; | 16 | medioPago: number; |
| 17 | idComanda: number; | 17 | idComanda: number; |
| 18 | subTotal = 0; | 18 | subTotal = 0; |
| 19 | maxCantidad = 50; | 19 | maxCantidad = 50; |
| 20 | 20 | ||
| 21 | constructor( | 21 | constructor( |
| 22 | private http: HttpClient, | 22 | private http: HttpClient, |
| 23 | private clienteService: ClienteService, | 23 | private clienteService: ClienteService, |
| 24 | ) { } | 24 | ) { } |
| 25 | 25 | ||
| 26 | getById(id) { | 26 | getById(id) { |
| 27 | return this.http.get(`${this.urlDeboSuite}/articulos/${id}`); | 27 | return this.http.get(`${this.urlDeboSuite}/articulos/${id}`); |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | getAll() { | 30 | getAll() { |
| 31 | return this.http.get(`${this.urlDeboSuite}/articulos/`); | 31 | return this.http.get(`${this.urlDeboSuite}/articulos/`); |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | getAllWithPaginator(page: number = 1) { | 34 | getAllWithPaginator(page: number = 1) { |
| 35 | return this.http.get(`${this.urlDeboSuite}/articulos/${page}`); | 35 | return this.http.get(`${this.urlDeboSuite}/articulos/${page}`); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | substractCant(articulo: IArticulo) { | 38 | substractCant(articulo: IArticulo) { |
| 39 | if (articulo.cantidad === 1) return; | 39 | if (articulo.cantidad === 1) return; |
| 40 | articulo.cantidad--; | 40 | articulo.cantidad--; |
| 41 | this.calcularTotal(); | 41 | this.calcularTotal(); |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | addCant(articulo: IArticulo) { | 44 | addCant(articulo: IArticulo) { |
| 45 | if (articulo.cantidad >= this.maxCantidad) return; | 45 | if (articulo.cantidad >= this.maxCantidad) return; |
| 46 | articulo.cantidad++; | 46 | articulo.cantidad++; |
| 47 | this.calcularTotal(); | 47 | this.calcularTotal(); |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | calcularTotal() { | 50 | calcularTotal() { |
| 51 | this.subTotal = 0; | 51 | this.subTotal = 0; |
| 52 | this.carrito.forEach(articulo => { | 52 | this.carrito.forEach(articulo => { |
| 53 | this.subTotal += (articulo.PreVen * articulo.cantidad); | 53 | this.subTotal += (articulo.PreVen * articulo.cantidad); |
| 54 | }); | 54 | }); |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | setArticulo(articulo: IArticulo) { | 57 | setArticulo(articulo: IArticulo) { |
| 58 | articulo.cantidad = 1; | 58 | articulo.cantidad = 1; |
| 59 | for (const articuloCarrito of this.carrito) { | 59 | for (const articuloCarrito of this.carrito) { |
| 60 | if (articuloCarrito.id === articulo.id && !articulo.productos) { | 60 | if (articuloCarrito.id === articulo.id && !articulo.productos) { |
| 61 | articuloCarrito.cantidad++; | 61 | articuloCarrito.cantidad++; |
| 62 | this.calcularTotal(); | 62 | this.calcularTotal(); |
| 63 | return; | 63 | return; |
| 64 | } | 64 | } |
| 65 | } | 65 | } |
| 66 | this.setArticulosSinImagen([articulo]); | 66 | this.setArticulosSinImagen([articulo]); |
| 67 | this.carrito.unshift(articulo); | 67 | this.carrito.unshift(articulo); |
| 68 | this.calcularTotal(); | 68 | this.calcularTotal(); |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | deleteArticulo(index: number) { | 71 | deleteArticulo(index: number) { |
| 72 | this.carrito.splice(index, 1); | 72 | this.carrito.splice(index, 1); |
| 73 | this.calcularTotal(); | 73 | this.calcularTotal(); |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | pay(dataPago: any) { | 76 | pay(dataPago: any) { |
| 77 | return new Observable((observer) => { | 77 | return new Observable((observer) => { |
| 78 | this.clienteService.getById(-1) | 78 | this.clienteService.getById(-1) |
| 79 | .subscribe(cliente => { | 79 | .subscribe(cliente => { |
| 80 | this.markArticuloInPromoAsRemoved(); | 80 | this.markArticuloInPromoAsRemoved(); |
| 81 | this.http.post(`${this.urlDeboSuite}/comprobante/guardar/${this.medioPago}`, { | 81 | this.http.post(`${this.urlDeboSuite}/comprobante/guardar/${this.medioPago}`, { |
| 82 | productos: this.carrito, | 82 | productos: this.carrito, |
| 83 | cliente, | 83 | cliente, |
| 84 | origen: 'autoservicio', | 84 | origen: 'autoservicio', |
| 85 | codigoVendedor: 5, | 85 | codigoVendedor: 5, |
| 86 | puntoVenta: APP_SETTINGS.puntoVenta, | 86 | puntoVenta: APP_SETTINGS.puntoVenta, |
| 87 | pedidoAnombreDe: dataPago.pedidoAnombreDe, | 87 | pedidoAnombreDe: dataPago.pedidoAnombreDe, |
| 88 | numeroPlanilla: APP_SETTINGS.numeroPlanilla, | 88 | numeroPlanilla: APP_SETTINGS.numeroPlanilla, |
| 89 | pedidoParaLlevar: localStorage.getItem('pedidoParaLlevar'), | 89 | pedidoParaLlevar: localStorage.getItem('pedidoParaLlevar'), |
| 90 | terminal: APP_SETTINGS.terminal | 90 | terminal: APP_SETTINGS.terminal |
| 91 | }) | 91 | }) |
| 92 | .subscribe((data) => { | 92 | .subscribe((data) => { |
| 93 | observer.next(data); | 93 | observer.next(data); |
| 94 | observer.complete(); | 94 | observer.complete(); |
| 95 | }, err => { | 95 | }, err => { |
| 96 | observer.error(err); | 96 | observer.error(err); |
| 97 | observer.complete(); | 97 | observer.complete(); |
| 98 | }); | 98 | }); |
| 99 | }); | 99 | }); |
| 100 | }); | 100 | }); |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | cleanShoppingCar() { | 103 | cleanShoppingCar() { |
| 104 | this.articuloAcargar = undefined; | 104 | this.articuloAcargar = undefined; |
| 105 | this.promoAcargar = undefined; | 105 | this.promoAcargar = undefined; |
| 106 | this.carrito = []; | 106 | this.carrito = []; |
| 107 | this.calcularTotal(); | ||
| 107 | } | 108 | } |
| 108 | 109 | ||
| 109 | setArticulosSinImagen(articulos: IArticulo[]) { | 110 | setArticulosSinImagen(articulos: IArticulo[]) { |
| 110 | articulos.forEach((articulo: IArticulo) => { | 111 | articulos.forEach((articulo: IArticulo) => { |
| 111 | articulo.imagenes = !articulo.imagenes ? [{ imagen: 'noImage.jpg' }] : | 112 | articulo.imagenes = !articulo.imagenes ? [{ imagen: 'noImage.jpg' }] : |
| 112 | !articulo.imagenes.length ? [{ imagen: 'noImage.jpg' }] : articulo.imagenes; | 113 | !articulo.imagenes.length ? [{ imagen: 'noImage.jpg' }] : articulo.imagenes; |
| 113 | }); | 114 | }); |
| 114 | } | 115 | } |
| 115 | 116 | ||
| 116 | markArticuloInPromoAsRemoved() { | 117 | markArticuloInPromoAsRemoved() { |
| 117 | this.carrito.forEach((articuloCarrito: IArticulo) => { | 118 | this.carrito.forEach((articuloCarrito: IArticulo) => { |
| 118 | if (articuloCarrito.PRO) { | 119 | if (articuloCarrito.PRO) { |
| 119 | articuloCarrito.productos.forEach((articulo: IArticulo) => { | 120 | articuloCarrito.productos.forEach((articulo: IArticulo) => { |
| 120 | if (articulo.cantidadAdicionada === 0) { | 121 | if (articulo.cantidadAdicionada === 0) { |
| 121 | articulo.cantidad = 0; | 122 | articulo.cantidad = 0; |
| 122 | articulo.importeValorExtra = 0; | 123 | articulo.importeValorExtra = 0; |
| 123 | } | 124 | } |
| 124 | }); | 125 | }); |
| 125 | } | 126 | } |
| 126 | }); | 127 | }); |
| 127 | } | 128 | } |
| 128 | } | 129 | } |
| 129 | 130 |
src/app/shared/cancelar-compra/cancelar-compra.component.ts
| 1 | import { Component, OnInit, OnDestroy } from '@angular/core'; | 1 | import { Component, OnInit, OnDestroy } from '@angular/core'; |
| 2 | import { Location } from '@angular/common'; | 2 | import { Location } from '@angular/common'; |
| 3 | import { ArticuloService } from 'src/app/services/articulo/articulo.service'; | 3 | import { ArticuloService } from 'src/app/services/articulo/articulo.service'; |
| 4 | import { Router } from '@angular/router'; | 4 | import { Router } from '@angular/router'; |
| 5 | 5 | ||
| 6 | @Component({ | 6 | @Component({ |
| 7 | selector: 'app-cancelar-compra', | 7 | selector: 'app-cancelar-compra', |
| 8 | templateUrl: './cancelar-compra.component.html', | 8 | templateUrl: './cancelar-compra.component.html', |
| 9 | styleUrls: ['./cancelar-compra.component.scss'] | 9 | styleUrls: ['./cancelar-compra.component.scss'] |
| 10 | }) | 10 | }) |
| 11 | export class CancelarCompraComponent implements OnInit, OnDestroy { | 11 | export class CancelarCompraComponent implements OnInit, OnDestroy { |
| 12 | timer: any; | 12 | timer: any; |
| 13 | 13 | ||
| 14 | constructor( | 14 | constructor( |
| 15 | private location: Location, | 15 | private location: Location, |
| 16 | private router: Router, | 16 | private router: Router, |
| 17 | private articuloService: ArticuloService | 17 | private articuloService: ArticuloService |
| 18 | ) { } | 18 | ) { } |
| 19 | 19 | ||
| 20 | ngOnInit() { | 20 | ngOnInit() { |
| 21 | this.timer = setTimeout(() => { | 21 | this.timer = setTimeout(() => { |
| 22 | this.limpiarCarritoYvolver(); | 22 | this.limpiarCarritoYvolver(); |
| 23 | }, 90000); | 23 | }, 90000); |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | ngOnDestroy() { | 26 | ngOnDestroy() { |
| 27 | clearTimeout(this.timer); | 27 | clearTimeout(this.timer); |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | volverPreviousPage() { | 30 | volverPreviousPage() { |
| 31 | this.location.back(); | 31 | this.location.back(); |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | limpiarCarritoYvolver() { | 34 | limpiarCarritoYvolver() { |
| 35 | this.articuloService.carrito = []; | 35 | this.articuloService.cleanShoppingCar(); |
| 36 | this.articuloService.calcularTotal(); | ||
| 37 | this.router.navigate(['/']); | 36 | this.router.navigate(['/']); |
| 38 | } | 37 | } |
| 39 | } | 38 | } |
| 40 | 39 |