Inform user on currency disabling and where a currency may still be used. #2432

This commit is contained in:
James Cole
2019-08-18 08:46:36 +02:00
parent 4d2c5c1b58
commit 525f69cf63
4 changed files with 76 additions and 35 deletions

View File

@@ -212,7 +212,11 @@ class CurrencyController extends Controller
} }
if ($this->repository->currencyInUse($currency)) { if ($this->repository->currencyInUse($currency)) {
$request->session()->flash('error', (string)trans('firefly.cannot_disable_currency', ['name' => e($currency->name)]));
$location = $this->repository->currencyInUseAt($currency);
$message = (string)trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]);
$request->session()->flash('error', $message);
Log::channel('audit')->info(sprintf('Tried to disable currency %s but is in use.', $currency->code)); Log::channel('audit')->info(sprintf('Tried to disable currency %s but is in use.', $currency->code));
return redirect(route('currencies.index')); return redirect(route('currencies.index'));
@@ -233,6 +237,10 @@ class CurrencyController extends Controller
app('preferences')->mark(); app('preferences')->mark();
} }
if ('EUR' === $currency->code) {
session()->flash('warning', (string)trans('firefly.disable_EUR_side_effects'));
}
session()->flash('success', (string)trans('firefly.currency_is_now_disabled', ['name' => $currency->name])); session()->flash('success', (string)trans('firefly.currency_is_now_disabled', ['name' => $currency->name]));
return redirect(route('currencies.index')); return redirect(route('currencies.index'));

View File

