diff --git a/app/Console/Commands/VerifyDatabase.php b/app/Console/Commands/VerifyDatabase.php index 31b8b89658..d754f98899 100644 --- a/app/Console/Commands/VerifyDatabase.php +++ b/app/Console/Commands/VerifyDatabase.php @@ -28,12 +28,15 @@ namespace FireflyIII\Console\Commands; use Crypt; use DB; use FireflyIII\Models\Account; +use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AccountType; use FireflyIII\Models\Budget; +use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\Category; use FireflyIII\Models\LinkType; use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\Transaction; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\User\UserRepositoryInterface; @@ -41,6 +44,7 @@ use FireflyIII\User; use Illuminate\Console\Command; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Collection; use Log; use Schema; use stdClass; @@ -95,6 +99,7 @@ class VerifyDatabase extends Command $this->fixDoubleAmounts(); $this->fixBadMeta(); $this->removeBills(); + $this->enableCurrencies(); return 0; } @@ -150,6 +155,45 @@ class VerifyDatabase extends Command } } + /** + * Will make sure that all currencies in use are actually enabled. + */ + private function enableCurrencies(): void + { + $found = []; + // get all meta entries + /** @var Collection $meta */ + $meta = AccountMeta::where('name', 'currency_id')->groupBy('data')->get(['data']); + foreach ($meta as $entry) { + $found[] = (int)$entry->data; + } + + // get all from journals: + /** @var Collection $journals */ + $journals = TransactionJournal::groupBy('transaction_currency_id')->get(['transaction_currency_id']); + foreach ($journals as $entry) { + $found[] = (int)$entry->transaction_currency_id; + } + + // get all from transactions + /** @var Collection $transactions */ + $transactions = Transaction::groupBy('transaction_currency_id')->get(['transaction_currency_id']); + foreach ($transactions as $entry) { + $found[] = (int)$entry->transaction_currency_id; + } + + // get all from budget limits + /** @var Collection $limits */ + $limits = BudgetLimit::groupBy('transaction_currency_id')->get(['transaction_currency_id']); + foreach ($limits as $entry) { + $found[] = (int)$entry->transaction_currency_id; + } + + $found = array_unique($found); + TransactionCurrency::whereIn('id', $found)->update(['enabled' => true]); + + } + /** * Fix the situation where the matching transactions of a journal somehow have non-matching categories or budgets. * diff --git a/app/Models/TransactionCurrency.php b/app/Models/TransactionCurrency.php index 9bfe9e23c1..9309e4c8f7 100644 --- a/app/Models/TransactionCurrency.php +++ b/app/Models/TransactionCurrency.php @@ -95,4 +95,13 @@ class TransactionCurrency extends Model { return $this->hasMany(Transaction::class); } + + /** + * @codeCoverageIgnore + * @return HasMany + */ + public function budgetLimits(): HasMany + { + return $this->hasMany(BudgetLimit::class); + } } diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php index 3ae7a2a796..fc8f3a2611 100644 --- a/app/Repositories/Currency/CurrencyRepository.php +++ b/app/Repositories/Currency/CurrencyRepository.php @@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Currency; use Carbon\Carbon; use FireflyIII\Factory\TransactionCurrencyFactory; use FireflyIII\Models\AccountMeta; +use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\CurrencyExchangeRate; use FireflyIII\Models\Preference; use FireflyIII\Models\TransactionCurrency; @@ -93,6 +94,14 @@ class CurrencyRepository implements CurrencyRepositoryInterface return true; } + // is being used in budget limits + $budgetLimit = BudgetLimit::where('transaction_currency_id', $currency->id)->count(); + if ($budgetLimit > 0) { + Log::debug(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit)); + + return true; + } + // is the default currency for the user or the system $defaultCode = app('preferences')->getForUser($this->user, 'currencyPreference', config('firefly.default_currency', 'EUR'))->data; if ($currency->code === $defaultCode) {