From 8bb3b3d19255bcb108ad658f14eb41d9133a2be6 Mon Sep 17 00:00:00 2001 From: DeepnaPooja Date: Tue, 22 Sep 2026 16:59:38 +0530 Subject: [PATCH] Update loading sheet filters and orders --- .../LoadingEvent/ILoadingSheetRepository.cs | 6 ++-- .../LoadingEvent/LoadingSheetController.cs | 4 ++- .../LoadingEvent/LoadingSheetRepository.cs | 33 ++++++++++++++++--- .../LoadingEvent/LoadingSheetService.cs | 15 +++++++-- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Distribution/Features/LoadingEvent/ILoadingSheetRepository.cs b/Distribution/Features/LoadingEvent/ILoadingSheetRepository.cs index 44c24bf..f13212c 100644 --- a/Distribution/Features/LoadingEvent/ILoadingSheetRepository.cs +++ b/Distribution/Features/LoadingEvent/ILoadingSheetRepository.cs @@ -6,10 +6,12 @@ public interface ILoadingSheetRepository { Task GetByIdAsync(Guid id); Task GetActiveByRideIdAsync(Guid rideId); - Task> GetAllAsync(Guid? rideId, LoadingSheetStatus? status, int page, int pageSize); - Task GetCountAsync(Guid? rideId, LoadingSheetStatus? status); + Task> GetAllAsync(Guid? rideId, LoadingSheetStatus? status, Guid? routeId, Guid? regionId, int page, int pageSize); + Task GetCountAsync(Guid? rideId, LoadingSheetStatus? status, Guid? routeId, Guid? regionId); Task AddAsync(LoadingSheet sheet); void Update(LoadingSheet sheet); void RemoveItems(IEnumerable items); Task AddItemsAsync(IEnumerable items); + Task> GetShopPOIdsByItemIdsAsync(IEnumerable shopPOItemIds); + Task ReplaceOrdersAsync(Guid loadingSheetId, List shopPOIds); } diff --git a/Distribution/Features/LoadingEvent/LoadingSheetController.cs b/Distribution/Features/LoadingEvent/LoadingSheetController.cs index 2c8ec3d..96fc079 100644 --- a/Distribution/Features/LoadingEvent/LoadingSheetController.cs +++ b/Distribution/Features/LoadingEvent/LoadingSheetController.cs @@ -12,10 +12,12 @@ public class LoadingSheetController(LoadingSheetService service, ICurrentUserSer public async Task GetAll( [FromQuery] Guid? rideId, [FromQuery] LoadingSheetStatus? status, + [FromQuery] Guid? routeId, + [FromQuery] Guid? regionId, [FromQuery] int page = 1, [FromQuery] int pageSize = 20) { - var result = await service.GetAllAsync(rideId, status, page, pageSize); + var result = await service.GetAllAsync(rideId, status, routeId, regionId, page, pageSize); return Ok(result); } diff --git a/Distribution/Features/LoadingEvent/LoadingSheetRepository.cs b/Distribution/Features/LoadingEvent/LoadingSheetRepository.cs index b4080e4..a8b8430 100644 --- a/Distribution/Features/LoadingEvent/LoadingSheetRepository.cs +++ b/Distribution/Features/LoadingEvent/LoadingSheetRepository.cs @@ -17,24 +17,28 @@ public class LoadingSheetRepository(AppDBContext context) : ILoadingSheetReposit await context.LoadingSheets .FirstOrDefaultAsync(s => s.FK_RideId == rideId && s.Status == LoadingSheetStatus.Planned); - public async Task> GetAllAsync(Guid? rideId, LoadingSheetStatus? status, int page, int pageSize) => + public async Task> GetAllAsync(Guid? rideId, LoadingSheetStatus? status, Guid? routeId, Guid? regionId, int page, int pageSize) => await context.LoadingSheets .Include(s => s.Items).ThenInclude(i => i.Item) .Include(s => s.Items).ThenInclude(i => i.Warehouse) .Include(s => s.Orders) .Where(s => (rideId == null || s.FK_RideId == rideId) && - (status == null || s.Status == status)) + (status == null || s.Status == status) && + (routeId == null || s.Orders.Any(o => o.ShopPO.Store.FK_RouteId == routeId)) && + (regionId == null || s.Orders.Any(o => o.ShopPO.Store.Route.FK_RegionId == regionId))) .OrderByDescending(s => s.CreatedAt) .Skip((page - 1) * pageSize) .Take(pageSize) .ToListAsync(); - public async Task GetCountAsync(Guid? rideId, LoadingSheetStatus? status) => + public async Task GetCountAsync(Guid? rideId, LoadingSheetStatus? status, Guid? routeId, Guid? regionId) => await context.LoadingSheets .CountAsync(s => (rideId == null || s.FK_RideId == rideId) && - (status == null || s.Status == status)); + (status == null || s.Status == status) && + (routeId == null || s.Orders.Any(o => o.ShopPO.Store.FK_RouteId == routeId)) && + (regionId == null || s.Orders.Any(o => o.ShopPO.Store.Route.FK_RegionId == regionId))); public async Task AddAsync(LoadingSheet sheet) => await context.LoadingSheets.AddAsync(sheet); @@ -47,4 +51,25 @@ public class LoadingSheetRepository(AppDBContext context) : ILoadingSheetReposit public async Task AddItemsAsync(IEnumerable items) => await context.LoadingSheetItems.AddRangeAsync(items); + + public async Task> GetShopPOIdsByItemIdsAsync(IEnumerable shopPOItemIds) => + await context.ShopPOItems + .Where(i => shopPOItemIds.Contains(i.Id)) + .Select(i => i.FK_ShopPOId) + .Distinct() + .ToListAsync(); + + public async Task ReplaceOrdersAsync(Guid loadingSheetId, List shopPOIds) + { + var current = await context.LoadingSheetOrders + .Where(o => o.FK_LoadingSheetId == loadingSheetId) + .ToListAsync(); + context.LoadingSheetOrders.RemoveRange(current); + + await context.LoadingSheetOrders.AddRangeAsync(shopPOIds.Select(shopPOId => new LoadingSheetOrder + { + FK_LoadingSheetId = loadingSheetId, + FK_ShopPOId = shopPOId + })); + } } diff --git a/Distribution/Features/LoadingEvent/LoadingSheetService.cs b/Distribution/Features/LoadingEvent/LoadingSheetService.cs index a37d1e1..07507cd 100644 --- a/Distribution/Features/LoadingEvent/LoadingSheetService.cs +++ b/Distribution/Features/LoadingEvent/LoadingSheetService.cs @@ -11,10 +11,10 @@ public class LoadingSheetService( IStockRepository stockRepo, IUnitOfWork uow) { - public async Task> GetAllAsync(Guid? rideId, LoadingSheetStatus? status, int page, int pageSize) + public async Task> GetAllAsync(Guid? rideId, LoadingSheetStatus? status, Guid? routeId, Guid? regionId, int page, int pageSize) { - var total = await repo.GetCountAsync(rideId, status); - var items = await repo.GetAllAsync(rideId, status, page, pageSize); + var total = await repo.GetCountAsync(rideId, status, routeId, regionId); + var items = await repo.GetAllAsync(rideId, status, routeId, regionId, page, pageSize); return new PagedResponse(page, pageSize, total, items.Select(ToResponse).ToList()); } @@ -64,6 +64,14 @@ public class LoadingSheetService( if (sheet.Status != LoadingSheetStatus.Planned) throw new InvalidOperationException("Items can only be changed while the sheet is Planned"); + var shopPOItemIds = request.Items + .Where(i => i.FK_ShopPOItemId.HasValue) + .Select(i => i.FK_ShopPOItemId!.Value) + .ToList(); + var shopPOIds = shopPOItemIds.Count > 0 + ? await repo.GetShopPOIdsByItemIdsAsync(shopPOItemIds) + : new List(); + await uow.BeginAsync(ct); try { @@ -76,6 +84,7 @@ public class LoadingSheetService( FK_ShopPOItemId = i.FK_ShopPOItemId, QuantityDispatched = i.Quantity })); + await repo.ReplaceOrdersAsync(sheet.Id, shopPOIds); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; }