Clean up commands.

This commit is contained in:
James Cole
2024-12-28 07:35:20 +01:00
parent 89ab360391
commit 0a089efcac
40 changed files with 105 additions and 162 deletions

View File

@@ -28,6 +28,8 @@ use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException; use Carbon\Exceptions\InvalidDateException;
use Carbon\Exceptions\InvalidFormatException; use Carbon\Exceptions\InvalidFormatException;
use FireflyIII\Models\Preference; use FireflyIII\Models\Preference;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Facades\Steam;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
@@ -68,8 +70,8 @@ abstract class Controller extends BaseController
function ($request, $next) { function ($request, $next) {
$this->parameters = $this->getParameters(); $this->parameters = $this->getParameters();
if (auth()->check()) { if (auth()->check()) {
$language = app('steam')->getLanguage(); $language = Steam::getLanguage();
$this->convertToNative = app('preferences')->get('convert_to_native', false)->data; $this->convertToNative = Amount::convertToNative();
app()->setLocale($language); app()->setLocale($language);
} }

View File

@@ -123,8 +123,8 @@ class BasicController extends Controller
private function getBalanceInformation(Carbon $start, Carbon $end): array private function getBalanceInformation(Carbon $start, Carbon $end): array
{ {
// some config settings // some config settings
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = Amount::convertToNative();
$default = app('amount')->getDefaultCurrency(); $default = Amount::getDefaultCurrency();
// prep some arrays: // prep some arrays:
$incomes = []; $incomes = [];
$expenses = []; $expenses = [];

View File

@@ -59,6 +59,10 @@ class CorrectsNativeAmounts extends Command
*/ */
public function handle(): int public function handle(): int
{ {
if(!config('cer.enabled')) {
$this->friendlyInfo('This command will not run because currency exchange rates are disabled.');
return 0;
}
Log::debug('Will update all native amounts. This may take some time.'); Log::debug('Will update all native amounts. This may take some time.');
$this->friendlyWarning('Recalculating native amounts for all objects. This may take some time!'); $this->friendlyWarning('Recalculating native amounts for all objects. This may take some time!');

View File

@@ -76,9 +76,6 @@ class AddsTransactionIdentifiers extends Command
$this->updateJournalIdentifiers($journal); $this->updateJournalIdentifiers($journal);
} }
if (0 === $this->count) {
$this->friendlyPositive('All split journal transaction identifiers are OK.');
}
if (0 !== $this->count) { if (0 !== $this->count) {
$this->friendlyInfo(sprintf('Fixed %d split journal transaction identifier(s).', $this->count)); $this->friendlyInfo(sprintf('Fixed %d split journal transaction identifier(s).', $this->count));
} }

View File

@@ -70,7 +70,6 @@ class RemovesDatabaseDecryption extends Command
private function decryptTable(string $table, array $fields): void private function decryptTable(string $table, array $fields): void
{ {
if ($this->isDecrypted($table)) { if ($this->isDecrypted($table)) {
$this->friendlyInfo(sprintf('No decryption required for table "%s".', $table));
return; return;
} }

View File

@@ -62,9 +62,6 @@ class UpgradesAccountCurrencies extends Command
} }
$this->updateAccountCurrencies(); $this->updateAccountCurrencies();
if (0 === $this->count) {
$this->friendlyPositive('All account currencies are OK.');
}
if (0 !== $this->count) { if (0 !== $this->count) {
$this->friendlyInfo(sprintf('Corrected %d account(s).', $this->count)); $this->friendlyInfo(sprintf('Corrected %d account(s).', $this->count));
} }

View File

@@ -73,9 +73,6 @@ class UpgradesAccountMetaData extends Command
$this->markAsExecuted(); $this->markAsExecuted();
if (0 === $count) {
$this->friendlyPositive('All account meta is OK.');
}
if (0 !== $count) { if (0 !== $count) {
$this->friendlyInfo(sprintf('Renamed %d account meta entries (entry).', $count)); $this->friendlyInfo(sprintf('Renamed %d account meta entries (entry).', $count));
} }

View File

@@ -79,9 +79,6 @@ class UpgradesAttachments extends Command
++$count; ++$count;
} }
} }
if (0 === $count) {
$this->friendlyPositive('All attachments are OK.');
}
if (0 !== $count) { if (0 !== $count) {
$this->friendlyInfo(sprintf('Updated %d attachment(s).', $count)); $this->friendlyInfo(sprintf('Updated %d attachment(s).', $count));
} }

View File

