mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-23 22:35:03 +00:00
Inform user on currency disabling and where a currency may still be used. #2432
This commit is contained in:
@@ -212,7 +212,11 @@ class CurrencyController extends Controller
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
return redirect(route('currencies.index'));
|
||||
@@ -233,6 +237,10 @@ class CurrencyController extends Controller
|
||||
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]));
|
||||
|
||||
return redirect(route('currencies.index'));
|
||||
|
@@ -65,7 +65,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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));
|
||||
$countJournals = $this->countJournals($currency);
|
||||
if ($countJournals > 0) {
|
||||
Log::info(sprintf('Count journals is %d, return true.', $countJournals));
|
||||
|
||||
return true;
|
||||
return 'journals';
|
||||
}
|
||||
|
||||
// is the only currency left
|
||||
if (1 === $this->getAll()->count()) {
|
||||
Log::info('Is the last currency in the system, return true. ');
|
||||
|
||||
return true;
|
||||
return 'last_left';
|
||||
}
|
||||
|
||||
// is being used in accounts:
|
||||
@@ -96,7 +108,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
if ($meta > 0) {
|
||||
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
|
||||
|
||||
return true;
|
||||
return 'account_meta';
|
||||
}
|
||||
|
||||
// is being used in bills:
|
||||
@@ -104,7 +116,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
if ($bills > 0) {
|
||||
Log::info(sprintf('Used in %d bills as currency, return true. ', $bills));
|
||||
|
||||
return true;
|
||||
return 'bills';
|
||||
}
|
||||
|
||||
// is being used in recurring transactions
|
||||
@@ -114,15 +126,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
if ($recurringAmount > 0 || $recurringForeign > 0) {
|
||||
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)
|
||||
$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) {
|
||||
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
|
||||
|
||||
return true;
|
||||
return 'account_meta';
|
||||
}
|
||||
|
||||
// is being used in available budgets
|
||||
@@ -130,7 +145,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
if ($availableBudgets > 0) {
|
||||
Log::info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets));
|
||||
|
||||
return true;
|
||||
return 'available_budgets';
|
||||
}
|
||||
|
||||
// is being used in budget limits
|
||||
@@ -138,7 +153,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
if ($budgetLimit > 0) {
|
||||
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
|
||||
@@ -146,20 +161,20 @@ class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
if ($currency->code === $defaultCode) {
|
||||
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');
|
||||
// $result = $currency->code === $defaultSystemCode;
|
||||
// if (true === $result) {
|
||||
// Log::info('Is the default currency of the SYSTEM, return true.');
|
||||
//
|
||||
// return true;
|
||||
// return 'system_fallback';
|
||||
// }
|
||||
Log::debug('Currency is not used, return false.');
|
||||
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -54,6 +54,15 @@ interface CurrencyRepositoryInterface
|
||||
*/
|
||||
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
|
||||
*
|
||||
|
@@ -638,7 +638,16 @@ return [
|
||||
'update_currency' => 'Update currency',
|
||||
'new_default_currency' => ':name is now the default currency.',
|
||||
'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.',
|
||||
'cannot_disable_currency_last_left' => 'Cannot disable :name because it is the last enabled currency.',
|
||||
'cannot_disable_currency_account_meta' => 'Cannot disable :name because it is used in asset accounts.',
|
||||
'cannot_disable_currency_bills' => 'Cannot disable :name because it is used in bills.',
|
||||
'cannot_disable_currency_recurring' => 'Cannot disable :name because it is used in recurring transactions.',
|
||||
'cannot_disable_currency_available_budgets' => 'Cannot disable :name because it is used in available budgets.',
|
||||
'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.',
|
||||
|
Reference in New Issue
Block a user