From cdaa56febda23bc632f1c7f9ddcf96ce46812208 Mon Sep 17 00:00:00 2001 From: DungTT Date: Wed, 23 Sep 2026 09:59:41 +0700 Subject: [PATCH] fix - ASS-OLD --- ASSET_DATA_REPLACEMENT.md | 4 +-- public/js/app.js | 41 ++++++++++++++++++++----------- public/modals.html | 18 +++++++------- test/asset-archive-routes.test.js | 10 ++++++++ test/asset-archive-ui.test.js | 35 +++++++++++++++++++++++--- 5 files changed, 78 insertions(+), 30 deletions(-) diff --git a/ASSET_DATA_REPLACEMENT.md b/ASSET_DATA_REPLACEMENT.md index 4c146f9..db68a93 100644 --- a/ASSET_DATA_REPLACEMENT.md +++ b/ASSET_DATA_REPLACEMENT.md @@ -8,7 +8,7 @@ Chức năng **Xóa danh mục hiện tại** đưa các tài sản hiện tại 2. Đăng nhập bằng tài khoản **Admin** hoặc **Quản lý tài sản**. Mở **Quản lý tài sản** → **Xóa danh mục hiện tại**, đọc nội dung rồi xác nhận. Thao tác áp dụng cho toàn bộ danh mục hiện tại, kể cả tài sản ở trang khác hoặc đang bị bộ lọc ẩn. 3. Upload file tài sản mới bằng chức năng nhập Excel hiện có. Mã tài sản được phép trùng với mã trong dữ liệu cũ. Bản ghi mới có tồn kho riêng; lịch sử cũ tiếp tục gắn với bản ghi cũ. 4. Mở **Mượn/Trả tài sản**, chọn bộ lọc **Đơn → Còn đang mượn**. Chọn thêm **Tài sản → Tài sản cũ** nếu chỉ muốn xem các phiếu của danh mục đã xóa. Số lượng còn mượn bằng số đã mượn trừ số đã được xác nhận trả. Đơn trả đang chờ duyệt chưa được trừ. -5. Mở **Tài sản cũ còn mượn** để đối chiếu cả người mượn được nhập từ Excel hoặc ghi khi xuất trực tiếp mà chưa có phiếu mượn riêng. Màn hình **Tài sản tôi đang mượn** cũng giữ các tài sản cũ còn đang mượn. +5. Mở **Tài sản cũ** để xem toàn bộ tài sản đã xóa khỏi danh mục hiện tại, kể cả tài sản không còn người mượn. Danh sách vẫn hiển thị số lượng còn mượn và người mượn được nhập từ Excel hoặc ghi khi xuất trực tiếp mà chưa có phiếu mượn riêng. Màn hình **Tài sản tôi đang mượn** cũng giữ các tài sản cũ còn đang mượn. ## Xử lý mượn trả sau khi thay dữ liệu @@ -17,7 +17,7 @@ Chức năng **Xóa danh mục hiện tại** đưa các tài sản hiện tại - Tài sản cũ không được sửa hoặc xuất/mượn thêm. Đơn mượn cũ còn chờ xử lý không được duyệt; người quản lý có thể từ chối đơn đó. - Xóa một tài sản hoặc **Xóa đã chọn** cũng giữ lịch sử theo cùng cơ chế. -Không dùng `DELETE`/`TRUNCATE` bảng tài sản, xóa các bảng mượn trả, hoặc tạo lại database để làm trống danh mục. Chức năng trong giao diện giữ bản ghi cũ để tiếp tục theo dõi số lượng chưa trả. Dữ liệu đã bị xóa bởi phiên bản trước chỉ có thể lấy lại từ bản sao lưu phù hợp. +Không dùng `DELETE`/`TRUNCATE` bảng tài sản, xóa các bảng mượn trả, hoặc tạo lại database để làm trống danh mục. Chức năng trong giao diện chỉ đánh dấu `IsArchived = 1` trong database; toàn bộ bản ghi cũ vẫn còn và xem được tại **Tài sản cũ**. Dữ liệu đã bị xóa bởi phiên bản trước chỉ có thể lấy lại từ bản sao lưu phù hợp. ## Triển khai diff --git a/public/js/app.js b/public/js/app.js index 98d0eed..dfb34f8 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -2896,6 +2896,7 @@ class AccountManager { item.BorrowerName, item.AssetCode, item.AssetName, + isArchived ? 'Tài sản cũ' : 'Danh mục hiện tại', this.getAssetRequestTypeMeta(item.RequestType).label, this.getAssetRequestStatusMeta(item.RequestStatus, item).label, item.Unit, @@ -5072,7 +5073,7 @@ class AccountManager { class="border border-amber-300 text-amber-700 px-4 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 hover:bg-amber-50" > history - Tài sản cũ còn mượn + Tài sản cũ
-

