Skip to content

Firefly III: a personal finances manager

This should fix #308

James Colec3ccc4ccdfe3
diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php
index a5da5fdcba..dc795f2f42 100644
--- a/app/Http/Controllers/CurrencyController.php
+++ b/app/Http/Controllers/CurrencyController.php
@@ -81,20 +81,19 @@ class CurrencyController extends Controller
}
/**
- * @param CurrencyRepositoryInterface $repository
* @param TransactionCurrency $currency
*
* @return \Illuminate\Http\RedirectResponse|View
*/
- public function delete(CurrencyRepositoryInterface $repository, TransactionCurrency $currency)
+ public function delete(TransactionCurrency $currency)
{
-
- if ($repository->countJournals($currency) > 0) {
+ if (!$this->canDeleteCurrency($currency)) {
Session::flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
return redirect(route('currency.index'));
}
+
// put previous url in session
Session::put('currency.delete.url', URL::previous());
Session::flash('gaEventCategory', 'currency');
@@ -106,15 +105,14 @@ class CurrencyController extends Controller
}
/**
- * @param CurrencyRepositoryInterface $repository
* @param TransactionCurrency $currency
*
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
- public function destroy(CurrencyRepositoryInterface $repository, TransactionCurrency $currency)
+ public function destroy(TransactionCurrency $currency)
{
- if ($repository->countJournals($currency) > 0) {
+ if (!$this->canDeleteCurrency($currency)) {
Session::flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
return redirect(route('currency.index'));
@@ -229,4 +227,39 @@ class CurrencyController extends Controller
}
+ /**
+ * @param TransactionCurrency $currency
+ *
+ * @return bool
+ */
+ private function canDeleteCurrency(TransactionCurrency $currency): bool
+ {
+ $repository = app(CurrencyRepositoryInterface::class);
+
+ // has transactions still
+ if ($repository->countJournals($currency) > 0) {
+ return false;
+ }
+
+ // is the only currency left
+ if ($repository->get()->count() === 1) {
+ return false;
+ }
+
+ // is the default currency for the user or the system
+ $defaultCode = Preferences::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR'))->data;
+ if ($currency->code === $defaultCode) {
+ return false;
+ }
+
+ // is the default currency for the system
+ $defaultSystemCode = env('DEFAULT_CURRENCY', 'EUR');
+ if ($currency->code === $defaultSystemCode) {
+ return false;
+ }
+
+ // can be deleted
+ return true;
+ }
+
}
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index ea1810f81f..b94dd8165f 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -359,7 +359,7 @@ return [
'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 there are still transactions attached to it!',
+ 'cannot_delete_currency' => 'Cannot delete :name because it is still in use.',
'deleted_currency' => 'Currency :name deleted',
'created_currency' => 'Currency :name created',
'updated_currency' => 'Currency :name updated',