using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using PSTW_CentralSystem.Areas.Inventory.Models; using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.Models; namespace PSTW_CentralSystem.Controllers.API.Reporting { [ApiController] [Route("[controller]")] [Authorize] public class ReportingAPI : Controller { private readonly ILogger _logger; private readonly CentralSystemContext _centralDbContext; private readonly UserManager _userManager; private readonly RoleManager _roleManager; public ReportingAPI(ILogger logger, CentralSystemContext centralDbContext, UserManager userManager, RoleManager roleManager) { _logger = logger; _centralDbContext = centralDbContext; _userManager = userManager; _roleManager = roleManager; } #region ItemReport [HttpPost("GetInventoryReport/{deptId}")] public async Task GetInventoryReport(int? deptId) { try { var user = await _userManager.GetUserAsync(User); List items = new List(); if (deptId == null || deptId == 0) { items = await _centralDbContext.Items .Include("CreatedBy") .Include("Department") .Include("Product") .ToListAsync(); } else { items = await _centralDbContext.Items .Include("CreatedBy") .Include("Department") .Include("Product") .Where(i => i.DepartmentId == deptId) .ToListAsync(); } var itemListWithDetails = items.Select(item => new { item.ItemID, item.UniqueID, item.CompanyId, item.DepartmentId, item.ProductId, item.SerialNumber, item.Quantity, item.Supplier, PurchaseDate = item.PurchaseDate.ToString("dd/MM/yyyy"), item.PONo, item.Currency, item.DefaultPrice, item.CurrencyRate, item.ConvertPrice, item.DODate, item.Warranty, EndWDate = item.EndWDate.ToString("dd/MM/yyyy"), InvoiceDate = item.InvoiceDate?.ToString("dd/MM/yyyy"), item.Department?.DepartmentName, CreatedBy = item.CreatedBy!.UserName, item.Product!.ProductName, item.Product!.Category, //CurrentUser = item.Movement?.FromUser?.UserName, CurrentUser = item.Movement?.FromUser?.UserName, CurrentStore = item.Movement?.FromStore?.StoreName, CurrentStation = item.Movement?.FromStation?.StationName, QRString = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.Value}/I/{item.UniqueID}" // Generate QR String }).ToList(); int itemCountRegistered = items.Count; int itemCountStillInStock = items.Where(i => i.Quantity > 0).Count(); var itemsMovementsThisMonth = _centralDbContext.ItemMovements .Where(i => i.Date.Month == DateTime.Now.Month); int itemCountRegisteredThisMonth = itemsMovementsThisMonth.Count(i => i.Action == "Register"); int itemCountStockOutThisMonth = itemsMovementsThisMonth.Count(i => i.Action == "Stock Out"); var lastMonth = DateTime.Now.AddMonths(-1).Month; var itemsMovementsLastMonth = _centralDbContext.ItemMovements .Where(i => i.Date.Month == lastMonth); int itemCountRegisteredLastMonth = itemsMovementsLastMonth.Count(i => i.Action == "Register"); int itemCountStockOutLastMonth = itemsMovementsLastMonth.Count(i => i.Action == "Stock Out"); var report = new { itemCountRegistered, itemCountStillInStock, itemCountRegisteredThisMonth, itemCountStockOutThisMonth, itemCountRegisteredLastMonth, itemCountStockOutLastMonth }; return Json(report); } catch (Exception ex) { return BadRequest(ex.Message); } } #endregion } }