Check stock availability when creating an order
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using Distribution.Features.Item;
|
using Distribution.Features.Item;
|
||||||
using Distribution.Features.PriceList;
|
using Distribution.Features.PriceList;
|
||||||
using Distribution.Features.Store;
|
using Distribution.Features.Store;
|
||||||
|
using Distribution.Features.Stock;
|
||||||
using Distribution.Infra.Authorization;
|
using Distribution.Infra.Authorization;
|
||||||
using Distribution.Infra.UoW;
|
using Distribution.Infra.UoW;
|
||||||
using Distribution.Models;
|
using Distribution.Models;
|
||||||
@@ -12,6 +13,7 @@ public class OrderService(
|
|||||||
IOrderRepository repo,
|
IOrderRepository repo,
|
||||||
IStoreRepository storeRepo,
|
IStoreRepository storeRepo,
|
||||||
IItemRepository itemRepo,
|
IItemRepository itemRepo,
|
||||||
|
IStockRepository stockRepo,
|
||||||
PriceListService priceListService,
|
PriceListService priceListService,
|
||||||
IUnitOfWork uow,
|
IUnitOfWork uow,
|
||||||
ICurrentUserService currentUser,
|
ICurrentUserService currentUser,
|
||||||
@@ -56,6 +58,19 @@ public class OrderService(
|
|||||||
return requestedUnitPrice.Value;
|
return requestedUnitPrice.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// checkStockAvailability (mainPlan.md §1): confirms the requested quantity does not exceed
|
||||||
|
/// total on-hand stock for the item, summed across every warehouse/vehicle location. Does not
|
||||||
|
/// reserve or decrement stock — actual decrement happens later at loading/delivery time
|
||||||
|
/// (04_loading_event.md / future 05-06 ride execution), not at order placement.
|
||||||
|
/// </summary>
|
||||||
|
private async Task<bool> HasSufficientStockAsync(Guid itemId, decimal quantity)
|
||||||
|
{
|
||||||
|
var balances = await stockRepo.GetBalancesByItemAsync(itemId);
|
||||||
|
var totalAvailable = balances.Sum(b => b.Quantity);
|
||||||
|
return quantity <= totalAvailable;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<List<OrderResponse>> GetAllAsync()
|
public async Task<List<OrderResponse>> GetAllAsync()
|
||||||
{
|
{
|
||||||
var orders = (await repo.GetAllAsync()).Where(o => o.IsActive).ToList();
|
var orders = (await repo.GetAllAsync()).Where(o => o.IsActive).ToList();
|
||||||
@@ -116,6 +131,8 @@ public class OrderService(
|
|||||||
var orderItems = new List<Models.OrderItem>();
|
var orderItems = new List<Models.OrderItem>();
|
||||||
foreach (var i in request.Items)
|
foreach (var i in request.Items)
|
||||||
{
|
{
|
||||||
|
if (!await HasSufficientStockAsync(i.ItemId, i.Quantity)) return null;
|
||||||
|
|
||||||
var unitPrice = await ResolveUnitPriceAsync(i.ItemId, i.UnitPrice, store, request.OrderDate);
|
var unitPrice = await ResolveUnitPriceAsync(i.ItemId, i.UnitPrice, store, request.OrderDate);
|
||||||
if (unitPrice is null) return null;
|
if (unitPrice is null) return null;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user