Commit a052e0f38ac587dd433b992d992a72cd9fd8e947
1 parent
a00709688b
Exists in
develop
Fix
Al setear articulos al carrito
Showing
1 changed file
with
1 additions
and
1 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.length) { | 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 | }) | 90 | }) |
| 91 | .subscribe((data) => { | 91 | .subscribe((data) => { |
| 92 | observer.next(data); | 92 | observer.next(data); |
| 93 | observer.complete(); | 93 | observer.complete(); |
| 94 | }); | 94 | }); |
| 95 | }); | 95 | }); |
| 96 | }); | 96 | }); |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | cleanShoppingCar() { | 99 | cleanShoppingCar() { |
| 100 | this.articuloAcargar = undefined; | 100 | this.articuloAcargar = undefined; |
| 101 | this.promoAcargar = undefined; | 101 | this.promoAcargar = undefined; |
| 102 | this.carrito = []; | 102 | this.carrito = []; |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | setArticulosSinImagen(articulos: IArticulo[]) { | 105 | setArticulosSinImagen(articulos: IArticulo[]) { |
| 106 | articulos.forEach((articulo: IArticulo) => { | 106 | articulos.forEach((articulo: IArticulo) => { |
| 107 | articulo.imagenes = !articulo.imagenes ? [{ imagen: 'noImage.jpg' }] : | 107 | articulo.imagenes = !articulo.imagenes ? [{ imagen: 'noImage.jpg' }] : |
| 108 | !articulo.imagenes.length ? [{ imagen: 'noImage.jpg' }] : articulo.imagenes; | 108 | !articulo.imagenes.length ? [{ imagen: 'noImage.jpg' }] : articulo.imagenes; |
| 109 | }); | 109 | }); |
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | markArticuloInPromoAsRemoved() { | 112 | markArticuloInPromoAsRemoved() { |
| 113 | this.carrito.forEach((articuloCarrito: IArticulo) => { | 113 | this.carrito.forEach((articuloCarrito: IArticulo) => { |
| 114 | if (articuloCarrito.PRO) { | 114 | if (articuloCarrito.PRO) { |
| 115 | articuloCarrito.productos.forEach((articulo: IArticulo) => { | 115 | articuloCarrito.productos.forEach((articulo: IArticulo) => { |
| 116 | if (articulo.cantidadAdicionada === 0) { | 116 | if (articulo.cantidadAdicionada === 0) { |
| 117 | articulo.cantidad = 0; | 117 | articulo.cantidad = 0; |
| 118 | articulo.importeValorExtra = 0; | 118 | articulo.importeValorExtra = 0; |
| 119 | } | 119 | } |
| 120 | }); | 120 | }); |
| 121 | } | 121 | } |
| 122 | }); | 122 | }); |
| 123 | } | 123 | } |
| 124 | } | 124 | } |
| 125 | 125 |