This should fix #308

Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
James Cole
2016-09-08 21:10:24 +02:00
parent 6c5cd705c0
commit c3ccc4ccdf
2 changed files with 41 additions and 8 deletions

View File

@@ -81,20 +81,19 @@ class CurrencyController extends Controller
} }
/** /**
* @param CurrencyRepositoryInterface $repository
* @param TransactionCurrency $currency * @param TransactionCurrency $currency
* *
* @return \Illuminate\Http\RedirectResponse|View * @return \Illuminate\Http\RedirectResponse|View
*/ */
public function delete(CurrencyRepositoryInterface $repository, TransactionCurrency $currency) public function delete(TransactionCurrency $currency)
{ {
if (!$this->canDeleteCurrency($currency)) {
if ($repository->countJournals($currency) > 0) {
Session::flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name])); Session::flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
return redirect(route('currency.index')); return redirect(route('currency.index'));
} }
// put previous url in session // put previous url in session
Session::put('currency.delete.url', URL::previous()); Session::put('currency.delete.url', URL::previous());
Session::flash('gaEventCategory', 'currency'); Session::flash('gaEventCategory', 'currency');
@@ -106,15 +105,14 @@ class CurrencyController extends Controller
} }
/** /**
* @param CurrencyRepositoryInterface $repository
* @param TransactionCurrency $currency * @param TransactionCurrency $currency
* *
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
* @throws \Exception * @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])); Session::flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
return redirect(route('currency.index')); 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;
}
} }

View File

@@ -359,7 +359,7 @@ return [
'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 there are still transactions attached to it!', 'cannot_delete_currency' => 'Cannot delete :name because it is still in use.',
'deleted_currency' => 'Currency :name deleted', 'deleted_currency' => 'Currency :name deleted',
'created_currency' => 'Currency :name created', 'created_currency' => 'Currency :name created',
'updated_currency' => 'Currency :name updated', 'updated_currency' => 'Currency :name updated',