fixed sub category delete issues
This commit is contained in:
parent
c9e31569e2
commit
500cb4cd5d
|
|
@ -18,6 +18,8 @@ class DeleteCategory extends Job implements ShouldDelete
|
||||||
event(new CategoryDeleting($this->model));
|
event(new CategoryDeleting($this->model));
|
||||||
|
|
||||||
\DB::transaction(function () {
|
\DB::transaction(function () {
|
||||||
|
$this->deleteSubCategories($this->model);
|
||||||
|
|
||||||
$this->model->delete();
|
$this->model->delete();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -26,6 +28,15 @@ class DeleteCategory extends Job implements ShouldDelete
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function deleteSubCategories($model)
|
||||||
|
{
|
||||||
|
$model->delete();
|
||||||
|
|
||||||
|
foreach ($model->sub_categories as $sub_category) {
|
||||||
|
$this->deleteSubCategories($sub_category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if this action is applicable.
|
* Determine if this action is applicable.
|
||||||
*/
|
*/
|
||||||
|
|
@ -39,7 +50,7 @@ class DeleteCategory extends Job implements ShouldDelete
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can not delete the last category by type
|
// Can not delete the last category by type
|
||||||
if (Category::where('type', $this->model->type)->count() == 1) {
|
if (Category::where('type', $this->model->type)->count() == 1 && $this->model->parent_id === null) {
|
||||||
$message = trans('messages.error.last_category', ['type' => strtolower(trans_choice('general.' . $this->model->type . 's', 1))]);
|
$message = trans('messages.error.last_category', ['type' => strtolower(trans_choice('general.' . $this->model->type . 's', 1))]);
|
||||||
|
|
||||||
throw new LastCategoryDelete($message);
|
throw new LastCategoryDelete($message);
|
||||||
|
|
@ -50,10 +61,18 @@ class DeleteCategory extends Job implements ShouldDelete
|
||||||
|
|
||||||
throw new \Exception($message);
|
throw new \Exception($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ($this->model->sub_categories as $sub_category) {
|
||||||
|
$this->getSubCategoryRelationships($sub_category);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRelationships(): array
|
public function getRelationships($model = null): array
|
||||||
{
|
{
|
||||||
|
if (! $model) {
|
||||||
|
$model = $this->model;
|
||||||
|
}
|
||||||
|
|
||||||
$rels = [
|
$rels = [
|
||||||
'items' => 'items',
|
'items' => 'items',
|
||||||
'invoices' => 'invoices',
|
'invoices' => 'invoices',
|
||||||
|
|
@ -61,16 +80,29 @@ class DeleteCategory extends Job implements ShouldDelete
|
||||||
'transactions' => 'transactions',
|
'transactions' => 'transactions',
|
||||||
];
|
];
|
||||||
|
|
||||||
$relationships = $this->countRelationships($this->model, $rels);
|
$relationships = $this->countRelationships($model, $rels);
|
||||||
|
|
||||||
if ($this->model->id == setting('default.income_category')) {
|
if ($model->id == setting('default.income_category')) {
|
||||||
$relationships[] = strtolower(trans_choice('general.incomes', 1));
|
$relationships[] = strtolower(trans_choice('general.incomes', 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->model->id == setting('default.expense_category')) {
|
if ($model->id == setting('default.expense_category')) {
|
||||||
$relationships[] = strtolower(trans_choice('general.expenses', 1));
|
$relationships[] = strtolower(trans_choice('general.expenses', 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $relationships;
|
return $relationships;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getSubCategoryRelationships($model)
|
||||||
|
{
|
||||||
|
if ($relationships = $this->getRelationships($model)) {
|
||||||
|
$message = trans('messages.warning.deleted', ['name' => $model->name, 'text' => implode(', ', $relationships)]);
|
||||||
|
|
||||||
|
throw new \Exception($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($model->sub_categories as $sub_category) {
|
||||||
|
$this->getSubCategoryRelationships($sub_category);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue