Update Item & Product

This commit is contained in:
ArifHilmi 2025-03-12 07:42:57 +08:00
parent 9f1283f7c9
commit 58f3fc4c48
3 changed files with 38 additions and 14 deletions

View File

@ -603,8 +603,8 @@
"data": "convertPrice", "data": "convertPrice",
}, },
{ {
"title": "Invoice Date", "title": "Register Date",
"data": "invoiceDate", "data": "createDate",
}, },
{ {
"title": "Warranty Until", "title": "Warranty Until",
@ -672,11 +672,12 @@
}) })
// 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').off('click', '.delete-btn').on('click', '.delete-btn', function () {
const itemId = $(this).data('id'); const itemId = $(this).data('id');
self.deleteItem(itemId); self.deleteItem(itemId);
}); });
$('#itemDatatable tbody').on('click', '.print-btn', function () { $('#itemDatatable tbody').on('click', '.print-btn', function () {
const $button = $(this); // The clicked button const $button = $(this); // The clicked button
const $row = $button.closest('tr'); // The parent row of the button const $row = $button.closest('tr'); // The parent row of the button
@ -692,9 +693,8 @@
// For expanded view: Find the img in the first column of the current row // For expanded view: Find the img in the first column of the current row
imageSrc = $row.find('td:nth-child(1) img').attr('src'); imageSrc = $row.find('td:nth-child(1) img').attr('src');
} }
if (imageSrc) { if (imageSrc) {
self.printItem(itemId, imageSrc); // Call the print function with the itemId and imageSrc self.printItem(itemId, imageSrc); //\ Call the print function with the itemId and imageSrc
} else { } else {
console.error("Image source not found."); console.error("Image source not found.");
} }
@ -876,8 +876,6 @@
.row($(`.delete-btn[data-id="${itemId}"]`).closest('tr')) .row($(`.delete-btn[data-id="${itemId}"]`).closest('tr'))
.remove() .remove()
.draw(); .draw();
} else {
alert(result.message);
} }
} }
catch (error) { catch (error) {
@ -893,7 +891,6 @@
this.thisQRInfo.uniqueID = itemId; this.thisQRInfo.uniqueID = itemId;
const uniqueQR = itemId; const uniqueQR = itemId;
const container = document.getElementById("QrContainer"); const container = document.getElementById("QrContainer");
if (!container) { if (!container) {
console.error("Container not found."); console.error("Container not found.");
return; return;
@ -946,7 +943,11 @@
console.error("Items list is not available or is not an array."); console.error("Items list is not available or is not an array.");
return null; return null;
} }
return this.items.find(item => item.uniqueID === uniqueID);
//FIX ERROR
const foundItem = this.items.find(item => String(item.uniqueID).trim() === String(uniqueID).trim());
return foundItem ? JSON.parse(JSON.stringify(foundItem)) : null;
}, },
printQRInfo() { printQRInfo() {
// Create a virtual DOM element // Create a virtual DOM element
@ -1025,7 +1026,6 @@
}); });
}, },
}, },
}); });
</script> </script>

View File

