This commit is contained in:
MOHD ARIFF 2026-02-03 11:14:57 +08:00
parent 0a9417aa8a
commit 74119f7222
2 changed files with 85 additions and 0 deletions

View File

@ -118,6 +118,7 @@
mounted() { mounted() {
this.fetchUser(); this.fetchUser();
this.fetchProductList(); this.fetchProductList();
this.fetchCurrentReport();
this.fetchDepartmentsCompaniesList(); this.fetchDepartmentsCompaniesList();
}, },
watch: { watch: {
@ -130,6 +131,24 @@
} }
}, },
methods: { 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() { async fetchUser() {
try { try {
const response = await fetch(`/IdentityAPI/GetUserInformation/`, { const response = await fetch(`/IdentityAPI/GetUserInformation/`, {

View File

@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using PSTW_CentralSystem.Areas.Inventory.Models; using PSTW_CentralSystem.Areas.Inventory.Models;
using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.DBContext;
using PSTW_CentralSystem.Models; using PSTW_CentralSystem.Models;
@ -26,6 +27,12 @@ namespace PSTW_CentralSystem.Controllers.API
_roleManager = roleManager; _roleManager = roleManager;
} }
public class ProductReport
{
public string? ProductName { get; set; } = default!;
public int Quantity { get; set; }
}
#region ItemReport #region ItemReport
[HttpPost("GetInventoryReport/{deptId}")] [HttpPost("GetInventoryReport/{deptId}")]
@ -118,6 +125,65 @@ namespace PSTW_CentralSystem.Controllers.API
#endregion #endregion
#region Monthly Report
[HttpPost("GetMonthlyReport")]
public async Task<IActionResult> GetMonthlyReport([FromBody] string FormDate = "2026/1/31", string DeptId = "PA")
{
try
{
var currentProductBalance = new List<ProductReport>();
var result = await _centralDbContext.Products
.Select(p => new { p.ProductName, p.QuantityJSON })
.Where(p => p.QuantityJSON != null)
.ToListAsync();
var quantityJsonDict = new Dictionary<string, Dictionary<string,int>>();
foreach (var item in result)
{
quantityJsonDict[item.ProductName] = JsonConvert.DeserializeObject<Dictionary<string, int>>(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 #region Report Inventory ii
[HttpPost("GetInventoryReport")] [HttpPost("GetInventoryReport")]
public async Task<IActionResult> GetInventoryReport([FromBody] ReportFilterDTO filter) public async Task<IActionResult> GetInventoryReport([FromBody] ReportFilterDTO filter)