bakery-erp / src /BakeryErp.Web /Pages /Inventory.cshtml.cs
Neil67's picture
Deploy Bakery ERP Space
13240d3
Raw
History Blame Contribute Delete
4.31 kB
using BakeryErp.Infrastructure.Database;
using BakeryErp.Infrastructure.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace BakeryErp.Web.Pages;
public sealed class InventoryModel : PageModel
{
private readonly InventoryRepository _inventoryRepository;
private readonly MasterDataLookupRepository _lookupRepository;
public InventoryModel(InventoryRepository inventoryRepository, MasterDataLookupRepository lookupRepository)
{
_inventoryRepository = inventoryRepository;
_lookupRepository = lookupRepository;
}
[BindProperty]
public LocalInventoryTransaction Form { get; set; } = new()
{
TransactionType = LocalInventoryTransactionType.GoodsReceipt,
Unit = "kg"
};
[BindProperty]
public string? OccurredDateText { get; set; }
[TempData]
public string? Message { get; set; }
[TempData]
public string? ErrorMessage { get; set; }
public IReadOnlyList<LocalStockBalance> StockBalances { get; private set; } = [];
public IReadOnlyList<LocalInventoryTransaction> Transactions { get; private set; } = [];
public IReadOnlyList<LookupOption> WarehouseOptions { get; private set; } = [];
public IReadOnlyList<ItemLookupOption> ItemOptions { get; private set; } = [];
public InventoryReport Report { get; private set; } = new(0, 0, 0, 0, 0);
public IReadOnlyList<LocalInventoryTransactionType> TransactionTypeOptions { get; } =
[
LocalInventoryTransactionType.GoodsReceipt,
LocalInventoryTransactionType.StockOut,
LocalInventoryTransactionType.StockAdjustment,
LocalInventoryTransactionType.Scrap,
LocalInventoryTransactionType.StoreReturn
];
public void OnGet(Guid? editId)
{
LoadPageData();
if (editId is null)
{
Form = new LocalInventoryTransaction
{
TransactionNumber = $"INV-{DateTime.Today:yyyyMMdd}-{Transactions.Count + 1:000}",
TransactionType = LocalInventoryTransactionType.GoodsReceipt,
OccurredAt = DateTime.UtcNow,
Unit = "kg"
};
OccurredDateText = DateTime.Today.ToString("yyyy-MM-dd");
return;
}
Form = _inventoryRepository.FindTransaction(editId.Value) ?? new LocalInventoryTransaction();
OccurredDateText = Form.OccurredAt.ToLocalTime().ToString("yyyy-MM-dd");
}
public IActionResult OnPostSave()
{
Form.OccurredAt = ParseOccurredAt();
var result = _inventoryRepository.SaveTransaction(Form);
if (!result.IsSuccess)
{
ErrorMessage = result.ErrorMessage;
return RedirectToPage();
}
Message = "庫存異動已儲存,等待審核入帳。";
return RedirectToPage();
}
public IActionResult OnPostApprove(Guid id)
{
var result = _inventoryRepository.Approve(id);
if (!result.IsSuccess)
{
ErrorMessage = result.ErrorMessage;
return RedirectToPage();
}
Message = "庫存異動已審核並更新即時庫存。";
return RedirectToPage();
}
public IActionResult OnPostCancel(Guid id)
{
_inventoryRepository.Cancel(id);
Message = "庫存異動已取消。";
return RedirectToPage();
}
public string GetStatusClass(LocalInventoryTransactionStatus status)
{
return status switch
{
LocalInventoryTransactionStatus.Approved => "ok",
LocalInventoryTransactionStatus.PendingApproval => "warn",
LocalInventoryTransactionStatus.Cancelled => "off",
_ => "info"
};
}
private DateTime ParseOccurredAt()
{
return DateOnly.TryParse(OccurredDateText, out var date)
? date.ToDateTime(TimeOnly.MinValue).ToUniversalTime()
: DateTime.UtcNow;
}
private void LoadPageData()
{
StockBalances = _inventoryRepository.ListBalances();
Transactions = _inventoryRepository.ListTransactions();
WarehouseOptions = _lookupRepository.Warehouses();
ItemOptions = _lookupRepository.Items();
Report = _inventoryRepository.GetReport();
}
}