Fix exchange rate seeder

This commit is contained in:
James Cole 2023-07-26 07:08:34 +02:00
parent dde7bcfc4c
commit d1232192ce
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
2 changed files with 37 additions and 27 deletions

View File

@ -29,7 +29,6 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Collection;
use Log;
/**
* Class ExchangeRateSeeder
@ -45,27 +44,34 @@ class ExchangeRateSeeder extends Seeder
{
$count = User::count();
if (0 === $count) {
Log::debug('Will not seed exchange rates yet.');
app('log')->debug('Will not seed exchange rates yet.');
return;
}
$users = User::get();
$date = config('cer.date');
$rates = config('cer.rates');
$users = User::get();
$date = config('cer.date');
$rates = config('cer.rates');
$usable = [];
foreach ($rates as $rate) {
$from = $this->getCurrency($rate[0]);
$to = $this->getCurrency($rate[1]);
if (null !== $from && null !== $to) {
$usable[] = [$from, $to, $rate[2]];
$euro = $this->getCurrency('EUR');
if (null === $euro) {
return;
}
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 */
foreach ($users as $user) {
foreach ($usable as $rate) {
if (!$this->hasRate($user, $rate[0], $rate[1], $date)) {
$this->addRate($user, $rate[0], $rate[1], $date, $rate[2]);
if (!$this->hasRate($user, $euro, $rate[0], $date)) {
$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 $to
* @param string $date
* @param string $date
*
* @return bool
*/
private function hasRate(User $user, TransactionCurrency $from, TransactionCurrency $to, string $date): bool
{
return $user->currencyExchangeRates()
->where('from_currency_id', $from->id)
->where('to_currency_id', $to->id)
->where('date', $date)
->count() > 0;
->where('from_currency_id', $from->id)
->where('to_currency_id', $to->id)
->where('date', $date)
->count() > 0;
}
/**
* @param User $user
* @param User $user
* @param TransactionCurrency $from
* @param TransactionCurrency $to
* @param string $date
* @param float $rate
* @param string $date
* @param float $rate
*
* @return void
*/
private function addRate(User $user, TransactionCurrency $from, TransactionCurrency $to, string $date, float $rate): void
{
/** @var User $user */
CurrencyExchangeRate::create(
[
'user_id' => $user->id,
'user_id' => $user->id,
'user_group_id' => $user->user_group_id ?? null,
'from_currency_id' => $from->id,
'to_currency_id' => $to->id,
'date' => $date,
'rate' => $rate,
'to_currency_id' => $to->id,
'date' => $date,
'rate' => $rate,
]
);
}

View File

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