Số dư người mượn của tài sản đã xóa khỏi danh mục, gồm cả dữ liệu nhập Excel và xuất trực tiếp. Các hồ sơ trùng mã được theo dõi riêng.

-

Nếu có đơn mượn gốc, tạo đơn trả tại Mượn/Trả tài sản để ghi nhận vào hồ sơ cũ. Dữ liệu chỉ có số dư người mượn cần đối chiếu riêng, cùng với tồn kho danh mục mới.

- - +

Toàn bộ tài sản đã xóa khỏi danh mục hiện tại vẫn được lưu trong cơ sở dữ liệu, kể cả tài sản không còn ai mượn. Các hồ sơ trùng mã được theo dõi riêng.

+

Lịch sử mượn/trả được giữ theo từng hồ sơ cũ. Nếu còn đơn mượn, tạo đơn trả tại Mượn/Trả tài sản.

+ +
- +
- +
MãTài sảnNgười mượnCòn mượnĐơn vịDự án / Phòng ban
MãTài sảnNgày xóaTồn đầuNhậpXuấtTồn cuốiCòn mượnĐơn vịNgười mượnPhòng ban / Dự án
@@ -1071,7 +1071,7 @@

Xóa tài sản

-

Xóa tài sản - khỏi danh mục hiện tại? Lịch sử mượn/trả và số lượng còn đang mượn vẫn được giữ trong tài sản cũ.

+

Xóa tài sản - khỏi danh mục hiện tại? Toàn bộ hồ sơ, lịch sử mượn/trả và số lượng còn đang mượn vẫn được giữ trong Tài sản cũ.

