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
Do not allow add shipment if stock is out of
KrzysztofPajak611ba828e43f
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shipment/AddShipment.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shipment/AddShipment.cshtmlindex 633cc0426..410b637d8 100644--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shipment/AddShipment.cshtml+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shipment/AddShipment.cshtml@@ -1,16 +1,12 @@-@using System.Text-@model ShipmentModel+@model ShipmentModel@{//page titleViewBag.Title = string.Format(Loc["Admin.Orders.Shipments.AddNew.Title"], Model.OrderId);- var itemsFromMultipleWarehouses = Model.Items- .Where(x => x.AllowToChooseWarehouse && x.AvailableWarehouses.Count > 0)- .ToList();}<form asp-area="@Constants.AreaAdmin" asp-controller="Shipment" asp-action="AddShipment" method="post"asp-route-orderId="@Context.Request.Query["orderId"]">-+<div asp-validation-summary="All"></div><div class="col-md-12">@@ -53,176 +49,126 @@</div></div>- @if (itemsFromMultipleWarehouses.Any())- {- <script>- $(document).ready(function () {- $('#shipment-save').click(function () {- return validateWarehouseAvailability();- });- $('#shipment-save-continue').click(function () {- return validateWarehouseAvailability();- });- });--- function validateWarehouseAvailability() {- @{- var sb = new StringBuilder();- for (int i = 0; i <= itemsFromMultipleWarehouses.Count - 1; i++)- {- var item = itemsFromMultipleWarehouses[i];- sb.AppendFormat("validateAvailableQuantity('{0}')", item.OrderItemId);- if (i != itemsFromMultipleWarehouses.Count - 1)- {- sb.Append(" && ");- }- }- }- var valid = @(Html.Raw(sb.ToString()));- if (!valid) {- return confirm('@Loc["Admin.Orders.Shipments.Products.Warehouse.QuantityNotEnough"]');- }- return true;- }-- function validateAvailableQuantity(orderItemId) {- var enteredValue = parseInt($('#qtyToAdd' + orderItemId).val(), 10);- if (enteredValue <= 0)- return true;- var maxValue = parseInt($('#qtyToAdd' + orderItemId).attr('max'), 10);- if (enteredValue > maxValue)- return false;- var reservedValue = parseInt($('#warehouse_' + orderItemId).find(':selected').data('reserved-qty'), 10);- var plannedValue = parseInt($('#warehouse_' + orderItemId).find(':selected').data('planned-qty'), 10);- var availableToAdd = reservedValue - plannedValue;- return enteredValue <= availableToAdd;- }-+ <h4><strong>@Loc["Admin.Orders.Shipments.Products"]</strong></h4>- </script>- }-- <h4><strong>@Loc["Admin.Orders.Shipments.Products"]</strong></h4>-- <div class="table-responsive visible-xs-block visible-sm-block visible-md-block visible-lg-block">- <table class="table table-bordered table-striped table-hover">- <thead>- <tr>- <th>- @Loc["Admin.Orders.Shipments.Products.ProductName"]- </th>- <th>- @Loc["Admin.Orders.Shipments.Products.SKU"]- </th>- <th>- @Loc["Admin.Orders.Shipments.Products.Warehouse"]- </th>- <th>- @Loc["Admin.Orders.Shipments.Products.ItemWeight"]- </th>- <th>- @Loc["Admin.Orders.Shipments.Products.ItemDimensions"]- </th>- <th>- @Loc["Admin.Orders.Shipments.Products.QtyOrdered"]- </th>- <th>- @Loc["Admin.Orders.Shipments.Products.QtyShipped"]- </th>- <th>- @Loc["Admin.Orders.Shipments.Products.QtyToShip"]- </th>- </tr>- </thead>- <tbody>- @foreach (var item in Model.Items)- {- <tr>- <td>- <div style="padding-left:10px;padding-right:10px;">- <em><a href="@Url.Action("Edit", "Product", new {id = item.ProductId, area = Constants.AreaAdmin})">@item.ProductName</a></em>- @if (!String.IsNullOrEmpty(item.AttributeInfo))- {- <p>- @Html.Raw(item.AttributeInfo)- </p>- }- @if (item.ShipSeparately)- {- <p>- <strong>@Loc["Admin.Orders.Shipments.Products.ShipSeparately.Warning"]</strong>- @Loc["Admin.Orders.Shipments.Products.ShipSeparately"]- </p>- }- </div>- </td>- <td>- <div style="padding-left:10px;padding-right:10px;">- @item.Sku- </div>- </td>- <td>- <div>- @if (item.AllowToChooseWarehouse)- {- if (item.AvailableWarehouses.Count > 0)- {- <select name="warehouse_@(item.OrderItemId)" id="warehouse_@(item.OrderItemId)">- @foreach (var warehouse in item.AvailableWarehouses)- {- var selected = warehouse.WarehouseId == item.WarehouseId ? true : false;- <option value="@(warehouse.WarehouseId)" data-stock-qty="@(warehouse.StockQuantity)"- data-reserved-qty="@(warehouse.ReservedQuantity)"- selected="@selected">- @Loc["Admin.Orders.Shipments.Products.Warehouse.ChooseQty", warehouse.WarehouseName, warehouse.StockQuantity, warehouse.ReservedQuantity]- </option>- }- </select>- }- else- {- <div>@Loc["Admin.Orders.Shipments.Products.Warehouse.NotAvailabe"]</div>- }- }- else+ <div class="table-responsive visible-xs-block visible-sm-block visible-md-block visible-lg-block">+ <table class="table table-bordered table-striped table-hover">+ <thead>+ <tr>+ <th>+ @Loc["Admin.Orders.Shipments.Products.ProductName"]+ </th>+ <th>+ @Loc["Admin.Orders.Shipments.Products.SKU"]+ </th>+ <th>+ @Loc["Admin.Orders.Shipments.Products.Warehouse"]+ </th>+ <th>+ @Loc["Admin.Orders.Shipments.Products.ItemWeight"]+ </th>+ <th>+ @Loc["Admin.Orders.Shipments.Products.ItemDimensions"]+ </th>+ <th>+ @Loc["Admin.Orders.Shipments.Products.QtyOrdered"]+ </th>+ <th>+ @Loc["Admin.Orders.Shipments.Products.QtyShipped"]+ </th>+ <th>+ @Loc["Admin.Orders.Shipments.Products.QtyToShip"]+ </th>+ </tr>+ </thead>+ <tbody>+ @foreach (var item in Model.Items)+ {+ <tr>+ <td>+ <div style="padding-left:10px;padding-right:10px;">+ <em><a href="@Url.Action("Edit", "Product", new {id = item.ProductId, area = Constants.AreaAdmin})">@item.ProductName</a></em>+ @if (!String.IsNullOrEmpty(item.AttributeInfo))+ {+ <p>+ @Html.Raw(item.AttributeInfo)+ </p>+ }+ @if (item.ShipSeparately)+ {+ <p>+ <strong>@Loc["Admin.Orders.Shipments.Products.ShipSeparately.Warning"]</strong>+ @Loc["Admin.Orders.Shipments.Products.ShipSeparately"]+ </p>+ }+ </div>+ </td>+ <td>+ <div style="padding-left:10px;padding-right:10px;">+ @item.Sku+ </div>+ </td>+ <td>+ <div>+ @if (item.AllowToChooseWarehouse)+ {+ if (item.AvailableWarehouses.Count > 0)+ {+ <select name="warehouse_@(item.OrderItemId)" id="warehouse_@(item.OrderItemId)">+ @foreach (var warehouse in item.AvailableWarehouses){- //display warehouses- for (int i = 0; i < item.AvailableWarehouses.Count; i++)- {- var warehouse = item.AvailableWarehouses[i];- @warehouse.WarehouseName- if (i != item.AvailableWarehouses.Count - 1)- {- <br />- }- }+ var selected = warehouse.WarehouseId == item.WarehouseId ? true : false;+ <option value="@(warehouse.WarehouseId)" data-stock-qty="@(warehouse.StockQuantity)"+ data-reserved-qty="@(warehouse.ReservedQuantity)"+ selected="@selected">+ @Loc["Admin.Orders.Shipments.Products.Warehouse.ChooseQty", warehouse.WarehouseName, warehouse.StockQuantity, warehouse.ReservedQuantity]+ </option>}- </div>- </td>- <td>- @item.ItemWeight- </td>- <td>- @item.ItemDimensions- </td>- <td>- @item.QuantityOrdered- </td>- <td>- @item.QuantityInAllShipments- </td>- <td>- <input name="qtyToAdd@(item.OrderItemId)" type="number" value="@item.QuantityToAdd"- id="qtyToAdd@(item.OrderItemId)" class="form-control" style="width:100px;" max="@item.QuantityToAdd"/>- </td>- </tr>- }- </tbody>- </table>- </div>-+ </select>+ }+ else+ {+ <div>@Loc["Admin.Orders.Shipments.Products.Warehouse.NotAvailabe"]</div>+ }+ }+ else+ {+ //display warehouses+ for (int i = 0; i < item.AvailableWarehouses.Count; i++)+ {+ var warehouse = item.AvailableWarehouses[i];+ @warehouse.WarehouseName+ if (i != item.AvailableWarehouses.Count - 1)+ {+ <br />+ }+ }+ }+ </div>+ </td>+ <td>+ @item.ItemWeight+ </td>+ <td>+ @item.ItemDimensions+ </td>+ <td>+ @item.QuantityOrdered+ </td>+ <td>+ @item.QuantityInAllShipments+ </td>+ <td>+ <input name="qtyToAdd@(item.OrderItemId)" type="number" value="@item.QuantityToAdd"+ id="qtyToAdd@(item.OrderItemId)" class="form-control" style="width:100px;" max="@item.QuantityToAdd" />+ </td>+ </tr>+ }+ </tbody>+ </table>+ </div>+</form>diff --git a/src/Web/Grand.Web.Admin/Controllers/ShipmentController.cs b/src/Web/Grand.Web.Admin/Controllers/ShipmentController.csindex 29e2f34f7..d7c4afc85 100644--- a/src/Web/Grand.Web.Admin/Controllers/ShipmentController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/ShipmentController.cs@@ -223,7 +223,14 @@ namespace Grand.Web.Admin.Controllers}var sh = await _shipmentViewModelService.PrepareShipment(order, orderItems.ToList(), form);Shipment shipment = sh.shipment;- //if we have at least one item in the shipment, then save it+ //check stock+ var (valid, message) = await _shipmentViewModelService.ValidStockShipment(shipment);+ if (!valid)+ {+ Error(message);+ return RedirectToAction("AddShipment", new { orderId = orderId });+ }+ //if we have at least one item in the shipment, then save itif (shipment != null && shipment.ShipmentItems.Count > 0){shipment.TotalWeight = sh.totalWeight;diff --git a/src/Web/Grand.Web.Admin/Interfaces/IShipmentViewModelService.cs b/src/Web/Grand.Web.Admin/Interfaces/IShipmentViewModelService.csindex df9c5bdbb..49a071878 100644--- a/src/Web/Grand.Web.Admin/Interfaces/IShipmentViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Interfaces/IShipmentViewModelService.cs@@ -12,7 +12,6 @@ namespace Grand.Web.Admin.Interfaces{Task<ShipmentModel> PrepareShipmentModel(Shipment shipment, bool prepareProducts, bool prepareShipmentEvent = false);Task<int> GetStockQty(Product product, string warehouseId);- Task<int> GetPlannedQty(Product product, string warehouseId);Task<int> GetReservedQty(Product product, string warehouseId);Task LogShipment(string shipmentId, string message);Task<IList<ShipmentModel.ShipmentNote>> PrepareShipmentNotes(Shipment shipment);@@ -21,6 +20,7 @@ namespace Grand.Web.Admin.InterfacesTask<ShipmentListModel> PrepareShipmentListModel();Task<ShipmentModel> PrepareShipmentModel(Order order);Task<(Shipment shipment, decimal? totalWeight)> PrepareShipment(Order order, IList<OrderItem> orderItems, IFormCollection form);+ Task<(bool valid, string message)> ValidStockShipment(Shipment shipment);Task<(IEnumerable<Shipment> shipments, int totalCount)> PrepareShipments(ShipmentListModel model, int pageIndex, int pageSize);}}diff --git a/src/Web/Grand.Web.Admin/Services/ShipmentViewModelService.cs b/src/Web/Grand.Web.Admin/Services/ShipmentViewModelService.csindex 2f241419b..5fff0372e 100644--- a/src/Web/Grand.Web.Admin/Services/ShipmentViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/ShipmentViewModelService.cs@@ -9,7 +9,6 @@ using Grand.Business.Common.Interfaces.Localization;using Grand.Business.Common.Interfaces.Logging;using Grand.Business.Storage.Interfaces;using Grand.Domain.Catalog;-using Grand.Domain.Customers;using Grand.Domain.Directory;using Grand.Domain.Orders;using Grand.Domain.Shipping;@@ -42,6 +41,7 @@ namespace Grand.Web.Admin.Servicesprivate readonly ITranslationService _translationService;private readonly IDownloadService _downloadService;private readonly IShippingService _shippingService;+ private readonly IStockQuantityService _stockQuantityService;private readonly MeasureSettings _measureSettings;private readonly ShippingSettings _shippingSettings;private readonly ShippingProviderSettings _shippingProviderSettings;@@ -61,6 +61,7 @@ namespace Grand.Web.Admin.ServicesITranslationService translationService,IDownloadService downloadService,IShippingService shippingService,+ IStockQuantityService stockQuantityService,MeasureSettings measureSettings,ShippingSettings shippingSettings,ShippingProviderSettings shippingProviderSettings)@@ -79,6 +80,7 @@ namespace Grand.Web.Admin.Services_translationService = translationService;_downloadService = downloadService;_shippingService = shippingService;+ _stockQuantityService = stockQuantityService;_measureSettings = measureSettings;_shippingSettings = shippingSettings;_shippingProviderSettings = shippingProviderSettings;@@ -93,8 +95,7 @@ namespace Grand.Web.Admin.Servicesvar baseDimensionIn = baseDimension != null ? baseDimension.Name : "";var order = await _orderService.GetOrderById(shipment.OrderId);- var model = new ShipmentModel- {+ var model = new ShipmentModel {Id = shipment.Id,ShipmentNumber = shipment.ShipmentNumber,OrderId = shipment.OrderId,@@ -134,8 +135,7 @@ namespace Grand.Web.Admin.Servicesif (product != null){var warehouse = await _warehouseService.GetWarehouseById(shipmentItem.WarehouseId);- var shipmentItemModel = new ShipmentModel.ShipmentItemModel- {+ var shipmentItemModel = new ShipmentModel.ShipmentItemModel {Id = shipmentItem.Id,OrderItemId = orderItem.Id,ProductId = orderItem.ProductId,@@ -217,24 +217,6 @@ namespace Grand.Web.Admin.Servicesreturn _qty.Count > 0 ? _qty.Min() : 0;}- public virtual async Task<int> GetPlannedQty(Product product, string warehouseId)- {- List<int> _qty = new List<int>();- foreach (var item in product.BundleProducts)- {- var p1 = await _productService.GetProductById(item.ProductId);- if (p1.UseMultipleWarehouses)- {- var stock = p1.ProductWarehouseInventory.FirstOrDefault(x => x.WarehouseId == warehouseId);- if (stock != null)- {- _qty.Add((stock.StockQuantity - stock.ReservedQuantity) / item.Quantity);- }- }- }- return _qty.Count > 0 ? _qty.Min() : 0;- }-public virtual async Task<int> GetReservedQty(Product product, string warehouseId){List<int> _qty = new List<int>();@@ -261,8 +243,7 @@ namespace Grand.Web.Admin.Services.OrderByDescending(on => on.CreatedOnUtc)){var download = await _downloadService.GetDownloadById(shipmentNote.DownloadId);- shipmentNoteModels.Add(new ShipmentModel.ShipmentNote- {+ shipmentNoteModels.Add(new ShipmentModel.ShipmentNote {Id = shipmentNote.Id,ShipmentId = shipment.Id,DownloadId = String.IsNullOrEmpty(shipmentNote.DownloadId) ? "" : shipmentNote.DownloadId,@@ -278,8 +259,7 @@ namespace Grand.Web.Admin.Servicespublic virtual async Task InsertShipmentNote(Shipment shipment, string downloadId, bool displayToCustomer, string message){- var shipmentNote = new ShipmentNote- {+ var shipmentNote = new ShipmentNote {DisplayToCustomer = displayToCustomer,Note = message,DownloadId = downloadId,@@ -350,8 +330,7 @@ namespace Grand.Web.Admin.Services}public virtual async Task<ShipmentModel> PrepareShipmentModel(Order order){- var model = new ShipmentModel- {+ var model = new ShipmentModel {OrderId = order.Id,OrderNumber = order.OrderNumber};@@ -386,8 +365,7 @@ namespace Grand.Web.Admin.Servicesif (maxQtyToAdd <= 0)continue;- var shipmentItemModel = new ShipmentModel.ShipmentItemModel- {+ var shipmentItemModel = new ShipmentModel.ShipmentItemModel {OrderItemId = orderItem.Id,ProductId = orderItem.ProductId,ProductName = product.Name,@@ -416,8 +394,7 @@ namespace Grand.Web.Admin.Servicesvar warehouse = await _warehouseService.GetWarehouseById(pwi.WarehouseId);if (warehouse != null){- shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo- {+ shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo {WarehouseId = warehouse.Id,WarehouseName = warehouse.Name,StockQuantity = pwi.StockQuantity,@@ -432,8 +409,7 @@ namespace Grand.Web.Admin.Servicesvar warehouse = await _warehouseService.GetWarehouseById(product.WarehouseId);if (warehouse != null){- shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo- {+ shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo {WarehouseId = warehouse.Id,WarehouseName = warehouse.Name,StockQuantity = product.StockQuantity@@ -458,8 +434,7 @@ namespace Grand.Web.Admin.Servicesvar warehouse = await _warehouseService.GetWarehouseById(pwi.WarehouseId);if (warehouse != null){- shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo- {+ shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo {WarehouseId = warehouse.Id,WarehouseName = warehouse.Name,StockQuantity = pwi.StockQuantity,@@ -475,8 +450,7 @@ namespace Grand.Web.Admin.Servicesvar warehouse = await _warehouseService.GetWarehouseById(product.WarehouseId);if (warehouse != null){- shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo- {+ shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo {WarehouseId = warehouse.Id,WarehouseName = warehouse.Name,StockQuantity = product.StockQuantity@@ -492,8 +466,7 @@ namespace Grand.Web.Admin.Servicesvar warehouse = await _warehouseService.GetWarehouseById(product.WarehouseId);if (warehouse != null){- shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo- {+ shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo {WarehouseId = warehouse.Id,WarehouseName = warehouse.Name,StockQuantity = await GetStockQty(product, orderItem.WarehouseId),@@ -509,8 +482,7 @@ namespace Grand.Web.Admin.Servicesvar warehouses = await _warehouseService.GetAllWarehouses();foreach (var warehouse in warehouses){- shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo- {+ shipmentItemModel.AvailableWarehouses.Add(new ShipmentModel.ShipmentItemModel.WarehouseInfo {WarehouseId = warehouse.Id,WarehouseName = warehouse.Name,StockQuantity = await GetStockQty(product, warehouse.Id),@@ -526,6 +498,33 @@ namespace Grand.Web.Admin.Servicesreturn model;}+ public virtual async Task<(bool valid, string message)> ValidStockShipment(Shipment shipment)+ {+ foreach (var item in shipment.ShipmentItems)+ {+ var product = await _productService.GetProductById(item.ProductId);+ if (product.ManageInventoryMethodId == ManageInventoryMethod.ManageStock)+ {+ var stock = _stockQuantityService.GetTotalStockQuantity(product, useReservedQuantity: false, warehouseId: item.WarehouseId);+ if (stock - item.Quantity < 0)+ return (false, $"Out of stock for product {product.Name}");+ }+ if (product.ManageInventoryMethodId == ManageInventoryMethod.ManageStockByAttributes)+ {+ var combination = _productAttributeParser.FindProductAttributeCombination(product, item.Attributes);+ if (combination == null)+ return (false, $"Can't find combination for product {product.Name}");++ var stock = _stockQuantityService.GetTotalStockQuantityForCombination(product, combination, useReservedQuantity: false, warehouseId: item.WarehouseId);+ if (stock - item.Quantity < 0)+ return (false, $"Out of stock for product {product.Name}");+ }++ }++ return (true, string.Empty);+ }+public virtual async Task<(Shipment shipment, decimal? totalWeight)> PrepareShipment(Order order, IList<OrderItem> orderItems, IFormCollection form){Shipment shipment = null;@@ -593,8 +592,7 @@ namespace Grand.Web.Admin.Services{var trackingNumber = form["TrackingNumber"];var adminComment = form["AdminComment"];- shipment = new Shipment- {+ shipment = new Shipment {OrderId = order.Id,SeId = order.SeId,TrackingNumber = trackingNumber,@@ -611,8 +609,7 @@ namespace Grand.Web.Admin.Services}}//create a shipment item- var shipmentItem = new ShipmentItem- {+ var shipmentItem = new ShipmentItem {ProductId = orderItem.ProductId,OrderItemId = orderItem.Id,Quantity = qtyToAdd,