@@ -73,9 +73,6 @@ class UpgradesBillsToRules extends Command
$this->migrateUser($user); $this->migrateUser($user);
} }
if (0 === $this->count) {
$this->friendlyPositive('All bills are OK.');
}
if (0 !== $this->count) { if (0 !== $this->count) {
$this->friendlyInfo(sprintf('Verified and fixed %d bill(s).', $this->count)); $this->friendlyInfo(sprintf('Verified and fixed %d bill(s).', $this->count));
} }

View File

@@ -77,9 +77,6 @@ class UpgradesBudgetLimits extends Command
} }
} }
} }
if (0 === $count) {
$this->friendlyPositive('All budget limits are OK.');
}
$this->markAsExecuted(); $this->markAsExecuted();
return 0; return 0;

View File

@@ -55,7 +55,6 @@ class UpgradesCreditCardLiabilities extends Command
$ccType = AccountType::where('type', AccountType::CREDITCARD)->first(); $ccType = AccountType::where('type', AccountType::CREDITCARD)->first();
$debtType = AccountType::where('type', AccountType::DEBT)->first(); $debtType = AccountType::where('type', AccountType::DEBT)->first();
if (null === $ccType || null === $debtType) { if (null === $ccType || null === $debtType) {
$this->friendlyPositive('No incorrectly stored credit card liabilities.');
$this->markAsExecuted(); $this->markAsExecuted();
return 0; return 0;
@@ -73,9 +72,6 @@ class UpgradesCreditCardLiabilities extends Command
'Credit card liability types are no longer supported and have been converted to generic debts. See: https://bit.ly/FF3-credit-cards' 'Credit card liability types are no longer supported and have been converted to generic debts. See: https://bit.ly/FF3-credit-cards'
); );
} }
if (0 === $accounts->count()) {
$this->friendlyPositive('No incorrectly stored credit card liabilities.');
}
$this->markAsExecuted(); $this->markAsExecuted();
return 0; return 0;

View File

@@ -57,7 +57,6 @@ class UpgradesDatabase extends Command
'upgrade:480-account-meta', 'upgrade:480-account-meta',
'upgrade:481-recurrence-meta', 'upgrade:481-recurrence-meta',
'upgrade:500-tag-locations', 'upgrade:500-tag-locations',
'upgrade:550-recurrence-type',
'upgrade:560-liabilities', 'upgrade:560-liabilities',
'upgrade:600-liabilities', 'upgrade:600-liabilities',
'upgrade:550-budget-limit-periods', 'upgrade:550-budget-limit-periods',

View File

@@ -68,7 +68,7 @@ class UpgradesJournalMetaData extends Command
private function isMigrated(): bool private function isMigrated(): bool
{ {
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false); $configVar = app('fireflyconfig')->get(UpgradesToGroups::CONFIG_NAME, false);
return (bool) $configVar->data; return (bool) $configVar->data;
} }

View File

@@ -72,9 +72,6 @@ class UpgradesJournalNotes extends Command
++$count; ++$count;
} }
if (0 === $count) {
$this->friendlyPositive('No notes to migrate.');
}
if (0 !== $count) { if (0 !== $count) {
$this->friendlyInfo(sprintf('Migrated %d note(s).', $count)); $this->friendlyInfo(sprintf('Migrated %d note(s).', $count));
} }

View File

@@ -52,9 +52,6 @@ class UpgradesRecurrenceMetaData extends Command
} }
$count = $this->migrateMetaData(); $count = $this->migrateMetaData();
if (0 === $count) {
$this->friendlyPositive('No recurrence meta data migrated.');
}
if ($count > 0) { if ($count > 0) {
$this->friendlyInfo(sprintf('Migrated %d meta data entries', $count)); $this->friendlyInfo(sprintf('Migrated %d meta data entries', $count));
} }

View File

@@ -1,67 +0,0 @@
<?php
/*
* MigrateRecurrenceType.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Console\Commands\Upgrade;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use Illuminate\Console\Command;
class UpgradesRecurrenceType extends Command
{
use ShowsFriendlyMessages;
public const string CONFIG_NAME = '550_migrate_recurrence_type';
protected $description = 'Migrate transaction type of recurring transaction.';
protected $signature = 'upgrade:550-recurrence-type {--F|force : Force the execution of this command.}';
/**
* Execute the console command.
*/
public function handle(): int
{
if ($this->isExecuted() && true !== $this->option('force')) {
$this->friendlyInfo('This command has already been executed.');
return 0;
}
$this->friendlyWarning('This command has been disabled.');
$this->markAsExecuted();
return 0;
}
private function isExecuted(): bool
{
$configVar = app('fireflyconfig')->get(self::CONFIG_NAME, false);
return (bool) $configVar?->data;
}
private function markAsExecuted(): void
{
app('fireflyconfig')->set(self::CONFIG_NAME, true);
}
}

