@using Groceries.Data @using Microsoft.EntityFrameworkCore @inject IDbContextFactory DbContextFactory
@* Ensure form action/method are used for implicit submission instead of barcode button *@
Item
@foreach (var item in items.DistinctBy(item => item.Brand)) { @foreach (var item in items) {
@*@CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol*@
@{ var step = unit == null ? "1" : "0.001"; }
@ChildContent
@code { [Parameter, EditorRequired] public required TransactionItem TransactionItem { get; set; } [Parameter] public RenderFragment? ChildContent { get; set; } [Parameter(CaptureUnmatchedValues = true)] public Dictionary? AdditionalAttributes { get; set; } private record ItemModel(Guid Id, string Brand, string Name, decimal? Price, decimal? Quantity); private ItemBarcode? barcode; private ItemModel[] items = []; private ItemModel? selectedItem; private decimal? price; private decimal? quantity; private string? unit; protected override async Task OnParametersSetAsync() { barcode = TransactionItem.Item?.Barcodes.FirstOrDefault(); using var dbContext = DbContextFactory.CreateDbContext(); items = await dbContext.Items .OrderBy(item => item.Brand) .ThenBy(item => item.Name) .LeftJoin( dbContext.ItemPurchases.Where(purchase => purchase.IsLastPurchase), item => item.Id, lastPurchase => lastPurchase.ItemId, (item, lastPurchase) => new ItemModel( item.Id, item.Brand, item.Name, lastPurchase != null ? lastPurchase.Price : null, lastPurchase != null ? lastPurchase.Quantity : null)) .ToArrayAsync(); selectedItem = items.SingleOrDefault(item => item.Id == TransactionItem.ItemId); price = TransactionItem.Price >= 0 ? TransactionItem.Price : selectedItem?.Price; quantity = TransactionItem.Quantity >= 0 ? TransactionItem.Quantity : selectedItem?.Quantity; unit = TransactionItem.Unit; if (unit == null) { quantity ??= 1; } } }