From 38c79c3dc455e7d2fb2eb3669e8ac34985c91312 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 19 Feb 2019 19:59:02 +0100 Subject: [PATCH] Upgrade fix for Softaculous. --- app/Console/Commands/UpgradeDatabase.php | 39 ++++++++++++++++-------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/app/Console/Commands/UpgradeDatabase.php b/app/Console/Commands/UpgradeDatabase.php index f255beb7e7..9a5cbeaf37 100644 --- a/app/Console/Commands/UpgradeDatabase.php +++ b/app/Console/Commands/UpgradeDatabase.php @@ -268,11 +268,11 @@ class UpgradeDatabase extends Command if (!Schema::hasTable('transaction_journals')) { return; } - $subQuery = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') - ->whereNull('transaction_journals.deleted_at') - ->whereNull('transactions.deleted_at') - ->groupBy(['transaction_journals.id']) - ->select(['transaction_journals.id', DB::raw('COUNT(transactions.id) AS t_count')]); + $subQuery = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->whereNull('transaction_journals.deleted_at') + ->whereNull('transactions.deleted_at') + ->groupBy(['transaction_journals.id']) + ->select(['transaction_journals.id', DB::raw('COUNT(transactions.id) AS t_count')]); /** @noinspection PhpStrictTypeCheckingInspection */ $result = DB::table(DB::raw('(' . $subQuery->toSql() . ') AS derived')) ->mergeBindings($subQuery->getQuery()) @@ -294,23 +294,38 @@ class UpgradeDatabase extends Command */ public function updateAccountCurrencies(): void { + Log::debug('Now in updateAccountCurrencies()'); + + $defaultConfig = (string)config('firefly.default_currency', 'EUR'); + Log::debug(sprintf('System default currency is "%s"', $defaultConfig)); + $accounts = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') ->whereIn('account_types.type', [AccountType::DEFAULT, AccountType::ASSET])->get(['accounts.*']); /** @var AccountRepositoryInterface $repository */ $repository = app(AccountRepositoryInterface::class); $accounts->each( - function (Account $account) use ($repository) { + function (Account $account) use ($repository, $defaultConfig) { $repository->setUser($account->user); // get users preference, fall back to system pref. - $defaultCurrencyCode = app('preferences')->getForUser($account->user, 'currencyPreference', config('firefly.default_currency', 'EUR'))->data; - $defaultCurrency = TransactionCurrency::where('code', $defaultCurrencyCode)->first(); - $accountCurrency = (int)$repository->getMetaValue($account, 'currency_id'); - $openingBalance = $account->getOpeningBalance(); - $obCurrency = (int)$openingBalance->transaction_currency_id; + + // expand and debug routine. + $defaultCurrencyCode = app('preferences')->getForUser($account->user, 'currencyPreference', $defaultConfig)->data; + Log::debug(sprintf('Default currency code is "%s"', var_export($defaultCurrencyCode, true))); + if (!is_string($defaultCurrencyCode)) { + $defaultCurrencyCode = $defaultConfig; + Log::debug(sprintf('Default currency code is not a string, now set to "%s"', $defaultCurrencyCode)); + } + $defaultCurrency = TransactionCurrency::where('code', $defaultCurrencyCode)->first(); + $accountCurrency = (int)$repository->getMetaValue($account, 'currency_id'); + $openingBalance = $account->getOpeningBalance(); + $obCurrency = (int)$openingBalance->transaction_currency_id; if (null === $defaultCurrency) { - throw new UnexpectedValueException('The default currency is NULL, and this is more or less impossible.'); + throw new UnexpectedValueException(sprintf('User has a preference for "%s", but this currency does not exist.', $defaultCurrencyCode)); } + Log::debug( + sprintf('Found default currency #%d (%s) while searching for "%s"', $defaultCurrency->id, $defaultCurrency->code, $defaultCurrencyCode) + ); // both 0? set to default currency: if (0 === $accountCurrency && 0 === $obCurrency) {