@ -145,7 +145,6 @@
}, },
methods: { methods: {
initiateTable() { initiateTable() {
console.log(this.products)
this.productDatatable = $('#productDatatable').DataTable({ this.productDatatable = $('#productDatatable').DataTable({
"data": this.products, "data": this.products,
"columns": [ "columns": [
@ -180,7 +179,7 @@
"title": "Delete", "title": "Delete",
"data": "productId", "data": "productId",
"render": function (data) { "render": function (data) {
var deleteButton = `<button type="button" class="btn btn-danger delete-btn" data-id="${data.productId}">Delete</button>`; var deleteButton = `<button type="button" class="btn btn-danger delete-btn" data-id="${data}">Delete</button>`;
return deleteButton; return deleteButton;
}, },
} }

View File

@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Templates.BlazorIdentity.Pages;
using Mono.TextTemplating; using Mono.TextTemplating;
using Newtonsoft.Json; using Newtonsoft.Json;
using PSTW_CentralSystem.Areas.Inventory.Models; using PSTW_CentralSystem.Areas.Inventory.Models;
@ -265,6 +266,7 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory
var userRole = await _userManager.GetRolesAsync(user); var userRole = await _userManager.GetRolesAsync(user);
var isAdmin = userRole.Contains("SystemAdmin") || userRole.Contains("SuperAdmin") || userRole.Contains("Finance"); var isAdmin = userRole.Contains("SystemAdmin") || userRole.Contains("SuperAdmin") || userRole.Contains("Finance");
List<ItemModel> itemList = new List<ItemModel>(); List<ItemModel> itemList = new List<ItemModel>();
List<ItemMovementModel> createDate = new List<ItemMovementModel>();
// Get the item list // Get the item list
if (isAdmin) if (isAdmin)
{ {
@ -296,14 +298,22 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory
.ThenInclude(m => m!.FromUser) .ThenInclude(m => m!.FromUser)
.Where(i => i.DepartmentId == user.departmentId) .Where(i => i.DepartmentId == user.departmentId)
.ToListAsync(); .ToListAsync();
} }
// Get the departments list (DepartmentId references Departments) // Get the departments list (DepartmentId references Departments)
var departments = await _centralDbContext.Departments.ToListAsync(); var departments = await _centralDbContext.Departments.ToListAsync();
var createDates = await _centralDbContext.ItemMovements.Where(r => r.Action == "Register").ToListAsync();
// Buat dictionary agar lebih cepat dicari berdasarkan ItemID
var createDateDict = await _centralDbContext.ItemMovements.Where(r => r.Action == "Register").GroupBy(r => r.ItemId).ToDictionaryAsync(g => g.Key, g => g.Min(m => m.Date));
// Now join items with users and departments manually // Now join items with users and departments manually
var itemListWithDetails = itemList.Select(item => new var itemListWithDetails = itemList.Select(item => new
{ {
createDate = createDateDict.ContainsKey(item.ItemID) ? createDateDict[item.ItemID].ToString("dd/MM/yyyy HH:mm:ss") : null,
item.ItemID, item.ItemID,
item.UniqueID, item.UniqueID,
item.CompanyId, item.CompanyId,
@ -324,6 +334,7 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory
EndWDate = item.EndWDate.ToString("dd/MM/yyyy"), EndWDate = item.EndWDate.ToString("dd/MM/yyyy"),
InvoiceDate = item.InvoiceDate?.ToString("dd/MM/yyyy"), InvoiceDate = item.InvoiceDate?.ToString("dd/MM/yyyy"),
item.Department?.DepartmentName, item.Department?.DepartmentName,
RegisterDate = createDate,
CreatedBy =item.CreatedBy!.UserName, CreatedBy =item.CreatedBy!.UserName,
item.Product!.ProductName, item.Product!.ProductName,
item.Product!.ProductShortName, item.Product!.ProductShortName,
@ -465,12 +476,26 @@ namespace PSTW_CentralSystem.Controllers.API.Inventory
return NotFound(new { success = false, message = "Item not found" }); return NotFound(new { success = false, message = "Item not found" });
} }
// Get related item movements
var itemMovements = await _centralDbContext.ItemMovements
.Where(i => i.ItemId == item.ItemID)
.ToListAsync();
// Remove all related item movements
if (itemMovements.Any())
{
_centralDbContext.ItemMovements.RemoveRange(itemMovements);
await _centralDbContext.SaveChangesAsync();
}
// Remove the item itself
_centralDbContext.Items.Remove(item); _centralDbContext.Items.Remove(item);
await _centralDbContext.SaveChangesAsync(); await _centralDbContext.SaveChangesAsync();
return Ok(new { success = true, message = "Item deleted successfully" }); return Ok(new { success = true, message = "Item deleted successfully" });
} }
[HttpPost("GetItem/{id}")] // Endpoint to retrieve an item by its ID [HttpPost("GetItem/{id}")] // Endpoint to retrieve an item by its ID
public async Task<IActionResult> GetItem(string id) public async Task<IActionResult> GetItem(string id)
{ {