From 144bc29eb32566fd1b16e8edc8b7322638c3b1d5 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 31 Mar 2020 07:03:37 +0200 Subject: [PATCH] Fix issue with budgets. --- .../Controllers/Budget/IndexController.php | 1 + app/Repositories/Budget/BudgetRepository.php | 2 +- app/Support/Amount.php | 27 +++++++++++++------ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/Budget/IndexController.php b/app/Http/Controllers/Budget/IndexController.php index 69d4e2dc4d..2a51d1588b 100644 --- a/app/Http/Controllers/Budget/IndexController.php +++ b/app/Http/Controllers/Budget/IndexController.php @@ -236,6 +236,7 @@ class IndexController extends Controller $repository->setBudgetOrder($budget, $index + 1); } } + app('preferences')->mark(); return response()->json(['OK']); } diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 07fdebbc86..36ba46b896 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -189,7 +189,7 @@ class BudgetRepository implements BudgetRepositoryInterface { /** @var Collection $set */ $set = $this->user->budgets()->where('active', 1) - ->orderBy('order', 'ASC') + ->orderBy('order', 'DESC') ->orderBy('name', 'ASC') ->get(); diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 0d8a905742..c5b69fbdbf 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -25,11 +25,9 @@ namespace FireflyIII\Support; use Crypt; use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; -use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Support\Collection; use Log; -use Preferences as Prefs; /** * Class Amount. @@ -204,7 +202,7 @@ class Amount if ($cache->has()) { return $cache->get(); // @codeCoverageIgnore } - $currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR')); + $currencyPreference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR')); $currency = TransactionCurrency::where('code', $currencyPreference->data)->first(); if ($currency) { @@ -230,7 +228,7 @@ class Amount if ($cache->has()) { return $cache->get(); // @codeCoverageIgnore } - $currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR')); + $currencyPreference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR')); $currency = TransactionCurrency::where('code', $currencyPreference->data)->first(); $cache->store($currency->symbol); @@ -252,6 +250,18 @@ class Amount return $this->getDefaultCurrencyByUser($user); } + /** + * @return \FireflyIII\Models\TransactionCurrency + */ + public function getSystemCurrency(): TransactionCurrency + { + if ('testing' === config('app.env')) { + Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__)); + } + + return TransactionCurrency::where('code', 'EUR')->first(); + } + /** * @param User $user * @@ -268,17 +278,18 @@ class Amount if ($cache->has()) { return $cache->get(); // @codeCoverageIgnore } - $currencyPreference = Prefs::getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR')); + $currencyPreference = app('preferences')->getForUser($user, 'currencyPreference', config('firefly.default_currency', 'EUR')); + $currencyPrefStr = $currencyPreference ? $currencyPreference->data : 'EUR'; // at this point the currency preference could be encrypted, if coming from an old version. Log::debug('Going to try to decrypt users currency preference.'); - $currencyCode = $this->tryDecrypt((string)$currencyPreference->data); + $currencyCode = $this->tryDecrypt((string) $currencyPrefStr); // could still be json encoded: if (strlen($currencyCode) > 3) { - $currencyCode = json_decode($currencyCode, true) ?? 'EUR'; + $currencyCode = json_decode($currencyCode, true, 512, JSON_THROW_ON_ERROR) ?? 'EUR'; } - + /** @var TransactionCurrency $currency */ $currency = TransactionCurrency::where('code', $currencyCode)->first(); if (null === $currency) { // get EUR