prevent deletion of last category by type #304

This commit is contained in:
denisdulici 2018-04-24 01:03:01 +03:00
parent 15e0662b76
commit 483ffa396e
5 changed files with 39 additions and 15 deletions

View File

@ -20,8 +20,12 @@ class Categories extends Controller
$transfer_id = Category::transfer(); $transfer_id = Category::transfer();
$types = collect(['expense' => 'Expense', 'income' => 'Income', 'item' => 'Item', 'other' => 'Other']) $types = collect([
->prepend(trans('general.all_type', ['type' => trans_choice('general.types', 2)]), ''); 'expense' => trans_choice('general.expenses', 1),
'income' => trans_choice('general.incomes', 1),
'item' => trans_choice('general.items', 1),
'other' => trans_choice('general.others', 1),
])->prepend(trans('general.all_type', ['type' => trans_choice('general.types', 2)]), '');
return view('settings.categories.index', compact('categories', 'types', 'transfer_id')); return view('settings.categories.index', compact('categories', 'types', 'transfer_id'));
} }
@ -43,7 +47,14 @@ class Categories extends Controller
*/ */
public function create() public function create()
{ {
return view('settings.categories.create'); $types = [
'expense' => trans_choice('general.expenses', 1),
'income' => trans_choice('general.incomes', 1),
'item' => trans_choice('general.items', 1),
'other' => trans_choice('general.others', 1),
];
return view('settings.categories.create', compact('types'));
} }
/** /**
@ -73,9 +84,16 @@ class Categories extends Controller
*/ */
public function edit(Category $category) public function edit(Category $category)
{ {
$transfer_id = Category::transfer(); $types = [
'expense' => trans_choice('general.expenses', 1),
'income' => trans_choice('general.incomes', 1),
'item' => trans_choice('general.items', 1),
'other' => trans_choice('general.others', 1),
];
return view('settings.categories.edit', compact('category', 'transfer_id')); $type_disabled = (Category::where('type', $category->type)->count() == 1) ?: false;
return view('settings.categories.edit', compact('category', 'types', 'type_disabled'));
} }
/** /**
@ -122,6 +140,15 @@ class Categories extends Controller
*/ */
public function destroy(Category $category) public function destroy(Category $category)
{ {
// Can not delete the last category by type
if (Category::where('type', $category->type)->count() == 1) {
$message = trans('messages.error.last_category', ['type' => strtolower(trans_choice('general.' . $category->type . 's', 1))]);
flash($message)->warning();
return redirect('settings/categories');
}
$relationships = $this->countRelationships($category, [ $relationships = $this->countRelationships($category, [
'items' => 'items', 'items' => 'items',
'invoices' => 'invoices', 'invoices' => 'invoices',
@ -130,11 +157,6 @@ class Categories extends Controller
'payments' => 'payments', 'payments' => 'payments',
]); ]);
// Can't delete transfer category
if ($category->id == Category::transfer()) {
return redirect('settings/categories');
}
if (empty($relationships)) { if (empty($relationships)) {
$category->delete(); $category->delete();

View File

@ -37,6 +37,7 @@ return [
'updates' => 'Update|Updates', 'updates' => 'Update|Updates',
'numbers' => 'Number|Numbers', 'numbers' => 'Number|Numbers',
'statuses' => 'Status|Statuses', 'statuses' => 'Status|Statuses',
'others' => 'Other|Others',
'dashboard' => 'Dashboard', 'dashboard' => 'Dashboard',
'banking' => 'Banking', 'banking' => 'Banking',

View File

@ -14,6 +14,7 @@ return [
'not_user_company' => 'Error: You are not allowed to manage this company!', 'not_user_company' => 'Error: You are not allowed to manage this company!',
'customer' => 'Error: User not created! :name already uses this email address.', 'customer' => 'Error: User not created! :name already uses this email address.',
'no_file' => 'Error: No file selected!', 'no_file' => 'Error: No file selected!',
'last_category' => 'Error: Can not delete the last :type category!',
], ],
'warning' => [ 'warning' => [
'deleted' => 'Warning: You are not allowed to delete <b>:name</b> because it has :text related.', 'deleted' => 'Warning: You are not allowed to delete <b>:name</b> because it has :text related.',

View File

@ -10,7 +10,7 @@
<div class="box-body"> <div class="box-body">
{{ Form::textGroup('name', trans('general.name'), 'id-card-o') }} {{ Form::textGroup('name', trans('general.name'), 'id-card-o') }}
{{ Form::selectGroup('type', trans_choice('general.types', 1), 'bars', ['expense' => 'Expense', 'income' => 'Income', 'item' => 'Item', 'other' => 'Other'], config('general.types')) }} {{ Form::selectGroup('type', trans_choice('general.types', 1), 'bars', $types, config('general.types')) }}
<div class="form-group col-md-6 required {{ $errors->has('color') ? 'has-error' : ''}}"> <div class="form-group col-md-6 required {{ $errors->has('color') ? 'has-error' : ''}}">
{!! Form::label('color', trans('general.color'), ['class' => 'control-label']) !!} {!! Form::label('color', trans('general.color'), ['class' => 'control-label']) !!}

View File

@ -14,11 +14,11 @@
<div class="box-body"> <div class="box-body">
{{ Form::textGroup('name', trans('general.name'), 'id-card-o') }} {{ Form::textGroup('name', trans('general.name'), 'id-card-o') }}
@if ($category->id == $transfer_id) @if ($type_disabled)
{{ Form::selectGroup('type', trans_choice('general.types', 1), 'bars', ['expense' => 'Expense', 'income' => 'Income', 'item' => 'Item', 'other' => 'Other'], null, ['required' => 'required', 'disabled' => 'disabled']) }} {{ Form::selectGroup('type', trans_choice('general.types', 1), 'bars', $types, null, ['required' => 'required', 'disabled' => 'disabled']) }}
<input type="hidden" name="type" value="other" /> <input type="hidden" name="type" value="{{ $category->type }}" />
@else @else
{{ Form::selectGroup('type', trans_choice('general.types', 1), 'bars', ['expense' => 'Expense', 'income' => 'Income', 'item' => 'Item', 'other' => 'Other']) }} {{ Form::selectGroup('type', trans_choice('general.types', 1), 'bars', $types) }}
@endif @endif
<div class="form-group col-md-6 required {{ $errors->has('color') ? 'has-error' : ''}}"> <div class="form-group col-md-6 required {{ $errors->has('color') ? 'has-error' : ''}}">