item request api

This commit is contained in:
ameerulrasyid 2025-02-20 07:42:06 +08:00
parent 5f4e8c6c22
commit d9e67e6139
4 changed files with 207 additions and 107 deletions

View File

@ -7,10 +7,10 @@ namespace PSTW_CentralSystem.Areas.Inventory.Models
{ {
[Key] [Key]
public int requestId { get; set; } public int requestId { get; set; }
public int DepartmentId { get; set; } //public int DepartmentId { get; set; }
[ForeignKey("DepartmentId")] //[ForeignKey("DepartmentId")]
public virtual ItemModel? Department { get; set; } //public virtual ItemModel? Department { get; set; }
[ForeignKey("ItemID")] //[ForeignKey("ItemID")]
public string? remark { get; set; } public string? remark { get; set; }
public string? status { get; set; } public string? status { get; set; }
public DateTime requestDate { get; set; } public DateTime requestDate { get; set; }

View File

@ -648,8 +648,8 @@
"data": this.items, "data": this.items,
"columns": [ "columns": [
{ {
"title": "Unique Id", "title": "Request ID",
"data": "itemId", "data": "requestId",
"createdCell": function (td, cellData, rowData, row, col) { "createdCell": function (td, cellData, rowData, row, col) {
// Assign a unique ID to the <td> element // Assign a unique ID to the <td> element
$(td).attr('id', `qr${cellData}`); $(td).attr('id', `qr${cellData}`);
@ -657,110 +657,98 @@
}, },
{ {
"title": "Print", "title": "Print",
"data": "itemId", "data": "requestId",
"render": function (data, type, full, meta) { "render": function (data, type, full, meta) {
var printButton = `<button type="button" class="btn btn-success print-btn" data-id="${data}">Print</button>`; var printButton = `<button type="button" class="btn btn-success print-btn" data-id="${data}">Print</button>`;
return printButton; return printButton;
}, },
"className": "align-middle", "className": "align-middle",
}, },
{
"title": "To User",
"data": "toUser",
},
{
"title": "Last User",
"data": "lastStore",
},
{
"title": "Action",
"data": "action",
},
{
"title": "Latest Status",
"data": "latestStatus",
},
{
"title": "Quantity",
"data": "quantity",
},
{
"title": "To Station",
"data": "toStation",
},
{
"title": "To Store",
"data": "toStore",
},
{
"title": "To Other",
"data": "toOther",
},
{ {
"title": "Remark", "title": "Remark",
"data": "remark", "data": "remark",
}, },
{ {
"title": "Note", "title": "Status",
"data": "consignmentNote", "data": "status",
}, },
{ {
"title": "Send Date", "title": "Request Date",
"data": "date", "data": "requestDate",
}, },
{ {
"title": "Last User", "title": "Approval Date",
"data": "lastUser", "data": "approvalDate",
}, },
{
"title": "Last Store",
"data": "lastStore",
},
{
"title": "Last Station",
"data": "lastStation",
},
{
"title": "Latest Status",
"data": "latestStatus",
},
{
"title": "Receive Date",
"data": "date",
},
{
"title": "Completion",
"data": "movementComplete",
},
// { // {
// "title": "Warranty Until", // "title": "Quantity",
// "data": "warranty", // "data": "quantity",
// "render": function (data, type, full, meta) {
// if (data > 0) { return full.endWDate }
// else { return data }
// }
// }, // },
// { // {
// "title": "Location", // "title": "To Station",
// "data": "currentUser", // "data": "toStation",
// "render": function (data, type, full, meta) { // },
// currentUser = data ?? null; // {
// currentStore = full.currentStore ?? 'N/A'; // "title": "To Store",
// currentStation = full.currentStation ?? 'N/A'; // "data": "toStore",
// return `User: ${currentUser}<br> // },
// Store: ${currentStore}<br> // {
// Station: ${currentStation}` // "title": "To Other",
// } // "data": "toOther",
// },
// {
// "title": "Remark",
// "data": "remark",
// },
// {
// "title": "Note",
// "data": "consignmentNote",
// },
// {
// "title": "Send Date",
// "data": "date",
// },
// {
// "title": "Last User",
// "data": "lastUser",
// },
// {
// "title": "Last Store",
// "data": "lastStore",
// },
// {
// "title": "Last Station",
// "data": "lastStation",
// },
// {
// "title": "Latest Status",
// "data": "latestStatus",
// },
// {
// "title": "Receive Date",
// "data": "date",
// },
// {
// "title": "Completion",
// "data": "movementComplete",
// }, // },
{ {
"title": "Delete", "title": "Reject",
"data": "itemId", "data": "requestId",
"render": function (data) { "render": function (data) {
var deleteButton = `<button type="button" class="btn btn-danger delete-btn" data-id="${data}">Delete</button>`; var deleteButton = `<button type="button" class="btn btn-danger reject-btn" data-id="${data}">Reject</button>`;
return deleteButton; return deleteButton;
}, },
"className": "align-middle", "className": "align-middle",
},
{
"title": "Approve",
"data": "requestId",
"render": function (data) {
var approveButton = `<button type="button" class="btn btn-success approve-btn" data-id="${data}">Approve</button>`;
return approveButton;
},
"className": "align-middle",
} }
], ],
responsive: true, responsive: true,
@ -769,10 +757,10 @@
const api = this.api(); const api = this.api();
api.rows().every(function () { api.rows().every(function () {
const data = this.data(); // Row data const data = this.data(); // Row data
const containerId = `qr${data.itemId}`; const containerId = `qr${data.requestId}`;
const container = $(`#${containerId}`); const container = $(`#${containerId}`);
container.empty(); container.empty();
container.append(`${data.itemId}`); container.append(`${data.requestId}`);
// console.log(container[0]); // console.log(container[0]);
if (container) { if (container) {
// Generate QR code only if not already generated // Generate QR code only if not already generated
@ -793,9 +781,14 @@
}) })
// Attach click event listener to the delete buttons // Attach click event listener to the delete buttons
$('#itemDatatable tbody').on('click', '.delete-btn', function () { $('#itemDatatable tbody').on('click', '.reject-btn', function () {
const itemId = $(this).data('id'); const itemId = $(this).data('id');
self.deleteItem(itemId); self.rejectRequest(itemId);
});
$('#itemDatatable tbody').on('click', '.approve-btn', function () {
const itemId = $(this).data('id');
self.approveRequest(itemId);
}); });
$('#itemDatatable tbody').on('click', '.print-btn', function () { $('#itemDatatable tbody').on('click', '.print-btn', function () {
@ -827,8 +820,8 @@
async fetchItem() { async fetchItem() {
try { try {
// const token = localStorage.getItem('token'); // Get the token from localStorage // const token = localStorage.getItem('token'); // Get the token from localStorage
const response = await fetch('/InvMainAPI/ItemMovementList', { const response = await fetch('/InvMainAPI/ItemRequestList', {
method: 'POST', // Specify the HTTP method method: 'GET', // Specify the HTTP method
headers: { headers: {
'Content-Type': 'application/json', // Set content type 'Content-Type': 'application/json', // Set content type
// 'Authorization': `Bearer ${token}` // Include the token in the headers // 'Authorization': `Bearer ${token}` // Include the token in the headers
@ -927,7 +920,7 @@
}, },
async fetchStore() { async fetchStore() {
try { try {
const response = await fetch('/InvMainAPI/StoreList', { const response = await fetch('/InvMainAPI/StoreList/', {
method: 'POST', // Specify the HTTP method method: 'POST', // Specify the HTTP method
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -1028,13 +1021,13 @@
this.EndWDate = null; this.EndWDate = null;
} }
}, },
async deleteItem(itemId) { async approveRequest(itemId) {
if (!confirm("Are you sure you want to delete this item?")) { // if (!confirm("Are you sure you want to approve this request?")) {
return; // return;
} // }
try { try {
const response = await fetch(`/InvMainAPI/DeleteItem/${itemId}`, { const response = await fetch(`/InvMainAPI/ApproveRequest/${itemId}`, {
method: 'DELETE', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
@ -1043,18 +1036,56 @@
if (result.success) { if (result.success) {
alert(result.message); alert(result.message);
// Remove the row from DataTables
this.itemDatatable //static update
.row($(`.delete-btn[data-id="${itemId}"]`).closest('tr')) const row = $(`.approve-btn[data-id="${itemId}"]`).closest('tr');
.remove() this.itemDatatable.row(row)
.data({ ...this.itemDatatable.row(row)
.data(), status: "Approved" })
.draw(); .draw();
} else { } else {
alert(result.message); alert(result.message);
} }
} }
catch (error) { catch (error) {
console.error("Error deleting item:", error); console.error("Error approving request:", error);
alert("An error occurred while deleting the item."); // alert("An error occurred while approving the request.");
}
finally {
this.loading = false;
}
},
async rejectRequest(itemId) {
if (!confirm("Are you sure you want to reject this request?")) {
return;
}
try {
const response = await fetch(`/InvMainAPI/RejectRequest/${itemId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
const result = await response.json();
if (result.success) {
alert(result.message);
//static update
const row = $(`.approve-btn[data-id="${itemId}"]`).closest('tr');
this.itemDatatable.row(row)
.data({ ...this.itemDatatable.row(row)
.data(), status: "Rejected" })
.draw();
} else {
alert(result.message);
}
}
catch (error) {
console.error("Error rejecting request:", error);
// alert("An error occurred while rejecting the request.");
} }
finally { finally {
this.loading = false; this.loading = false;

View File

@ -627,6 +627,51 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory
#endregion ItemMovement #endregion ItemMovement
#region ItemRequest
[HttpGet("ItemRequestList")]
public async Task<IActionResult> ItemRequestList()
{
var itemRequestList = await _centralDbContext.Request.ToListAsync();
return Json(itemRequestList);
}
[HttpPost("ApproveRequest/{id}")]
public async Task<IActionResult> ApproveRequest(int id)
{
var Request = await _centralDbContext.Request.FindAsync(id);
if (Request == null)
{
return NotFound(new { success = false, message = "Request not found" });
}
Request.status = "Approved";
Request.approvalDate = DateTime.UtcNow;
_centralDbContext.SaveChanges();
return Ok(new { success = true, message = "Request Approved Successfully", data = Request });
}
[HttpPost("RejectRequest/{id}")]
public async Task<IActionResult> RejectRequest(int id)
{
var Request = await _centralDbContext.Request.FindAsync(id);
if (Request == null)
{
return NotFound(new { success = false, message = "Request not found" });
}
Request.status = "Rejected";
Request.approvalDate = DateTime.UtcNow;
_centralDbContext.SaveChanges();
return Ok(new { success = true, message="Request Rejected Successfully", data=Request });
}
#endregion ItemRequest
#region ItemReport #region ItemReport
[HttpPost("GetInventoryReport/{deptId}")] [HttpPost("GetInventoryReport/{deptId}")]
@ -764,5 +809,29 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory
return Ok(new { success = true, message = "Station deleted successfully" }); return Ok(new { success = true, message = "Station deleted successfully" });
} }
#endregion Station #endregion Station
#region Store
[HttpPost("StoreList")]
public async Task<IActionResult> StoreList()
{
var storeList = await _centralDbContext.Stores.ToListAsync();
return Json(storeList);
}
#endregion Store
#region User
[HttpPost("UserList")]
public async Task<IActionResult> UserList()
{
var userList = await _centralDbContext.Users.ToListAsync();
return Json(userList);
}
#endregion User
} }
} }

View File

@ -91,7 +91,7 @@ namespace PSTW_CentralSystem.DBContext
public DbSet<DepartmentModel> Departments { get; set; } public DbSet<DepartmentModel> Departments { get; set; }
public DbSet<ManufacturerModel> Manufacturers { get; set; } public DbSet<ManufacturerModel> Manufacturers { get; set; }
public DbSet<ItemModel> Items { get; set; } public DbSet<ItemModel> Items { get; set; }
public DbSet<RequestModel> Requests { get; set; } public DbSet<RequestModel> Request { get; set; }
public DbSet<ProductModel> Products { get; set; } public DbSet<ProductModel> Products { get; set; }
public DbSet<SupplierModel> Suppliers { get; set; } public DbSet<SupplierModel> Suppliers { get; set; }
public DbSet<InventoryMasterModel> InventoryMasters { get; set; } public DbSet<InventoryMasterModel> InventoryMasters { get; set; }