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.

TransactionObserver.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Observers;
  3. use App\Enums\Accounting\JournalEntryType;
  4. use App\Enums\Accounting\TransactionType;
  5. use App\Models\Accounting\Account;
  6. use App\Models\Accounting\JournalEntry;
  7. use App\Models\Accounting\Transaction;
  8. use App\Utilities\Currency\CurrencyAccessor;
  9. use App\Utilities\Currency\CurrencyConverter;
  10. use Illuminate\Support\Facades\DB;
  11. class TransactionObserver
  12. {
  13. /**
  14. * Handle the Transaction "created" event.
  15. */
  16. public function created(Transaction $transaction): void
  17. {
  18. if ($transaction->type === TransactionType::Journal) {
  19. return;
  20. }
  21. $chartAccount = $transaction->account;
  22. $bankAccount = $transaction->bankAccount->account;
  23. $debitAccount = $transaction->type === TransactionType::Withdrawal ? $chartAccount : $bankAccount;
  24. $creditAccount = $transaction->type === TransactionType::Withdrawal ? $bankAccount : $chartAccount;
  25. $this->createJournalEntries($transaction, $debitAccount, $creditAccount);
  26. }
  27. private function createJournalEntries(Transaction $transaction, Account $debitAccount, Account $creditAccount): void
  28. {
  29. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  30. $transactionCurrency = $transaction->bankAccount->account->currency_code; // only account which would have a different currency compared to the default currency
  31. if ($transactionCurrency !== $defaultCurrency) {
  32. $convertedTransactionAmount = $this->convertToDefaultCurrency($transaction->amount, $transactionCurrency, $defaultCurrency);
  33. } else {
  34. $convertedTransactionAmount = $transaction->amount;
  35. }
  36. $debitAccount->journalEntries()->create([
  37. 'company_id' => $transaction->company_id,
  38. 'transaction_id' => $transaction->id,
  39. 'type' => JournalEntryType::Debit,
  40. 'amount' => $convertedTransactionAmount,
  41. 'description' => $transaction->description,
  42. ]);
  43. $creditAccount->journalEntries()->create([
  44. 'company_id' => $transaction->company_id,
  45. 'transaction_id' => $transaction->id,
  46. 'type' => JournalEntryType::Credit,
  47. 'amount' => $convertedTransactionAmount,
  48. 'description' => $transaction->description,
  49. ]);
  50. }
  51. private function convertToDefaultCurrency(string $amount, string $fromCurrency, string $toCurrency): string
  52. {
  53. $amountInCents = CurrencyConverter::prepareForAccessor($amount, $fromCurrency);
  54. $convertedAmountInCents = CurrencyConverter::convertBalance($amountInCents, $fromCurrency, $toCurrency);
  55. return CurrencyConverter::prepareForMutator($convertedAmountInCents, $toCurrency);
  56. }
  57. /**
  58. * Handle the Transaction "updated" event.
  59. */
  60. public function updated(Transaction $transaction): void
  61. {
  62. if ($transaction->type === TransactionType::Journal || $this->hasRelevantChanges($transaction) === false) {
  63. return;
  64. }
  65. $chartAccount = $transaction->account;
  66. $bankAccount = $transaction->bankAccount?->account;
  67. if (! $chartAccount || ! $bankAccount) {
  68. return;
  69. }
  70. $journalEntries = $transaction->journalEntries;
  71. $debitEntry = $journalEntries->where('type', JournalEntryType::Debit)->first();
  72. $creditEntry = $journalEntries->where('type', JournalEntryType::Credit)->first();
  73. if (! $debitEntry || ! $creditEntry) {
  74. return;
  75. }
  76. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  77. $transactionCurrency = $transaction->bankAccount->account->currency_code; // only account which would have a different currency compared to the default currency
  78. if ($transactionCurrency !== $defaultCurrency) {
  79. $convertedTransactionAmount = $this->convertToDefaultCurrency($transaction->amount, $transactionCurrency, $defaultCurrency);
  80. } else {
  81. $convertedTransactionAmount = $transaction->amount;
  82. }
  83. $debitAccount = $transaction->type === TransactionType::Withdrawal ? $chartAccount : $bankAccount;
  84. $creditAccount = $transaction->type === TransactionType::Withdrawal ? $bankAccount : $chartAccount;
  85. $this->updateJournalEntriesForTransaction($debitEntry, $debitAccount, $convertedTransactionAmount);
  86. $this->updateJournalEntriesForTransaction($creditEntry, $creditAccount, $convertedTransactionAmount);
  87. }
  88. protected function hasRelevantChanges(Transaction $transaction): bool
  89. {
  90. return $transaction->wasChanged(['amount', 'account_id', 'bank_account_id', 'type']);
  91. }
  92. protected function updateJournalEntriesForTransaction(JournalEntry $journalEntry, Account $account, string $convertedTransactionAmount): void
  93. {
  94. DB::transaction(static function () use ($journalEntry, $account, $convertedTransactionAmount) {
  95. $journalEntry->update([
  96. 'account_id' => $account->id,
  97. 'amount' => $convertedTransactionAmount,
  98. ]);
  99. });
  100. }
  101. /**
  102. * Handle the Transaction "deleting" event.
  103. */
  104. public function deleting(Transaction $transaction): void
  105. {
  106. DB::transaction(static function () use ($transaction) {
  107. $transaction->journalEntries()->each(fn (JournalEntry $entry) => $entry->delete());
  108. });
  109. }
  110. /**
  111. * Handle the Transaction "deleted" event.
  112. */
  113. public function deleted(Transaction $transaction): void
  114. {
  115. //
  116. }
  117. /**
  118. * Handle the Transaction "restored" event.
  119. */
  120. public function restored(Transaction $transaction): void
  121. {
  122. //
  123. }
  124. /**
  125. * Handle the Transaction "force deleted" event.
  126. */
  127. public function forceDeleted(Transaction $transaction): void
  128. {
  129. //
  130. }
  131. }