fix mươn tài sản đã xuất

This commit is contained in:
2026-05-15 10:17:40 +07:00
parent cb4d5b9520
commit 12c6380ceb
2 changed files with 80 additions and 15 deletions

View File

@@ -525,15 +525,40 @@ class AccountManager {
return `${code} - ${name}`.replace(/^\s*-\s*|\s*-\s*$/g, '').trim() || name || '-- Chọn tài sản --';
}
isBorrowAssetRequestMode() {
const typeInput = document.getElementById('assetBorrowRequestTypeInput');
return this.normalizeAssetRequestType(typeInput?.value || this.assetBorrowRequestType) === 'borrow';
}
isAssetAvailableForBorrow(asset) {
if (!asset) {
return false;
}
const endingBalance = this.parseOptionalNonNegativeInteger(asset?.EndingBalance ?? asset?.endingBalance);
if (endingBalance !== null) {
return endingBalance > 0;
}
const status = String(asset?.Status || asset?.status || '').trim().toLowerCase();
return status !== 'exported';
}
getAssetBorrowProductById(assetIdValue) {
const assetId = Number(assetIdValue);
if (!Number.isFinite(assetId) || assetId <= 0) {
return null;
}
return this.assetBorrowProductItems.find(item => Number(item?.AssetId) === assetId)
const asset = this.assetBorrowProductItems.find(item => Number(item?.AssetId) === assetId)
|| this.assets.find(item => Number(item?.AssetId) === assetId)
|| null;
if (this.isBorrowAssetRequestMode() && !this.isAssetAvailableForBorrow(asset)) {
return null;
}
return asset;
}
updateAssetBorrowProductDisplay(assetIdValue) {
@@ -654,9 +679,12 @@ class AccountManager {
const encodedKeyword = encodeURIComponent(this.assetBorrowProductQuery);
const offset = this.assetBorrowProductOffset;
const limit = this.assetBorrowProductLimit;
const borrowableOnly = this.isBorrowAssetRequestMode();
const appendRows = (rows = [], hasMore = false) => {
const source = Array.isArray(rows) ? rows : [];
const rowsArray = Array.isArray(rows) ? rows : [];
const source = rowsArray
.filter(asset => !borrowableOnly || this.isAssetAvailableForBorrow(asset));
if (source.length) {
const merged = new Map(
this.assetBorrowProductItems.map(item => [String(item.AssetId), item])
@@ -665,8 +693,8 @@ class AccountManager {
merged.set(String(item.AssetId), item);
});
this.assetBorrowProductItems = Array.from(merged.values());
this.assetBorrowProductOffset += source.length;
}
this.assetBorrowProductOffset += rowsArray.length;
this.assetBorrowProductHasMore = Boolean(hasMore);
this.assetBorrowProductLoading = false;
@@ -686,6 +714,10 @@ class AccountManager {
const source = Array.isArray(this.assets) ? this.assets : [];
const normalized = this.assetBorrowProductQuery.toLowerCase();
const filtered = source.filter(asset => {
if (borrowableOnly && !this.isAssetAvailableForBorrow(asset)) {
return false;
}
if (!normalized) {
return true;
}
@@ -705,7 +737,7 @@ class AccountManager {
};
try {
const response = await fetch(`${this.apiBase}/assets/search?q=${encodedKeyword}&limit=${limit}&offset=${offset}`, {
const response = await fetch(`${this.apiBase}/assets/search?q=${encodedKeyword}&limit=${limit}&offset=${offset}&borrowableOnly=${borrowableOnly ? '1' : '0'}`, {
headers: this.getAuthHeaders(false)
});
const data = await response.json();
@@ -3773,7 +3805,7 @@ class AccountManager {
await this.searchAssetBorrowProducts('', '', { reset: true });
if (!this.assetBorrowProductItems.length) {
this.notifyWarning('Hiện chưa có tài sản để tạo đơn.');
this.notifyWarning(isReturnRequest ? 'Hiện chưa có tài sản để tạo đơn trả.' : 'Hiện chưa có tài sản còn tồn cuối kỳ để tạo đơn mượn.');
return;
}
@@ -3805,6 +3837,19 @@ class AccountManager {
return;
}
const selectedAsset = this.assetBorrowProductItems.find(item => Number(item?.AssetId) === assetId)
|| this.assets.find(item => Number(item?.AssetId) === assetId)
|| null;
if (requestType === 'borrow' && selectedAsset && !this.isAssetAvailableForBorrow(selectedAsset)) {
this.notifyWarning('Tài sản đã xuất hoặc hết tồn cuối kỳ, không thể tạo đơn mượn.');
await this.searchAssetBorrowProducts(
document.getElementById('assetBorrowProductSearchInput')?.value || '',
'',
{ reset: true }
);
return;
}
const borrowDate = String(dateInput?.value || '').trim() || this.toDateInputValue(new Date());
const unit = String(unitInput?.value || '').trim();
const borrowerName = String(requesterInput?.value || this.getCurrentUserDisplayName() || '').trim();