-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Working on reworking Inventory (moderately messy) * Adding method to fake interface * Adding InventoryPart to Price Variant Product * Adapting inventory management methods to dictionary inventories * Adapting product availability displays to dictionary inventory * Hiding unavailable Price Variant Products items * Handling item quantity verification * Using new method * Renaming property * Handling out of stock message * Adding editor for inventory values * Updating inventory keys upon SKU change * Updating Product's inventory to its SKU from "DEFAULT" * Code styling adjustments * Also updating CanBeBought entry keys * WIP something * Working on reworking inventory keys updating * Trying to resolve bug with dictionaries * Fixing user-facing variant availability display * Same * Removing * Adding StringComparer to view model's property * Finding workaround for giga dictionaries bug * Removing unnecessary code * Initializing new inventory when creating a new Product item * Eliminating small bug with workaround * Completing workaround * Applying filtering in other driver as well * Initializing new inventories in drivers * Removing unnecessary placement * Reverting * Fixing out of stock message display conditions * Fixing submit button clickability * Donating brain cell to spell-checking * Adding minor improvements * Adding inventory filtering extension method
- Loading branch information
Showing
29 changed files
with
445 additions
and
81 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/Modules/OrchardCore.Commerce.Inventory/Drivers/InventoryPartDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using OrchardCore.Commerce.Inventory.Models; | ||
using OrchardCore.Commerce.Inventory.ViewModels; | ||
using OrchardCore.ContentManagement.Display.ContentDisplay; | ||
using OrchardCore.ContentManagement.Display.Models; | ||
using OrchardCore.DisplayManagement.ModelBinding; | ||
using OrchardCore.DisplayManagement.Views; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace OrchardCore.Commerce.Inventory.Drivers; | ||
|
||
public class InventoryPartDisplayDriver : ContentPartDisplayDriver<InventoryPart> | ||
{ | ||
private readonly IHttpContextAccessor _hca; | ||
|
||
public InventoryPartDisplayDriver(IHttpContextAccessor hca) => _hca = hca; | ||
|
||
public override IDisplayResult Display(InventoryPart part, BuildPartDisplayContext context) => | ||
Initialize<InventoryPartViewModel>(GetDisplayShapeType(context), viewModel => BuildViewModel(viewModel, part)) | ||
.Location("Detail", "Content:26") | ||
.Location("Summary", "Meta"); | ||
|
||
public override IDisplayResult Edit(InventoryPart part, BuildPartEditorContext context) => | ||
Initialize<InventoryPartViewModel>(GetEditorShapeType(context), viewModel => BuildViewModel(viewModel, part)); | ||
|
||
public override async Task<IDisplayResult> UpdateAsync( | ||
InventoryPart part, | ||
IUpdateModel updater, | ||
UpdatePartEditorContext context) | ||
{ | ||
var viewModel = new InventoryPartViewModel(); | ||
if (await updater.TryUpdateModelAsync(viewModel, Prefix)) | ||
{ | ||
var currentSku = _hca.HttpContext.Request.Form["ProductPart.Sku"].ToString().ToUpperInvariant(); | ||
var skuBefore = viewModel.Inventory.FirstOrDefault().Key != null | ||
? viewModel.Inventory.FirstOrDefault().Key.Split("-").First() | ||
: "DEFAULT"; | ||
|
||
part.Inventory.Clear(); | ||
part.Inventory.AddRange(viewModel.Inventory); | ||
|
||
// If SKU was changed, inventory keys need to be updated. | ||
if (!string.IsNullOrEmpty(currentSku) && currentSku != skuBefore) | ||
{ | ||
part.InventoryKeys.Clear(); | ||
|
||
var newInventory = new Dictionary<string, int>(); | ||
var oldInventory = part.Inventory.ToDictionary(key => key.Key, value => value.Value); | ||
foreach (var inventoryEntry in oldInventory) | ||
{ | ||
var updatedKey = oldInventory.Count > 1 | ||
? currentSku + "-" + inventoryEntry.Key.Split('-').Last() | ||
: currentSku; | ||
|
||
part.Inventory.Remove(inventoryEntry.Key); | ||
newInventory.Add(updatedKey, inventoryEntry.Value); | ||
|
||
part.InventoryKeys.Add(updatedKey); | ||
} | ||
|
||
part.Inventory.Clear(); | ||
part.Inventory.AddRange(newInventory); | ||
} | ||
|
||
part.ProductSku = currentSku; | ||
} | ||
|
||
return await EditAsync(part, context); | ||
} | ||
|
||
// Despite the Clear() calls inside UpdateAsync(), the Inventory property retains its old values along with the | ||
// new ones, hence the filtering below. | ||
private static void BuildViewModel(InventoryPartViewModel model, InventoryPart part) | ||
{ | ||
var inventory = part.Inventory ?? new Dictionary<string, int>(); | ||
if (inventory.Any()) | ||
{ | ||
// Workaround for InventoryPart storing the outdated inventory entries along with the updated ones. | ||
var filteredInventory = part.Inventory.FilterOutdatedEntries(part.InventoryKeys); | ||
|
||
model.Inventory.AddRange(filteredInventory); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Modules/OrchardCore.Commerce.Inventory/Extensions/InventoryDictionaryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace OrchardCore.Commerce.Inventory; | ||
|
||
public static class InventoryDictionaryExtensions | ||
{ | ||
public static IDictionary<string, int> FilterOutdatedEntries( | ||
this IDictionary<string, int> inventory, IList<string> inventoryKeys) => | ||
inventory | ||
.Where(inventory => inventoryKeys.Contains(inventory.Key)) | ||
.ToDictionary(key => key.Key, value => value.Value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 9 additions & 1 deletion
10
src/Modules/OrchardCore.Commerce.Inventory/Models/InventoryPart.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
using OrchardCore.ContentFields.Fields; | ||
using OrchardCore.ContentManagement; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace OrchardCore.Commerce.Inventory.Models; | ||
|
||
public class InventoryPart : ContentPart | ||
{ | ||
public BooleanField AllowsBackOrder { get; set; } = new(); | ||
public BooleanField IgnoreInventory { get; set; } = new(); | ||
public NumericField Inventory { get; set; } = new(); | ||
|
||
public IDictionary<string, int> Inventory { get; } = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); | ||
|
||
public NumericField MaximumOrderQuantity { get; set; } = new(); | ||
public NumericField MinimumOrderQuantity { get; set; } = new(); | ||
public HtmlField OutOfStockMessage { get; set; } = new(); | ||
|
||
public IList<string> InventoryKeys { get; } = new List<string>(); | ||
|
||
public string ProductSku { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Modules/OrchardCore.Commerce.Inventory/ViewModels/InventoryPartViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace OrchardCore.Commerce.Inventory.ViewModels; | ||
|
||
public class InventoryPartViewModel | ||
{ | ||
public bool AllowsBackOrder { get; set; } | ||
public bool IgnoreInventory { get; set; } | ||
|
||
public IDictionary<string, int> Inventory { get; } = new Dictionary<string, int>(); | ||
|
||
public int MaximumOrderQuantity { get; set; } | ||
public int MinimumOrderQuantity { get; set; } | ||
public string OutOfStockMessage { get; set; } | ||
} |
11 changes: 0 additions & 11 deletions
11
src/Modules/OrchardCore.Commerce.Inventory/Views/InventoryPart-Inventory.cshtml
This file was deleted.
Oops, something went wrong.
8 changes: 7 additions & 1 deletion
8
src/Modules/OrchardCore.Commerce.Inventory/Views/InventoryPart-OutOfStockMessage.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Modules/OrchardCore.Commerce.Inventory/Views/InventoryPart.Edit.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@model InventoryPartViewModel | ||
|
||
<h3>@T["Inventories"]</h3> | ||
<div class="row"> | ||
@foreach (var inventoryKey in Model.Inventory.Keys) | ||
{ | ||
var inventoryTitle = Model.Inventory.Count > 1 ? inventoryKey.Split('-').Last() : string.Empty; | ||
|
||
<div class="col-md-3"> | ||
<div class="mb-3" asp-validation-class-for="Inventory[inventoryKey]"> | ||
<label asp-for="Inventory[inventoryKey]">@inventoryTitle @T["Inventory"] </label> | ||
<div class="input-group"> | ||
<input asp-for="Inventory[inventoryKey]" class="form-control text-muted" /> | ||
</div> | ||
<span asp-validation-for="Inventory[inventoryKey]"></span> | ||
</div> | ||
</div> | ||
} | ||
</div> |
16 changes: 16 additions & 0 deletions
16
src/Modules/OrchardCore.Commerce.Inventory/Views/InventoryPart.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@model InventoryPartViewModel | ||
|
||
@if (Model.Inventory is { } inventory) | ||
{ | ||
<div class="pb-3 field field-type-numericfield field-name-inventory-part-inventory"> | ||
@foreach (var inventoryEntry in inventory) | ||
{ | ||
var inventoryTitle = inventory.Count > 1 ? inventoryEntry.Key.Split('-').Last() : string.Empty; | ||
|
||
<span class="d-block w-100"> | ||
<strong class="field-name-inventory-part-inventory-title">@inventoryTitle @T["Inventory"]:</strong> | ||
@T["{0}", inventoryEntry.Value] | ||
</span> | ||
} | ||
</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.