View File

@@ -72,9 +72,6 @@ class UpgradesToGroups extends Command
if (0 !== $this->count) { if (0 !== $this->count) {
$this->friendlyInfo(sprintf('Migrated %d transaction journal(s).', $this->count)); $this->friendlyInfo(sprintf('Migrated %d transaction journal(s).', $this->count));
} }
if (0 === $this->count) {
$this->friendlyPositive('No journals to migrate to groups.');
}
$this->markAsMigrated(); $this->markAsMigrated();
return 0; return 0;
@@ -363,9 +360,6 @@ class UpgradesToGroups extends Command
$this->giveGroup($array); $this->giveGroup($array);
} }
} }
if (0 === $total) {
$this->friendlyPositive('No need to convert transaction journals.');
}
} }
private function giveGroup(array $array): void private function giveGroup(array $array): void

View File

@@ -69,12 +69,6 @@ class UpgradesTransferCurrencies extends Command
$this->startUpdateRoutine(); $this->startUpdateRoutine();
$this->markAsExecuted(); $this->markAsExecuted();
if (0 === $this->count) {
$this->friendlyPositive('All transfers have correct currency information.');
return 0;
}
$this->friendlyInfo(sprintf('Verified currency information of %d transfer(s).', $this->count)); $this->friendlyInfo(sprintf('Verified currency information of %d transfer(s).', $this->count));
return 0; return 0;

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface; use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Http\Api\ExchangeRateConverter; use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -43,6 +44,9 @@ class AccountObserver
private function updateNativeAmount(Account $account): void private function updateNativeAmount(Account $account): void
{ {
if(!Amount::convertToNative($account->user)) {
return;
}
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup); $userCurrency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
$repository = app(AccountRepositoryInterface::class); $repository = app(AccountRepositoryInterface::class);
$currency = $repository->getAccountCurrency($account); $currency = $repository->getAccountCurrency($account);

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Handlers\Observer; namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\AutoBudget; use FireflyIII\Models\AutoBudget;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Http\Api\ExchangeRateConverter; use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -44,6 +45,9 @@ class AutoBudgetObserver
private function updateNativeAmount(AutoBudget $autoBudget): void private function updateNativeAmount(AutoBudget $autoBudget): void
{ {
if(!Amount::convertToNative($autoBudget->budget->user)) {
return;
}
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($autoBudget->budget->user->userGroup); $userCurrency = app('amount')->getDefaultCurrencyByUserGroup($autoBudget->budget->user->userGroup);
$autoBudget->native_amount = null; $autoBudget->native_amount = null;
if ($autoBudget->transactionCurrency->id !== $userCurrency->id) { if ($autoBudget->transactionCurrency->id !== $userCurrency->id) {

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Handlers\Observer; namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\AvailableBudget;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Http\Api\ExchangeRateConverter; use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -44,6 +45,9 @@ class AvailableBudgetObserver
private function updateNativeAmount(AvailableBudget $availableBudget): void private function updateNativeAmount(AvailableBudget $availableBudget): void
{ {
if(!Amount::convertToNative($availableBudget->user)) {
return;
}
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($availableBudget->user->userGroup); $userCurrency = app('amount')->getDefaultCurrencyByUserGroup($availableBudget->user->userGroup);
$availableBudget->native_amount = null; $availableBudget->native_amount = null;
if ($availableBudget->transactionCurrency->id !== $userCurrency->id) { if ($availableBudget->transactionCurrency->id !== $userCurrency->id) {

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Handlers\Observer; namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Http\Api\ExchangeRateConverter; use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -55,6 +56,9 @@ class BillObserver
private function updateNativeAmount(Bill $bill): void private function updateNativeAmount(Bill $bill): void
{ {
if(!Amount::convertToNative($bill->user)) {
return;
}
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($bill->user->userGroup); $userCurrency = app('amount')->getDefaultCurrencyByUserGroup($bill->user->userGroup);
$bill->native_amount_min = null; $bill->native_amount_min = null;
$bill->native_amount_max = null; $bill->native_amount_max = null;

View File

@@ -44,6 +44,9 @@ class BudgetLimitObserver
private function updateNativeAmount(BudgetLimit $budgetLimit): void private function updateNativeAmount(BudgetLimit $budgetLimit): void
{ {
if(!Amount::convertToNative($budgetLimit->budget->user)) {
return;
}
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($budgetLimit->budget->user->userGroup); $userCurrency = app('amount')->getDefaultCurrencyByUserGroup($budgetLimit->budget->user->userGroup);
$budgetLimit->native_amount = null; $budgetLimit->native_amount = null;
if ($budgetLimit->transactionCurrency->id !== $userCurrency->id) { if ($budgetLimit->transactionCurrency->id !== $userCurrency->id) {

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Handlers\Observer; namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Http\Api\ExchangeRateConverter; use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -44,6 +45,9 @@ class PiggyBankEventObserver
private function updateNativeAmount(PiggyBankEvent $event): void private function updateNativeAmount(PiggyBankEvent $event): void
{ {
if(!Amount::convertToNative($event->piggyBank->accounts()->first()->user)) {
return;
}
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($event->piggyBank->accounts()->first()->user->userGroup); $userCurrency = app('amount')->getDefaultCurrencyByUserGroup($event->piggyBank->accounts()->first()->user->userGroup);
$event->native_amount = null; $event->native_amount = null;
if ($event->piggyBank->transactionCurrency->id !== $userCurrency->id) { if ($event->piggyBank->transactionCurrency->id !== $userCurrency->id) {

View File

@@ -78,6 +78,7 @@ class PiggyBankObserver
if ($piggyBank->transactionCurrency->id !== $userCurrency->id) { if ($piggyBank->transactionCurrency->id !== $userCurrency->id) {
$converter = new ExchangeRateConverter(); $converter = new ExchangeRateConverter();
$converter->setIgnoreSettings(true); $converter->setIgnoreSettings(true);
$converter->setUserGroup($group);
$piggyBank->native_target_amount = $converter->convert($piggyBank->transactionCurrency, $userCurrency, today(), $piggyBank->target_amount); $piggyBank->native_target_amount = $converter->convert($piggyBank->transactionCurrency, $userCurrency, today(), $piggyBank->target_amount);
} }
$piggyBank->saveQuietly(); $piggyBank->saveQuietly();

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Handlers\Observer; namespace FireflyIII\Handlers\Observer;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Http\Api\ExchangeRateConverter; use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\Support\Models\AccountBalanceCalculator; use FireflyIII\Support\Models\AccountBalanceCalculator;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -67,6 +68,9 @@ class TransactionObserver
private function updateNativeAmount(Transaction $transaction): void private function updateNativeAmount(Transaction $transaction): void
{ {
if(!Amount::convertToNative($transaction->transactionJournal->user)) {
return;
}
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($transaction->transactionJournal->user->userGroup); $userCurrency = app('amount')->getDefaultCurrencyByUserGroup($transaction->transactionJournal->user->userGroup);
$transaction->native_amount = null; $transaction->native_amount = null;
$transaction->native_foreign_amount = null; $transaction->native_foreign_amount = null;

View File

@@ -66,7 +66,7 @@ class NetWorth implements NetWorthInterface
public function byAccounts(Collection $accounts, Carbon $date): array public function byAccounts(Collection $accounts, Carbon $date): array
{ {
// start in the past, end in the future? use $date // start in the past, end in the future? use $date
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = Amount::convertToNative();
$ids = implode(',', $accounts->pluck('id')->toArray()); $ids = implode(',', $accounts->pluck('id')->toArray());
$cache = new CacheProperties(); $cache = new CacheProperties();
$cache->addProperty($date); $cache->addProperty($date);

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers; namespace FireflyIII\Http\Controllers;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Facades\Steam; use FireflyIII\Support\Facades\Steam;
use FireflyIII\Support\Http\Controllers\RequestInformation; use FireflyIII\Support\Http\Controllers\RequestInformation;
use FireflyIII\Support\Http\Controllers\UserNavigation; use FireflyIII\Support\Http\Controllers\UserNavigation;
@@ -121,7 +122,7 @@ abstract class Controller extends BaseController
$language = Steam::getLanguage(); $language = Steam::getLanguage();
$locale = Steam::getLocale(); $locale = Steam::getLocale();
$darkMode = app('preferences')->get('darkMode', 'browser')->data; $darkMode = app('preferences')->get('darkMode', 'browser')->data;
$this->convertToNative =app('preferences')->get('convert_to_native', false)->data; $this->convertToNative =Amount::convertToNative();
$page = $this->getPageName(); $page = $this->getPageName();
$shownDemo = $this->hasSeenDemo(); $shownDemo = $this->hasSeenDemo();
View::share('language', $language); View::share('language', $language);

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\Http\Controllers\ExchangeRates;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use Illuminate\View\View; use Illuminate\View\View;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class IndexController extends Controller class IndexController extends Controller
{ {
@@ -46,6 +47,9 @@ class IndexController extends Controller
return $next($request); return $next($request);
} }
); );
if(!config('cer.enabled')) {
throw new NotFoundHttpException();
}
} }
public function index(): View public function index(): View

View File

@@ -527,7 +527,7 @@ class BillRepository implements BillRepositoryInterface
Log::debug(sprintf('sumPaidInRange from %s to %s', $start->toW3cString(), $end->toW3cString())); Log::debug(sprintf('sumPaidInRange from %s to %s', $start->toW3cString(), $end->toW3cString()));
$bills = $this->getActiveBills(); $bills = $this->getActiveBills();
$return = []; $return = [];
$convertToNative = app('preferences')->getForUser($this->user, 'convert_to_native', false)->data; $convertToNative = Amount::convertToNative($this->user);
$default = app('amount')->getDefaultCurrency(); $default = app('amount')->getDefaultCurrency();
/** @var Bill $bill */ /** @var Bill $bill */
@@ -572,7 +572,7 @@ class BillRepository implements BillRepositoryInterface
app('log')->debug(sprintf('Now in sumUnpaidInRange("%s", "%s")', $start->format('Y-m-d'), $end->format('Y-m-d'))); app('log')->debug(sprintf('Now in sumUnpaidInRange("%s", "%s")', $start->format('Y-m-d'), $end->format('Y-m-d')));
$bills = $this->getActiveBills(); $bills = $this->getActiveBills();
$return = []; $return = [];
$convertToNative = app('preferences')->getForUser($this->user, 'convert_to_native', false)->data; $convertToNative = Amount::convertToNative($this->user);
$default = app('amount')->getDefaultCurrency(); $default = app('amount')->getDefaultCurrency();
/** @var Bill $bill */ /** @var Bill $bill */

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\Repositories\Budget;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
@@ -134,8 +135,8 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
; ;
// use native amount if necessary? // use native amount if necessary?
$convertToNative = app('preferences')->getForUser($this->user, 'convert_to_native', false)->data; $convertToNative = Amount::convertToNative($this->user);
$default = app('amount')->getDefaultCurrency(); $default = Amount::getDefaultCurrency();
/** @var AvailableBudget $availableBudget */ /** @var AvailableBudget $availableBudget */
foreach ($availableBudgets as $availableBudget) { foreach ($availableBudgets as $availableBudget) {

View File

@@ -223,8 +223,8 @@ class OperationsRepository implements OperationsRepositoryInterface
$selection = new Collection(); $selection = new Collection();
// default currency information for native stuff. // default currency information for native stuff.
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = Amount::convertToNative($this->user);
$default = app('amount')->getDefaultCurrency(); $default = Amount::getDefaultCurrency();
/** @var Account $account */ /** @var Account $account */
foreach ($subset as $account) { foreach ($subset as $account) {

View File

@@ -154,8 +154,8 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface
$journals = $collector->getExtractedJournals(); $journals = $collector->getExtractedJournals();
$array = []; $array = [];
// default currency information for native stuff. // default currency information for native stuff.
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = Amount::convertToNative($this->user);;
$default = app('amount')->getDefaultCurrency(); $default = Amount::getDefaultCurrency();
foreach ($journals as $journal) { foreach ($journals as $journal) {
// Almost the same as in \FireflyIII\Repositories\Budget\OperationsRepository::sumExpenses // Almost the same as in \FireflyIII\Repositories\Budget\OperationsRepository::sumExpenses

View File

@@ -331,8 +331,8 @@ class OperationsRepository implements OperationsRepositoryInterface
$collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value]); $collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value]);
// default currency information for native stuff. // default currency information for native stuff.
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = Amount::convertToNative($this->user);;
$default = app('amount')->getDefaultCurrency(); $default = Amount::getDefaultCurrency();
if (null !== $accounts && $accounts->count() > 0) { if (null !== $accounts && $accounts->count() > 0) {
$collector->setAccounts($accounts); $collector->setAccounts($accounts);
} }
@@ -411,8 +411,8 @@ class OperationsRepository implements OperationsRepositoryInterface
} }
$collector->setCategories($categories); $collector->setCategories($categories);
$journals = $collector->getExtractedJournals(); $journals = $collector->getExtractedJournals();
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = Amount::convertToNative($this->user);;
$default = app('amount')->getDefaultCurrency(); $default = Amount::getDefaultCurrency();
$array = []; $array = [];
foreach ($journals as $journal) { foreach ($journals as $journal) {

View File

@@ -30,7 +30,6 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\UserGroup; use FireflyIII\Models\UserGroup;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
/** /**
* Class Amount. * Class Amount.
@@ -54,13 +53,13 @@ class Amount
*/ */
public function getAmountFromJournal(array $journal): string public function getAmountFromJournal(array $journal): string
{ {
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = $this->convertToNative();
$currency = app('amount')->getDefaultCurrency(); $currency = $this->getDefaultCurrency();
$field = $convertToNative && $currency->id !== $journal['currency_id'] ? 'native_amount' : 'amount'; $field = $convertToNative && $currency->id !== $journal['currency_id'] ? 'native_amount' : 'amount';
$amount = $journal[$field] ?? '0'; $amount = $journal[$field] ?? '0';
// Log::debug(sprintf('Field is %s, amount is %s', $field, $amount)); // Log::debug(sprintf('Field is %s, amount is %s', $field, $amount));
// fallback, the transaction has a foreign amount in $currency. // fallback, the transaction has a foreign amount in $currency.
if ($convertToNative && null !== $journal['foreign_amount'] && $currency->id === (int)$journal['foreign_currency_id']) { if ($convertToNative && null !== $journal['foreign_amount'] && $currency->id === (int) $journal['foreign_currency_id']) {
$amount = $journal['foreign_amount']; $amount = $journal['foreign_amount'];
// Log::debug(sprintf('Overruled, amount is now %s', $amount)); // Log::debug(sprintf('Overruled, amount is now %s', $amount));
} }
@@ -68,22 +67,30 @@ class Amount
return $amount; return $amount;
} }
public function convertToNative(?User $user = null): bool
{
if (null === $user) {
return \FireflyIII\Support\Facades\Preferences::get('convert_to_native', false)->data && config('cer.enabled');
}
return \FireflyIII\Support\Facades\Preferences::getForUser($user, 'convert_to_native', false)->data && config('cer.enabled');
}
/** /**
* Experimental function to see if we can quickly and quietly get the amount from a journal. * Experimental function to see if we can quickly and quietly get the amount from a journal.
* This depends on the user's default currency and the wish to have it converted. * This depends on the user's default currency and the wish to have it converted.
*/ */
public function getAmountFromJournalObject(TransactionJournal $journal): string public function getAmountFromJournalObject(TransactionJournal $journal): string
{ {
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = $this->convertToNative();
$currency = app('amount')->getDefaultCurrency(); $currency = $this->getDefaultCurrency();
$field = $convertToNative && $currency->id !== $journal->transaction_currency_id ? 'native_amount' : 'amount'; $field = $convertToNative && $currency->id !== $journal->transaction_currency_id ? 'native_amount' : 'amount';
/** @var null|Transaction $sourceTransaction */ /** @var null|Transaction $sourceTransaction */
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first(); $sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
if (null === $sourceTransaction) { if (null === $sourceTransaction) {
return '0'; return '0';
} }
$amount = $sourceTransaction->{$field}; $amount = $sourceTransaction->{$field} ?? '0';
if ((int) $sourceTransaction->foreign_currency_id === $currency->id) { if ((int) $sourceTransaction->foreign_currency_id === $currency->id) {
// use foreign amount instead! // use foreign amount instead!
$amount = (string) $sourceTransaction->foreign_amount; // hard coded to be foreign amount. $amount = (string) $sourceTransaction->foreign_amount; // hard coded to be foreign amount.
@@ -102,15 +109,15 @@ class Amount
*/ */
public function formatFlat(string $symbol, int $decimalPlaces, string $amount, ?bool $coloured = null): string public function formatFlat(string $symbol, int $decimalPlaces, string $amount, ?bool $coloured = null): string
{ {
$locale = app('steam')->getLocale(); $locale = app('steam')->getLocale();
$rounded = app('steam')->bcround($amount, $decimalPlaces); $rounded = app('steam')->bcround($amount, $decimalPlaces);
$coloured ??= true; $coloured ??= true;
$fmt = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $fmt = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol); $fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol);
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces); $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $decimalPlaces);
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces); $fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimalPlaces);
$result = (string) $fmt->format((float) $rounded); // intentional float $result = (string) $fmt->format((float) $rounded); // intentional float
if (true === $coloured) { if (true === $coloured) {
if (1 === bccomp($rounded, '0')) { if (1 === bccomp($rounded, '0')) {
@@ -156,7 +163,7 @@ class Amount
public function getDefaultCurrencyByUserGroup(UserGroup $userGroup): TransactionCurrency public function getDefaultCurrencyByUserGroup(UserGroup $userGroup): TransactionCurrency
{ {
$cache = new CacheProperties(); $cache = new CacheProperties();
$cache->addProperty('getDefaultCurrencyByGroup'); $cache->addProperty('getDefaultCurrencyByGroup');
$cache->addProperty($userGroup->id); $cache->addProperty($userGroup->id);
if ($cache->has()) { if ($cache->has()) {
@@ -219,20 +226,20 @@ class Amount
private function getLocaleInfo(): array private function getLocaleInfo(): array
{ {
// get config from preference, not from translation: // get config from preference, not from translation:
$locale = app('steam')->getLocale(); $locale = app('steam')->getLocale();
$array = app('steam')->getLocaleArray($locale); $array = app('steam')->getLocaleArray($locale);
setlocale(LC_MONETARY, $array); setlocale(LC_MONETARY, $array);
$info = localeconv(); $info = localeconv();
// correct variables // correct variables
$info['n_cs_precedes'] = $this->getLocaleField($info, 'n_cs_precedes'); $info['n_cs_precedes'] = $this->getLocaleField($info, 'n_cs_precedes');
$info['p_cs_precedes'] = $this->getLocaleField($info, 'p_cs_precedes'); $info['p_cs_precedes'] = $this->getLocaleField($info, 'p_cs_precedes');
$info['n_sep_by_space'] = $this->getLocaleField($info, 'n_sep_by_space'); $info['n_sep_by_space'] = $this->getLocaleField($info, 'n_sep_by_space');
$info['p_sep_by_space'] = $this->getLocaleField($info, 'p_sep_by_space'); $info['p_sep_by_space'] = $this->getLocaleField($info, 'p_sep_by_space');
$fmt = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $fmt = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$info['mon_decimal_point'] = $fmt->getSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL); $info['mon_decimal_point'] = $fmt->getSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL);
$info['mon_thousands_sep'] = $fmt->getSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL); $info['mon_thousands_sep'] = $fmt->getSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL);
@@ -255,7 +262,7 @@ class Amount
public static function getAmountJsConfig(bool $sepBySpace, int $signPosn, string $sign, bool $csPrecedes): string public static function getAmountJsConfig(bool $sepBySpace, int $signPosn, string $sign, bool $csPrecedes): string
{ {
// negative first: // negative first:
$space = ' '; $space = ' ';
// require space between symbol and amount? // require space between symbol and amount?
if (false === $sepBySpace) { if (false === $sepBySpace) {
@@ -264,11 +271,11 @@ class Amount
// there are five possible positions for the "+" or "-" sign (if it is even used) // there are five possible positions for the "+" or "-" sign (if it is even used)
// pos_a and pos_e could be the ( and ) symbol. // pos_a and pos_e could be the ( and ) symbol.
$posA = ''; // before everything $posA = ''; // before everything
$posB = ''; // before currency symbol $posB = ''; // before currency symbol
$posC = ''; // after currency symbol $posC = ''; // after currency symbol
$posD = ''; // before amount $posD = ''; // before amount
$posE = ''; // after everything $posE = ''; // after everything
// format would be (currency before amount) // format would be (currency before amount)
// AB%sC_D%vE // AB%sC_D%vE
@@ -310,11 +317,11 @@ class Amount
} }
// default is amount before currency // default is amount before currency
$format = $posA.$posD.'%v'.$space.$posB.'%s'.$posC.$posE; $format = $posA . $posD . '%v' . $space . $posB . '%s' . $posC . $posE;
if ($csPrecedes) { if ($csPrecedes) {
// alternative is currency before amount // alternative is currency before amount
$format = $posA.$posB.'%s'.$posC.$space.$posD.'%v'.$posE; $format = $posA . $posB . '%s' . $posC . $space . $posD . '%v' . $posE;
} }
return $format; return $format;

View File

@@ -30,6 +30,7 @@ use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\CacheProperties; use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Facades\Steam; use FireflyIII\Support\Facades\Steam;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -46,7 +47,7 @@ trait ChartGeneration
protected function accountBalanceChart(Collection $accounts, Carbon $start, Carbon $end): array // chart helper method. protected function accountBalanceChart(Collection $accounts, Carbon $start, Carbon $end): array // chart helper method.
{ {
// chart properties for cache: // chart properties for cache:
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = Amount::convertToNative();
$cache = new CacheProperties(); $cache = new CacheProperties();
$cache->addProperty($start); $cache->addProperty($start);
$cache->addProperty($end); $cache->addProperty($end);

View File

@@ -310,8 +310,8 @@ class Steam
public function finalAccountBalance(Account $account, Carbon $date): array public function finalAccountBalance(Account $account, Carbon $date): array
{ {
Log::debug(sprintf('Now in finalAccountBalance(#%d, "%s", "%s")', $account->id, $account->name, $date->format('Y-m-d H:i:s'))); Log::debug(sprintf('Now in finalAccountBalance(#%d, "%s", "%s")', $account->id, $account->name, $date->format('Y-m-d H:i:s')));
$native = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup); $native = \FireflyIII\Support\Facades\Amount::getDefaultCurrencyByUserGroup($account->user->userGroup);
$convertToNative = app('preferences')->getForUser($account->user, 'convert_to_native', false)->data; $convertToNative = \FireflyIII\Support\Facades\Amount::convertToNative($account->user);
$accountCurrency = $this->getAccountCurrency($account); $accountCurrency = $this->getAccountCurrency($account);
$hasCurrency = null !== $accountCurrency; $hasCurrency = null !== $accountCurrency;
$currency = $hasCurrency ? $accountCurrency : $native; $currency = $hasCurrency ? $accountCurrency : $native;

View File

@@ -70,7 +70,7 @@ class General extends AbstractExtension
$info = Steam::finalAccountBalance($account, $date); $info = Steam::finalAccountBalance($account, $date);
$currency = Steam::getAccountCurrency($account); $currency = Steam::getAccountCurrency($account);
$default = Amount::getDefaultCurrency(); $default = Amount::getDefaultCurrency();
$convertToNative = app('preferences')->get('convert_to_native', false)->data; $convertToNative = Amount::convertToNative();
$useNative = $convertToNative && $default->id !== $currency->id; $useNative = $convertToNative && $default->id !== $currency->id;
$strings = []; $strings = [];
foreach ($info as $key => $balance) { foreach ($info as $key => $balance) {

View File

@@ -219,12 +219,14 @@
<span>{{ 'currencies'|_ }}</span> <span>{{ 'currencies'|_ }}</span>
</a> </a>
</li> </li>
{% if config('cer.enabled') %}
<li class="{{ activeRoutePartial('exchange-rates') }}"> <li class="{{ activeRoutePartial('exchange-rates') }}">
<a class="{{ activeRoutePartial('exchange-rates') }}" href="{{ route('exchange-rates.index') }}"> <a class="{{ activeRoutePartial('exchange-rates') }}" href="{{ route('exchange-rates.index') }}">
<span class="fa fa-angle-right fa-fw"></span> <span class="fa fa-angle-right fa-fw"></span>
<span>{{ 'menu_exchange_rates_index'|_ }}</span> <span>{{ 'menu_exchange_rates_index'|_ }}</span>
</a> </a>
</li> </li>
{% endif %}
{% if hasRole('owner') %} {% if hasRole('owner') %}
<li class="{{ activeRoutePartial('admin') }}"> <li class="{{ activeRoutePartial('admin') }}">
<a class="{{ activeRoutePartial('admin') }}" href="{{ route('admin.index') }}"> <a class="{{ activeRoutePartial('admin') }}" href="{{ route('admin.index') }}">

View File

@@ -100,7 +100,8 @@
{{ ExpandedForm.date('fiscalYearStart',fiscalYearStart,{ 'label' : 'pref_fiscal_year_start_label'|_ }) }} {{ ExpandedForm.date('fiscalYearStart',fiscalYearStart,{ 'label' : 'pref_fiscal_year_start_label'|_ }) }}
</div> </div>
{# conversion back to natiev #} {# conversion back to native #}
{% if config('cer.enabled') %}
<div class="preferences-box"> <div class="preferences-box">
<h3>{{ 'pref_convert_to_native'|_ }}</h3> <h3>{{ 'pref_convert_to_native'|_ }}</h3>
<p class="text-info"> <p class="text-info">
@@ -108,6 +109,7 @@
</p> </p>
{{ ExpandedForm.checkbox('convertToNative','1',convertToNative,{ 'label' : 'pref_convert_native_help'|_ }) }} {{ ExpandedForm.checkbox('convertToNative','1',convertToNative,{ 'label' : 'pref_convert_native_help'|_ }) }}
</div> </div>
{% endif %}
</div> </div>
{# general settings column B #} {# general settings column B #}