update quản lí dự án

This commit is contained in:
2026-09-23 15:54:40 +07:00
parent bd8ba94301
commit 3201a18fd4
15 changed files with 674 additions and 197 deletions

File diff suppressed because one or more lines are too long

View File

@@ -2462,6 +2462,7 @@ class AccountManager {
asset.UsedQuantity,
asset.Department,
asset.Project,
this.formatAssetProjectsText(asset),
asset.Location,
asset.Custodian,
asset.Borrower,
@@ -6633,6 +6634,44 @@ class AccountManager {
this.setupConsumableExportHistoryPagerListeners();
}
getAssetProjectAllocations(asset) {
let rows = asset?.ProjectAllocations || [];
if (typeof rows === 'string') {
try { rows = JSON.parse(rows); } catch { rows = []; }
}
const projects = new Map();
for (const row of Array.isArray(rows) ? rows : []) {
const name = String(row.projectName || '').trim();
const quantity = Number(row.quantity);
if (!name || !Number.isSafeInteger(quantity) || quantity <= 0) continue;
const key = name.toLowerCase();
const existing = projects.get(key) || { projectName: name, quantity: 0 };
existing.quantity += quantity;
projects.set(key, existing);
}
const result = [...projects.values()];
const allocated = result.reduce((sum, row) => sum + row.quantity, 0);
const unattributed = Math.max(this.buildAssetQuantityMetrics(asset).exportInPeriod - allocated, 0);
if (unattributed) result.push({ projectName: 'Chưa xác định dự án', quantity: unattributed });
return result;
}
formatAssetProjectsText(asset) {
return this.getAssetProjectAllocations(asset)
.map(row => `${row.projectName}: ${row.quantity}${asset?.Unit ? ` ${asset.Unit}` : ''}`).join('\n');
}
formatAssetProjectsHtml(asset) {
const rows = this.getAssetProjectAllocations(asset);
return rows.length ? rows.map(row => `<div class="whitespace-nowrap">${this.escapeHtml(row.projectName)}: <strong>${row.quantity}</strong> ${this.escapeHtml(asset?.Unit || '')}</div>`).join('') : '-';
}
getAssetAvailableForExport(asset) {
const metrics = this.buildAssetQuantityMetrics(asset);
const allocated = this.getAssetProjectAllocations(asset).reduce((sum, row) => sum + row.quantity, 0);
return Math.min(metrics.endingBalance, Math.max(metrics.quantity + metrics.importInPeriod - Math.max(metrics.exportInPeriod, metrics.borrowerExportInPeriod, allocated), 0));
}
getAssetMobileCardHtml(asset, rowNumber, canManageAssets) {
const statusMeta = this.getAssetStatusMeta(asset?.Status);
const assetId = Number(asset?.AssetId);
@@ -6659,10 +6698,11 @@ class AccountManager {
<div class="mt-3 grid grid-cols-2 gap-x-3 gap-y-2 border-t border-slate-100 pt-3 text-xs">
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Model</span><span class="text-slate-700">${safe(asset?.Model)}</span></div>
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Serial</span><span class="text-slate-700">${safe(asset?.SerialNumber)}</span></div>
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Tổng tài sản</span><span class="font-bold text-slate-800">${this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod}</span></div>
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Tồn cuối kỳ</span><span class="font-bold text-slate-800">${endingBalance}${unit ? ` ${safe(unit)}` : ''}</span></div>
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Đang mượn</span><span class="font-bold text-slate-800">${borrowedQuantity}${unit ? ` ${safe(unit)}` : ''}</span></div>
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Phòng ban</span><span class="text-slate-700">${safe(asset?.Department)}</span></div>
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Dự án</span><span class="text-slate-700">${safe(asset?.Project)}</span></div>
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Dự án</span><div class="text-slate-700">${this.formatAssetProjectsHtml(asset)}</div></div>
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Vị trí</span><span class="text-slate-700">${safe(asset?.Location)}</span></div>
<div><span class="block text-[10px] font-bold uppercase text-slate-400">Phụ trách</span><span class="text-slate-700">${safe(asset?.Custodian)}</span></div>
</div>
@@ -6788,12 +6828,13 @@ class AccountManager {
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Model</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Serial</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Số lượng (Tồn đầu kỳ)</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tổng tài sản</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Nhập trong kỳ</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Xuất trong kỳ</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Tồn cuối kỳ</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Đơn vị</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Phòng ban</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Dự án</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Dự án đang xuất / SL</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">Người phụ trách</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500" style="width: 132px; min-width: 132px; white-space: nowrap;">Trạng thái</th>
<th class="px-4 py-2.5 text-[10px] font-bold uppercase tracking-wider text-slate-500">SL hàng mới</th>
@@ -6825,12 +6866,13 @@ class AccountManager {
<td class="px-4 py-3 text-sm text-slate-600">${asset.Model || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.SerialNumber || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Quantity || 0}</td>
<td class="px-4 py-3 text-sm font-semibold text-slate-700">${this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.ImportInPeriod ?? 0}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.ExportInPeriod ?? 0}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.EndingBalance ?? 0}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Unit || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Department || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Project || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${this.formatAssetProjectsHtml(asset)}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Custodian || '-'}</td>
<td class="px-4 py-3 text-sm" style="width: 132px; min-width: 132px; white-space: nowrap;">
<span class="inline-block px-2 py-1 rounded text-xs font-semibold ${statusMeta.className}" style="display: inline-block; white-space: nowrap; word-break: keep-all; overflow-wrap: normal;">${statusMeta.label}</span>
@@ -6915,12 +6957,13 @@ class AccountManager {
<td class="px-4 py-3 text-sm text-slate-600">${asset.Model || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.SerialNumber || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Quantity || 0}</td>
<td class="px-4 py-3 text-sm font-semibold text-slate-700">${this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.ImportInPeriod ?? 0}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.ExportInPeriod ?? 0}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.EndingBalance ?? 0}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Unit || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Department || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Project || '-'}</td>
<td class="px-4 py-3 text-sm text-slate-600">${this.formatAssetProjectsHtml(asset)}</td>
<td class="px-4 py-3 text-sm text-slate-600">${asset.Custodian || '-'}</td>
<td class="px-4 py-3 text-sm" style="width: 132px; min-width: 132px; white-space: nowrap;">
<span class="inline-block px-2 py-1 rounded text-xs font-semibold ${statusMeta.className}" style="display: inline-block; white-space: nowrap; word-break: keep-all; overflow-wrap: normal;">${statusMeta.label}</span>
@@ -7299,6 +7342,7 @@ class AccountManager {
['Model', asset?.Model],
['Sd serial', asset?.SerialNumber],
['Số lượng (Tồn đầu kỳ)', `${asset?.Quantity || 0} ${asset?.Unit || ''}`.trim()],
['Tổng tài sản', this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod],
['Nhập trong kỳ', asset?.ImportInPeriod ?? 0],
['Xuất trong kỳ', asset?.ExportInPeriod ?? 0],
['Tồn cuối kỳ', asset?.EndingBalance ?? 0],
@@ -7306,7 +7350,7 @@ class AccountManager {
['SL đã qua sử dụng', asset?.UsedQuantity ?? 0],
['SL đang mượn', asset?.ExportInPeriod ?? 0],
['Phòng ban', asset?.Department],
['Dự án', asset?.Project],
['Dự án đang xuất / Số lượng', this.formatAssetProjectsHtml(asset)],
['Vị trí', asset?.Location],
['Người phụ trách', asset?.Custodian],
['Ngày mua', this.formatDateOnly(asset?.PurchaseDate)],
@@ -7809,6 +7853,49 @@ class AccountManager {
}
}
collectBorrowProjectAllocations() {
const rows = [{
projectName: String(document.getElementById('borrowAssetProjectInput')?.value || '').trim(),
quantity: Number(document.getElementById('borrowQuantityInput')?.value)
}];
document.querySelectorAll('#borrowAdditionalProjects .borrow-project-row').forEach(row => {
rows.push({ projectName: row.querySelector('select').value.trim(), quantity: Number(row.querySelector('input').value) });
});
return rows;
}
updateBorrowAllocationSummary() {
const total = this.collectBorrowProjectAllocations().reduce((sum, row) => sum + (Number.isFinite(row.quantity) ? row.quantity : 0), 0);
const available = Number(document.getElementById('borrowCurrentEndingInput')?.value) || 0;
const summary = document.getElementById('borrowAllocationSummary');
if (summary) {
summary.textContent = `Tổng xuất: ${total} / ${available} có thể xuất · Còn lại: ${Math.max(available - total, 0)}${total > available ? ' — Vượt số lượng cho phép' : ''}`;
summary.classList.toggle('text-red-600', total > available);
}
}
addBorrowProjectRow() {
const container = document.getElementById('borrowAdditionalProjects');
if (!container || container.children.length >= 99) return;
const row = document.createElement('div');
row.className = 'borrow-project-row flex items-end gap-2';
row.innerHTML = `
<label class="flex-1 text-xs text-slate-600">Dự án
<select class="w-full border border-slate-200 rounded-lg text-sm py-2 px-3" required>
<option value="">-- Chọn dự án --</option>
${this.getUniqueAssetProjectNames().map(name => `<option value="${this.escapeHtml(name)}">${this.escapeHtml(name)}</option>`).join('')}
</select>
</label>
<label class="text-xs text-slate-600" style="width: 100px;">Số lượng
<input type="number" min="1" step="1" value="1" class="w-full border border-slate-200 rounded-lg text-sm py-2 px-3" required>
</label>
<button type="button" class="px-2 py-2 text-sm text-red-600" aria-label="Xóa dòng dự án">Xóa</button>`;
row.querySelector('input').max = document.getElementById('borrowCurrentEndingInput').value;
row.querySelector('button').addEventListener('click', () => { row.remove(); this.updateBorrowAllocationSummary(); });
container.appendChild(row);
this.updateBorrowAllocationSummary();
}
async openBorrowAssetModal() {
if (!this.ensureAssetManagePermission('xuat tai san')) {
return;
@@ -7827,7 +7914,8 @@ class AccountManager {
}
const metrics = this.buildAssetQuantityMetrics(asset);
if (metrics.endingBalance <= 0) {
const available = this.getAssetAvailableForExport(asset);
if (available <= 0) {
this.notifyWarning('Tài sản đã hết tồn cuối kỳ, không thể xuất thêm.');
return;
}
@@ -7853,14 +7941,20 @@ class AccountManager {
}
assetNameInput.value = `${asset.AssetCode || ''} - ${asset.AssetName || ''}`.trim();
endingInput.value = String(metrics.endingBalance);
endingInput.value = String(available);
quantityInput.value = '1';
quantityInput.min = '1';
quantityInput.max = String(metrics.endingBalance);
quantityInput.max = String(available);
this.refreshBorrowAssetUserOptions('');
this.refreshBorrowAssetProjectOptions(String(asset?.Project || '').trim());
noteInput.value = '';
document.getElementById('borrowAdditionalProjects').innerHTML = '';
document.getElementById('borrowAssetStockSummary').textContent =
`Tổng tài sản: ${metrics.quantity + metrics.importInPeriod} · Đang xuất: ${metrics.exportInPeriod} · Có thể xuất: ${available}`;
document.getElementById('addBorrowProjectBtn').onclick = () => this.addBorrowProjectRow();
document.getElementById('borrowAssetForm').oninput = () => this.updateBorrowAllocationSummary();
this.updateBorrowAllocationSummary();
if (borrowByInput) {
borrowByInput.value = this.getCurrentUserDisplayName();
@@ -7882,8 +7976,6 @@ class AccountManager {
const assetIdInput = document.getElementById('borrowAssetIdInput');
const borrowerInput = document.getElementById('borrowAssetUserInput');
const projectInput = document.getElementById('borrowAssetProjectInput');
const quantityInput = document.getElementById('borrowQuantityInput');
const noteInput = document.getElementById('borrowAssetNoteInput');
const selectedAssetId = Number(assetIdInput?.value || this.pendingBorrowAssetId);
@@ -7904,30 +7996,33 @@ class AccountManager {
return;
}
const projectName = String(projectInput?.value || '').trim();
if (!projectName) {
this.notifyWarning('Vui lòng chọn dự án cần xuất.');
const allocations = this.collectBorrowProjectAllocations();
if (allocations.some(row => !row.projectName)) {
this.notifyWarning('Vui lòng chọn dự án cho tất cả các dòng.');
return;
}
const borrowQuantity = this.parseNonNegativeInteger(quantityInput?.value ?? 0, 0);
if (borrowQuantity <= 0) {
this.notifyWarning('Số lượng xuất phải lớn hơn 0.');
if (allocations.some(row => !Number.isSafeInteger(row.quantity) || row.quantity <= 0)) {
this.notifyWarning('Số lượng của mỗi dự án phải là số nguyên dương.');
return;
}
const borrowQuantity = allocations.reduce((sum, row) => sum + row.quantity, 0);
const exportNote = String(noteInput?.value || '').trim();
const currentMetrics = this.buildAssetQuantityMetrics(asset);
if (currentMetrics.endingBalance <= 0) {
const available = this.getAssetAvailableForExport(asset);
if (available <= 0) {
this.notifyWarning('Tài sản đã hết tồn cuối kỳ, không thể xuất thêm.');
return;
}
if (borrowQuantity > currentMetrics.endingBalance) {
this.notifyWarning(`Số lượng xuất (${borrowQuantity}) vượt quá tồn cuối kỳ (${currentMetrics.endingBalance}).`);
if (borrowQuantity > available) {
this.notifyWarning(`Số lượng xuất (${borrowQuantity}) vượt quá tồn cuối kỳ (${available}).`);
return;
}
if (this.assetExportSubmitting) return;
this.assetExportSubmitting = true;
const submitButton = document.querySelector('#borrowAssetForm button[type="submit"]');
if (submitButton) submitButton.disabled = true;
try {
const response = await fetch(`${this.apiBase}/assets/${selectedAssetId}/export`, {
method: 'POST',
@@ -7936,7 +8031,7 @@ class AccountManager {
quantity: borrowQuantity,
borrowerName,
custodianName: borrowerName,
projectName,
allocations,
note: exportNote
})
});
@@ -7959,6 +8054,9 @@ class AccountManager {
} catch (err) {
console.error(err);
this.notifyFailure('Xuất tài sản thất bại');
} finally {
this.assetExportSubmitting = false;
if (submitButton) submitButton.disabled = false;
}
}
@@ -10128,12 +10226,14 @@ class AccountManager {
'Model': asset.Model || '',
'Serial Number': asset.SerialNumber || '',
'Quantity': asset.Quantity || 0,
'Tổng tài sản': this.buildAssetQuantityMetrics(asset).quantity + this.buildAssetQuantityMetrics(asset).importInPeriod,
'Import In Period': asset.ImportInPeriod ?? 0,
'Export In Period': asset.ExportInPeriod ?? 0,
'Ending Balance': asset.EndingBalance ?? 0,
'Unit': asset.Unit || '',
'Department': asset.Department || '',
'Project': asset.Project || '',
'Dự án đang xuất / Số lượng': this.formatAssetProjectsText(asset),
'Location': asset.Location || '',
'Custodian': asset.Custodian || '',
'Borrower': asset.Borrower || '',

View File

@@ -260,7 +260,7 @@
</select>
</div>
<div>
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Dự án</label>
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Dự án mặc định</label>
<select id="assetProjectInput" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3">
<option value="">-- Chọn dự án --</option>
</select>
@@ -688,43 +688,49 @@
<span class="material-symbols-outlined">close</span>
</button>
</div>
<form id="borrowAssetForm" class="p-6 space-y-4">
<form id="borrowAssetForm" class="p-6 space-y-4 overflow-y-auto" style="max-height: 80vh;">
<input type="hidden" id="borrowAssetIdInput">
<p id="borrowAssetStockSummary" class="text-sm font-semibold text-slate-700"></p>
<div>
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Tài sản</label>
<input type="text" id="borrowAssetNameInput" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3 bg-slate-50" readonly>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Tồn cuối kỳ hiện tại</label>
<label for="borrowCurrentEndingInput" class="text-xs font-bold text-slate-500 block mb-1">Còn có thể xuất</label>
<input type="number" id="borrowCurrentEndingInput" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3 bg-slate-50" readonly>
</div>
<div>
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Số lượng xuất</label>
<input type="number" id="borrowQuantityInput" min="1" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3" value="1" required>
</div>
<div>
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Người mượn</label>
<label for="borrowAssetUserInput" class="text-xs font-bold text-slate-500 block mb-1">Người mượn</label>
<select id="borrowAssetUserInput" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3" required>
<option value="">-- Chọn người mượn --</option>
</select>
</div>
<div>
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Xuất cho dự án</label>
<select id="borrowAssetProjectInput" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3" required>
<option value="">-- Chọn dự án --</option>
</select>
<div class="md:col-span-2 space-y-3">
<div class="flex items-end gap-2">
<label for="borrowAssetProjectInput" class="flex-1 text-xs text-slate-600">Dự án
<select id="borrowAssetProjectInput" class="w-full border border-slate-200 rounded-lg text-sm py-2 px-3" required>
<option value="">-- Chọn dự án --</option>
</select>
</label>
<label for="borrowQuantityInput" class="text-xs text-slate-600" style="width: 100px;">Số lượng
<input type="number" id="borrowQuantityInput" min="1" step="1" class="w-full border border-slate-200 rounded-lg text-sm py-2 px-3" value="1" required>
</label>
</div>
<div id="borrowAdditionalProjects" class="space-y-3"></div>
<button type="button" id="addBorrowProjectBtn" class="mt-2 px-3 py-2 text-xs font-bold text-primary border border-slate-200 rounded-lg">+ Thêm dự án</button>
<p id="borrowAllocationSummary" class="mt-2 text-sm font-semibold" aria-live="polite"></p>
</div>
<div>
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Người xuất</label>
<label for="borrowByInput" class="text-xs font-bold text-slate-500 block mb-1">Người xuất</label>
<input type="text" id="borrowByInput" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3 bg-slate-50" readonly>
</div>
<div class="md:col-span-2">
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Role người thao tác</label>
<div>
<label for="borrowRoleInput" class="text-xs font-bold text-slate-500 block mb-1">Role người thao tác</label>
<input type="text" id="borrowRoleInput" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3 bg-slate-50" readonly>
</div>
<div class="md:col-span-2">
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Ghi chú xuất</label>
<label for="borrowAssetNoteInput" class="text-xs font-bold text-slate-500 block mb-1">Ghi chú xuất</label>
<textarea id="borrowAssetNoteInput" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3 h-20 resize-none" placeholder="Nhập ghi chú (nếu có)"></textarea>
</div>
</div>