diff --git a/test/asset-archive-routes.test.js b/test/asset-archive-routes.test.js index ea408df..5c2f74f 100644 --- a/test/asset-archive-routes.test.js +++ b/test/asset-archive-routes.test.js @@ -121,6 +121,16 @@ test('current and archived inventory are selected separately', async () => { } }); +test('borrow and return history includes requests linked to archived assets', async () => { + const oldBorrow = { BorrowId: 21, AssetId: 17, RequestType: 'borrow', IsArchived: true }; + const oldReturn = { BorrowId: 22, AssetId: 17, RequestType: 'return', IsArchived: true }; + const fixture = createFixture(() => ({ recordset: [oldBorrow, oldReturn] })); + const response = await fixture.invoke('get', '/api/asset-borrows'); + assert.deepEqual(Array.from(response.body.data, row => row.BorrowId), [21, 22]); + assert.match(fixture.queries[0].query, /FROM AssetBorrowRequests br\s+LEFT JOIN AssetInventory ai ON ai.AssetId = br.AssetId/); + assert.doesNotMatch(fixture.queries[0].query, /WHERE\s+(?:br\.|ai\.)?IsArchived\s*=\s*0/i); +}); + test('new borrow requests cannot target an archived asset', async () => { const fixture = createFixture(() => ({ recordset: [{ AssetId: 17, IsArchived: true, Quantity: 10, EndingBalance: 7, diff --git a/test/asset-archive-ui.test.js b/test/asset-archive-ui.test.js index f502c20..2ecfb1b 100644 --- a/test/asset-archive-ui.test.js +++ b/test/asset-archive-ui.test.js @@ -11,13 +11,19 @@ function fixture({ allowed = true, confirmed = true, responseSuccess = true } = const refreshed = []; const notifications = []; const button = { disabled: false }; + const archiveTableBody = { innerHTML: '' }; + const archiveCount = { textContent: '' }; const fetch = async (url, options) => { requests.push({ url, options }); return { ok: responseSuccess, json: async () => ({ success: responseSuccess, data: { archivedCount: 2 } }) }; }; const document = { addEventListener() {}, - getElementById: id => id === 'archiveAllAssetsBtn' ? button : null + getElementById: id => ({ + archiveAllAssetsBtn: button, + archivedAssetBorrowsTableBody: archiveTableBody, + archivedAssetBorrowsCount: archiveCount + })[id] || null }; const context = vm.createContext({ document, window: { fetch }, fetch, console }); vm.runInContext(`${source}\napp = Object.create(AccountManager.prototype); globalThis.manager = app;`, context); @@ -41,7 +47,7 @@ function fixture({ allowed = true, confirmed = true, responseSuccess = true } = notifyFailure: message => notifications.push(message), notifyWarning: message => notifications.push(message) }); - return { manager, requests, refreshed, notifications, button }; + return { manager, requests, refreshed, notifications, button, archiveTableBody, archiveCount }; } function borrow(BorrowId, overrides = {}) { @@ -58,10 +64,14 @@ test('all history retains completed loans and partial loans with related returns manager.assetBorrows = [ borrow(1), borrow(2, { RelatedReturnCount: 1, ReturnedQuantity: 2, RemainingQuantity: 3 }), - borrow(3, { RequestStatus: 'returned', ReturnedQuantity: 5, RemainingQuantity: 0 }), - borrow(4, { RequestType: 'return' }) + borrow(3, { RequestStatus: 'returned', ReturnedQuantity: 5, RemainingQuantity: 0, IsArchived: true }), + borrow(4, { RequestType: 'return', IsArchived: true }) ]; assert.deepEqual(Array.from(manager.getFilteredAssetBorrows(), row => row.BorrowId), [1, 2, 3, 4]); + manager.assetBorrowCatalogFilter = 'archived'; + assert.deepEqual(Array.from(manager.getFilteredAssetBorrows(), row => row.BorrowId), [3, 4]); + manager.assetBorrowSearchTerm = 'tài sản cũ'; + assert.deepEqual(Array.from(manager.getFilteredAssetBorrows(), row => row.BorrowId), [3, 4]); }); test('outstanding loans include archived, partial, and pending-return loans, excluding other statuses', () => { @@ -110,6 +120,23 @@ test('legacy borrower summaries remain visible without any borrow requests or cu assert.deepEqual(Array.from(manager.getCurrentUserBorrowedAssets(), row => [row.AssetId, row.BorrowedQuantityByCurrentUser]), [[1, 2]]); }); +test('old asset list includes every archived record, including assets with no outstanding borrower', () => { + const { manager, archiveTableBody, archiveCount } = fixture(); + manager.archivedAssets = [ + { AssetId: 1, AssetCode: 'BORROWED', AssetName: 'Máy đang mượn', Borrower: [{ name: 'An', quantity: 2 }], ExportInPeriod: 2, IsArchived: true }, + { AssetId: 2, AssetCode: 'IN-STOCK', AssetName: 'Máy trong kho', Borrower: '', ExportInPeriod: 0, IsArchived: true }, + { AssetId: 3, AssetCode: 'RETURNED', AssetName: 'Máy đã trả', Borrower: '', ExportInPeriod: 0, IsArchived: true } + ]; + assert.deepEqual(Array.from(manager.getFilteredArchivedAssets(), row => row.AssetId), [1, 2, 3]); + manager.renderArchivedAssetBorrowsTable(); + assert.match(archiveTableBody.innerHTML, /BORROWED/); + assert.match(archiveTableBody.innerHTML, /IN-STOCK/); + assert.match(archiveTableBody.innerHTML, /RETURNED/); + assert.match(archiveCount.textContent, /3/); + manager.archivedAssetSearchTerm = 'trong kho'; + assert.deepEqual(Array.from(manager.getFilteredArchivedAssets(), row => row.AssetId), [2]); +}); + test('same-code current and archived assets remain separate in my borrowed assets', () => { const { manager } = fixture(); manager.getCurrentUserBorrowerNameKeys = () => ['an'];