update the migrations
This commit is contained in:
@@ -21,7 +21,10 @@ public record LoadingSheetItemResponse(
|
||||
Guid FK_WarehouseId,
|
||||
string WarehouseName,
|
||||
decimal QuantityDispatched,
|
||||
Guid? FK_ShopPOItemId);
|
||||
Guid? FK_ShopPOItemId,
|
||||
Guid? ShopPOId = null,
|
||||
Guid? StoreId = null,
|
||||
string? StoreName = null);
|
||||
|
||||
public record LoadingSheetOrderResponse(Guid ShopPOId);
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ public class LoadingSheetRepository(AppDBContext context) : ILoadingSheetReposit
|
||||
await context.LoadingSheets
|
||||
.Include(s => s.Items).ThenInclude(i => i.Item)
|
||||
.Include(s => s.Items).ThenInclude(i => i.Warehouse)
|
||||
.Include(s => s.Items).ThenInclude(i => i.ShopPOItem).ThenInclude(p => p!.ShopPO).ThenInclude(p => p.Store)
|
||||
.Include(s => s.Orders)
|
||||
.FirstOrDefaultAsync(s => s.Id == id);
|
||||
|
||||
@@ -21,6 +22,7 @@ public class LoadingSheetRepository(AppDBContext context) : ILoadingSheetReposit
|
||||
await context.LoadingSheets
|
||||
.Include(s => s.Items).ThenInclude(i => i.Item)
|
||||
.Include(s => s.Items).ThenInclude(i => i.Warehouse)
|
||||
.Include(s => s.Items).ThenInclude(i => i.ShopPOItem).ThenInclude(p => p!.ShopPO).ThenInclude(p => p.Store)
|
||||
.Include(s => s.Orders)
|
||||
.Where(s =>
|
||||
(rideId == null || s.FK_RideId == rideId) &&
|
||||
|
||||
@@ -148,28 +148,39 @@ public class LoadingSheetService(
|
||||
|
||||
private async Task ApplyLoadMovementsAsync(Guid sheetId, Guid vehicleId, List<LoadingSheetItem> items, Guid userId)
|
||||
{
|
||||
// Vehicle balances created during this confirm are only tracked, not yet saved, so a
|
||||
// later row for the same item would not find them via the repository and would try to
|
||||
// insert a duplicate (item, vehicle) balance.
|
||||
var createdVehicleBalances = new Dictionary<Guid, StockBalance>();
|
||||
|
||||
foreach (var item in items.Where(i => i.QuantityDispatched > 0))
|
||||
{
|
||||
var warehouseBalance = await stockRepo.GetBalanceAsync(item.FK_ItemId, item.FK_WarehouseId)
|
||||
?? throw new InvalidOperationException($"No stock found in warehouse for item {item.FK_ItemId}");
|
||||
?? throw new InvalidOperationException(
|
||||
$"No stock found for '{item.Item?.Name ?? item.FK_ItemId.ToString()}' in warehouse '{item.Warehouse?.Name ?? item.FK_WarehouseId.ToString()}'");
|
||||
|
||||
if (warehouseBalance.Quantity < item.QuantityDispatched)
|
||||
throw new InvalidOperationException($"Insufficient warehouse stock for item {item.FK_ItemId}");
|
||||
throw new InvalidOperationException(
|
||||
$"Insufficient stock for '{item.Item?.Name ?? item.FK_ItemId.ToString()}' in warehouse '{item.Warehouse?.Name ?? item.FK_WarehouseId.ToString()}' " +
|
||||
$"(available {warehouseBalance.Quantity}, needed {item.QuantityDispatched})");
|
||||
|
||||
warehouseBalance.Quantity -= item.QuantityDispatched;
|
||||
warehouseBalance.UpdatedAt = DateTime.Now;
|
||||
stockRepo.UpdateBalance(warehouseBalance);
|
||||
|
||||
var vehicleBalance = await stockRepo.GetBalanceAsync(item.FK_ItemId, vehicleId);
|
||||
var vehicleBalance = createdVehicleBalances.GetValueOrDefault(item.FK_ItemId)
|
||||
?? await stockRepo.GetBalanceAsync(item.FK_ItemId, vehicleId);
|
||||
if (vehicleBalance is null)
|
||||
{
|
||||
await stockRepo.AddBalanceAsync(new StockBalance
|
||||
var created = new StockBalance
|
||||
{
|
||||
FK_ItemId = item.FK_ItemId,
|
||||
LocationId = vehicleId,
|
||||
LocationType = LocationType.Vehicle,
|
||||
Quantity = item.QuantityDispatched
|
||||
});
|
||||
};
|
||||
createdVehicleBalances[item.FK_ItemId] = created;
|
||||
await stockRepo.AddBalanceAsync(created);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -199,6 +210,9 @@ public class LoadingSheetService(
|
||||
s.Items.Select(i => new LoadingSheetItemResponse(
|
||||
i.Id, i.FK_ItemId, i.Item?.Name ?? string.Empty,
|
||||
i.FK_WarehouseId, i.Warehouse?.Name ?? string.Empty,
|
||||
i.QuantityDispatched, i.FK_ShopPOItemId)).ToList(),
|
||||
i.QuantityDispatched, i.FK_ShopPOItemId,
|
||||
i.ShopPOItem?.FK_ShopPOId,
|
||||
i.ShopPOItem?.ShopPO?.FK_StoreId,
|
||||
i.ShopPOItem?.ShopPO?.Store?.StoreName)).ToList(),
|
||||
s.Orders.Select(o => new LoadingSheetOrderResponse(o.FK_ShopPOId)).ToList());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user