Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Refactor IStockQuantityService, adjust FormatStockMessage
support6b4305e3d202
diff --git a/src/Business/Grand.Business.Catalog/Services/Products/StockQuantityService.cs b/src/Business/Grand.Business.Catalog/Services/Products/StockQuantityService.csindex 2d1485040..e4bb5d2b5 100644--- a/src/Business/Grand.Business.Catalog/Services/Products/StockQuantityService.cs+++ b/src/Business/Grand.Business.Catalog/Services/Products/StockQuantityService.cs@@ -1,5 +1,6 @@-using Grand.Business.Core.Interfaces.Catalog.Products;-using Grand.Business.Core.Interfaces.Common.Localization;+#nullable enable++using Grand.Business.Core.Interfaces.Catalog.Products;using Grand.Domain.Catalog;using Grand.Domain.Common;@@ -7,13 +8,6 @@ namespace Grand.Business.Catalog.Services.Products{public class StockQuantityService : IStockQuantityService{- private readonly ITranslationService _translationService;- public StockQuantityService(- ITranslationService translationService)- {- _translationService = translationService;- }-public virtual int GetTotalStockQuantity(Product product, bool useReservedQuantity = true,string warehouseId = "", bool total = false){@@ -29,7 +23,9 @@ namespace Grand.Business.Catalog.Services.Products{if (total){- return useReservedQuantity ? product.ProductWarehouseInventory.Sum(x => x.StockQuantity - x.ReservedQuantity) : product.ProductWarehouseInventory.Sum(x => x.StockQuantity);+ return useReservedQuantity+ ? product.ProductWarehouseInventory.Sum(x => x.StockQuantity - x.ReservedQuantity)+ : product.ProductWarehouseInventory.Sum(x => x.StockQuantity);}var pwi = product.ProductWarehouseInventory.FirstOrDefault(x => x.WarehouseId == warehouseId);@@ -39,14 +35,16 @@ namespace Grand.Business.Catalog.Services.Products{result -= pwi.ReservedQuantity;}+return result;}+if (string.IsNullOrEmpty(warehouseId) || string.IsNullOrEmpty(product.WarehouseId))return product.StockQuantity - (useReservedQuantity ? product.ReservedQuantity : 0);-+if (product.WarehouseId == warehouseId)return product.StockQuantity - (useReservedQuantity ? product.ReservedQuantity : 0);-+return 0;}@@ -73,108 +71,123 @@ namespace Grand.Business.Catalog.Services.Products{result -= pwi.ReservedQuantity;}+return result;}if (string.IsNullOrEmpty(warehouseId) || string.IsNullOrEmpty(product.WarehouseId))return combination.StockQuantity - (useReservedQuantity ? combination.ReservedQuantity : 0);-+if (product.WarehouseId == warehouseId)return combination.StockQuantity - (useReservedQuantity ? combination.ReservedQuantity : 0);-+return 0;}- public virtual string FormatStockMessage(Product product, string warehouseId, IList<CustomAttribute> attributes)+ public virtual (string resource, object? arg0) FormatStockMessage(Product product, string warehouseId,+ IList<CustomAttribute> attributes){if (product == null)throw new ArgumentNullException(nameof(product));var stockMessage = string.Empty;- switch (product.ManageInventoryMethodId)+ return product.ManageInventoryMethodId switch {+ ManageInventoryMethod.ManageStock => StockInventoryFormatStockMessage(product, warehouseId,+ stockMessage),+ ManageInventoryMethod.ManageStockByAttributes => StockByAttributesFormatStockMessage(product,+ warehouseId, attributes, stockMessage),+ _ => (stockMessage, null)+ };+ }++ private (string resource, object? arg0) StockByAttributesFormatStockMessage(Product product, string warehouseId,+ IList<CustomAttribute> attributes, string stockMessage)+ {+ if (!product.StockAvailability)+ {+ return (stockMessage, null);+ }++ var combination = product.FindProductAttributeCombination(attributes);+ if (combination != null){- case ManageInventoryMethod.ManageStock:+ //combination exists+ var stockQuantity =+ GetTotalStockQuantityForCombination(product, combination, warehouseId: warehouseId);+ if (stockQuantity > 0)+ {+ if (product.DisplayStockQuantity)+ //display "in stock" with stock quantity+ {+ return ("Products.Availability.InStockWithQuantity", stockQuantity);+ }++ //display "in stock" without stock quantity+ {+ return ("Products.Availability.InStock", null);+ }+ }++ //out of stock+ switch (product.BackorderModeId)+ {+ case BackorderMode.NoBackorders:{- #region Manage stock-- if (!product.StockAvailability)- return stockMessage;-- var stockQuantity = GetTotalStockQuantity(product, warehouseId: warehouseId);- if (stockQuantity > 0)- {- stockMessage = product.DisplayStockQuantity ?- string.Format(_translationService.GetResource("Products.Availability.InStockWithQuantity"), stockQuantity) :- _translationService.GetResource("Products.Availability.InStock");- }- else- {- switch (product.BackorderModeId)- {- case BackorderMode.NoBackorders:- stockMessage = _translationService.GetResource("Products.Availability.OutOfStock");- break;- case BackorderMode.AllowQtyBelowZero:- stockMessage = _translationService.GetResource("Products.Availability.Backordering");- break;- }- }-- #endregion+ return ("Products.Availability.Attributes.OutOfStock", null);}- break;- case ManageInventoryMethod.ManageStockByAttributes:+ case BackorderMode.AllowQtyBelowZero:{- #region Manage stock by attributes-- if (!product.StockAvailability)- return stockMessage;-- var combination = product.FindProductAttributeCombination(attributes);- if (combination != null)- {- //combination exists- var stockQuantity = GetTotalStockQuantityForCombination(product, combination, warehouseId: warehouseId);- if (stockQuantity > 0)- {- stockMessage = product.DisplayStockQuantity ?- //display "in stock" with stock quantity- string.Format(_translationService.GetResource("Products.Availability.InStockWithQuantity"), stockQuantity) :- //display "in stock" without stock quantity- _translationService.GetResource("Products.Availability.InStock");- }- else- {- //out of stock- switch (product.BackorderModeId)- {- case BackorderMode.NoBackorders:- stockMessage = _translationService.GetResource("Products.Availability.Attributes.OutOfStock");- break;- case BackorderMode.AllowQtyBelowZero:- stockMessage = _translationService.GetResource("Products.Availability.Attributes.Backordering");- break;- }- if (!combination.AllowOutOfStockOrders)- stockMessage = _translationService.GetResource("Products.Availability.Attributes.OutOfStock");- }- }- else- {- stockMessage = _translationService.GetResource("Products.Availability.AttributeCombinationsNotExists");- }-- #endregion+ return ("Products.Availability.Attributes.Backordering", null);}- break;- case ManageInventoryMethod.DontManageStock:- case ManageInventoryMethod.ManageStockByBundleProducts:- default:- return stockMessage;+ }++ if (!combination.AllowOutOfStockOrders)+ {+ return ("Products.Availability.Attributes.OutOfStock", null);+ }}- return stockMessage;+ else+ {+ return ("Products.Availability.AttributeCombinationsNotExists", null);+ }+ return (stockMessage, null);}+ private (string resource, object? arg0) StockInventoryFormatStockMessage(Product product, string warehouseId,+ string stockMessage)+ {+ if (!product.StockAvailability)+ {+ return (stockMessage, null);+ }++ var stockQuantity = GetTotalStockQuantity(product, warehouseId: warehouseId);+ if (stockQuantity > 0)+ {+ if (product.DisplayStockQuantity)+ {+ return ("Products.Availability.InStockWithQuantity", stockQuantity);+ }++ {+ return ("Products.Availability.InStock", null);+ }+ }++ switch (product.BackorderModeId)+ {+ case BackorderMode.NoBackorders:+ {+ return ("Products.Availability.OutOfStock", null);+ }+ case BackorderMode.AllowQtyBelowZero:+ {+ return ("Products.Availability.Backordering", null);+ }+ }++ return (stockMessage, null);+ }}-}+}\ No newline at end of filediff --git a/src/Business/Grand.Business.Core/Interfaces/Catalog/Products/IStockQuantityService.cs b/src/Business/Grand.Business.Core/Interfaces/Catalog/Products/IStockQuantityService.csindex bdc9ad712..bf5266491 100644--- a/src/Business/Grand.Business.Core/Interfaces/Catalog/Products/IStockQuantityService.cs+++ b/src/Business/Grand.Business.Core/Interfaces/Catalog/Products/IStockQuantityService.cs@@ -1,4 +1,5 @@-using Grand.Domain.Catalog;+#nullable enable+using Grand.Domain.Catalog;using Grand.Domain.Common;namespace Grand.Business.Core.Interfaces.Catalog.Products@@ -12,6 +13,6 @@ namespace Grand.Business.Core.Interfaces.Catalog.Productsint GetTotalStockQuantityForCombination(Product product, ProductAttributeCombination combination,bool useReservedQuantity = true, string warehouseId = "");- string FormatStockMessage(Product product, string warehouseId, IList<CustomAttribute> attributes);+ (string resource, object? arg0) FormatStockMessage(Product product, string warehouseId, IList<CustomAttribute> attributes);}}diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.csindex 81f4b8791..ceb62476a 100644--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs@@ -45,7 +45,7 @@ namespace Grand.Business.Catalog.Tests.Services.Products_mediatorMock = new Mock<IMediator>();_settings = new CatalogSettings();_cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), _mediatorMock.Object);- _stockQuantityService = new StockQuantityService(_translationService.Object);+ _stockQuantityService = new StockQuantityService();_inventoryManageService = new InventoryManageService(_repository, _repositoryInventoryJournal, _stockQuantityService, _cacheBase, _mediatorMock.Object, _settings);}diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeFormatterTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeFormatterTests.csindex a500f5f20..ce19870b7 100644--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeFormatterTests.cs+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeFormatterTests.cs@@ -49,8 +49,7 @@ namespace Grand.Business.Catalog.Tests.Services.Products_mediatorMock = new Mock<IMediator>();_aclServiceMock = new AclService();_productAttributeFormatter = new ProductAttributeFormatter(_workContextMock.Object, _productAttributeServiceMock.Object,- _translationServiceMock.Object, _taxServiceMock.Object, _priceFormatterMock.Object, _pricingServiceMock.Object,- _productServiceMock.Object);+ _taxServiceMock.Object, _priceFormatterMock.Object, _pricingServiceMock.Object, _productServiceMock.Object);}[TestMethod()]diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/StockQuantityServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/StockQuantityServiceTests.csindex 467628776..df091fa81 100644--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/StockQuantityServiceTests.cs+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/StockQuantityServiceTests.cs@@ -12,19 +12,19 @@ namespace Grand.Business.Catalog.Tests.Services.Productspublic class StockQuantityServiceTests{private IStockQuantityService _stockQuantityService;- private ITranslationService _translationService;[TestInitialize()]public void Init(){+ /*var tempLocalizationService = new Mock<ITranslationService>();{tempLocalizationService.Setup(x => x.GetResource("Products.Availability.InStockWithQuantity")).Returns("{0}");tempLocalizationService.Setup(x => x.GetResource("Products.ExclTaxSuffix")).Returns("InStock");_translationService = tempLocalizationService.Object;}- _stockQuantityService = new StockQuantityService(- tempLocalizationService.Object);+ */+ _stockQuantityService = new StockQuantityService();}[TestMethod()]@@ -145,7 +145,7 @@ namespace Grand.Business.Catalog.Tests.Services.Products}[TestMethod()]- public void FormatStockMessageTest()+ public void FormatStockMessageTest_ManageStock_StockAvailability_DisplayStockQuantity_InStockWithQuantity(){//Arrangevar product = new Product {@@ -157,8 +157,25 @@ namespace Grand.Business.Catalog.Tests.Services.Products//Actvar result = _stockQuantityService.FormatStockMessage(product, "", new List<CustomAttribute>());//Assert- Assert.AreEqual("8765", result);+ Assert.AreEqual(product.StockQuantity, result.arg0);+ Assert.AreEqual("Products.Availability.InStockWithQuantity", result.resource);+ }++ [TestMethod()]+ public void FormatStockMessageTest_ManageStock_StockAvailability_InStock()+ {+ //Arrange+ var product = new Product {+ ManageInventoryMethodId = ManageInventoryMethod.ManageStock,+ StockQuantity = 8765,+ StockAvailability = true,+ DisplayStockQuantity = false+ };+ //Act+ var result = _stockQuantityService.FormatStockMessage(product, "", new List<CustomAttribute>());+ //Assert+ Assert.AreEqual(null, result.arg0);+ Assert.AreEqual("Products.Availability.InStock", result.resource);}-}}diff --git a/src/Tests/Grand.Business.Checkout.Tests/Validators/ShoppingCartValidatorsTests.cs b/src/Tests/Grand.Business.Checkout.Tests/Validators/ShoppingCartValidatorsTests.csindex d358cc23f..86bfbab8d 100644--- a/src/Tests/Grand.Business.Checkout.Tests/Validators/ShoppingCartValidatorsTests.cs+++ b/src/Tests/Grand.Business.Checkout.Tests/Validators/ShoppingCartValidatorsTests.cs@@ -44,7 +44,7 @@ namespace Grand.Business.Checkout.Tests.Validators_productAttributeService = new Mock<IProductAttributeService>();_productReservationServiceMock = new Mock<IProductReservationService>();_permissionServiceMock = new Mock<IPermissionService>();- _stockQuantityService = new StockQuantityService(_translationServiceMock.Object);+ _stockQuantityService = new StockQuantityService();_translationServiceMock.Setup(x => x.GetResource(It.IsAny<string>())).Returns("Name");}diff --git a/src/Web/Grand.Web/Controllers/ProductController.cs b/src/Web/Grand.Web/Controllers/ProductController.csindex 56e829e8b..cd8bce4a2 100644--- a/src/Web/Grand.Web/Controllers/ProductController.cs+++ b/src/Web/Grand.Web/Controllers/ProductController.cs@@ -10,6 +10,7 @@ using Grand.Business.Core.Interfaces.Customers;using Grand.Business.Core.Interfaces.Storage;using Grand.Business.Core.Utilities.Common.Security;using Grand.Domain.Catalog;+using Grand.Domain.Common;using Grand.Domain.Media;using Grand.Infrastructure;using Grand.Web.Commands.Models.Products;@@ -189,10 +190,10 @@ namespace Grand.Web.Controllersif (product == null)return new JsonResult("");- var stock = stockQuantityService.FormatStockMessage(product, model.WarehouseId, null);+ var stock = stockQuantityService.FormatStockMessage(product, model.WarehouseId, new List<CustomAttribute>());return Json(new{- stockAvailability = stock+ stockAvailability = string.Format(_translationService.GetResource(stock.resource), stock.arg0)});}diff --git a/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsAttributeChangeHandler.cs b/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsAttributeChangeHandler.csindex d1e92191d..00224eaeb 100644--- a/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsAttributeChangeHandler.cs+++ b/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsAttributeChangeHandler.cs@@ -104,9 +104,9 @@ namespace Grand.Web.Features.Handlers.Products}//stock- model.StockAvailability =- _stockQuantityService.FormatStockMessage(request.Product, warehouseId, customAttributes);-+ var stock = _stockQuantityService.FormatStockMessage(request.Product, warehouseId, customAttributes);+ model.StockAvailability = string.Format(_translationService.GetResource(stock.resource), stock.arg0);+//out of stock subscriptionif (request.Product.ManageInventoryMethodId is ManageInventoryMethod.ManageStockByAttributes or ManageInventoryMethod.ManageStock &&request.Product.BackorderModeId == BackorderMode.NoBackorders &&diff --git a/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsPageHandler.cs b/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsPageHandler.csindex 756311f68..ab7e460e9 100644--- a/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsPageHandler.cs+++ b/src/Web/Grand.Web/Features/Handlers/Products/GetProductDetailsPageHandler.cs@@ -388,7 +388,7 @@ namespace Grand.Web.Features.Handlers.ProductsMpn = product.Mpn,ShowGtin = _catalogSettings.ShowGtin,Gtin = product.Gtin,- StockAvailability = _stockQuantityService.FormatStockMessage(product, warehouseId, null),+ StockAvailability = StockAvailability(product, warehouseId, new List<CustomAttribute>()),UserFields = product.UserFields,HasSampleDownload = product.IsDownload && product.HasSampleDownload,DisplayDiscontinuedMessage =@@ -470,6 +470,13 @@ namespace Grand.Web.Features.Handlers.Products#endregion}+ private string StockAvailability(Product product, string warehouseId, List<CustomAttribute> attributes)+ {+ var stock = _stockQuantityService.FormatStockMessage(product, warehouseId, attributes);+ var stockAvailability = string.Format(_translationService.GetResource(stock.resource), stock.arg0);+ return stockAvailability;+ }+private async Task<ProductAskQuestionSimpleModel> PrepareProductAskQuestionSimpleModel(Product product){var customer = _workContext.CurrentCustomer;@@ -917,10 +924,9 @@ namespace Grand.Web.Features.Handlers.Products&& product.ProductAttributeCombinations.Any()&& product.ProductAttributeMappings.Count == 1){- var customattributes =+ var customAttributes =ProductExtensions.AddProductAttribute(null, attribute, attributeValue.Id);- stockAvailability =- _stockQuantityService.FormatStockMessage(product, string.Empty, customattributes);+ stockAvailability = StockAvailability(product, string.Empty, customAttributes.ToList());}var valueModel = new ProductDetailsModel.ProductAttributeValueModel {