fix mượn trả

This commit is contained in:
2026-05-15 10:05:42 +07:00
parent 4aa4ac0d57
commit e1b553ba79
2 changed files with 253 additions and 1 deletions

View File

@@ -3018,6 +3018,7 @@ class AccountManager {
const returnedQuantity = this.parseNonNegativeInteger(item?.ReturnedQuantity, 0);
const borrowQuantity = this.parseNonNegativeInteger(item?.BorrowQuantity, 0);
const remainingQuantity = this.parseNonNegativeInteger(item?.RemainingQuantity, Math.max(borrowQuantity - returnedQuantity, 0));
const canCreateReturn = this.canCreateAssetReturnRequestFromBorrow(item);
const returnProgressHtml = requestType === 'borrow' && returnedQuantity > 0 && statusMeta.value !== 'returned'
? `<div class="mt-1 text-[11px] text-slate-500 whitespace-nowrap">Đã trả ${returnedQuantity}/${borrowQuantity}, còn ${remainingQuantity}</div>`
: '';
@@ -3031,8 +3032,17 @@ class AccountManager {
<span class="material-symbols-outlined text-base">info</span>
</button>
`;
const returnActionHtml = canCreateReturn
? `<button class="asset-borrow-return-btn inline-flex items-center gap-1.5 px-3 py-1.5 rounded-md bg-emerald-600 hover:bg-emerald-700 text-white text-xs font-bold whitespace-nowrap" data-request-id="${requestId}" title="Tạo đơn trả từ đơn mượn này">
<span class="material-symbols-outlined text-sm">assignment_return</span>
Trả tài sản
</button>`
: '';
const cancelActionHtml = canCancel
? `<button class="asset-borrow-cancel-btn px-3 py-1.5 rounded-md bg-red-600 hover:bg-red-700 text-white text-xs font-bold whitespace-nowrap" data-request-id="${requestId}">Hủy đơn</button>`
: '';
const actionHtml = returnActionHtml || cancelActionHtml
? `<div class="flex items-center gap-2">${returnActionHtml}${cancelActionHtml}</div>`
: `<span class="text-xs text-slate-400">-</span>`;
return `
@@ -3056,11 +3066,28 @@ class AccountManager {
<td class="px-4 py-3 text-sm text-slate-600 max-w-xs">${this.escapeHtml(note || '-')}</td>
<td class="px-4 py-3 text-sm text-slate-600 max-w-xs">${this.escapeHtml(rejectReason || '-')}</td>
<td class="px-4 py-3 text-sm text-slate-600 text-center">${detailActionHtml}</td>
<td class="px-4 py-3 text-sm text-slate-600">${cancelActionHtml}</td>
<td class="px-4 py-3 text-sm text-slate-600">${actionHtml}</td>
</tr>
`;
}
canCreateAssetReturnRequestFromBorrow(item) {
if (this.normalizeAssetRequestType(item?.RequestType) !== 'borrow') {
return false;
}
if (this.normalizeAssetRequestStatus(item?.RequestStatus) !== 'approved') {
return false;
}
const borrowQuantity = this.parseNonNegativeInteger(item?.BorrowQuantity, 0);
const returnedQuantity = this.parseNonNegativeInteger(item?.ReturnedQuantity, 0);
const remainingQuantity = this.parseNonNegativeInteger(item?.RemainingQuantity, Math.max(borrowQuantity - returnedQuantity, 0));
const relatedReturnCount = this.parseNonNegativeInteger(item?.RelatedReturnCount, 0);
return borrowQuantity > 0 && remainingQuantity > 0 && relatedReturnCount <= 0;
}
canCurrentUserCancelAssetRequest(item) {
const status = this.normalizeAssetRequestStatus(item?.RequestStatus);
if (status !== 'pending') {
@@ -3577,6 +3604,15 @@ class AccountManager {
return;
}
const returnButton = event.target.closest('.asset-borrow-return-btn');
if (returnButton) {
const requestId = Number(returnButton.dataset.requestId);
if (Number.isFinite(requestId) && requestId > 0) {
this.createAssetReturnRequestFromBorrow(requestId, returnButton);
}
return;
}
const cancelButton = event.target.closest('.asset-borrow-cancel-btn');
if (!cancelButton) {
return;
@@ -3601,6 +3637,61 @@ class AccountManager {
this.setupAssetBorrowPagerListeners();
}
async createAssetReturnRequestFromBorrow(requestId, sourceButton = null) {
const targetId = Number(requestId);
if (!Number.isFinite(targetId) || targetId <= 0) {
this.notifyWarning('Không xác định được đơn mượn cần trả.');
return;
}
if (sourceButton) {
sourceButton.disabled = true;
sourceButton.classList.add('opacity-60', 'cursor-not-allowed');
}
try {
const response = await fetch(`${this.apiBase}/asset-borrows/${targetId}/return`, {
method: 'POST',
headers: this.getAuthHeaders(false)
});
const data = await response.json();
if (!response.ok || !data.success) {
this.notifyFailure(data.message || 'Tạo đơn trả tài sản thất bại');
return;
}
this.notifySuccess('Đã tạo đơn trả tài sản. Đơn đang chờ xử lý.');
await this.fetchAssetBorrows();
await this.fetchAssets();
if (this.currentPage === 'asset-borrows') {
this.renderAssetBorrowsTableBody();
}
if (this.currentPage === 'assets') {
this.renderAssetsTableBody();
}
if (this.currentPage === 'my-borrowed-assets') {
this.renderMyBorrowedAssetsTableBody();
}
const pendingModal = document.getElementById('assetPendingRequestsModal');
if (pendingModal?.classList.contains('open')) {
this.renderPendingAssetRequestsModal();
}
this.updatePendingAssetRequestsBadge();
} catch (err) {
console.error(err);
this.notifyFailure('Tạo đơn trả tài sản thất bại');
} finally {
if (sourceButton && document.body.contains(sourceButton)) {
sourceButton.disabled = false;
sourceButton.classList.remove('opacity-60', 'cursor-not-allowed');
}
}
}
async refreshAssetBorrowsUI() {
await this.fetchAssetBorrows();
if (this.currentPage === 'asset-borrows') {