Ver código fonte

remove company id in compound index and include retained earnings

3.x
Andrew Wallo 1 ano atrás
pai
commit
11d55f554b

+ 24
- 0
app/Services/AccountService.php Ver arquivo

@@ -65,6 +65,30 @@ class AccountService implements AccountHandler
65 65
         return new Money($endingBalance, $account->currency_code);
66 66
     }
67 67
 
68
+    public function getRetainedEarnings(string $startDate): Money
69
+    {
70
+        $revenueAccounts = Account::where('category', AccountCategory::Revenue)
71
+            ->get();
72
+
73
+        $expenseAccounts = Account::where('category', AccountCategory::Expense)
74
+            ->get();
75
+
76
+        $revenue = 0;
77
+        $expense = 0;
78
+
79
+        foreach ($revenueAccounts as $account) {
80
+            $revenue += $this->journalEntryRepository->sumCreditAmounts($account, $startDate);
81
+        }
82
+
83
+        foreach ($expenseAccounts as $account) {
84
+            $expense += $this->journalEntryRepository->sumDebitAmounts($account, $startDate);
85
+        }
86
+
87
+        $retainedEarnings = $revenue - $expense;
88
+
89
+        return new Money($retainedEarnings, CurrencyAccessor::getDefaultCurrency());
90
+    }
91
+
68 92
     private function calculateNetMovementByCategory(AccountCategory $category, int $debitBalance, int $creditBalance): int
69 93
     {
70 94
         return match ($category) {

+ 41
- 20
app/Services/ReportService.php Ver arquivo

@@ -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)) {

+ 2
- 2
database/migrations/2024_01_01_234943_create_transactions_table.php Ver arquivo

@@ -31,8 +31,8 @@ return new class extends Migration
31 31
             $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
32 32
             $table->timestamps();
33 33
 
34
-            $table->index(['company_id', 'account_id', 'posted_at']);
35
-            $table->index(['company_id', 'bank_account_id', 'posted_at']);
34
+            $table->index(['account_id', 'posted_at']);
35
+            $table->index(['bank_account_id', 'posted_at']);
36 36
         });
37 37
     }
38 38
 

+ 2
- 2
database/migrations/2024_01_11_062614_create_journal_entries_table.php Ver arquivo

@@ -23,8 +23,8 @@ return new class extends Migration
23 23
             $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
24 24
             $table->timestamps();
25 25
 
26
-            $table->index(['company_id', 'account_id', 'type']);
27
-            $table->index(['company_id', 'account_id', 'transaction_id']);
26
+            $table->index(['account_id', 'type']);
27
+            $table->index(['account_id', 'transaction_id']);
28 28
         });
29 29
     }
30 30
 

Carregando…
Cancelar
Salvar