From 8276201e838dc4d72e62cec234bdb6712ab72721 Mon Sep 17 00:00:00 2001 From: Mohd Ariff Date: Tue, 10 Mar 2026 20:44:44 +0800 Subject: [PATCH] Update --- Controllers/API/ReportingAPI.cs | 65 +++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/Controllers/API/ReportingAPI.cs b/Controllers/API/ReportingAPI.cs index 2f0d1a2..e81f5da 100644 --- a/Controllers/API/ReportingAPI.cs +++ b/Controllers/API/ReportingAPI.cs @@ -204,14 +204,14 @@ namespace PSTW_CentralSystem.Controllers.API var movementIn = thisMonthMovementIn.FirstOrDefault(m => m.ProductName == item.ProductName); if (movementIn != null && item.Quantity > 0) { - item.Quantity += movementIn.QuantityIn; - item.TotalPrice += (float)movementIn.PriceIn!; + item.Quantity -= movementIn.QuantityIn; + item.TotalPrice -= (float)movementIn.PriceIn!; } var movementOut = thisMonthMovementOut.FirstOrDefault(m => m.ProductName == item.ProductName); if (movementOut != null && item.Quantity > 0) { - item.Quantity -= movementOut.QuantityOut; - item.TotalPrice -= (float)movementOut.PriceOut!; + item.Quantity += movementOut.QuantityOut; + item.TotalPrice += (float)movementOut.PriceOut!; } } @@ -220,29 +220,46 @@ namespace PSTW_CentralSystem.Controllers.API .Where(u => u.Department!.DepartmentCode == DeptId) .ToListAsync(); + var departmentStations = await _centralDbContext.Stations + .Include(s => s.StationPic) + .Include(s => s.Department) + .Where(s => s.Department!.DepartmentCode == DeptId) + .ToListAsync(); + + var departmentStores = await _centralDbContext.Stores + .Include(s => s.Company) + .Where(s => s.Department!.DepartmentCode == DeptId) + .ToListAsync(); + var latestItemMovements = await _centralDbContext.ItemMovements .Include(m => m.Item) .Include(m => m.Item!.Product) .Include(m => m.NextUser) - .Where(m => m.ItemId != null && (ToUser > 0 ? m.ToUser != null : m.ToUser > 0) && (ToStation > 0 ? m.ToStation != null : m.ToStation > 0) && (ToStore > 0 ? m.ToStore != null : m.ToStore > 0)) + .Include(m => m.NextStation) + .Include(m => m.NextStore) .GroupBy(m => m.ItemId) .Select(g => g - .OrderByDescending(m => m.Date) - .ThenByDescending(m => m.Id) + .Where(m => m.Date <= parsedDate) + .OrderByDescending(m => m.Id) .FirstOrDefault()) .ToListAsync(); + //select latestItemMovements with the Touser is not null + var latestUserItemMovements = latestItemMovements.Where(m => m != null && m.ItemId != null && m.ToUser > 0 ).ToList(); + var latestStationItemMovements = latestItemMovements.Where(m => m != null && m.ItemId != null && m.ToStation > 0).ToList(); + var latestStoreItemMovements = latestItemMovements.Where(m => m != null && m.ItemId != null && m.ToStore > 0).ToList(); + var usersItemMovements = departmentUsers .Select(u => new { UserId = u.Id, UserName = u.UserName, UserFullName = u.FullName, - Items = latestItemMovements - .Where(m => m.ToUser == u.Id) + Items = latestUserItemMovements + .Where(m => m != null && m.ToUser == u.Id) .Select(m => new { - ItemId = m.ItemId, + ItemId = m!.ItemId, ItemName = m.Item!.Product!.ProductName, Quantity = m.Quantity, ItemPrice = m.Item!.ConvertPrice, @@ -252,14 +269,38 @@ namespace PSTW_CentralSystem.Controllers.API UniqueID = m.Item.UniqueID, }) .ToList(), - TotalItemPrice = latestItemMovements.Where(m => m.ToUser == u.Id).Sum(m => m!.Item!.ConvertPrice * m.Quantity), + TotalItemPrice = latestItemMovements.Where(m => m != null && m.ToUser == u.Id).Sum(m => m!.Item!.ConvertPrice * m.Quantity), + }) + .Where(u => u.Items.Count > 0).ToList(); + + var stationItemMovements = departmentStations + .Select(u => new + { + StationName = u.StationName, + StationPic = u.StationPic?.FullName, + Items = latestStationItemMovements + .Where(m => m != null && m.ToStation == u.StationId) + .Select(m => new + { + ItemId = m!.ItemId, + ItemName = m.Item!.Product!.ProductName, + Quantity = m.Quantity, + ItemPrice = m.Item!.ConvertPrice, + PO = m.Item!.PONo, + DO = m.Item!.DONo, + SerialNumber = m.Item.SerialNumber, + UniqueID = m.Item.UniqueID, + }) + .ToList(), + TotalItemPrice = latestItemMovements.Where(m => m != null && m.ToStation == u.StationId).Sum(m => m!.Item!.ConvertPrice * m.Quantity), }) .Where(u => u.Items.Count > 0).ToList(); var report = new { allProductInStore = currentProductBalance, - userItemBalance = usersItemMovements + userItemBalance = usersItemMovements, + stationItemBalance = stationItemMovements, }; return Json(report);