ng-easycommerce-v18 0.3.23-beta.1 → 0.3.23-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4968,8 +4968,15 @@ class CartService {
4968
4968
  * @param quantity la cantidad nueva.
4969
4969
  */
4970
4970
  updateItemQuantity(item, quantity) {
4971
+ // Validar antes de proceder
4971
4972
  if (this.validateQuantity(item, quantity) && (this.validatePriceAndCredits(item, quantity))) {
4972
- firstValueFrom(this._connection.put(this.updateItemQuantityApi(this.findItemByIdVariant(item.variant_id)), { 'quantity': Number(quantity) })).then(res => this.updateCartObj(res) && this.updateCartItemQuantity(item.variant_id, Number(quantity)), err => this.handleError(err));
4973
+ // Si pasa las validaciones, hacer la petición HTTP
4974
+ firstValueFrom(this._connection.put(this.updateItemQuantityApi(this.findItemByIdVariant(item.variant_id)), { 'quantity': Number(quantity) }))
4975
+ .then(res => this.updateCartObj(res) && this.updateCartItemQuantity(item.variant_id, Number(quantity)), err => this.handleError(err));
4976
+ return true; // Validaciones pasaron, operación iniciada
4977
+ }
4978
+ else {
4979
+ return false; // Validaciones fallaron
4973
4980
  }
4974
4981
  }
4975
4982
  /**
@@ -5050,14 +5057,25 @@ class CartService {
5050
5057
  * @returns
5051
5058
  */
5052
5059
  validateQuantity(item, quantity) {
5053
- if ((this.getCountFromItemInCart(item.productCode) + quantity) > item.product.variants[0].maximumItemsQuantity) {
5060
+ const actualQuantity = Number(this.getCountFromItemInCart(item.productCode)); // <-- Forzar a número
5061
+ const newQuantity = Number(quantity); // <-- Forzar a número
5062
+ if ((actualQuantity + newQuantity) > item.product.variants[0].maximumItemsQuantity) {
5054
5063
  this._toastService.show('maximum-items-quantity', { quantity: item.product.variants[0].maximumItemsQuantity });
5055
5064
  return false;
5056
5065
  }
5057
- if ((this.getCountFromItemInCart(item.productCode) + quantity) < item.product.variants[0].minimumItemsQuantity) {
5066
+ if ((actualQuantity + newQuantity) < item.product.variants[0].minimumItemsQuantity) {
5058
5067
  this._toastService.show('minimum-items-quantity', { quantity: item.product.variants[0].minimumItemsQuantity });
5059
5068
  return false;
5060
5069
  }
5070
+ if (item.product.variants[0].multipleQuantity && item.product.variants[0].multipleQuantity > 1) {
5071
+ if ((actualQuantity + newQuantity) % item.product.variants[0].multipleQuantity !== 0) {
5072
+ this._toastService.show('multiple-quantity-required', {
5073
+ multiple: item.product.variants[0].multipleQuantity,
5074
+ current: actualQuantity + newQuantity // <-- Ahora suma correctamente
5075
+ });
5076
+ return false;
5077
+ }
5078
+ }
5061
5079
  return true;
5062
5080
  }
5063
5081
  /**
@@ -5405,6 +5423,7 @@ class ProductDetailService {
5405
5423
  associated.minimumItemsQuantity = v.stock > 0 && 'minimumItemsQuantity' in v && v.minimumItemsQuantity > 0
5406
5424
  ? v.minimumItemsQuantity
5407
5425
  : (v.multipleQuantity ?? 1);
5426
+ associated.multipleQuantity = v.multipleQuantity ?? 1;
5408
5427
  // Precios al consumidor final
5409
5428
  associated.finalConsumer = {
5410
5429
  finalConsumerPrice: v.final_consumer_price,
@@ -5607,6 +5626,15 @@ class ProductDetailService {
5607
5626
  this._toastService.show('minimum-items-quantity', { quantity: asociatedData.minimumItemsQuantity });
5608
5627
  return false;
5609
5628
  }
5629
+ if (asociatedData.multipleQuantity && asociatedData.multipleQuantity > 1) {
5630
+ if ((actualQuantity + quantity) % asociatedData.multipleQuantity !== 0) {
5631
+ this._toastService.show('multiple-quantity-required', {
5632
+ multiple: asociatedData.multipleQuantity,
5633
+ current: actualQuantity + quantity
5634
+ });
5635
+ return false;
5636
+ }
5637
+ }
5610
5638
  return true;
5611
5639
  };
5612
5640
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: ProductDetailService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
@@ -7498,7 +7526,6 @@ class ProductEcComponent {
7498
7526
  return Math.round(discount);
7499
7527
  }
7500
7528
  plus(stock, multipleQuantity) {
7501
- console.log('Aumentando cantidad');
7502
7529
  const current = Number(this.quantity()); // <-- fuerza a número
7503
7530
  if (multipleQuantity && multipleQuantity > 0) {
7504
7531
  stock
@@ -7516,7 +7543,6 @@ class ProductEcComponent {
7516
7543
  }
7517
7544
  }
7518
7545
  less(multipleQuantity) {
7519
- console.log('Disminuyendo cantidad');
7520
7546
  const current = Number(this.quantity()); // <-- fuerza a número
7521
7547
  if (multipleQuantity && multipleQuantity > 0) {
7522
7548
  current > multipleQuantity
@@ -7528,7 +7554,6 @@ class ProductEcComponent {
7528
7554
  }
7529
7555
  }
7530
7556
  addToCart() {
7531
- console.log('Añadiendo al carrito');
7532
7557
  if (this.isAddingToCart() || this.quantity() <= 0)
7533
7558
  return;
7534
7559
  // Verificar que tenemos el producto y sus variantes
@@ -7547,22 +7572,73 @@ class ProductEcComponent {
7547
7572
  this._toastService.show('out-of-stock-actually');
7548
7573
  return;
7549
7574
  }
7575
+ // NUEVAS VALIDACIONES: Verificar restricciones del producto
7576
+ if (!this.validateQuantity(this.quantity())) {
7577
+ return; // La validación ya mostró el toast correspondiente
7578
+ }
7550
7579
  this.isAddingToCart.set(true);
7551
7580
  try {
7552
7581
  // Agregar al carrito usando CartService directamente
7553
7582
  this._cartService.addToCart(this.product, this.quantity(), firstVariant.code);
7554
- console.log('Producto agregado al carrito exitosamente');
7555
7583
  // Resetear cantidad a 1 después de agregar al carrito
7556
7584
  this.quantity.set(1);
7557
7585
  }
7558
7586
  catch (error) {
7559
- console.error('Error al agregar al carrito:', error);
7560
7587
  this._toastService.show('cant-buy');
7561
7588
  }
7562
7589
  setTimeout(() => {
7563
7590
  this.isAddingToCart.set(false);
7564
7591
  }, 2000);
7565
7592
  }
7593
+ /**
7594
+ * Valida las restricciones de cantidad del producto
7595
+ */
7596
+ validateQuantity(quantity) {
7597
+ if (!this.product.variants || this.product.variants.length === 0) {
7598
+ this._toastService.show('cant-buy');
7599
+ return false;
7600
+ }
7601
+ const variant = this.product.variants[0];
7602
+ // Obtener cantidad actual en el carrito
7603
+ const actualQuantity = this._cartService.getCountFromItemInCart(variant.code);
7604
+ const totalQuantity = Number(actualQuantity) + Number(quantity);
7605
+ // Obtener restricciones del producto/variante (con valores por defecto)
7606
+ const maximumItemsQuantity = this.product.maximumItemsQuantity ||
7607
+ variant.maximumItemsQuantity ||
7608
+ 999999;
7609
+ const minimumItemsQuantity = this.product.minimumItemsQuantity ||
7610
+ variant.minimumItemsQuantity ||
7611
+ 1;
7612
+ const multipleQuantity = this.product.multipleQuantity ||
7613
+ variant.multipleQuantity ||
7614
+ 1;
7615
+ // Validar cantidad máxima
7616
+ if (totalQuantity > maximumItemsQuantity) {
7617
+ this._toastService.show('maximum-items-quantity', { quantity: maximumItemsQuantity });
7618
+ return false;
7619
+ }
7620
+ // Validar cantidad mínima
7621
+ if (totalQuantity < minimumItemsQuantity) {
7622
+ this._toastService.show('minimum-items-quantity', { quantity: minimumItemsQuantity });
7623
+ return false;
7624
+ }
7625
+ // Validar múltiplo de cantidad
7626
+ if (multipleQuantity && multipleQuantity > 1) {
7627
+ if (totalQuantity % multipleQuantity !== 0) {
7628
+ this._toastService.show('multiple-quantity-required', {
7629
+ multiple: multipleQuantity,
7630
+ current: totalQuantity
7631
+ });
7632
+ return false;
7633
+ }
7634
+ }
7635
+ // Validar que no exceda el stock total
7636
+ if (totalQuantity > variant.stock) {
7637
+ this._toastService.show('out-of-stock-actually');
7638
+ return false;
7639
+ }
7640
+ return true; // Todas las validaciones pasaron
7641
+ }
7566
7642
  checkStock(stock) {
7567
7643
  if (this.quantity() >= stock)
7568
7644
  this.quantity.set(stock);
@@ -9784,28 +9860,56 @@ class CartItemEcComponent {
9784
9860
  // console.log(this.item, this.mediaUrl);
9785
9861
  }
9786
9862
  updateQuantity(stock) {
9863
+ const originalQuantity = this.item.quantity; // Guardar cantidad original
9787
9864
  if (this.quantity > 0 && this.quantity <= stock) {
9788
- this._cartService.updateItemQuantity(this.item, this.quantity);
9865
+ const success = this._cartService.updateItemQuantity(this.item, this.quantity);
9866
+ // Si la validación falló, restaurar la cantidad original
9867
+ if (!success) {
9868
+ this.quantity = originalQuantity;
9869
+ }
9789
9870
  }
9790
9871
  else {
9791
- this.quantity = this.item.quantity;
9872
+ // Restaurar cantidad original si está fuera de rango
9873
+ this.quantity = originalQuantity;
9792
9874
  this._toastService.show('out-of-stock-actually');
9793
9875
  }
9794
9876
  }
9795
- less(stock, value = 1) {
9877
+ plus(stock, value = 1) {
9796
9878
  if (this.isQuantityUpdating)
9797
9879
  return;
9798
9880
  this.isQuantityUpdating = true;
9799
- let quantity = this.item.quantity - value;
9800
- quantity > 0 && quantity <= stock ? this._cartService.updateItemQuantity(this.item, quantity) : this._toastService.show('out-of-stock-actually');
9881
+ let newQuantity = Number(this.quantity) + value; // Forzar a número
9882
+ // Verificar stock ANTES de llamar al servicio
9883
+ if (newQuantity > stock) {
9884
+ this._toastService.show('out-of-stock-actually');
9885
+ setTimeout(() => { this.isQuantityUpdating = false; }, 1000);
9886
+ return;
9887
+ }
9888
+ const success = this._cartService.updateItemQuantity(this.item, newQuantity);
9889
+ // Solo actualizar el input si la operación fue exitosa
9890
+ if (success) {
9891
+ this.quantity = newQuantity;
9892
+ }
9893
+ // Si success es false, NO mostrar mensaje aquí porque ya lo mostró validateQuantity()
9801
9894
  setTimeout(() => { this.isQuantityUpdating = false; }, 1000);
9802
9895
  }
9803
- plus(stock, value = 1) {
9896
+ less(stock, value = 1) {
9804
9897
  if (this.isQuantityUpdating)
9805
9898
  return;
9806
9899
  this.isQuantityUpdating = true;
9807
- let quantity = this.item.quantity + value;
9808
- quantity > 0 && quantity <= stock ? this._cartService.updateItemQuantity(this.item, quantity) : this._toastService.show('out-of-stock-actually');
9900
+ let newQuantity = Number(this.quantity) - value; // Forzar a número
9901
+ // Verificar cantidad mínima ANTES de llamar al servicio
9902
+ if (newQuantity <= 0) {
9903
+ // No permitir cantidades menores o iguales a 0
9904
+ setTimeout(() => { this.isQuantityUpdating = false; }, 1000);
9905
+ return;
9906
+ }
9907
+ const success = this._cartService.updateItemQuantity(this.item, newQuantity);
9908
+ // Solo actualizar el input si la operación fue exitosa
9909
+ if (success) {
9910
+ this.quantity = newQuantity;
9911
+ }
9912
+ // Si success es false, NO mostrar mensaje aquí porque ya lo mostró validateQuantity()
9809
9913
  setTimeout(() => { this.isQuantityUpdating = false; }, 1000);
9810
9914
  }
9811
9915
  deleteCartItem() {