Compare commits

...

2 Commits

Author SHA1 Message Date
bd8ba94301 fix UI 2026-09-23 15:14:54 +07:00
cdaa56febd fix - ASS-OLD 2026-09-23 09:59:41 +07:00
5 changed files with 98 additions and 32 deletions

View File

@@ -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

View File

@@ -117,7 +117,7 @@ class AccountManager {
this.assetBorrows = [];
this.assetBorrowSearchTerm = '';
this.assetBorrowTypeFilter = '';
this.assetBorrowCatalogFilter = '';
this.assetBorrowCatalogFilter = 'current';
this.myBorrowedAssetSearchTerm = '';
this.assetBorrowProductSearchTimer = undefined;
this.assetBorrowProductItems = [];
@@ -312,6 +312,10 @@ class AccountManager {
}
renderView(page) {
if (page === 'asset-borrows' && this.currentPage !== 'asset-borrows') {
this.assetBorrowCatalogFilter = 'current';
this.assetBorrowPage = 1;
}
this.currentPage = page;
const mainContent = document.getElementById('mainContent');
if (!mainContent) return;
@@ -2896,6 +2900,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 +5077,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"
>
<span class="material-symbols-outlined text-base">history</span>
Tài sản cũ còn mượn
Tài sản cũ
</button>
<button
id="addAssetBorrowRequestBtn"
@@ -5808,8 +5813,8 @@ class AccountManager {
const countNode = document.getElementById('bulkDeleteAssetsConfirmCount');
const titleNode = document.getElementById('bulkDeleteAssetsConfirmTitle');
const message = archiveAll
? 'Xóa toàn bộ danh mục tài sản hiện tại để nhập dữ liệu mới? Lịch sử và số lượng còn đang mượn được giữ trong tài sản cũ. Dữ liệu nhập mới được quản lý riêng, kể cả khi trùng mã tài sản.'
: `Xóa ${selectedCount} tài sản đã chọ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 toàn bộ danh mục tài sản hiện tại để nhập dữ liệu mới? Toàn bộ hồ sơ được lưu trong Tài sản cũ, gồm cả lịch sử và số lượng còn đang mượn. Dữ liệu nhập mới được quản lý riêng, kể cả khi trùng mã tài sản.'
: `Xóa ${selectedCount} tài sản đã chọ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ũ.`;
if (!modal || !messageNode || !countNode) {
return window.confirm(message);
@@ -6714,7 +6719,7 @@ class AccountManager {
</button>
<button id="openArchivedAssetBorrowsBtn" class="border border-amber-300 text-amber-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 hover:bg-amber-50">
<span class="material-symbols-outlined text-base">history</span>
Tài sản cũ còn mượn
Tài sản cũ
</button>
<button id="importAssetBtn" class="border border-slate-300 text-slate-700 px-3 py-2 rounded-lg text-xs font-bold flex items-center gap-1.5 transition-all active:scale-95 ${canManageAssets ? 'hover:bg-slate-100' : 'opacity-50 cursor-not-allowed'}" ${canManageAssets ? '' : 'disabled'}>
<span class="material-symbols-outlined text-base">upload_file</span>
@@ -7201,7 +7206,7 @@ class AccountManager {
}
}
getArchivedOutstandingAssets() {
getFilteredArchivedAssets() {
const search = String(this.archivedAssetSearchTerm || '').trim().toLowerCase();
return (this.archivedAssets || []).map(asset => {
const borrowerQuantity = this.parseBorrowerEntries(asset.Borrower)
@@ -7210,28 +7215,38 @@ class AccountManager {
...asset,
OutstandingQuantity: Math.max(borrowerQuantity, this.parseNonNegativeInteger(asset.ExportInPeriod, 0))
};
}).filter(asset => asset.OutstandingQuantity > 0 && (!search || [
asset.AssetCode, asset.AssetName, asset.Model, asset.SerialNumber,
this.formatBorrowerSummaryText(asset.Borrower), asset.Department, asset.Project, asset.Location
].some(value => String(value || '').toLowerCase().includes(search))));
}).filter(asset => !search || [
asset.AssetId, asset.AssetCode, asset.AssetName, asset.Model, asset.SerialNumber,
this.formatBorrowerSummaryText(asset.Borrower), asset.Department, asset.Project,
asset.Location, asset.Custodian, asset.Notes, this.formatDateOnly(asset.ArchivedDate)
].some(value => String(value || '').toLowerCase().includes(search)));
}
getArchivedOutstandingAssets() {
return this.getFilteredArchivedAssets().filter(asset => asset.OutstandingQuantity > 0);
}
renderArchivedAssetBorrowsTable() {
const tableBody = document.getElementById('archivedAssetBorrowsTableBody');
if (!tableBody) return;
const rows = this.getArchivedOutstandingAssets();
const rows = this.getFilteredArchivedAssets();
tableBody.innerHTML = rows.length ? rows.map(asset => `
<tr class="border-b border-slate-100">
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.AssetCode || '-')}<div class="text-[11px] text-slate-500">Hồ sơ cũ #${Number(asset.AssetId) || 0}</div></td>
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.AssetName || '-')}<div class="text-[11px] text-slate-500">${this.escapeHtml(asset.SerialNumber || '')}</div></td>
<td class="px-4 py-3 text-sm text-slate-700 whitespace-pre-line">${this.escapeHtml(this.formatBorrowerSummaryText(asset.Borrower) || 'Chưa có tên người mượn')}</td>
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.AssetName || '-')}<div class="text-[11px] text-slate-500">${this.escapeHtml([asset.Model, asset.SerialNumber].filter(Boolean).join(' · '))}</div></td>
<td class="px-4 py-3 text-sm text-slate-600 whitespace-nowrap">${this.escapeHtml(this.formatDateTime(asset.ArchivedDate))}</td>
<td class="px-4 py-3 text-sm text-slate-700">${this.parseNonNegativeInteger(asset.Quantity, 0)}</td>
<td class="px-4 py-3 text-sm text-slate-700">${this.parseNonNegativeInteger(asset.ImportInPeriod, 0)}</td>
<td class="px-4 py-3 text-sm text-slate-700">${this.parseNonNegativeInteger(asset.ExportInPeriod, 0)}</td>
<td class="px-4 py-3 text-sm font-bold text-slate-700">${this.parseNonNegativeInteger(asset.EndingBalance, 0)}</td>
<td class="px-4 py-3 text-sm font-bold text-slate-700">${asset.OutstandingQuantity}</td>
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.Unit || '-')}</td>
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml(asset.Project || asset.Department || '-')}</td>
<td class="px-4 py-3 text-sm text-slate-700 whitespace-pre-line">${this.escapeHtml(this.formatBorrowerSummaryText(asset.Borrower) || '-')}</td>
<td class="px-4 py-3 text-sm text-slate-700">${this.escapeHtml([asset.Department, asset.Project].filter(Boolean).join(' / ') || '-')}</td>
</tr>
`).join('') : '<tr><td colspan="6" class="px-4 py-8 text-center text-sm text-slate-500">Không có tài sản cũ còn mượn phù hợp.</td></tr>';
`).join('') : '<tr><td colspan="11" class="px-4 py-8 text-center text-sm text-slate-500">Không có tài sản cũ phù hợp.</td></tr>';
const count = document.getElementById('archivedAssetBorrowsCount');
if (count) count.textContent = `${rows.length} hồ sơ tài sản cũ còn mượn`;
if (count) count.textContent = `${rows.length}/${(this.archivedAssets || []).length} hồ sơ tài sản cũ`;
}
async openArchivedAssetBorrowsModal() {

View File

@@ -1033,25 +1033,25 @@
</div>
</div>
<!-- Archived asset borrower balances, including imports and direct exports. -->
<!-- All archived asset records, including those with no outstanding borrowers. -->
<div class="modal-backdrop fixed inset-0 z-[100] flex items-center justify-center bg-black/40 backdrop-blur-sm" id="archivedAssetBorrowsModal">
<div class="modal-content bg-white rounded-xl shadow-2xl border border-slate-200 overflow-hidden flex flex-col m-4" style="width:min(1100px, calc(100vw - 32px));max-height:calc(100vh - 32px);">
<div class="modal-content bg-white rounded-xl shadow-2xl border border-slate-200 overflow-hidden flex flex-col m-3" style="width:min(1600px, calc(100vw - 24px));max-height:calc(100vh - 24px);">
<div class="px-6 py-4 border-b border-slate-100 bg-slate-50 flex items-center justify-between gap-3">
<h3 class="text-base font-extrabold text-slate-800">Tài sản cũ còn mượn</h3>
<h3 class="text-base font-extrabold text-slate-800">Tài sản cũ</h3>
<button type="button" class="p-1.5 rounded-lg hover:bg-slate-200 text-slate-500" data-close-modal="archivedAssetBorrowsModal" aria-label="Đóng">
<span class="material-symbols-outlined">close</span>
</button>
</div>
<div class="px-6 py-4 border-b border-slate-100">
<p class="text-sm text-slate-600">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.</p>
<p class="mt-2 text-xs text-slate-500">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.</p>
<label for="archivedAssetBorrowsSearch" class="mt-3 block text-xs font-bold text-slate-600">Tìm tài sản hoặc người mượn</label>
<input id="archivedAssetBorrowsSearch" class="mt-1 w-full rounded-lg border border-slate-200 px-3 py-2 text-sm" placeholder="Mã, tên, serial, người mượn, dự án...">
<p class="text-sm text-slate-600">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.</p>
<p class="mt-2 text-xs text-slate-500">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.</p>
<label for="archivedAssetBorrowsSearch" class="mt-3 block text-xs font-bold text-slate-600">Tìm tài sản cũ</label>
<input id="archivedAssetBorrowsSearch" class="mt-1 w-full rounded-lg border border-slate-200 px-3 py-2 text-sm" placeholder="Mã, tên, model, serial, người mượn, dự án...">
</div>
<div class="overflow-auto flex-1">
<table class="w-full text-left border-collapse" style="min-width:780px;">
<table class="w-full text-left border-collapse" style="min-width:1450px;">
<thead class="sticky top-0 bg-slate-50 text-xs font-bold text-slate-500">
<tr><th class="px-4 py-3">Mã</th><th class="px-4 py-3">Tài sản</th><th class="px-4 py-3">Người mượn</th><th class="px-4 py-3">Còn mượn</th><th class="px-4 py-3">Đơn vị</th><th class="px-4 py-3">Dự án / Phòng ban</th></tr>
<tr><th class="px-4 py-3">Mã</th><th class="px-4 py-3">Tài sản</th><th class="px-4 py-3">Ngày xóa</th><th class="px-4 py-3">Tồn đầu</th><th class="px-4 py-3">Nhập</th><th class="px-4 py-3">Xuất</th><th class="px-4 py-3">Tồn cuối</th><th class="px-4 py-3">Còn mượn</th><th class="px-4 py-3">Đơn vị</th><th class="px-4 py-3">Người mượn</th><th class="px-4 py-3">Phòng ban / Dự án</th></tr>
</thead>
<tbody id="archivedAssetBorrowsTableBody"></tbody>
</table>
@@ -1071,7 +1071,7 @@
<h3 class="text-base font-extrabold text-red-700">Xóa tài sản</h3>
</div>
<div class="p-6">
<p class="text-sm text-slate-600 mb-6">Xóa tài sản <strong id="deleteAssetName">-</strong> 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ũ.</p>
<p class="text-sm text-slate-600 mb-6">Xóa tài sản <strong id="deleteAssetName">-</strong> 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ũ.</p>
<div class="flex gap-3">
<button type="button" class="flex-1 px-4 py-2 text-xs font-bold text-slate-600 border border-slate-200 rounded-lg" data-close-modal="deleteAssetModal">Hủy</button>
<button type="button" class="flex-1 px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg text-xs font-bold confirm-delete-asset">Xóa</button>

View File

@@ -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,

View File

@@ -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,28 @@ 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('opening borrow and return page selects current catalog without changing an open page filter', () => {
const { manager } = fixture();
manager.currentPage = 'assets';
manager.assetBorrowCatalogFilter = 'archived';
manager.assetBorrowPage = 3;
manager.renderView('asset-borrows');
assert.equal(manager.assetBorrowCatalogFilter, 'current');
assert.equal(manager.assetBorrowPage, 1);
manager.assetBorrowCatalogFilter = 'archived';
manager.renderView('asset-borrows');
assert.equal(manager.assetBorrowCatalogFilter, 'archived');
});
test('outstanding loans include archived, partial, and pending-return loans, excluding other statuses', () => {
@@ -110,6 +134,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'];