Verify routine will check currency usage.

This commit is contained in:
James Cole 2018-11-10 10:50:12 +01:00
parent e491dda229
commit 5cc840e390
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 62 additions and 0 deletions

View File

@ -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.
*

View File

@ -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);
}
}

View File

@ -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) {