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();
$types = collect(['expense' => 'Expense', 'income' => 'Income', 'item' => 'Item', 'other' => 'Other'])
->prepend(trans('general.all_type', ['type' => trans_choice('general.types', 2)]), '');
$types = collect([
'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'));
}
@ -43,7 +47,14 @@ class Categories extends Controller
*/
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)
{
$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)
{
// 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, [
'items' => 'items',
'invoices' => 'invoices',
@ -130,11 +157,6 @@ class Categories extends Controller
'payments' => 'payments',
]);
// Can't delete transfer category
if ($category->id == Category::transfer()) {
return redirect('settings/categories');
}
if (empty($relationships)) {
$category->delete();