fix mươn tài sản đã xuất
This commit is contained in:
@@ -4000,6 +4000,8 @@ app.post('/api/asset-borrows', async (req, res) => {
|
||||
AssetName,
|
||||
Quantity,
|
||||
ImportInPeriod,
|
||||
ExportInPeriod,
|
||||
EndingBalance,
|
||||
Borrower,
|
||||
Unit
|
||||
FROM AssetInventory
|
||||
@@ -4015,10 +4017,12 @@ app.post('/api/asset-borrows', async (req, res) => {
|
||||
const currentBorrowed = currentBorrowedEntries.reduce((sum, entry) => (
|
||||
sum + parseNonNegativeInteger(entry?.quantity, 0)
|
||||
), 0);
|
||||
const endingBalance = Math.max(
|
||||
const derivedEndingBalance = Math.max(
|
||||
parseNonNegativeInteger(asset.Quantity, 0) + parseNonNegativeInteger(asset.ImportInPeriod, 0) - currentBorrowed,
|
||||
0
|
||||
);
|
||||
const storedEndingBalance = parseOptionalNonNegativeInteger(asset.EndingBalance);
|
||||
const endingBalance = storedEndingBalance !== null ? storedEndingBalance : derivedEndingBalance;
|
||||
|
||||
const unit = String(req.body?.unit || '').trim() || String(asset.Unit || '').trim() || null;
|
||||
|
||||
@@ -4836,12 +4840,14 @@ app.get('/api/assets/search', async (req, res) => {
|
||||
const keywordLike = `%${rawKeyword}%`;
|
||||
const limit = Math.min(parsePositiveInteger(req.query.limit, 80), 200);
|
||||
const offset = parseNonNegativeInteger(req.query.offset, 0);
|
||||
const borrowableOnly = ['1', 'true', 'yes'].includes(String(req.query.borrowableOnly || '').trim().toLowerCase());
|
||||
|
||||
const result = await pool.request()
|
||||
.input('limit', sql.Int, limit)
|
||||
.input('offset', sql.Int, offset)
|
||||
.input('keyword', sql.NVarChar, rawKeyword)
|
||||
.input('keywordLike', sql.NVarChar, keywordLike)
|
||||
.input('borrowableOnly', sql.Bit, borrowableOnly ? 1 : 0)
|
||||
.query(`
|
||||
;WITH FilteredAssets AS (
|
||||
SELECT
|
||||
@@ -4849,14 +4855,23 @@ app.get('/api/assets/search', async (req, res) => {
|
||||
AssetCode,
|
||||
AssetName,
|
||||
Unit,
|
||||
EndingBalance,
|
||||
CASE
|
||||
WHEN ISNULL(EndingBalance, 0) <= 0 THEN 'exported'
|
||||
WHEN ISNULL(ExportInPeriod, 0) > 0 THEN 'in_use'
|
||||
ELSE 'in_stock'
|
||||
END AS Status,
|
||||
UpdatedDate,
|
||||
CASE WHEN @keyword <> '' AND AssetCode LIKE @keywordLike THEN 0 ELSE 1 END AS CodeRank,
|
||||
CASE WHEN @keyword <> '' AND AssetName LIKE @keywordLike THEN 0 ELSE 1 END AS NameRank
|
||||
FROM AssetInventory
|
||||
WHERE @keyword = ''
|
||||
OR AssetCode LIKE @keywordLike
|
||||
OR AssetName LIKE @keywordLike
|
||||
OR Model LIKE @keywordLike
|
||||
WHERE (@borrowableOnly = 0 OR ISNULL(EndingBalance, 0) > 0)
|
||||
AND (
|
||||
@keyword = ''
|
||||
OR AssetCode LIKE @keywordLike
|
||||
OR AssetName LIKE @keywordLike
|
||||
OR Model LIKE @keywordLike
|
||||
)
|
||||
),
|
||||
OrderedAssets AS (
|
||||
SELECT
|
||||
@@ -4864,6 +4879,8 @@ app.get('/api/assets/search', async (req, res) => {
|
||||
AssetCode,
|
||||
AssetName,
|
||||
Unit,
|
||||
EndingBalance,
|
||||
Status,
|
||||
ROW_NUMBER() OVER (
|
||||
ORDER BY
|
||||
CodeRank ASC,
|
||||
@@ -4873,17 +4890,20 @@ app.get('/api/assets/search', async (req, res) => {
|
||||
) AS RowNum
|
||||
FROM FilteredAssets
|
||||
)
|
||||
SELECT AssetId, AssetCode, AssetName, Unit
|
||||
SELECT AssetId, AssetCode, AssetName, Unit, EndingBalance, Status
|
||||
FROM OrderedAssets
|
||||
WHERE RowNum > @offset AND RowNum <= (@offset + @limit)
|
||||
ORDER BY RowNum;
|
||||
|
||||
SELECT COUNT(*) AS TotalCount
|
||||
FROM AssetInventory
|
||||
WHERE @keyword = ''
|
||||
OR AssetCode LIKE @keywordLike
|
||||
OR AssetName LIKE @keywordLike
|
||||
OR Model LIKE @keywordLike;
|
||||
WHERE (@borrowableOnly = 0 OR ISNULL(EndingBalance, 0) > 0)
|
||||
AND (
|
||||
@keyword = ''
|
||||
OR AssetCode LIKE @keywordLike
|
||||
OR AssetName LIKE @keywordLike
|
||||
OR Model LIKE @keywordLike
|
||||
);
|
||||
`);
|
||||
|
||||
const rows = Array.isArray(result.recordsets?.[0]) ? result.recordsets[0] : [];
|
||||
|
||||
Reference in New Issue
Block a user