feat: add initial balance support to accounts, enhancing account management and balance calculations across components
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
TopExpensesList,
|
||||
} from "@/components/statistics";
|
||||
import { useBankingData } from "@/lib/hooks";
|
||||
import { getAccountBalance } from "@/lib/account-utils";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
@@ -231,7 +232,19 @@ export default function StatisticsPage() {
|
||||
(a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()
|
||||
);
|
||||
|
||||
// Start with sum of initial balances for filtered accounts
|
||||
let runningBalance = 0;
|
||||
if (selectedAccounts.includes("all")) {
|
||||
runningBalance = data.accounts.reduce(
|
||||
(sum, acc) => sum + (acc.initialBalance || 0),
|
||||
0,
|
||||
);
|
||||
} else {
|
||||
runningBalance = data.accounts
|
||||
.filter((acc) => selectedAccounts.includes(acc.id))
|
||||
.reduce((sum, acc) => sum + (acc.initialBalance || 0), 0);
|
||||
}
|
||||
|
||||
const aggregatedBalanceByDate = new Map<string, number>();
|
||||
sortedFilteredTransactions.forEach((t) => {
|
||||
runningBalance += t.amount;
|
||||
@@ -254,10 +267,10 @@ export default function StatisticsPage() {
|
||||
accountBalances.set(account.id, new Map());
|
||||
});
|
||||
|
||||
// Calculate running balance per account
|
||||
// Calculate running balance per account (start with initialBalance)
|
||||
const accountRunningBalances = new Map<string, number>();
|
||||
data.accounts.forEach((account) => {
|
||||
accountRunningBalances.set(account.id, 0);
|
||||
accountRunningBalances.set(account.id, account.initialBalance || 0);
|
||||
});
|
||||
|
||||
sortedFilteredTransactions.forEach((t) => {
|
||||
@@ -280,7 +293,7 @@ export default function StatisticsPage() {
|
||||
const sortedDates = Array.from(allDates).sort();
|
||||
const lastBalances = new Map<string, number>();
|
||||
data.accounts.forEach((account) => {
|
||||
lastBalances.set(account.id, 0);
|
||||
lastBalances.set(account.id, account.initialBalance || 0);
|
||||
});
|
||||
|
||||
const perAccountBalanceData = sortedDates.map((date) => {
|
||||
|
||||
Reference in New Issue
Block a user