diff --git a/Controllers/API/ReportingAPI.cs b/Controllers/API/ReportingAPI.cs index 9454e1a..6403493 100644 --- a/Controllers/API/ReportingAPI.cs +++ b/Controllers/API/ReportingAPI.cs @@ -139,76 +139,20 @@ namespace PSTW_CentralSystem.Controllers.API { string FormDate = reportQuery.FormDate!; string DeptId = reportQuery.DeptId!; - var currentProductBalance = new List(); - var result = await _centralDbContext.Products - .Include(p => p.Items) - .Select(p => new { p.ProductName, p.QuantityJSON, p.Items}) - .Where(p => p.QuantityJSON != null) - .ToListAsync(); - var quantityJsonDict = new Dictionary>(); - foreach (var item in result) - { - quantityJsonDict[item.ProductName] = JsonConvert.DeserializeObject>(item.QuantityJSON!)!; - if (!quantityJsonDict.TryGetValue(item.ProductName, out var deptDict)) - { - continue; - } - - if (deptDict.TryGetValue(DeptId, out var quantity)) - { - currentProductBalance.Add(new ProductReport - { - ProductName = item.ProductName, - Quantity = quantity, - TotalPrice = item.Items!.Sum(i => i.ConvertPrice * i.Quantity) - }); - } - } DateTime parsedDate = DateTime.Parse(FormDate); - - var thisMonthMovementIn = await _centralDbContext.ItemMovements - .Include(m => m.Item) - .Include(m => m.Item!.Product) - .Include(m => m.Item!.Department) - .Where(m => m.Date.Month == parsedDate.Month && m.Date.Year == parsedDate.Year && m.Item!.Department!.DepartmentCode == DeptId && (m.Action == "Stock In" || m.Action == "Register")) - .Select(m => new + var currentProductBalance = new List(); + var productData = await _centralDbContext.Products + .Where(p => p.QuantityJSON != null) + .Select(p => new { - ProductName = m.Item!.Product!.ProductName, - QuantityIn = m.Quantity??0, - PriceIn = m.Quantity * m.Item!.ConvertPrice + p.ProductName, + p.QuantityJSON, }) .ToListAsync(); - - var thisMonthMovementOut = await _centralDbContext.ItemMovements - .Include(m => m.Item) - .Include(m => m.Item!.Product) - .Include(m => m.Item!.Department) - .Where(m => m.Date.Month == parsedDate.Month && m.Date.Year == parsedDate.Year && m.Item!.Department!.DepartmentCode == DeptId && (m.Action == "Stock Out" || m.Action == "Assign")) - .Select(m => new - { - ProductName = m.Item!.Product!.ProductName, - QuantityOut = m.Quantity??0, - PriceOut = m.Quantity * m.Item!.ConvertPrice - }) - .ToListAsync(); - - //value from currentProductBalance - value from thisMonthMovementOut + value from thisMonthMovementIn - foreach (var item in currentProductBalance) + foreach (var p in productData) { - var movementIn = thisMonthMovementIn.FirstOrDefault(m => m.ProductName == item.ProductName); - if (movementIn != null && item.Quantity > 0) - { - 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!; - } + var deptDict = JsonConvert.DeserializeObject>(p.QuantityJSON!); } - var departmentUsers = await _centralDbContext.Users .Include(u => u.Department) .Where(u => u.Department!.DepartmentCode == DeptId) @@ -295,7 +239,7 @@ namespace PSTW_CentralSystem.Controllers.API .Select(u => new { Department = u.DepartmentName, - Items = latestStationItemMovements + Items = latestStoreItemMovements .Where(m => m != null && m.ToStore > 0 && m.NextStore!.StoreName.Contains(u.DepartmentName)) .Select(m => new { @@ -309,7 +253,7 @@ namespace PSTW_CentralSystem.Controllers.API UniqueID = m.Item.UniqueID, }) .ToList(), - TotalItemPrice = latestStationItemMovements.Where(m => m != null && m.ToStore > 0 && m.NextStore!.StoreName.Contains(u.DepartmentName)).Select(m => m!.Item!.ConvertPrice).Sum(), + TotalItemPrice = latestStoreItemMovements.Where(m => m != null && m.ToStore > 0 && m.NextStore!.StoreName.Contains(u.DepartmentName)).Select(m => m!.Item!.ConvertPrice).Sum(), }) .Where(u => u.Items.Count > 0).FirstOrDefault(); @@ -386,7 +330,7 @@ namespace PSTW_CentralSystem.Controllers.API // 2. Fetch Movements DURING the range (Required for Stock Issue logic) var movementsInRange = await _centralDbContext.ItemMovements - .Include(m => m.Item).ThenInclude(i => i.Product) + .Include(m => m.Item).ThenInclude(i => i!.Product) .Include(m => m.FromStation).Include(m => m.FromUser) .Include(m => m.NextStation).Include(m => m.NextUser) .Include(m => m.NextStore)