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.

UpdateAccountBalances.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Listeners;
  3. use App\Enums\Accounting\TransactionType;
  4. use App\Events\CurrencyRateChanged;
  5. use App\Models\Accounting\Account;
  6. use App\Models\Accounting\Transaction;
  7. use App\Models\Banking\BankAccount;
  8. use App\Utilities\Currency\CurrencyConverter;
  9. use Illuminate\Support\Facades\DB;
  10. class UpdateAccountBalances
  11. {
  12. /**
  13. * Create the event listener.
  14. */
  15. public function __construct()
  16. {
  17. //
  18. }
  19. /**
  20. * Handle the event.
  21. */
  22. public function handle(CurrencyRateChanged $event): void
  23. {
  24. DB::transaction(static function () use ($event) {
  25. $bankAccounts = BankAccount::with('account')
  26. ->whereHas('account', static function ($query) use ($event) {
  27. $query->where('currency_code', $event->currency->code);
  28. })
  29. ->get();
  30. foreach ($bankAccounts as $bankAccount) {
  31. /** @var BankAccount $bankAccount */
  32. $account = $bankAccount->account;
  33. $oldConvertedBalanceInCents = $account->ending_balance->convert()->getConvertedValue();
  34. $newConvertedBalance = ($event->newRate / $event->oldRate) * $oldConvertedBalanceInCents;
  35. $newConvertedBalanceInCents = (int) round($newConvertedBalance);
  36. $differenceInCents = $newConvertedBalanceInCents - $oldConvertedBalanceInCents;
  37. if ($differenceInCents !== 0) {
  38. $gainOrLossAccountName = $differenceInCents > 0 ? 'Gain on Foreign Exchange' : 'Loss on Foreign Exchange';
  39. $gainOrLossAccount = Account::where('name', $gainOrLossAccountName)->first();
  40. $transactionType = $differenceInCents > 0 ? TransactionType::Deposit : TransactionType::Withdrawal;
  41. $description = "Exchange rate adjustment due to rate change from {$event->oldRate} to {$event->newRate}";
  42. $absoluteDifferenceAmountInCents = abs($differenceInCents);
  43. $formattedSimpleDifference = CurrencyConverter::prepareForMutator($absoluteDifferenceAmountInCents, $bankAccount->account->currency_code);
  44. Transaction::create([
  45. 'company_id' => $account->company_id,
  46. 'account_id' => $gainOrLossAccount->id,
  47. 'bank_account_id' => $bankAccount->id,
  48. 'type' => $transactionType,
  49. 'amount' => $formattedSimpleDifference,
  50. 'payment_channel' => 'other',
  51. 'posted_at' => now(),
  52. 'description' => $description,
  53. 'pending' => false,
  54. 'reviewed' => false,
  55. ]);
  56. }
  57. }
  58. });
  59. }
  60. }