Skip to content

Firefly III: a personal finances manager

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

James Cole525f69cf63c0
diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php
index ba9129c3b2..52e43664d9 100644
--- a/app/Http/Controllers/CurrencyController.php
+++ b/app/Http/Controllers/CurrencyController.php
@@ -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'));
diff --git a/app/Repositories/Currency/CurrencyRepository.php b/app/Repositories/Currency/CurrencyRepository.php
index 0091587cc9..3c52510fe6 100644
--- a/app/Repositories/Currency/CurrencyRepository.php
+++ b/app/Repositories/Currency/CurrencyRepository.php
@@ -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
- // $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;
- // }
+// // 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 'system_fallback';
+// }
Log::debug('Currency is not used, return false.');
- return false;
+ return null;
}
/**
diff --git a/app/Repositories/Currency/CurrencyRepositoryInterface.php b/app/Repositories/Currency/CurrencyRepositoryInterface.php
index 9dc3105287..4d3d929467 100644
--- a/app/Repositories/Currency/CurrencyRepositoryInterface.php
+++ b/app/Repositories/Currency/CurrencyRepositoryInterface.php
@@ -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
*
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 8ec753a2b6..a0480eed8c 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -629,22 +629,31 @@ return [
'create_new_asset' => 'Create new asset account',
'create_new_expense' => 'Create new expense account',
'create_new_revenue' => 'Create new revenue account',
- 'create_new_piggy_bank' => 'Create new piggy bank',
- 'create_new_bill' => 'Create new bill',
+ 'create_new_piggy_bank' => 'Create new piggy bank',
+ 'create_new_bill' => 'Create new bill',
// currencies:
- 'create_currency' => 'Create a new currency',
- 'store_currency' => 'Store new currency',
- '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.',
- '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.',
+ 'create_currency' => 'Create a new currency',
+ 'store_currency' => 'Store new currency',
+ '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_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.',
+ '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',
'default_currency' => 'default',
'currency_is_disabled' => 'Disabled',