import fix

This commit is contained in:
2026-05-06 11:14:10 +07:00
parent 197186eac8
commit 8b2a9d7afe
4 changed files with 264 additions and 57 deletions

View File

@@ -1637,6 +1637,19 @@ class AccountManager {
return parsed;
}
parseOptionalNonNegativeInteger(value) {
if (value === null || value === undefined || String(value).trim() === '') {
return null;
}
const parsed = Number.parseInt(String(value).replace(/,/g, '').trim(), 10);
if (!Number.isFinite(parsed) || parsed < 0) {
return null;
}
return parsed;
}
escapeHtml(value) {
return String(value || '')
.replace(/&/g, '&amp;')
@@ -1850,17 +1863,26 @@ class AccountManager {
const borrowerEntries = Array.isArray(borrowerEntriesOverride)
? this.parseBorrowerEntries(borrowerEntriesOverride)
: this.parseBorrowerEntries(asset?.Borrower ?? asset?.borrower);
const exportInPeriod = borrowerEntries.reduce((sum, entry) => (
const borrowerExportInPeriod = borrowerEntries.reduce((sum, entry) => (
sum + this.parseNonNegativeInteger(entry?.quantity, 0)
), 0);
const endingBalance = Math.max(quantity + importInPeriod - exportInPeriod, 0);
const storedExportInPeriod = this.parseOptionalNonNegativeInteger(asset?.ExportInPeriod ?? asset?.exportInPeriod);
const exportInPeriod = storedExportInPeriod !== null
? storedExportInPeriod
: borrowerExportInPeriod;
const computedEndingBalance = Math.max(quantity + importInPeriod - exportInPeriod, 0);
const storedEndingBalance = this.parseOptionalNonNegativeInteger(asset?.EndingBalance ?? asset?.endingBalance);
const endingBalance = storedEndingBalance !== null
? storedEndingBalance
: computedEndingBalance;
return {
quantity,
importInPeriod,
exportInPeriod,
endingBalance,
borrowerEntries
borrowerEntries,
borrowerExportInPeriod
};
}
@@ -1890,19 +1912,16 @@ class AccountManager {
return;
}
const metrics = this.buildAssetQuantityMetrics(
{
Quantity: quantityInput.value,
ImportInPeriod: importInput.value
},
this.editingAssetBorrowerEntries
);
const quantity = this.parseNonNegativeInteger(quantityInput.value, 0);
const importInPeriod = this.parseNonNegativeInteger(importInput.value, 0);
const exportInPeriod = this.parseNonNegativeInteger(exportInput?.value ?? 0, 0);
const endingBalance = Math.max(quantity + importInPeriod - exportInPeriod, 0);
if (exportInput) {
exportInput.value = String(metrics.exportInPeriod);
exportInput.value = String(exportInPeriod);
}
if (endingInput) {
endingInput.value = String(metrics.endingBalance);
endingInput.value = String(endingBalance);
}
}
@@ -3944,18 +3963,14 @@ class AccountManager {
collectAssetFormPayload() {
const quantity = this.parseNonNegativeInteger(document.getElementById('assetQuantityInput')?.value ?? 0, 0);
const importInPeriod = this.parseNonNegativeInteger(document.getElementById('assetImportInPeriodInput')?.value ?? 0, 0);
const exportInPeriod = this.parseNonNegativeInteger(document.getElementById('assetExportInPeriodInput')?.value ?? 0, 0);
const endingBalanceInput = this.parseOptionalNonNegativeInteger(document.getElementById('assetEndingBalanceInput')?.value ?? '');
const borrowerEntries = Array.isArray(this.editingAssetBorrowerEntries)
? this.editingAssetBorrowerEntries
: [];
const borrower = this.formatBorrowerEntries(borrowerEntries, '; ') || null;
const metrics = this.buildAssetQuantityMetrics(
{
Quantity: quantity,
ImportInPeriod: importInPeriod,
Borrower: borrower
},
borrowerEntries
);
const computedEndingBalance = Math.max(quantity + importInPeriod - exportInPeriod, 0);
const endingBalance = endingBalanceInput !== null ? endingBalanceInput : computedEndingBalance;
const purchasePrice = String(document.getElementById('assetPriceInput')?.value ?? '').trim();
@@ -3967,8 +3982,8 @@ class AccountManager {
serialNumber: document.getElementById('assetSerialInput')?.value?.trim() || '',
quantity,
importInPeriod,
exportInPeriod: metrics.exportInPeriod,
endingBalance: metrics.endingBalance,
exportInPeriod,
endingBalance,
unit: document.getElementById('assetUnitInput')?.value?.trim() || '',
department: document.getElementById('assetDepartmentInput')?.value?.trim() || '',
project: document.getElementById('assetProjectInput')?.value?.trim() || '',
@@ -3986,18 +4001,32 @@ class AccountManager {
return null;
}
const quantity = this.parseNonNegativeInteger(asset?.Quantity, 0);
const importInPeriod = this.parseNonNegativeInteger(asset?.ImportInPeriod, 0);
const baseExportInPeriod = this.parseNonNegativeInteger(asset?.ExportInPeriod, 0);
const baseEndingBalance = this.parseOptionalNonNegativeInteger(asset?.EndingBalance);
const resolvedBaseEndingBalance = baseEndingBalance !== null
? baseEndingBalance
: Math.max(quantity + importInPeriod - baseExportInPeriod, 0);
const borrowerEntries = Array.isArray(borrowerEntriesOverride)
? borrowerEntriesOverride
: this.parseBorrowerEntries(asset?.Borrower);
const borrower = this.formatBorrowerEntries(borrowerEntries, '; ') || null;
const metrics = this.buildAssetQuantityMetrics(
{
Quantity: asset?.Quantity,
ImportInPeriod: asset?.ImportInPeriod,
Borrower: borrower
},
borrowerEntries
);
let exportInPeriod = baseExportInPeriod;
let endingBalance = resolvedBaseEndingBalance;
if (Array.isArray(borrowerEntriesOverride)) {
const existingBorrowerEntries = this.parseBorrowerEntries(asset?.Borrower);
const previousBorrowerExport = existingBorrowerEntries.reduce((sum, entry) => (
sum + this.parseNonNegativeInteger(entry?.quantity, 0)
), 0);
const nextBorrowerExport = this.parseBorrowerEntries(borrowerEntriesOverride).reduce((sum, entry) => (
sum + this.parseNonNegativeInteger(entry?.quantity, 0)
), 0);
const exportDelta = nextBorrowerExport - previousBorrowerExport;
exportInPeriod = Math.max(baseExportInPeriod + exportDelta, 0);
endingBalance = Math.max(resolvedBaseEndingBalance - exportDelta, 0);
}
const rawPrice = asset?.PurchasePrice;
const normalizedPrice = rawPrice === undefined || rawPrice === null || String(rawPrice).trim() === ''
@@ -4010,10 +4039,10 @@ class AccountManager {
status: String(asset?.Status || 'in_use'),
model: String(asset?.Model || '').trim(),
serialNumber: String(asset?.SerialNumber || '').trim(),
quantity: metrics.quantity,
importInPeriod: metrics.importInPeriod,
exportInPeriod: metrics.exportInPeriod,
endingBalance: metrics.endingBalance,
quantity,
importInPeriod,
exportInPeriod,
endingBalance,
unit: String(asset?.Unit || '').trim(),
department: String(asset?.Department || '').trim(),
project: String(asset?.Project || '').trim(),