Commit 82667f75feb854354c7c92e756d54dfe65a49885

Authored by Eric Fernandez
Exists in master

Merge branch 'master' into 'master'

Master(mpuebla)

See merge request !36
src/app/components/inicio/inicio.component.ts
1 import { Component, OnInit, ViewChild, HostListener } from '@angular/core'; 1 import { Component, OnInit, ViewChild, HostListener } from '@angular/core';
2 import { PopoverDirective } from 'ngx-bootstrap'; 2 import { PopoverDirective } from 'ngx-bootstrap';
3 import { appSettings } from 'src/etc/AppSettings'; 3 import { appSettings } from 'src/etc/AppSettings';
4 import { Router } from '@angular/router'; 4 import { Router } from '@angular/router';
5 import { ProductoService } from 'src/app/services/producto.service'; 5 import { ProductoService } from 'src/app/services/producto.service';
6 import { Producto } from 'src/app/wrappers/producto'; 6 import { Producto } from 'src/app/wrappers/producto';
7 import { Sinonimo } from 'src/app/wrappers/sinonimo'; 7 import { Sinonimo } from 'src/app/wrappers/sinonimo';
8 8
9 @Component({ 9 @Component({
10 selector: 'app-inicio', 10 selector: 'app-inicio',
11 templateUrl: './inicio.component.html', 11 templateUrl: './inicio.component.html',
12 styleUrls: ['./inicio.component.scss'] 12 styleUrls: ['./inicio.component.scss']
13 }) 13 })
14 export class InicioComponent implements OnInit { 14 export class InicioComponent implements OnInit {
15 15
16 @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) { 16 @HostListener('document:keypress', ["$event"]) catchInput(e: KeyboardEvent) {
17 17
18 if (e.keyCode == 13) { 18 if (e.keyCode == 13) {
19 this.buscarByCodigoBarras(this.busqueda); 19 this.buscarByCodigoBarras(this.busqueda);
20 this.busqueda = ''; 20 this.busqueda = '';
21 } else { 21 } else {
22 this.busqueda += e.key; 22 this.busqueda += e.key;
23 } 23 }
24 24
25 }; 25 };
26 26
27 @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective; 27 @ViewChild('pop', { static: false }) popoverDirective: PopoverDirective;
28 private tienePromo = false; 28 private tienePromo = false;
29 private productoEsPromo = false; 29 private productoEsPromo = false;
30 private busqueda: string = ''; 30 private busqueda: string = '';
31 31
32 private productoAcargar: Producto; 32 private productoAcargar: Producto;
33 private promoAcargar: Producto; 33 private promoAcargar: Producto;
34 34
35 productos: Producto[] = []; 35 productos: Producto[] = [];
36 promociones: Producto[] = []; 36 promociones: Producto[] = [];
37 sinonimos: Sinonimo[] = []; 37 sinonimos: Sinonimo[] = [];
38 apiUrl: string = appSettings.apiUrl 38 apiUrl: string = appSettings.apiUrl
39 39
40 constructor( 40 constructor(
41 private router: Router, 41 private router: Router,
42 private productoService: ProductoService) { } 42 private productoService: ProductoService) { }
43 43
44 ngOnInit() { 44 ngOnInit() {
45 45
46 this.productoAcargar = this.productoService.productoAcargar; 46 this.productoAcargar = this.productoService.productoAcargar;
47 if (this.productoAcargar) { 47 if (this.productoAcargar) {
48 if (this.productoAcargar.PRO) { 48 if (this.productoAcargar.PRO) {
49 this.promociones.push(this.productoAcargar); 49 this.promociones.push(this.productoAcargar);
50 this.promoSeleccionada(this.productoAcargar); 50 this.promoSeleccionada(this.productoAcargar);
51 } 51 }
52 else 52 else
53 this.getPromociones(); 53 this.getPromociones();
54 } 54 }
55 this.getProductos(); 55 this.getProductos();
56 } 56 }
57 57
58 getPromociones() { 58 getPromociones() {
59 59
60 var sector = this.productoAcargar.CodSec; 60 var sector = this.productoAcargar.CodSec;
61 var codigo = this.productoAcargar.CodArt; 61 var codigo = this.productoAcargar.CodArt;
62 this.productoService.getPromociones(sector, codigo) 62 this.productoService.getPromociones(sector, codigo)
63 .subscribe((res: Producto[]) => { 63 .subscribe((res: Producto[]) => {
64 64
65 if (res.length === 0) { 65 if (res.length === 0) {
66 //Si no tiene promociones la cargará al carrito despues de un tiempo 66 //Si no tiene promociones la cargará al carrito despues de un tiempo
67 setTimeout(() => { 67 setTimeout(() => {
68 this.productoAcargar.cantidad = 1; 68 this.productoAcargar.cantidad = 1;
69 this.productoService.productos.push(this.productoAcargar); 69 this.productoService.productos.push(this.productoAcargar);
70 this.productoAcargar = this.productoService.productoAcargar = undefined; 70 this.productoAcargar = this.productoService.productoAcargar = undefined;
71 }, 2000) 71 }, 2000)
72 } else { 72 } else {
73 73
74 this.promociones = res; 74 this.promociones = res;
75 this.popoverDirective.show(); 75 this.popoverDirective.show();
76 } 76 }
77 }, error => { console.error(error); }) 77 }, error => { console.error(error); })
78 } 78 }
79 79
80 showPopover() { 80 showPopover() {
81 81
82 this.popoverDirective.show(); 82 this.popoverDirective.show();
83 } 83 }
84 84
85 getProductos() { 85 getProductos() {
86 this.productoService.getAll() 86 this.productoService.getAll()
87 .subscribe((productos: Producto[]) => { 87 .subscribe((productos: Producto[]) => {
88 this.productos = productos; 88 this.productos = productos;
89 }); 89 });
90 } 90 }
91 91
92 irBusquedaProductos(verPromociones) { 92 irBusquedaProductos(verPromociones) {
93 93
94 this.productoService.verCategoriasProductos = verPromociones; 94 this.productoService.verCategoriasProductos = verPromociones;
95 this.router.navigate(['busqueda-productos']); 95 this.router.navigate(['busqueda-productos']);
96 } 96 }
97 97
98 confirmarProducto() { 98 confirmarProducto() {
99 this.promoAcargar.cantidad = 1; 99 this.promoAcargar.cantidad = 1;
100 this.productoService.productos.push(this.promoAcargar); 100 this.productoService.productos.push(this.promoAcargar);
101 this.promoAcargar = undefined; 101 this.productoService.productoAcargar = this.promoAcargar = this.productoAcargar = undefined;
102 this.productoAcargar = undefined;
103 this.promociones = []; 102 this.promociones = [];
104 this.popoverDirective.hide(); 103 this.popoverDirective.hide();
105 } 104 }
106 105
107 deshacerCarga() { 106 deshacerCarga() {
108 107
109 if (this.sinonimos.length > 0) { 108 if (this.sinonimos.length > 0) {
110 this.sinonimos = []; 109 this.sinonimos = [];
111 this.popoverDirective.hide(); 110 this.popoverDirective.hide();
112 } 111 }
113 112
114 if (this.promoAcargar) { 113 if (this.promoAcargar) {
115 this.promoAcargar = undefined; 114 this.promoAcargar = undefined;
116 if (this.productoAcargar.PRO) { 115 if (this.productoAcargar.PRO) {
117 this.productoAcargar = undefined; 116 this.productoAcargar = undefined;
118 this.promociones = []; 117 this.promociones = [];
119 this.popoverDirective.hide(); 118 this.popoverDirective.hide();
120 } else { 119 } else {
121 this.popoverDirective.show(); 120 this.popoverDirective.show();
122 } 121 }
123 } else { 122 } else {
124 this.productoAcargar = undefined; 123 this.productoAcargar = undefined;
125 this.promociones = []; 124 this.promociones = [];
126 this.popoverDirective.hide(); 125 this.popoverDirective.hide();
127 } 126 }
128 } 127 }
129 128
130 promoSeleccionada($event: Producto) { 129 promoSeleccionada($event: Producto) {
131 130
132 this.promoAcargar = $event; 131 this.promoAcargar = $event;
133 this.promoAcargar.tieneSinonimos = true; 132 this.promoAcargar.tieneSinonimos = true;
134 if (this.promoAcargar.tieneSinonimos) { 133 if (this.promoAcargar.tieneSinonimos) {
135 var sector = this.promoAcargar.CodSec; 134 var sector = this.promoAcargar.CodSec;
136 var codigo = this.promoAcargar.CodArt; 135 var codigo = this.promoAcargar.CodArt;
137 this.productoService.getPromocionSinonimos(sector, codigo) 136 this.productoService.getPromocionSinonimos(sector, codigo)
138 .subscribe((res: Sinonimo[]) => { 137 .subscribe((res: Sinonimo[]) => {
139 138
140 res.forEach(sinonimo => { 139 res.forEach(sinonimo => {
141 140
142 sinonimo.productos.forEach(productoSinonimo => { 141 sinonimo.productos.forEach(productoSinonimo => {
143 142
144 this.promoAcargar.productos.forEach(productoPromo => { 143 this.promoAcargar.productos.forEach(productoPromo => {
145 144
146 if (productoSinonimo.id === productoPromo.id) { 145 if (productoSinonimo.id === productoPromo.id) {
147 sinonimo.cantidad = productoPromo.cantidad; 146 sinonimo.cantidad = productoPromo.cantidad;
148 sinonimo.productoPadre = productoPromo.id; 147 sinonimo.productoPadre = productoPromo.id;
149 } 148 }
150 }) 149 })
151 }) 150 })
152 }) 151 })
153 152
154 this.sinonimos = res; 153 this.sinonimos = res;
155 if (res.length > 0) 154 if (res.length > 0)
156 this.showPopover(); 155 this.showPopover();
156 else
157 this.popoverDirective.hide();
157 }) 158 })
158 } else { 159 } else {
159 this.popoverDirective.hide(); 160 this.popoverDirective.hide();
160 } 161 }
161 } 162 }
162 163
163 productosPersonalizados($event: Producto[]) { 164 productosPersonalizados($event: Producto[]) {
164 165
165 var productosPersonalizados = $event; 166 var productosPersonalizados = $event;
166 167
167 this.promoAcargar.productos.forEach(productoPromo => { 168 this.promoAcargar.productos.forEach(productoPromo => {
168 169
169 if (!productoPromo.idSinonimo) productosPersonalizados.push(productoPromo); 170 if (!productoPromo.idSinonimo) productosPersonalizados.push(productoPromo);
170 }) 171 })
171 172
172 this.promoAcargar.productos = productosPersonalizados; 173 this.promoAcargar.productos = productosPersonalizados;
174 this.confirmarProducto();
173 } 175 }
174 176
175 buscarByCodigoBarras(busqueda) { 177 buscarByCodigoBarras(busqueda) {
176 178
177 let producto = this.productos.filter(producto => { 179 let producto = this.productos.filter(producto => {
178 return producto.codigoBarra == busqueda; 180 return producto.codigoBarra == busqueda;
179 }); 181 });
180 182
181 if (producto.length) { 183 if (producto.length) {
182 184
183 this.productoAcargar = producto[0]; 185 this.productoAcargar = producto[0];
184 this.getPromociones(); 186 this.getPromociones();
185 187
186 } else { 188 } else {
187 alert('No se encuentra el producto'); 189 alert('No se encuentra el producto');
188 } 190 }
189 191
190 } 192 }
191 193
192 } 194 }
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 }
src/app/components/sidebar/sidebar.component.html
1 <div class="disable-user-select d-flex align-items-center flex-column h-100 pt-2 text-center"> 1 <div class="disable-user-select d-flex align-items-center flex-column h-100 pt-2 text-center">
2 2
3 <!-- ENCABEZADO --> 3 <!-- ENCABEZADO -->
4 <p class="h4 border-bottom border-white text-white mt-4 pb-2"> 4 <p class="h4 border-bottom border-white text-white mt-4 pb-2">
5 Mi compra 5 Mi compra
6 <i class="fa fa-shopping-cart" aria-hidden="true"></i> 6 <i class="fa fa-shopping-cart" aria-hidden="true"></i>
7 </p> 7 </p>
8 8
9 <div class="overflow-auto overflow-scroll mb-2 w-100"> 9 <div class="overflow-auto overflow-scroll mb-2 w-100">
10 <!-- PRODUCTOS CARRITO --> 10 <!-- PRODUCTOS CARRITO -->
11 <div 11 <div
12 class="slide-in-bl my-2 bg-white border-0 rounded-sm" 12 class="slide-in-bl my-2 bg-white border-0 rounded-sm"
13 *ngFor="let producto of productosCarrito.slice().reverse(); let i = index"> 13 *ngFor="let producto of productosCarrito.slice().reverse(); let i = index">
14 <!-- <img class="w-100 m-auto rounded-sm shadow" src="{{apiUrl}}/imagenes/{{producto.imagenes[0].imagen}}"> --> 14 <!-- <img class="w-100 m-auto rounded-sm shadow" src="{{apiUrl}}/imagenes/{{producto.imagenes[0].imagen}}"> -->
15 <div class="row m-0 p-0 px-1 py-1 shadow rounded-sm"> 15 <div class="row m-0 p-0 px-1 py-1 shadow rounded-sm">
16 <div class="col-12 p-0 pt-2 text-left my-auto"> 16 <div class="col-12 p-0 pt-2 text-left my-auto">
17 <p class="m-0 h6"><small>{{producto.DetArt}}</small></p> 17 <p class="m-0 h6"><small>{{producto.DetArt}}</small></p>
18 <p class="m-0 h6"><small>COD: {{producto.CodRub}}</small></p> 18 <p class="m-0 h6"><small>COD: {{producto.CodRub}}</small></p>
19 </div> 19 </div>
20 <div class="col-12 pr-1 text-right h6 my-auto "> 20 <div class="col-12 pr-1 text-right h6 my-auto ">
21 <p class="m-0">{{producto.PreVen | currency}}</p> 21 <p class="m-0">{{producto.PreVen | currency}}</p>
22 </div> 22 </div>
23 </div> 23 </div>
24 24
25 <!-- BOTONES --> 25 <!-- BOTONES -->
26 <div class="row m-0 d-flex justify-content-between"> 26 <div class="row m-0 d-flex justify-content-between">
27
27 <!-- SUMAR - RESTAR CANTIDAD --> 28 <!-- SUMAR - RESTAR CANTIDAD -->
28 <div class="col-auto px-1 my-2"> 29 <div class="col-auto px-1 my-2">
29 <div class="btn-group-sm btn-group float-left my-auto" role="group"> 30 <div class="btn-group-sm btn-group float-left my-auto" role="group">
30 <button type="button" class="btn btn-light btn-sm my-auto border" (click)="aumentarContador(i)"> 31 <button
32 type="button"
33 class="btn btn-light btn-sm my-auto border shadow"
34 (click)="aumentarCantidad(producto)">
31 <i class="fa fa-plus" aria-hidden="true"></i> 35 <i class="fa fa-plus" aria-hidden="true"></i>
32 </button> 36 </button>
33 <div class="bg-white border border-white px-3 my-auto text-dark h5"> 37 <div class="bg-white border border-white px-3 my-auto text-dark h5 shadow">
34 <small>{{producto.cantidad}}</small> 38 <small>{{producto.cantidad}}</small>
35 </div> 39 </div>
36 <button type="button" class="btn btn-light btn-sm my-auto border" (click)="decrementarContador(i)"> 40 <button
41 type="button"
42 class="btn btn-light btn-sm my-auto border shadow"
43 (click)="restarCantidad(producto)">
37 <i class="fa fa-minus" aria-hidden="true"></i> 44 <i class="fa fa-minus" aria-hidden="true"></i>
38 </button> 45 </button>
39 </div> 46 </div>
40 </div> 47 </div>
48
41 <!-- PERSONALIZAR --> 49 <!-- PERSONALIZAR -->
42 <div class="col-auto px-1 my-2"> 50 <div class="col-auto px-1 my-2">
43 <button 51 <button
44 *ngIf="esPersonalizable(producto)" 52 *ngIf="esPersonalizable(producto)"
45 type="button" 53 type="button"
46 class="btn btn-light btn-sm my-auto float-left border" 54 class="btn btn-light btn-sm my-auto float-left border shadow"
47 (click)="personalizarPromo"> 55 (click)="personalizarPromo">
48 <i class="fa fa-hand-o-up" aria-hidden="true"></i> 56 <i class="fa fa-hand-o-up" aria-hidden="true"></i>
49 </button> 57 </button>
50 </div> 58 </div>
59
51 <!-- BORRAR --> 60 <!-- BORRAR -->
52 <div class="col-auto px-1 my-2"> 61 <div class="col-auto px-1 my-2">
53 <button type="button" class="btn btn-secondary btn-sm my-auto" (click)="deleteProducto(i)"> 62 <button
54 <i class="fa fa-times" aria-hidden="true"></i> 63 type="button"
64 class="btn btn-secondary btn-sm my-auto shadow"
65 (click)="deleteProducto(producto, i)">
66 <i class="fa fa-trash" aria-hidden="true"></i>
55 </button> 67 </button>
56 </div> 68 </div>
57 </div> 69 </div>
58 </div> 70 </div>
59 </div> 71 </div>
60 72
61 <!-- TOTAL --> 73 <!-- TOTAL -->
62 <div class="card rounded-top-sm mt-auto blue-gradient border-0"> 74 <div class="card rounded-top-sm mt-auto blue-gradient border-0">
63 <div class="card-body row"> 75 <div class="card-body row">
64 <div class="col-12"> 76 <div class="col-12">
65 <p class="h4 border-bottom border-secondary text-secondary pb-2" *ngIf="cont === 1">({{cont}}) item</p> 77 <p
66 <p class="h4 border-bottom border-secondary text-secondary pb-2" *ngIf="cont > 1">({{cont}}) items</p> 78 class="h4 border-bottom border-secondary text-secondary pb-2">
79 ({{getCantidadProductos()}}) items
80 </p>
67 <p class="h3 text-secondary">Total</p> 81 <p class="h3 text-secondary">Total</p>
68 <p class="h3 font-weight-bold">{{getTotal() | currency}}</p> 82 <p class="h3 font-weight-bold">{{getTotal() | currency}}</p>
69 </div> 83 </div>
70 <div class="col-12"> 84 <div class="col-12">
71 <button type="button" class="btn btn-block btn-light btn-lg shadow mb-2 p-1" routerLink="/confirmacion-carrito"> 85 <button
86 *ngIf="cantTotal > 0"
87 type="button"
88 class="btn btn-block btn-light btn-lg shadow mb-2 p-1"
89 routerLink="/confirmacion-carrito">
72 <span class="font-weight-bold pr-1">Finalizar y Pagar</span> 90 <span class="font-weight-bold pr-1">Finalizar y Pagar</span>
73 <i class="fa fa-check text-success" aria-hidden="true"></i> 91 <i class="fa fa-check text-success" aria-hidden="true"></i>
74 </button> 92 </button>
75 <button 93 <button
76 type="button" 94 type="button"
77 class="btn btn-block btn-light shadow btn-sm shadow" 95 class="btn btn-block btn-light shadow btn-sm shadow"
78 [routerLink]="['/cancelar-compra']"> 96 [routerLink]="['/cancelar-compra']">
79 <span class="pr-1">Cancelar</span> 97 <span class="pr-1">Cancelar</span>
80 <i class="fa fa-times text-danger" aria-hidden="true"></i> 98 <i class="fa fa-times text-danger" aria-hidden="true"></i>
81 </button> 99 </button>
82 </div> 100 </div>
83 </div> 101 </div>
84 </div> 102 </div>
85 </div> 103 </div>
86 104
src/app/components/sidebar/sidebar.component.ts
1 import { Component, OnInit } from '@angular/core'; 1 import { Component, OnInit } from '@angular/core';
2 import { Producto } from 'src/app/wrappers/producto'; 2 import { Producto } from 'src/app/wrappers/producto';
3 import { appSettings } from 'src/etc/AppSettings'; 3 import { appSettings } from 'src/etc/AppSettings';
4 import { ProductoService } from 'src/app/services/producto.service'; 4 import { ProductoService } from 'src/app/services/producto.service';
5 5
6
7 @Component({ 6 @Component({
8 selector: 'app-sidebar', 7 selector: 'app-sidebar',
9 templateUrl: './sidebar.component.html', 8 templateUrl: './sidebar.component.html',
10 styleUrls: ['./sidebar.component.scss'], 9 styleUrls: ['./sidebar.component.scss'],
11 }) 10 })
12 export class SidebarComponent implements OnInit { 11 export class SidebarComponent implements OnInit {
13 12
14 private cont: number = 1; 13 private cantTotal: number = 0;
15 private min: number = 1; 14 private cantMin: number = 1;
16 private max: number = 50; 15 private cantMax: number = 50;
17 private total: number = 0; 16 private total: number = 0;
18 private apiUrl: string = appSettings.apiUrl; 17 private apiUrl: string = appSettings.apiUrl;
19 18
20 public productosCarrito: Producto[] = []; 19 public productosCarrito: Producto[] = [];
21 20
22 constructor(private productoService: ProductoService) { 21 constructor(private productoService: ProductoService) { }
23
24 this.productosCarrito = this.productoService.productos;
25 }
26 22
27 ngOnInit() { 23 ngOnInit() {
28 24
25 this.productosCarrito = this.productoService.productos;
29 } 26 }
30 27
31 getCantidadProductos() { 28 getCantidadProductos() {
32 29
33 for (let i = 0; i < this.productosCarrito.length; i++) { 30 setTimeout(() => {
34 this.productosCarrito[i].cantidad = 1 31 var cantTotalAux = 0;
35 this.cont++; 32 this.productosCarrito.forEach(producto => {
36 } 33
37 return this.cont; 34 cantTotalAux += producto.cantidad;
35 }, 250);
36 this.cantTotal = cantTotalAux;
37 })
38 return this.cantTotal;
38 } 39 }
39 40
40 getTotal() { 41 getTotal() {
41 42
42 let subTotal = 0; 43 var subTotal = 0;
43 for (let i = 0; i < this.productosCarrito.length; i++) { 44 for (let i = 0; i < this.productosCarrito.length; i++) {
44 subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad); 45 subTotal = subTotal + (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad);
45 } 46 }
46 return this.total = subTotal; 47 return this.total = subTotal;
47 } 48 }
48 49
49 public aumentarContador(index) { 50 aumentarCantidad(producto: Producto) {
50
51 this.cont++;
52 for (let i = 0; i < this.productosCarrito.length; i++) {
53 if (i === index) {
54 this.total = this.total + this.productosCarrito[i].PreVen;
55 return (this.productosCarrito[i].cantidad === this.max) ?
56 this.productosCarrito[i].cantidad : this.productosCarrito[i].cantidad++;
57
58 }
59 }
60 }
61
62 decrementarContador(index) {
63 51
64 for (let i = 0; i < this.productosCarrito.length; i++) { 52 if (producto.cantidad < this.cantMax) {
65 if (i === index && this.productosCarrito[i].cantidad > this.min) { 53 producto.cantidad++;
66 this.productosCarrito[i].cantidad--; 54 this.cantTotal++
67 this.cont--;
68 break;
69 }
70 } 55 }
71
72 this.getTotal()
73 } 56 }
74 57
75 setCantidadItems() { 58 restarCantidad(producto: Producto) {
76 59
77 this.cont = 0; 60 if (producto.cantidad > this.cantMin) {
78 for (let i = 0; i < this.productosCarrito.length; i++) { 61 producto.cantidad--;
79 this.cont += this.productosCarrito[i].cantidad; 62 this.cantTotal--;
80 }
81 }
82
83 deleteProducto(index: number) {
84
85 for (let i = 0; i < this.productosCarrito.length; i++) {
86 if (i === index) {
87 this.cont -= this.productosCarrito[i].cantidad;
88 //Elimina del total el PreVen de todo el item
89 this.total = this.total - (this.productosCarrito[i].PreVen * this.productosCarrito[i].cantidad);
90 this.productosCarrito.reverse().splice(i, 1);
91 return;
92 }
93 } 63 }
94 } 64 }
95 65
96 cleanCarrito() { 66 deleteProducto(producto: Producto, index: number) {
97 67
98 this.productoService.productos = this.productosCarrito = []; 68 this.cantTotal -= producto.cantidad;
99 this.total = 0; 69 this.total -= producto.PreVen * producto.cantidad;
100 this.cont = 0; 70 this.productosCarrito.reverse().splice(index, 1);
71 return;
101 } 72 }
102 73
103 esPersonalizable(producto: Producto) { 74 esPersonalizable(producto: Producto) {
104 75
105 var esPersonalizable: boolean = false; 76 var esPersonalizable: boolean = false;
106 if (producto.tieneSinonimos) 77 if (producto.tieneSinonimos)
107 esPersonalizable = true; 78 esPersonalizable = true;