|
@@ -74,25 +74,6 @@ class ReportService
|
74
|
74
|
);
|
75
|
75
|
}
|
76
|
76
|
|
77
|
|
- public function buildTrialBalanceReport(string $startDate, string $endDate, array $columns = []): ReportDTO
|
78
|
|
- {
|
79
|
|
- $allCategories = $this->accountService->getAccountCategoryOrder();
|
80
|
|
-
|
81
|
|
- $categoryGroupedAccounts = $this->getCategoryGroupedAccounts($allCategories);
|
82
|
|
-
|
83
|
|
- $balanceFields = ['debit_balance', 'credit_balance'];
|
84
|
|
-
|
85
|
|
- return $this->buildReport($allCategories, $categoryGroupedAccounts, function (Account $account) use ($startDate, $endDate) {
|
86
|
|
- $endingBalance = $this->accountService->getEndingBalance($account, $startDate, $endDate)?->getAmount() ?? 0;
|
87
|
|
-
|
88
|
|
- if ($endingBalance === 0) {
|
89
|
|
- return [];
|
90
|
|
- }
|
91
|
|
-
|
92
|
|
- return $this->calculateTrialBalance($account->category, $endingBalance);
|
93
|
|
- }, $balanceFields, $columns);
|
94
|
|
- }
|
95
|
|
-
|
96
|
77
|
public function buildAccountTransactionsReport(string $startDate, string $endDate, ?array $columns = null, ?string $accountId = 'all'): ReportDTO
|
97
|
78
|
{
|
98
|
79
|
$columns ??= [];
|
|
@@ -186,7 +167,7 @@ class ReportService
|
186
|
167
|
return new ReportDTO(categories: $reportCategories, fields: $columns);
|
187
|
168
|
}
|
188
|
169
|
|
189
|
|
- private function buildReport(array $allCategories, Collection $categoryGroupedAccounts, callable $balanceCalculator, array $balanceFields, array $allFields, ?callable $initializeCategoryBalances = null): ReportDTO
|
|
170
|
+ private function buildReport(array $allCategories, Collection $categoryGroupedAccounts, callable $balanceCalculator, array $balanceFields, array $allFields, ?callable $initializeCategoryBalances = null, bool $includeRetainedEarnings = false, ?string $startDate = null): ReportDTO
|
190
|
171
|
{
|
191
|
172
|
$accountCategories = [];
|
192
|
173
|
$reportTotalBalances = array_fill_keys($balanceFields, 0);
|
|
@@ -225,6 +206,27 @@ class ReportService
|
225
|
206
|
);
|
226
|
207
|
}
|
227
|
208
|
|
|
209
|
+ if ($includeRetainedEarnings && $categoryName === AccountCategory::Equity->getPluralLabel()) {
|
|
210
|
+ $retainedEarnings = $this->accountService->getRetainedEarnings($startDate);
|
|
211
|
+ $retainedEarningsAmount = $retainedEarnings->getAmount();
|
|
212
|
+
|
|
213
|
+ if ($retainedEarningsAmount >= 0) {
|
|
214
|
+ $categorySummaryBalances['credit_balance'] += $retainedEarningsAmount;
|
|
215
|
+ $categoryAccounts[] = new AccountDTO(
|
|
216
|
+ 'Retained Earnings',
|
|
217
|
+ 'RE',
|
|
218
|
+ $this->formatBalances(['debit_balance' => 0, 'credit_balance' => $retainedEarningsAmount])
|
|
219
|
+ );
|
|
220
|
+ } else {
|
|
221
|
+ $categorySummaryBalances['debit_balance'] += abs($retainedEarningsAmount);
|
|
222
|
+ $categoryAccounts[] = new AccountDTO(
|
|
223
|
+ 'Retained Earnings',
|
|
224
|
+ 'RE',
|
|
225
|
+ $this->formatBalances(['debit_balance' => abs($retainedEarningsAmount), 'credit_balance' => 0])
|
|
226
|
+ );
|
|
227
|
+ }
|
|
228
|
+ }
|
|
229
|
+
|
228
|
230
|
foreach ($balanceFields as $field) {
|
229
|
231
|
if (array_key_exists($field, $categorySummaryBalances)) {
|
230
|
232
|
$reportTotalBalances[$field] += $categorySummaryBalances[$field];
|
|
@@ -252,6 +254,25 @@ class ReportService
|
252
|
254
|
}
|
253
|
255
|
}
|
254
|
256
|
|
|
257
|
+ public function buildTrialBalanceReport(string $startDate, string $endDate, array $columns = []): ReportDTO
|
|
258
|
+ {
|
|
259
|
+ $allCategories = $this->accountService->getAccountCategoryOrder();
|
|
260
|
+
|
|
261
|
+ $categoryGroupedAccounts = $this->getCategoryGroupedAccounts($allCategories);
|
|
262
|
+
|
|
263
|
+ $balanceFields = ['debit_balance', 'credit_balance'];
|
|
264
|
+
|
|
265
|
+ return $this->buildReport($allCategories, $categoryGroupedAccounts, function (Account $account) use ($startDate, $endDate) {
|
|
266
|
+ $endingBalance = $this->accountService->getEndingBalance($account, $startDate, $endDate)?->getAmount() ?? 0;
|
|
267
|
+
|
|
268
|
+ if ($endingBalance === 0) {
|
|
269
|
+ return [];
|
|
270
|
+ }
|
|
271
|
+
|
|
272
|
+ return $this->calculateTrialBalance($account->category, $endingBalance);
|
|
273
|
+ }, $balanceFields, $columns, null, true, $startDate);
|
|
274
|
+ }
|
|
275
|
+
|
255
|
276
|
private function calculateTrialBalance(AccountCategory $category, int $endingBalance): array
|
256
|
277
|
{
|
257
|
278
|
if (in_array($category, [AccountCategory::Asset, AccountCategory::Expense], true)) {
|