Add loading sheet edit flow
This commit is contained in:
@@ -64,15 +64,14 @@ public class LoadingSheetService(
|
||||
if (sheet.Status != LoadingSheetStatus.Planned)
|
||||
throw new InvalidOperationException("Items can only be changed while the sheet is Planned");
|
||||
|
||||
// Upsert by (item, warehouse, shop PO item): a matching row gets its quantity
|
||||
// updated in place, a new combination is added. Rows for combinations not present
|
||||
// in this request are left untouched, so a later save can top up the same PO
|
||||
// item's remaining quantity from a different warehouse without losing what was
|
||||
// already planned from the first one.
|
||||
// The request is the full desired item list: a row matching (item, warehouse,
|
||||
// shop PO item) gets its quantity updated in place, a new combination is added,
|
||||
// and rows for combinations not present in the request are removed.
|
||||
await uow.BeginAsync(ct);
|
||||
try
|
||||
{
|
||||
var newItems = new List<LoadingSheetItem>();
|
||||
var keptItems = new List<LoadingSheetItem>();
|
||||
foreach (var input in request.Items)
|
||||
{
|
||||
var existing = sheet.Items.FirstOrDefault(i =>
|
||||
@@ -81,7 +80,10 @@ public class LoadingSheetService(
|
||||
i.FK_ShopPOItemId == input.FK_ShopPOItemId);
|
||||
|
||||
if (existing is not null)
|
||||
{
|
||||
existing.QuantityDispatched = input.Quantity;
|
||||
keptItems.Add(existing);
|
||||
}
|
||||
else
|
||||
newItems.Add(new LoadingSheetItem
|
||||
{
|
||||
@@ -92,10 +94,14 @@ public class LoadingSheetService(
|
||||
QuantityDispatched = input.Quantity
|
||||
});
|
||||
}
|
||||
|
||||
var removedItems = sheet.Items.Where(i => !keptItems.Contains(i)).ToList();
|
||||
if (removedItems.Count > 0)
|
||||
repo.RemoveItems(removedItems);
|
||||
if (newItems.Count > 0)
|
||||
await repo.AddItemsAsync(newItems);
|
||||
|
||||
var shopPOItemIds = sheet.Items.Select(i => i.FK_ShopPOItemId)
|
||||
var shopPOItemIds = keptItems.Select(i => i.FK_ShopPOItemId)
|
||||
.Concat(newItems.Select(i => i.FK_ShopPOItemId))
|
||||
.Where(x => x.HasValue)
|
||||
.Select(x => x!.Value)
|
||||
|
||||
Reference in New Issue
Block a user