You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccountService.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Services;
  3. use App\Contracts\AccountHandler;
  4. use App\Enums\Accounting\AccountCategory;
  5. use App\Models\Accounting\Account;
  6. use App\Models\Accounting\Transaction;
  7. use App\Models\Banking\BankAccount;
  8. use App\Repositories\Accounting\JournalEntryRepository;
  9. use App\Utilities\Currency\CurrencyAccessor;
  10. use App\ValueObjects\Money;
  11. class AccountService implements AccountHandler
  12. {
  13. public function __construct(
  14. protected JournalEntryRepository $journalEntryRepository
  15. ) {}
  16. public function getDebitBalance(Account $account, string $startDate, string $endDate): Money
  17. {
  18. $amount = $this->journalEntryRepository->sumDebitAmounts($account, $startDate, $endDate);
  19. return new Money($amount, $account->currency_code);
  20. }
  21. public function getCreditBalance(Account $account, string $startDate, string $endDate): Money
  22. {
  23. $amount = $this->journalEntryRepository->sumCreditAmounts($account, $startDate, $endDate);
  24. return new Money($amount, $account->currency_code);
  25. }
  26. public function getNetMovement(Account $account, string $startDate, string $endDate): Money
  27. {
  28. $debitBalance = $this->journalEntryRepository->sumDebitAmounts($account, $startDate, $endDate);
  29. $creditBalance = $this->journalEntryRepository->sumCreditAmounts($account, $startDate, $endDate);
  30. $netMovement = $this->calculateNetMovementByCategory($account->category, $debitBalance, $creditBalance);
  31. return new Money($netMovement, $account->currency_code);
  32. }
  33. public function getStartingBalance(Account $account, string $startDate): ?Money
  34. {
  35. if (in_array($account->category, [AccountCategory::Expense, AccountCategory::Revenue], true)) {
  36. return null;
  37. }
  38. $debitBalanceBefore = $this->journalEntryRepository->sumDebitAmounts($account, $startDate);
  39. $creditBalanceBefore = $this->journalEntryRepository->sumCreditAmounts($account, $startDate);
  40. $startingBalance = $this->calculateNetMovementByCategory($account->category, $debitBalanceBefore, $creditBalanceBefore);
  41. return new Money($startingBalance, $account->currency_code);
  42. }
  43. public function getEndingBalance(Account $account, string $startDate, string $endDate): ?Money
  44. {
  45. $netMovement = $this->getNetMovement($account, $startDate, $endDate)->getAmount();
  46. if (in_array($account->category, [AccountCategory::Expense, AccountCategory::Revenue], true)) {
  47. return new Money($netMovement, $account->currency_code);
  48. }
  49. $startingBalance = $this->getStartingBalance($account, $startDate)?->getAmount();
  50. $endingBalance = $startingBalance + $netMovement;
  51. return new Money($endingBalance, $account->currency_code);
  52. }
  53. private function calculateNetMovementByCategory(AccountCategory $category, int $debitBalance, int $creditBalance): int
  54. {
  55. return match ($category) {
  56. AccountCategory::Asset, AccountCategory::Expense => $debitBalance - $creditBalance,
  57. AccountCategory::Liability, AccountCategory::Equity, AccountCategory::Revenue => $creditBalance - $debitBalance,
  58. };
  59. }
  60. public function getBalances(Account $account, string $startDate, string $endDate): array
  61. {
  62. $debitBalance = $this->getDebitBalance($account, $startDate, $endDate)->getAmount();
  63. $creditBalance = $this->getCreditBalance($account, $startDate, $endDate)->getAmount();
  64. $netMovement = $this->getNetMovement($account, $startDate, $endDate)->getAmount();
  65. $balances = [
  66. 'debit_balance' => $debitBalance,
  67. 'credit_balance' => $creditBalance,
  68. 'net_movement' => $netMovement,
  69. ];
  70. if (! in_array($account->category, [AccountCategory::Expense, AccountCategory::Revenue], true)) {
  71. $balances['starting_balance'] = $this->getStartingBalance($account, $startDate)?->getAmount();
  72. $balances['ending_balance'] = $this->getEndingBalance($account, $startDate, $endDate)?->getAmount();
  73. }
  74. return $balances;
  75. }
  76. public function getTotalBalanceForAllBankAccounts(string $startDate, string $endDate): Money
  77. {
  78. $bankAccounts = BankAccount::with('account')
  79. ->get();
  80. $totalBalance = 0;
  81. foreach ($bankAccounts as $bankAccount) {
  82. $account = $bankAccount->account;
  83. if ($account) {
  84. $endingBalance = $this->getEndingBalance($account, $startDate, $endDate)?->getAmount() ?? 0;
  85. $totalBalance += $endingBalance;
  86. }
  87. }
  88. return new Money($totalBalance, CurrencyAccessor::getDefaultCurrency());
  89. }
  90. public function getAccountCategoryOrder(): array
  91. {
  92. return [
  93. AccountCategory::Asset->getPluralLabel(),
  94. AccountCategory::Liability->getPluralLabel(),
  95. AccountCategory::Equity->getPluralLabel(),
  96. AccountCategory::Revenue->getPluralLabel(),
  97. AccountCategory::Expense->getPluralLabel(),
  98. ];
  99. }
  100. public function getEarliestTransactionDate(): string
  101. {
  102. $earliestDate = Transaction::oldest('posted_at')
  103. ->value('posted_at');
  104. return $earliestDate ?? now()->format('Y-m-d');
  105. }
  106. }