diff --git a/Areas/Inventory/Views/ItemMovement/ItemMovementUser.cshtml b/Areas/Inventory/Views/ItemMovement/ItemMovementUser.cshtml index 76626aa..19da61d 100644 --- a/Areas/Inventory/Views/ItemMovement/ItemMovementUser.cshtml +++ b/Areas/Inventory/Views/ItemMovement/ItemMovementUser.cshtml @@ -609,67 +609,55 @@ }, groupedByStation() { - let grouped = {}; - this.itemMovements.forEach((movement) => { + let groupedByItem = this.itemMovements.reduce((acc, movement) => { + if (!acc[movement.uniqueID]) { + acc[movement.uniqueID] = { + uniqueID: movement.uniqueID, + movements: [], + }; + } + acc[movement.uniqueID].movements.push(movement); + return acc; + }, {}); - if (movement.toStation !== null) { - let station = movement.toStationName; - let itemId = movement.uniqueID; + let groupedByStation = {}; - if (!grouped[station]) { - grouped[station] = {}; - } + Object.keys(groupedByItem).forEach(itemId => { + let movements = groupedByItem[itemId].movements + .sort((a, b) => b.id - a.id); // Newest → Oldest - if (!grouped[station][itemId]) { - grouped[station][itemId] = { uniqueID: itemId, movements: [] }; - } + // Find first occurrence of 'Return' complete + let stopIndex = movements.findIndex(m => + m.toOther === 'Return' && m.movementComplete == 1 + ); - grouped[station][itemId].movements.push(movement); + // Remove older movements + if (stopIndex !== -1) { + movements = movements.slice(0, stopIndex); } - if (movement.lastStation !== null) { - let station = movement.lastStationName; - let itemId = movement.uniqueID; + if (movements.length > 0) { + let latestMovement = movements[0]; + let station = latestMovement.lastStationName || latestMovement.toStationName || "Self Assigned"; - if (!grouped[station]) { - grouped[station] = {}; + if (!groupedByStation[station]) { + groupedByStation[station] = {}; } - if (!grouped[station][itemId]) { - grouped[station][itemId] = { uniqueID: itemId, movements: [] }; - } - - grouped[station][itemId].movements.push(movement); + groupedByStation[station][itemId] = { uniqueID: itemId, movements }; } - - if (movement.lastStation == null && movement.toStation == null) { - - let station = "Self Assigned"; - let itemId = movement.uniqueID; - - if (!grouped[station]) { - grouped[station] = {}; - } - - if (!grouped[station][itemId]) { - grouped[station][itemId] = { uniqueID: itemId, movements: [] }; - } - - grouped[station][itemId].movements.push(movement); - } - }); - // Sort stations and move "Unassign Station" to the last position - let sortedKeys = Object.keys(grouped).sort((a, b) => { - if (a === "Unassign Station") return 1; + // 4️⃣ **Sort stations & move 'Unassign Station' to last** + let sortedKeys = Object.keys(groupedByStation).sort((a, b) => { + if (a === "Unassign Station") return 1; if (b === "Unassign Station") return -1; - return a.localeCompare(b); + return a.localeCompare(b); }); let sortedGrouped = {}; - sortedKeys.forEach((key) => { - sortedGrouped[key] = grouped[key]; + sortedKeys.forEach(key => { + sortedGrouped[key] = groupedByStation[key]; }); return sortedGrouped; @@ -746,8 +734,6 @@ showDetails: false, })); - console.log(this.itemMovements); - this.renderTables(); } catch (error) { console.error("Error fetching item:", error); @@ -767,154 +753,131 @@ if (this.itemMovementCompleteDatatable) { this.itemMovementCompleteDatatable.destroy(); } - if(this.stationDatatable) { + if (this.stationDatatable) { this.stationDatatable.destroy(); } + // Get latest movement per uniqueID + function getLatestMovements(data) { + let latestMovements = {}; + data.forEach(movement => { + let id = movement.uniqueID; + if (!latestMovements[id] || latestMovements[id].id < movement.id) { + latestMovements[id] = movement; + } + }); + return Object.values(latestMovements); + } + + // Distribute items based on priority + let latestMovements = getLatestMovements(this.itemMovements); + let notCompleteData = []; + let completeData = []; + let assignedData = []; + + latestMovements.forEach(movement => { + if (movement.movementComplete == 0) { + notCompleteData.push(movement); + } else if (movement.movementComplete == 1 && movement.action !== "Assign") { + completeData.push(movement); + } else if (movement.movementComplete == 1 && movement.action == "Assign") { + assignedData.push(movement); + } + }); + + // Table 1: Not Complete Movements this.itemMovementNotCompleteDatatable = $("#itemMovementNotCompleteDatatable").DataTable({ - data: this.itemMovements.filter((m) => m.movementComplete == 0), + data: notCompleteData, columns: [ { title: "Unique Id", data: "id" }, + { title: "Product Name", data: "productName" }, { title: "Product Code", data: "uniqueID" }, { title: "Action", data: "action" }, { title: "Send Date", data: "sendDate" }, + { title: "Start Status", data: "toOther" }, { title: "From User", data: "toUserName" }, { title: "Last User", data: "lastUserName" }, { title: "From Station", data: "toStationName" }, { title: "From Store", data: "toStoreName" }, - { title: "Start Status", data: "toOther" }, { title: "Quantity", data: "quantity" }, - { - title: "Note", - data: "consignmentNote", - render: function (data, type, full, meta) { - if (!data) { - return "No Document"; - } - - // Check if the document is an image based on file extension - var isImage = /\.(jpeg|jpg|png|gif)$/i.test(data); - var isPdf = /\.pdf$/i.test(data); - - if (isImage) { - return ` - Image - `; - } - else if (isPdf) { - return ` - PDF Document -
View PDF -
`; - } else { - return `Download File`; - } - }, - }, + { title: "Note", data: "consignmentNote", render: renderFile }, { title: "Remark", data: "remark" }, ], responsive: true, }); + // Table 2: Completed Movements this.itemMovementCompleteDatatable = $("#itemMovementCompleteDatatable").DataTable({ - data: this.itemMovements.filter((m) => m.movementComplete == 1 && m.action !== "Assign"), + data: completeData, columns: [ { title: "Unique Id", data: "id" }, + { title: "Product Name", data: "productName" }, { title: "Product Code", data: "uniqueID" }, { title: "Send Date", data: "sendDate" }, { title: "Receive Date", data: "receiveDate" }, { title: "Action", data: "action" }, + { title: "Start Status", data: "toOther" }, + { title: "Latest Status", data: "latestStatus" }, { title: "From User", data: "toUserName" }, { title: "Last User", data: "lastUserName" }, { title: "From Station", data: "toStationName" }, { title: "Last Station", data: "lastStationName" }, { title: "From Store", data: "toStoreName" }, { title: "Last Store", data: "lastStoreName" }, - { title: "Start Status", data: "toOther" }, - { title: "Latest Status", data: "latestStatus" }, { title: "Qty", data: "quantity" }, - { title: "Note", - data: "consignmentNote", - render: function (data, type, full, meta) { - if (!data) { - return "No Document"; - } - - // Check if the document is an image based on file extension - var isImage = /\.(jpeg|jpg|png|gif)$/i.test(data); - var isPdf = /\.pdf$/i.test(data); - - if (isImage) { - return ` - Image - `; - } - else if (isPdf) { - return ` - PDF Document -
View PDF -
`; - } else { - return `Download File`; - } - }, - }, + { title: "Note", data: "consignmentNote", render: renderFile }, { title: "Remark", data: "remark" }, ], responsive: true, }); + // Table 3: Assigned Movements this.stationDatatable = $("#stationDatatable").DataTable({ - data: this.itemMovements.filter((m) => m.action === "Assign" ), + data: assignedData, columns: [ { title: "Unique Id", data: "id" }, + { title: "Product Name", data: "productName" }, { title: "Product Code", data: "uniqueID" }, { title: "Assign Date", data: "sendDate" }, { title: "From User", data: "toUserName" }, { title: "From Station", data: "toStationName" }, { title: "Last Station", data: "lastStationName" }, { title: "Qty", data: "quantity" }, - { - title: "Note", - data: "consignmentNote", - render: function (data, type, full, meta) { - if (!data) { - return "No Document"; - } - - // Check if the document is an image based on file extension - var isImage = /\.(jpeg|jpg|png|gif)$/i.test(data); - var isPdf = /\.pdf$/i.test(data); - - if (isImage) { - return ` - Image - `; - } - else if (isPdf) { - return ` - PDF Document -
View PDF -
`; - } else { - return `Download File`; - } - }, - }, + { title: "Note", data: "consignmentNote", render: renderFile }, { title: "Remark", data: "remark" }, ], responsive: true, }); + + // Function to render file (image/PDF) + function renderFile(data, type, full, meta) { + if (!data) { + return "No Document"; + } + var isImage = /\.(jpeg|jpg|png|gif)$/i.test(data); + var isPdf = /\.pdf$/i.test(data); + if (isImage) { + return ` + Image + `; + } else if (isPdf) { + return ` + PDF Document +
View PDF +
`; + } else { + return `Download File`; + } + } }, toggleCategory(itemId) { this.categoryVisible[itemId] = !this.categoryVisible[itemId]; + + this.detailsVisible = {}; + this.historyVisible = {}; }, toggleHistory(itemId) { diff --git a/Areas/Inventory/Views/ItemMovement/ItemRequest.cshtml b/Areas/Inventory/Views/ItemMovement/ItemRequest.cshtml index 4ab343d..54e1c2b 100644 --- a/Areas/Inventory/Views/ItemMovement/ItemRequest.cshtml +++ b/Areas/Inventory/Views/ItemMovement/ItemRequest.cshtml @@ -462,7 +462,7 @@ }, }, { - "title": "Product Id", + "title": "Product Name", "data": "productName", }, { @@ -570,7 +570,7 @@ }, }, { - "title": "Product Id", + "title": "Product Name", "data": "productName", }, {