Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TransactionObserver.php 5.0KB

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