|
@@ -33,11 +33,9 @@ class AccountService implements AccountHandler
|
33
|
33
|
|
34
|
34
|
public function getNetMovement(Account $account, string $startDate, string $endDate): Money
|
35
|
35
|
{
|
36
|
|
- $debitBalance = $this->journalEntryRepository->sumDebitAmounts($account, $startDate, $endDate);
|
37
|
|
- $creditBalance = $this->journalEntryRepository->sumCreditAmounts($account, $startDate, $endDate);
|
38
|
|
- $netMovement = $this->calculateNetMovementByCategory($account->category, $debitBalance, $creditBalance);
|
|
36
|
+ $balances = $this->calculateBalances($account, $startDate, $endDate);
|
39
|
37
|
|
40
|
|
- return new Money($netMovement, $account->currency_code);
|
|
38
|
+ return new Money($balances['net_movement'], $account->currency_code);
|
41
|
39
|
}
|
42
|
40
|
|
43
|
41
|
public function getStartingBalance(Account $account, string $startDate): ?Money
|
|
@@ -46,23 +44,23 @@ class AccountService implements AccountHandler
|
46
|
44
|
return null;
|
47
|
45
|
}
|
48
|
46
|
|
49
|
|
- $debitBalanceBefore = $this->journalEntryRepository->sumDebitAmounts($account, $startDate);
|
50
|
|
- $creditBalanceBefore = $this->journalEntryRepository->sumCreditAmounts($account, $startDate);
|
51
|
|
- $startingBalance = $this->calculateNetMovementByCategory($account->category, $debitBalanceBefore, $creditBalanceBefore);
|
|
47
|
+ $balances = $this->calculateStartingBalances($account, $startDate);
|
52
|
48
|
|
53
|
|
- return new Money($startingBalance, $account->currency_code);
|
|
49
|
+ return new Money($balances['starting_balance'], $account->currency_code);
|
54
|
50
|
}
|
55
|
51
|
|
56
|
52
|
public function getEndingBalance(Account $account, string $startDate, string $endDate): ?Money
|
57
|
53
|
{
|
58
|
|
- $netMovement = $this->getNetMovement($account, $startDate, $endDate)->getAmount();
|
|
54
|
+ $calculatedBalances = $this->calculateBalances($account, $startDate, $endDate);
|
|
55
|
+ $startingBalances = $this->calculateStartingBalances($account, $startDate);
|
|
56
|
+
|
|
57
|
+ $netMovement = $calculatedBalances['net_movement'];
|
59
|
58
|
|
60
|
59
|
if (in_array($account->category, [AccountCategory::Expense, AccountCategory::Revenue], true)) {
|
61
|
60
|
return new Money($netMovement, $account->currency_code);
|
62
|
61
|
}
|
63
|
62
|
|
64
|
|
- $startingBalance = $this->getStartingBalance($account, $startDate)?->getAmount();
|
65
|
|
- $endingBalance = $startingBalance + $netMovement;
|
|
63
|
+ $endingBalance = $startingBalances['starting_balance'] + $netMovement;
|
66
|
64
|
|
67
|
65
|
return new Money($endingBalance, $account->currency_code);
|
68
|
66
|
}
|
|
@@ -75,24 +73,54 @@ class AccountService implements AccountHandler
|
75
|
73
|
};
|
76
|
74
|
}
|
77
|
75
|
|
78
|
|
- public function getBalances(Account $account, string $startDate, string $endDate): array
|
|
76
|
+ private function calculateBalances(Account $account, string $startDate, string $endDate): array
|
79
|
77
|
{
|
80
|
|
- $debitBalance = $this->getDebitBalance($account, $startDate, $endDate)->getAmount();
|
81
|
|
- $creditBalance = $this->getCreditBalance($account, $startDate, $endDate)->getAmount();
|
82
|
|
- $netMovement = $this->getNetMovement($account, $startDate, $endDate)->getAmount();
|
|
78
|
+ $debitBalance = $this->journalEntryRepository->sumDebitAmounts($account, $startDate, $endDate);
|
|
79
|
+ $creditBalance = $this->journalEntryRepository->sumCreditAmounts($account, $startDate, $endDate);
|
83
|
80
|
|
84
|
|
- $balances = [
|
|
81
|
+ return [
|
85
|
82
|
'debit_balance' => $debitBalance,
|
86
|
83
|
'credit_balance' => $creditBalance,
|
87
|
|
- 'net_movement' => $netMovement,
|
|
84
|
+ 'net_movement' => $this->calculateNetMovementByCategory($account->category, $debitBalance, $creditBalance),
|
|
85
|
+ ];
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ private function calculateStartingBalances(Account $account, string $startDate): array
|
|
89
|
+ {
|
|
90
|
+ $debitBalanceBefore = $this->journalEntryRepository->sumDebitAmounts($account, $startDate);
|
|
91
|
+ $creditBalanceBefore = $this->journalEntryRepository->sumCreditAmounts($account, $startDate);
|
|
92
|
+
|
|
93
|
+ return [
|
|
94
|
+ 'debit_balance_before' => $debitBalanceBefore,
|
|
95
|
+ 'credit_balance_before' => $creditBalanceBefore,
|
|
96
|
+ 'starting_balance' => $this->calculateNetMovementByCategory($account->category, $debitBalanceBefore, $creditBalanceBefore),
|
88
|
97
|
];
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ public function getBalances(Account $account, string $startDate, string $endDate, array $fields): array
|
|
101
|
+ {
|
|
102
|
+ $balances = [];
|
|
103
|
+ $calculatedBalances = $this->calculateBalances($account, $startDate, $endDate);
|
|
104
|
+
|
|
105
|
+ // Calculate starting balances only if needed
|
|
106
|
+ $startingBalances = null;
|
|
107
|
+ $needStartingBalances = ! in_array($account->category, [AccountCategory::Expense, AccountCategory::Revenue], true)
|
|
108
|
+ && (in_array('starting_balance', $fields) || in_array('ending_balance', $fields));
|
|
109
|
+
|
|
110
|
+ if ($needStartingBalances) {
|
|
111
|
+ $startingBalances = $this->calculateStartingBalances($account, $startDate);
|
|
112
|
+ }
|
89
|
113
|
|
90
|
|
- if (! in_array($account->category, [AccountCategory::Expense, AccountCategory::Revenue], true)) {
|
91
|
|
- $balances['starting_balance'] = $this->getStartingBalance($account, $startDate)?->getAmount();
|
92
|
|
- $balances['ending_balance'] = $this->getEndingBalance($account, $startDate, $endDate)?->getAmount();
|
|
114
|
+ foreach ($fields as $field) {
|
|
115
|
+ $balances[$field] = match ($field) {
|
|
116
|
+ 'debit_balance', 'credit_balance', 'net_movement' => $calculatedBalances[$field],
|
|
117
|
+ 'starting_balance' => $needStartingBalances ? $startingBalances['starting_balance'] : null,
|
|
118
|
+ 'ending_balance' => $needStartingBalances ? $startingBalances['starting_balance'] + $calculatedBalances['net_movement'] : null,
|
|
119
|
+ default => null,
|
|
120
|
+ };
|
93
|
121
|
}
|
94
|
122
|
|
95
|
|
- return $balances;
|
|
123
|
+ return array_filter($balances, static fn ($value) => $value !== null);
|
96
|
124
|
}
|
97
|
125
|
|
98
|
126
|
public function getTotalBalanceForAllBankAccounts(string $startDate, string $endDate): Money
|