Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TransactionObserver.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // Additional check to avoid duplication during replication
  18. if ($transaction->journalEntries()->exists() || $transaction->type->isJournal() || str_starts_with($transaction->description, '(Copy of)')) {
  19. return;
  20. }
  21. [$debitAccount, $creditAccount] = $this->determineAccounts($transaction);
  22. if ($debitAccount === null || $creditAccount === null) {
  23. return;
  24. }
  25. $this->createJournalEntries($transaction, $debitAccount, $creditAccount);
  26. }
  27. /**
  28. * Handle the Transaction "updated" event.
  29. */
  30. public function updated(Transaction $transaction): void
  31. {
  32. $transaction->refresh(); // DO NOT REMOVE
  33. if ($transaction->type->isJournal() || $this->hasRelevantChanges($transaction) === false) {
  34. return;
  35. }
  36. $journalEntries = $transaction->journalEntries;
  37. $debitEntry = $journalEntries->where('type', JournalEntryType::Debit)->first();
  38. $creditEntry = $journalEntries->where('type', JournalEntryType::Credit)->first();
  39. if ($debitEntry === null || $creditEntry === null) {
  40. return;
  41. }
  42. [$debitAccount, $creditAccount] = $this->determineAccounts($transaction);
  43. if ($debitAccount === null || $creditAccount === null) {
  44. return;
  45. }
  46. $convertedTransactionAmount = $this->getConvertedTransactionAmount($transaction);
  47. $this->updateJournalEntriesForTransaction($debitEntry, $debitAccount, $convertedTransactionAmount);
  48. $this->updateJournalEntriesForTransaction($creditEntry, $creditAccount, $convertedTransactionAmount);
  49. }
  50. /**
  51. * Handle the Transaction "deleting" event.
  52. */
  53. public function deleting(Transaction $transaction): void
  54. {
  55. DB::transaction(static function () use ($transaction) {
  56. $transaction->journalEntries()->each(fn (JournalEntry $entry) => $entry->delete());
  57. });
  58. }
  59. private function determineAccounts(Transaction $transaction): array
  60. {
  61. $chartAccount = $transaction->account;
  62. $bankAccount = $transaction->bankAccount?->account;
  63. $debitAccount = $transaction->type->isWithdrawal() ? $chartAccount : $bankAccount;
  64. $creditAccount = $transaction->type->isWithdrawal() ? $bankAccount : $chartAccount;
  65. return [$debitAccount, $creditAccount];
  66. }
  67. private function createJournalEntries(Transaction $transaction, Account $debitAccount, Account $creditAccount): void
  68. {
  69. $convertedTransactionAmount = $this->getConvertedTransactionAmount($transaction);
  70. $debitAccount->journalEntries()->create([
  71. 'company_id' => $transaction->company_id,
  72. 'transaction_id' => $transaction->id,
  73. 'type' => JournalEntryType::Debit,
  74. 'amount' => $convertedTransactionAmount,
  75. 'description' => $transaction->description,
  76. ]);
  77. $creditAccount->journalEntries()->create([
  78. 'company_id' => $transaction->company_id,
  79. 'transaction_id' => $transaction->id,
  80. 'type' => JournalEntryType::Credit,
  81. 'amount' => $convertedTransactionAmount,
  82. 'description' => $transaction->description,
  83. ]);
  84. }
  85. private function getConvertedTransactionAmount(Transaction $transaction): string
  86. {
  87. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  88. $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
  89. $chartAccountCurrency = $transaction->account->currency_code;
  90. if ($bankAccountCurrency !== $defaultCurrency) {
  91. return $this->convertToDefaultCurrency($transaction->amount, $bankAccountCurrency, $defaultCurrency);
  92. } elseif ($chartAccountCurrency !== $defaultCurrency) {
  93. return $this->convertToDefaultCurrency($transaction->amount, $chartAccountCurrency, $defaultCurrency);
  94. }
  95. return $transaction->amount;
  96. }
  97. private function convertToDefaultCurrency(string $amount, string $fromCurrency, string $toCurrency): string
  98. {
  99. $amountInCents = CurrencyConverter::prepareForAccessor($amount, $fromCurrency);
  100. $convertedAmountInCents = CurrencyConverter::convertBalance($amountInCents, $fromCurrency, $toCurrency);
  101. return CurrencyConverter::prepareForMutator($convertedAmountInCents, $toCurrency);
  102. }
  103. private function hasRelevantChanges(Transaction $transaction): bool
  104. {
  105. return $transaction->wasChanged(['amount', 'account_id', 'bank_account_id', 'type']);
  106. }
  107. private function updateJournalEntriesForTransaction(JournalEntry $journalEntry, Account $account, string $convertedTransactionAmount): void
  108. {
  109. DB::transaction(static function () use ($journalEntry, $account, $convertedTransactionAmount) {
  110. $journalEntry->update([
  111. 'account_id' => $account->id,
  112. 'amount' => $convertedTransactionAmount,
  113. ]);
  114. });
  115. }
  116. }