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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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::convertFormattedBalance($amount, $fromCurrency, $toCurrency);
  54. return money($amountInCents, $toCurrency)->formatSimple();
  55. }
  56. /**
  57. * Handle the Transaction "updated" event.
  58. */
  59. public function updated(Transaction $transaction): void
  60. {
  61. if ($transaction->type === TransactionType::Journal || $this->hasRelevantChanges($transaction) === false) {
  62. return;
  63. }
  64. $chartAccount = $transaction->account;
  65. $bankAccount = $transaction->bankAccount?->account;
  66. if (! $chartAccount || ! $bankAccount) {
  67. return;
  68. }
  69. $journalEntries = $transaction->journalEntries;
  70. $debitEntry = $journalEntries->where('type', JournalEntryType::Debit)->first();
  71. $creditEntry = $journalEntries->where('type', JournalEntryType::Credit)->first();
  72. if (! $debitEntry || ! $creditEntry) {
  73. return;
  74. }
  75. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  76. $transactionCurrency = $transaction->bankAccount->account->currency_code; // only account which would have a different currency compared to the default currency
  77. if ($transactionCurrency !== $defaultCurrency) {
  78. $convertedTransactionAmount = $this->convertToDefaultCurrency($transaction->amount, $transactionCurrency, $defaultCurrency);
  79. } else {
  80. $convertedTransactionAmount = $transaction->amount;
  81. }
  82. $debitAccount = $transaction->type === TransactionType::Withdrawal ? $chartAccount : $bankAccount;
  83. $creditAccount = $transaction->type === TransactionType::Withdrawal ? $bankAccount : $chartAccount;
  84. $this->updateJournalEntriesForTransaction($debitEntry, $debitAccount, $convertedTransactionAmount);
  85. $this->updateJournalEntriesForTransaction($creditEntry, $creditAccount, $convertedTransactionAmount);
  86. }
  87. protected function hasRelevantChanges(Transaction $transaction): bool
  88. {
  89. return $transaction->wasChanged(['amount', 'account_id', 'bank_account_id', 'type']);
  90. }
  91. protected function updateJournalEntriesForTransaction(JournalEntry $journalEntry, Account $account, string $convertedTransactionAmount): void
  92. {
  93. DB::transaction(static function () use ($journalEntry, $account, $convertedTransactionAmount) {
  94. $journalEntry->update([
  95. 'account_id' => $account->id,
  96. 'amount' => $convertedTransactionAmount,
  97. ]);
  98. });
  99. }
  100. /**
  101. * Handle the Transaction "deleting" event.
  102. */
  103. public function deleting(Transaction $transaction): void
  104. {
  105. DB::transaction(static function () use ($transaction) {
  106. $transaction->journalEntries()->each(fn (JournalEntry $entry) => $entry->delete());
  107. });
  108. }
  109. /**
  110. * Handle the Transaction "deleted" event.
  111. */
  112. public function deleted(Transaction $transaction): void
  113. {
  114. //
  115. }
  116. /**
  117. * Handle the Transaction "restored" event.
  118. */
  119. public function restored(Transaction $transaction): void
  120. {
  121. //
  122. }
  123. /**
  124. * Handle the Transaction "force deleted" event.
  125. */
  126. public function forceDeleted(Transaction $transaction): void
  127. {
  128. //
  129. }
  130. }