Fix exchange rate seeder

This commit is contained in:
James Cole
2023-07-26 07:08:34 +02:00
parent dde7bcfc4c
commit d1232192ce
2 changed files with 37 additions and 27 deletions

View File

@@ -29,7 +29,6 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log;
/** /**
* Class ExchangeRateSeeder * Class ExchangeRateSeeder
@@ -45,27 +44,34 @@ class ExchangeRateSeeder extends Seeder
{ {
$count = User::count(); $count = User::count();
if (0 === $count) { if (0 === $count) {
Log::debug('Will not seed exchange rates yet.'); app('log')->debug('Will not seed exchange rates yet.');
return; return;
} }
$users = User::get(); $users = User::get();
$date = config('cer.date'); $date = config('cer.date');
$rates = config('cer.rates'); $rates = config('cer.rates');
$usable = []; $usable = [];
foreach ($rates as $rate) { $euro = $this->getCurrency('EUR');
$from = $this->getCurrency($rate[0]); if (null === $euro) {
$to = $this->getCurrency($rate[1]); return;
if (null !== $from && null !== $to) { }
$usable[] = [$from, $to, $rate[2]]; foreach ($rates as $currencyCode => $rate) {
// grab opposing currency
$foreign = $this->getCurrency($currencyCode);
if (null !== $foreign) {
// save rate in array:
$usable[] = [$foreign, $rate];
app('log')->debug(sprintf('Have default exchange rate from %s to %s.', $euro->code, $foreign->code));
} }
} }
unset($rates, $from, $to, $rate); unset($rates, $foreign, $rate);
// for each user, for each rate, check and save
/** @var User $user */ /** @var User $user */
foreach ($users as $user) { foreach ($users as $user) {
foreach ($usable as $rate) { foreach ($usable as $rate) {
if (!$this->hasRate($user, $rate[0], $rate[1], $date)) { if (!$this->hasRate($user, $euro, $rate[0], $date)) {
$this->addRate($user, $rate[0], $rate[1], $date, $rate[2]); $this->addRate($user, $euro, $rate[0], $date, $rate[1]);
} }
} }
} }
@@ -82,41 +88,41 @@ class ExchangeRateSeeder extends Seeder
} }
/** /**
* @param User $user * @param User $user
* @param TransactionCurrency $from * @param TransactionCurrency $from
* @param TransactionCurrency $to * @param TransactionCurrency $to
* @param string $date * @param string $date
* *
* @return bool * @return bool
*/ */
private function hasRate(User $user, TransactionCurrency $from, TransactionCurrency $to, string $date): bool private function hasRate(User $user, TransactionCurrency $from, TransactionCurrency $to, string $date): bool
{ {
return $user->currencyExchangeRates() return $user->currencyExchangeRates()
->where('from_currency_id', $from->id) ->where('from_currency_id', $from->id)
->where('to_currency_id', $to->id) ->where('to_currency_id', $to->id)
->where('date', $date) ->where('date', $date)
->count() > 0; ->count() > 0;
} }
/** /**
* @param User $user * @param User $user
* @param TransactionCurrency $from * @param TransactionCurrency $from
* @param TransactionCurrency $to * @param TransactionCurrency $to
* @param string $date * @param string $date
* @param float $rate * @param float $rate
* *
* @return void * @return void
*/ */
private function addRate(User $user, TransactionCurrency $from, TransactionCurrency $to, string $date, float $rate): void private function addRate(User $user, TransactionCurrency $from, TransactionCurrency $to, string $date, float $rate): void
{ {
/** @var User $user */
CurrencyExchangeRate::create( CurrencyExchangeRate::create(
[ [
'user_id' => $user->id, 'user_id' => $user->id,
'user_group_id' => $user->user_group_id ?? null,
'from_currency_id' => $from->id, 'from_currency_id' => $from->id,
'to_currency_id' => $to->id, 'to_currency_id' => $to->id,
'date' => $date, 'date' => $date,
'rate' => $rate, 'rate' => $rate,
] ]
); );
} }

View File

@@ -2614,6 +2614,10 @@ return [
'ale_action_remove_from_piggy' => 'Piggy bank', 'ale_action_remove_from_piggy' => 'Piggy bank',
'ale_action_add_tag' => 'Added tag', 'ale_action_add_tag' => 'Added tag',
// dashboard
'enable_auto_convert' => 'Enable currency conversion',
'disable_auto_convert' => 'Disable currency conversion',
]; ];
// Ignore this comment // Ignore this comment