Skip to content

Firefly III: a personal finances manager

Fix exchange rate seeder

James Coled1232192ce02
diff --git a/database/seeders/ExchangeRateSeeder.php b/database/seeders/ExchangeRateSeeder.php
index df117a4f10..a46b56148e 100644
--- a/database/seeders/ExchangeRateSeeder.php
+++ b/database/seeders/ExchangeRateSeeder.php
@@ -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,
]
);
}
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 6c838d1dd1..3e19cb2fb6 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -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