whereHas('account', static function ($query) use ($event) { $query->where('currency_code', $event->currency->code); }) ->get(); foreach ($bankAccounts as $bankAccount) { /** @var BankAccount $bankAccount */ $account = $bankAccount->account; $oldConvertedBalanceInCents = $account->ending_balance->convert()->getConvertedAmount(); $ratio = $event->newRate / $event->oldRate; $newConvertedBalance = bcmul($oldConvertedBalanceInCents, $ratio, 2); $newConvertedBalanceInCents = (int) round($newConvertedBalance); $differenceInCents = $newConvertedBalanceInCents - $oldConvertedBalanceInCents; if ($differenceInCents !== 0) { $gainOrLossAccountName = $differenceInCents > 0 ? 'Gain on Foreign Exchange' : 'Loss on Foreign Exchange'; $gainOrLossAccount = Account::where('name', $gainOrLossAccountName)->first(); $transactionType = $differenceInCents > 0 ? TransactionType::Deposit : TransactionType::Withdrawal; $description = "Exchange rate adjustment due to rate change from {$event->oldRate} to {$event->newRate}"; $absoluteDifferenceAmountInCents = abs($differenceInCents); $formattedSimpleDifference = CurrencyConverter::prepareForMutator($absoluteDifferenceAmountInCents, $bankAccount->account->currency_code); Transaction::create([ 'company_id' => $account->company_id, 'account_id' => $gainOrLossAccount->id, 'bank_account_id' => $bankAccount->id, 'type' => $transactionType, 'amount' => $formattedSimpleDifference, 'payment_channel' => 'other', 'posted_at' => now(), 'description' => $description, 'pending' => false, 'reviewed' => false, ]); } } }); } }