@@ -65,7 +65,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
*/ */
public function countJournals(TransactionCurrency $currency): int public function countJournals(TransactionCurrency $currency): int
{ {
return $currency->transactions()->count() + $currency->transactionJournals()->count(); return $currency->transactions()->whereNull('deleted_at')->count() + $currency->transactionJournals()->whereNull('deleted_at')->count();
} }
@@ -75,20 +75,32 @@ class CurrencyRepository implements CurrencyRepositoryInterface
* @return bool * @return bool
*/ */
public function currencyInUse(TransactionCurrency $currency): bool public function currencyInUse(TransactionCurrency $currency): bool
{
$result = $this->currencyInUseAt($currency);
return null !== $result;
}
/**
* @param TransactionCurrency $currency
*
* @return string|null
*/
public function currencyInUseAt(TransactionCurrency $currency): ?string
{ {
Log::debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code)); Log::debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code));
$countJournals = $this->countJournals($currency); $countJournals = $this->countJournals($currency);
if ($countJournals > 0) { if ($countJournals > 0) {
Log::info(sprintf('Count journals is %d, return true.', $countJournals)); Log::info(sprintf('Count journals is %d, return true.', $countJournals));
return true; return 'journals';
} }
// is the only currency left // is the only currency left
if (1 === $this->getAll()->count()) { if (1 === $this->getAll()->count()) {
Log::info('Is the last currency in the system, return true. '); Log::info('Is the last currency in the system, return true. ');
return true; return 'last_left';
} }
// is being used in accounts: // is being used in accounts:
@@ -96,7 +108,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($meta > 0) { if ($meta > 0) {
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
return true; return 'account_meta';
} }
// is being used in bills: // is being used in bills:
@@ -104,7 +116,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($bills > 0) { if ($bills > 0) {
Log::info(sprintf('Used in %d bills as currency, return true. ', $bills)); Log::info(sprintf('Used in %d bills as currency, return true. ', $bills));
return true; return 'bills';
} }
// is being used in recurring transactions // is being used in recurring transactions
@@ -114,15 +126,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($recurringAmount > 0 || $recurringForeign > 0) { if ($recurringAmount > 0 || $recurringForeign > 0) {
Log::info(sprintf('Used in %d recurring transactions as (foreign) currency id, return true. ', $recurringAmount + $recurringForeign)); Log::info(sprintf('Used in %d recurring transactions as (foreign) currency id, return true. ', $recurringAmount + $recurringForeign));
return true; return 'recurring';
} }
// is being used in accounts (as integer) // is being used in accounts (as integer)
$meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((int)$currency->id))->count(); $meta = AccountMeta
::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
->whereNull('accounts.deleted_at')
->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode((int)$currency->id))->count();
if ($meta > 0) { if ($meta > 0) {
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta)); Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
return true; return 'account_meta';
} }
// is being used in available budgets // is being used in available budgets
@@ -130,7 +145,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($availableBudgets > 0) { if ($availableBudgets > 0) {
Log::info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets)); Log::info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets));
return true; return 'available_budgets';
} }
// is being used in budget limits // is being used in budget limits
@@ -138,7 +153,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($budgetLimit > 0) { if ($budgetLimit > 0) {
Log::info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit)); Log::info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit));
return true; return 'budget_limits';
} }
// is the default currency for the user or the system // is the default currency for the user or the system
@@ -146,20 +161,20 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($currency->code === $defaultCode) { if ($currency->code === $defaultCode) {
Log::info('Is the default currency of the user, return true.'); Log::info('Is the default currency of the user, return true.');
return true; return 'current_default';
} }
// is the default currency for the system // // is the default currency for the system
// $defaultSystemCode = config('firefly.default_currency', 'EUR'); // $defaultSystemCode = config('firefly.default_currency', 'EUR');
// $result = $currency->code === $defaultSystemCode; // $result = $currency->code === $defaultSystemCode;
// if (true === $result) { // if (true === $result) {
// Log::info('Is the default currency of the SYSTEM, return true.'); // Log::info('Is the default currency of the SYSTEM, return true.');
// //
// return true; // return 'system_fallback';
// } // }
Log::debug('Currency is not used, return false.'); Log::debug('Currency is not used, return false.');
return false; return null;
} }
/** /**

View File

@@ -54,6 +54,15 @@ interface CurrencyRepositoryInterface
*/ */
public function currencyInUse(TransactionCurrency $currency): bool; public function currencyInUse(TransactionCurrency $currency): bool;
/**
* Currency is in use where exactly.
*
* @param TransactionCurrency $currency
*
* @return string|null
*/
public function currencyInUseAt(TransactionCurrency $currency): ?string;
/** /**
* @param TransactionCurrency $currency * @param TransactionCurrency $currency
* *

View File

@@ -629,22 +629,31 @@ return [
'create_new_asset' => 'Create new asset account', 'create_new_asset' => 'Create new asset account',
'create_new_expense' => 'Create new expense account', 'create_new_expense' => 'Create new expense account',
'create_new_revenue' => 'Create new revenue account', 'create_new_revenue' => 'Create new revenue account',
'create_new_piggy_bank' => 'Create new piggy bank', 'create_new_piggy_bank' => 'Create new piggy bank',
'create_new_bill' => 'Create new bill', 'create_new_bill' => 'Create new bill',
// currencies: // currencies:
'create_currency' => 'Create a new currency', 'create_currency' => 'Create a new currency',
'store_currency' => 'Store new currency', 'store_currency' => 'Store new currency',
'update_currency' => 'Update currency', 'update_currency' => 'Update currency',
'new_default_currency' => ':name is now the default currency.', 'new_default_currency' => ':name is now the default currency.',
'cannot_delete_currency' => 'Cannot delete :name because it is still in use.', 'cannot_delete_currency' => 'Cannot delete :name because it is still in use.',
'cannot_disable_currency' => 'Cannot disable :name because it is still in use.', 'cannot_disable_currency_journals' => 'Cannot disable :name because transactions are still using it.',
'deleted_currency' => 'Currency :name deleted', 'cannot_disable_currency_last_left' => 'Cannot disable :name because it is the last enabled currency.',
'created_currency' => 'Currency :name created', 'cannot_disable_currency_account_meta' => 'Cannot disable :name because it is used in asset accounts.',
'could_not_store_currency' => 'Could not store the new currency.', 'cannot_disable_currency_bills' => 'Cannot disable :name because it is used in bills.',
'updated_currency' => 'Currency :name updated', 'cannot_disable_currency_recurring' => 'Cannot disable :name because it is used in recurring transactions.',
'ask_site_owner' => 'Please ask :owner to add, remove or edit currencies.', 'cannot_disable_currency_available_budgets' => 'Cannot disable :name because it is used in available budgets.',
'currencies_intro' => 'Firefly III supports various currencies which you can set and enable here.', 'cannot_disable_currency_budget_limits' => 'Cannot disable :name because it is used in budget limits.',
'cannot_disable_currency_current_default' => 'Cannot disable :name because it is the current default currency.',
'cannot_disable_currency_system_fallback' => 'Cannot disable :name because it is the system default currency.',
'disable_EUR_side_effects' => 'The Euro is the system\'s emergency fallback currency. Disabling it may have unintended side-effects and may void your warranty.',
'deleted_currency' => 'Currency :name deleted',
'created_currency' => 'Currency :name created',
'could_not_store_currency' => 'Could not store the new currency.',
'updated_currency' => 'Currency :name updated',
'ask_site_owner' => 'Please ask :owner to add, remove or edit currencies.',
'currencies_intro' => 'Firefly III supports various currencies which you can set and enable here.',
'make_default_currency' => 'Make default', 'make_default_currency' => 'Make default',
'default_currency' => 'default', 'default_currency' => 'default',
'currency_is_disabled' => 'Disabled', 'currency_is_disabled' => 'Disabled',