feat(reports): adjust financial report calculations to maintain sign for accuracy

This commit is contained in:
2025-08-10 20:13:04 +00:00
parent 10ca6f2992
commit db46612ea2
2 changed files with 73 additions and 42 deletions

View File

@@ -122,11 +122,11 @@ export class Reports {
const entry: IIncomeStatementEntry = {
accountNumber: account.accountNumber,
accountName: account.accountName,
amount: Math.abs(balance),
amount: balance, // Keep the sign for correct calculation
};
revenueEntries.push(entry);
totalRevenue += Math.abs(balance);
totalRevenue += balance; // Revenue accounts normally have credit balance (positive)
}
}
@@ -138,23 +138,24 @@ export class Reports {
const entry: IIncomeStatementEntry = {
accountNumber: account.accountNumber,
accountName: account.accountName,
amount: Math.abs(balance),
amount: balance, // Keep the sign - negative balance reduces expenses
};
expenseEntries.push(entry);
totalExpenses += Math.abs(balance);
totalExpenses += balance; // Expense accounts normally have debit balance (positive)
// But credit balances (negative) reduce total expenses
}
}
// Calculate percentages
// Calculate percentages using absolute values to avoid negative percentages
revenueEntries.forEach((entry) => {
entry.percentage =
totalRevenue > 0 ? (entry.amount / totalRevenue) * 100 : 0;
totalRevenue !== 0 ? (Math.abs(entry.amount) / Math.abs(totalRevenue)) * 100 : 0;
});
expenseEntries.forEach((entry) => {
entry.percentage =
totalRevenue > 0 ? (entry.amount / totalRevenue) * 100 : 0;
totalRevenue !== 0 ? (Math.abs(entry.amount) / Math.abs(totalRevenue)) * 100 : 0;
});
// Sort entries by account number
@@ -214,7 +215,7 @@ export class Reports {
const entry: IBalanceSheetEntry = {
accountNumber: account.accountNumber,
accountName: account.accountName,
amount: Math.abs(balance),
amount: balance, // Keep the sign for display
};
// Classify as current or fixed based on account class
@@ -224,7 +225,7 @@ export class Reports {
fixedAssets.push(entry);
}
totalAssets += Math.abs(balance);
totalAssets += balance; // Add with sign to get correct total
}
}
@@ -240,7 +241,7 @@ export class Reports {
const entry: IBalanceSheetEntry = {
accountNumber: account.accountNumber,
accountName: account.accountName,
amount: Math.abs(balance),
amount: balance, // Keep the sign for display
};
// Classify as current or long-term based on account number
@@ -253,7 +254,7 @@ export class Reports {
longTermLiabilities.push(entry);
}
totalLiabilities += Math.abs(balance);
totalLiabilities += balance; // Add with sign to get correct total
}
}
@@ -268,23 +269,27 @@ export class Reports {
const entry: IBalanceSheetEntry = {
accountNumber: account.accountNumber,
accountName: account.accountName,
amount: Math.abs(balance),
amount: balance, // Keep the sign for display
};
equityEntries.push(entry);
totalEquity += Math.abs(balance);
totalEquity += balance; // Add with sign to get correct total
}
}
// Add current year profit/loss
// Add current year profit/loss only if accounts haven't been closed
// Check if revenue/expense accounts have non-zero balances (indicates not closed)
const incomeStatement = await this.getIncomeStatement(params);
if (incomeStatement.netIncome !== 0) {
// Only add current year profit/loss if we have unclosed revenue/expense accounts
// (i.e., the income statement shows non-zero revenue or expenses)
if (incomeStatement.netIncome !== 0 && (incomeStatement.totalRevenue !== 0 || incomeStatement.totalExpenses !== 0)) {
equityEntries.push({
accountNumber: '9999',
accountName: 'Current Year Profit/Loss',
amount: Math.abs(incomeStatement.netIncome),
amount: incomeStatement.netIncome, // Keep the sign
});
totalEquity += Math.abs(incomeStatement.netIncome);
totalEquity += incomeStatement.netIncome; // Add with sign
}
// Sort entries