diff --git a/Areas/Report/Views/Reporting/InventoryReport.cshtml b/Areas/Report/Views/Reporting/InventoryReport.cshtml index e72d6d9..e525b2b 100644 --- a/Areas/Report/Views/Reporting/InventoryReport.cshtml +++ b/Areas/Report/Views/Reporting/InventoryReport.cshtml @@ -66,67 +66,82 @@

Report Inquiry

- -
-
-
-
-
-

Department

- - -
+
+
+
+
+
+

Department

+ + +
+
-
-
-
-

Date filter

- -
+
+
+

Date filter

+ +
+
-
-
-
-

Position Filter

- -
+
+
-
- -
-
-
- -
-
-
-
-
-
-

User List

- +
+ +
+
+
+
+
+
+

User List

+ +
+
+
+
-
-
+
+
+

Store

+
+
+
+
-
-
-
-

Store

+
+
+

Store

+
+
+
+
-
-
+
+
+

Store

+
+
+
+
@@ -173,13 +188,14 @@ selectedSingleDepartment: [], selectedItem: [], selectedCategory: [], - selectedPosition: [], usersInfo: [], submitBody: null, formResponse: null, selectedUser: [], userItemsTable: null, - itemsTable: null, + stationItemsTable: null, + storeItemsTable: null, + storeAllItemsTable: null, activeTab: "userTab" } }, @@ -202,6 +218,7 @@ } // if (newVal && newVal.items) { const tableData = newVal?.items ? newVal.items : []; + var app = this; this.$nextTick(() => { this.userItemsTable = $('#userItemsTable').DataTable({ data: tableData , @@ -223,13 +240,18 @@ ` ); }, + "createdRow": function (row, data, dataIndex) { + $(row).on('click', function () { + app.fetchItemMovement(data.uniqueID); + }); + } }); }); } }, methods: { async generateReport(){ - if(this.selectedMonth == null || this.selectedSingleDepartment.length == 0 || this.selectedPosition.length == 0) + if(this.selectedMonth == null || this.selectedSingleDepartment.length == 0) { var msg = []; if(this.selectedMonth == null) @@ -240,10 +262,6 @@ { msg.push("Department"); } - if(this.selectedPosition.length == 0) - { - msg.push("Position"); - } alert(msg.length > 1 ? msg.join(" & ") + " cannot be empty" : msg[0] + " cannot be empty"); } else{ @@ -251,9 +269,6 @@ formDate: new Date(this.selectedMonth.year, this.selectedMonth.month + 1, 0, 23, 59, 59).toISOString().slice(0, 19), // selectedCategory: this.selectedCategory, deptId: this.selectedSingleDepartment.departmentCode, - toUser: this.selectedPosition.positionId == 1 ? 1 : 0, - toStation: this.selectedPosition.positionId == 2 ? 1 : 0, - toStore: this.selectedPosition.positionId == 3 ? 1 : 0, } this.fetchCurrentReport(); } @@ -276,11 +291,14 @@ this.userItemsTable = null; this.selectedUser = null; } - if (this.itemsTable) { - this.itemsTable.clear().destroy(); - this.itemsTable = null; + if (this.stationItemsTable) { + this.stationItemsTable.clear().destroy(); + this.stationItemsTable = null; + } + if (this.storeItemsTable) { + this.storeItemsTable.clear().destroy(); + this.storeItemsTable = null; } - console.log(data); } else { console.error(`Failed to fetch user: ${response.statusText}`); @@ -397,10 +415,54 @@ } }, initTable() { - this.userItemsTable = $('#userItemsTable').DataTable(); this.$nextTick(() => { - this.itemsTable = $('#itemsTable').DataTable({ - data: this.formResponse.allProductInStore , + this.userItemsTable = $('#userItemsTable').DataTable(); + const stationTableData = this.formResponse.stationItemBalance ? this.formResponse.stationItemBalance : []; + this.stationItemsTable = $('#stationItemsTable').DataTable({ + data: stationTableData.items, + columns: [ + { data: 'itemName', title: 'Item Name' }, + { data: 'uniqueID', title: 'Unique ID' }, + { data: 'quantity', title: 'Quantity' }, + { data: 'itemPrice', title: 'Item Price' }, + ], + destroy: true, + drawCallback: function () { + const total = stationTableData.totalItemPrice; + $(this.api().table().node()).find('tbody tr.total-row').remove(); + $(this.api().table().node()).find('tbody').append( + ` + Total + ${total} + ` + ); + }, + }); + const storeTableData = this.formResponse.storeItemBalance ? this.formResponse.storeItemBalance : []; + this.storeItemsTable = $('#storeItemsTable').DataTable({ + data: storeTableData.items, + columns: [ + { data: 'itemName', title: 'Item Name' }, + { data: 'uniqueID', title: 'Unique ID' }, + { data: 'quantity', title: 'Quantity' }, + { data: 'itemPrice', title: 'Item Price' }, + + ], + destroy: true, + drawCallback: function () { + const total = storeTableData.totalItemPrice; + $(this.api().table().node()).find('tbody tr.total-row').remove(); + $(this.api().table().node()).find('tbody').append( + ` + Total + ${total} + ` + ); + }, + }); + const storeAllTableData = this.formResponse.allProductInStore ? this.formResponse.allProductInStore : []; + this.storeAllItemsTable = $('#storeAllItemsTable').DataTable({ + data: storeAllTableData , columns: [ { data: 'productName', title: 'Item Name' }, { data: 'quantity', title: 'Quantity' }, @@ -411,6 +473,27 @@ }); }); }, + async fetchItemMovement(data){ + try { + const response = await fetch(`/ReportingAPI/GetItemMovement/`, { + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data), + method: 'POST', + }); + if (response.ok) { + const data = await response.json(); + this.itemMovement = data; + } + else { + console.error(`Failed to fetch item movement: ${response.statusText}`); + } + } + catch (error) { + console.error('There was a problem with the fetch operation:', error); + } + } // selectedUserFunc() { // if (this.userItemsTable) { // this.userItemsTable.destroy(); diff --git a/Controllers/API/Inventory/InvMainAPI.cs b/Controllers/API/Inventory/InvMainAPI.cs index 692c72b..41611c0 100644 --- a/Controllers/API/Inventory/InvMainAPI.cs +++ b/Controllers/API/Inventory/InvMainAPI.cs @@ -1920,8 +1920,10 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory [HttpPost("UserList")] public async Task UserList() { - - var userList = await _centralDbContext.Users.Include(i => i.Department).ToListAsync(); + //get current user + var thisUser = await _userManager.GetUserAsync(User); + var thisUserDept = thisUser!.departmentId; + var userList = await _centralDbContext.Users.Include(i => i.Department).Where(i => i.departmentId == thisUserDept).ToListAsync(); return Json(userList.Select(i => new { i.Id, diff --git a/Controllers/API/ReportingAPI.cs b/Controllers/API/ReportingAPI.cs index e81f5da..9454e1a 100644 --- a/Controllers/API/ReportingAPI.cs +++ b/Controllers/API/ReportingAPI.cs @@ -38,9 +38,6 @@ namespace PSTW_CentralSystem.Controllers.API { public string? FormDate { get; set; } public string? DeptId { get; set; } - public int ToUser { get; set; } - public int ToStation { get; set; } - public int ToStore { get; set; } } #region ItemReport @@ -142,9 +139,6 @@ namespace PSTW_CentralSystem.Controllers.API { string FormDate = reportQuery.FormDate!; string DeptId = reportQuery.DeptId!; - int ToUser = reportQuery.ToUser; - int ToStation = reportQuery.ToStation; - int ToStore = reportQuery.ToStore; var currentProductBalance = new List(); var result = await _centralDbContext.Products .Include(p => p.Items) @@ -226,9 +220,9 @@ namespace PSTW_CentralSystem.Controllers.API .Where(s => s.Department!.DepartmentCode == DeptId) .ToListAsync(); - var departmentStores = await _centralDbContext.Stores + var departmentStores = await _centralDbContext.Departments .Include(s => s.Company) - .Where(s => s.Department!.DepartmentCode == DeptId) + .Where(s => s.DepartmentCode == DeptId) .ToListAsync(); var latestItemMovements = await _centralDbContext.ItemMovements @@ -237,6 +231,7 @@ namespace PSTW_CentralSystem.Controllers.API .Include(m => m.NextUser) .Include(m => m.NextStation) .Include(m => m.NextStore) + .Where(m => m.MovementComplete == true) .GroupBy(m => m.ItemId) .Select(g => g .Where(m => m.Date <= parsedDate) @@ -269,7 +264,7 @@ namespace PSTW_CentralSystem.Controllers.API UniqueID = m.Item.UniqueID, }) .ToList(), - TotalItemPrice = latestItemMovements.Where(m => m != null && m.ToUser == u.Id).Sum(m => m!.Item!.ConvertPrice * m.Quantity), + TotalItemPrice = latestItemMovements.Where(m => m != null && m.ToUser == u.Id).Select(m => m!.Item!.ConvertPrice).Sum(), }) .Where(u => u.Items.Count > 0).ToList(); @@ -292,15 +287,39 @@ namespace PSTW_CentralSystem.Controllers.API UniqueID = m.Item.UniqueID, }) .ToList(), - TotalItemPrice = latestItemMovements.Where(m => m != null && m.ToStation == u.StationId).Sum(m => m!.Item!.ConvertPrice * m.Quantity), + TotalItemPrice = latestItemMovements.Where(m => m != null && m.ToStation == u.StationId).Select(m => m!.Item!.ConvertPrice).Sum(), }) - .Where(u => u.Items.Count > 0).ToList(); + .Where(u => u.Items.Count > 0).FirstOrDefault(); + + var storeItemMovements = departmentStores + .Select(u => new + { + Department = u.DepartmentName, + Items = latestStationItemMovements + .Where(m => m != null && m.ToStore > 0 && m.NextStore!.StoreName.Contains(u.DepartmentName)) + .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 = latestStationItemMovements.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(); var report = new { allProductInStore = currentProductBalance, userItemBalance = usersItemMovements, stationItemBalance = stationItemMovements, + storeItemBalance = storeItemMovements, }; return Json(report); @@ -311,7 +330,37 @@ namespace PSTW_CentralSystem.Controllers.API } } #endregion + #region Item Movement report + [HttpPost("GetItemMovement")] + public async Task GetItemMovement([FromBody] String data) + { + try + { + if (string.IsNullOrEmpty(data)) + { + return BadRequest("Invalid Unique ID"); + } + var itemMovement = await _centralDbContext.ItemMovements + .Include(im => im.Item) + .Where(im => im.Item!.UniqueID == data) + .OrderByDescending(im => im.Id) + .Select(im => new + { + From = im.LastUser != null ? im.FromUser!.FullName : im.LastStation != null ? im.FromStation!.StationName : im.LastStore != null ? im.FromStore!.StoreName : null, + To = im.ToUser != null ? im.NextUser!.FullName : im.ToStation != null ? im.NextStation!.StationName : im.ToStore != null ? im.NextStore!.StoreName : null, + Date = im.Date, + MovementStatus = im.MovementComplete + }) + .ToListAsync(); + return Json(itemMovement); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + #endregion #region Report Inventory ii [HttpPost("GetInventoryReport")] public async Task GetInventoryReport([FromBody] ReportFilterDTO filter)