From 74119f72223091394d165aca92a6f77315518984 Mon Sep 17 00:00:00 2001 From: Mohd Ariff Date: Tue, 3 Feb 2026 11:14:57 +0800 Subject: [PATCH] Update --- .../Views/Reporting/InventoryReport.cshtml | 19 ++++++ Controllers/API/ReportingAPI.cs | 66 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/Areas/Report/Views/Reporting/InventoryReport.cshtml b/Areas/Report/Views/Reporting/InventoryReport.cshtml index 2705493..040efdd 100644 --- a/Areas/Report/Views/Reporting/InventoryReport.cshtml +++ b/Areas/Report/Views/Reporting/InventoryReport.cshtml @@ -118,6 +118,7 @@ mounted() { this.fetchUser(); this.fetchProductList(); + this.fetchCurrentReport(); this.fetchDepartmentsCompaniesList(); }, watch: { @@ -130,6 +131,24 @@ } }, methods: { + async fetchCurrentReport(){ + try { + const response = await fetch(`/ReportingAPI/GetMonthlyReport/`, { + method: 'POST', + }); + if (response.ok) { + const data = await response.json(); + console.log(data); + } + else { + console.error(`Failed to fetch user: ${response.statusText}`); + } + } + catch (error) { + console.error('There was a problem with the fetch operation:', error); + } + }, + async fetchUser() { try { const response = await fetch(`/IdentityAPI/GetUserInformation/`, { diff --git a/Controllers/API/ReportingAPI.cs b/Controllers/API/ReportingAPI.cs index 1f70369..403d3d5 100644 --- a/Controllers/API/ReportingAPI.cs +++ b/Controllers/API/ReportingAPI.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; using PSTW_CentralSystem.Areas.Inventory.Models; using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.Models; @@ -26,6 +27,12 @@ namespace PSTW_CentralSystem.Controllers.API _roleManager = roleManager; } + public class ProductReport + { + public string? ProductName { get; set; } = default!; + public int Quantity { get; set; } + } + #region ItemReport [HttpPost("GetInventoryReport/{deptId}")] @@ -118,6 +125,65 @@ namespace PSTW_CentralSystem.Controllers.API #endregion + #region Monthly Report + [HttpPost("GetMonthlyReport")] + public async Task GetMonthlyReport([FromBody] string FormDate = "2026/1/31", string DeptId = "PA") + { + try + { + var currentProductBalance = new List(); + var result = await _centralDbContext.Products + .Select(p => new { p.ProductName, p.QuantityJSON }) + .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 + }); + } + } + DateTime parsedDate = DateTime.Parse(FormDate); + var thisMonthMovements = 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, + }) + .ToListAsync(); + //value from currentProductBalance - value from thisMonthMovements + foreach (var item in currentProductBalance) + { + var movement = thisMonthMovements.FirstOrDefault(m => m.ProductName == item.ProductName); + if (movement != null && item.Quantity > 0) + { + item.Quantity -= movement.QuantityOut; + } + } + return Json(currentProductBalance); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + #endregion + #region Report Inventory ii [HttpPost("GetInventoryReport")] public async Task GetInventoryReport([FromBody] ReportFilterDTO filter)