fixed sub category delete issues

This commit is contained in:
Cihan Şentürk 2024-04-26 17:52:38 +03:00 committed by GitHub
parent c9e31569e2
commit 500cb4cd5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 37 additions and 5 deletions

View File

@ -18,6 +18,8 @@ class DeleteCategory extends Job implements ShouldDelete
event(new CategoryDeleting($this->model));
\DB::transaction(function () {
$this->deleteSubCategories($this->model);
$this->model->delete();
});
@ -26,6 +28,15 @@ class DeleteCategory extends Job implements ShouldDelete
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.
*/
@ -39,7 +50,7 @@ class DeleteCategory extends Job implements ShouldDelete
}
// 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))]);
throw new LastCategoryDelete($message);
@ -50,10 +61,18 @@ class DeleteCategory extends Job implements ShouldDelete
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 = [
'items' => 'items',
'invoices' => 'invoices',
@ -61,16 +80,29 @@ class DeleteCategory extends Job implements ShouldDelete
'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));
}
if ($this->model->id == setting('default.expense_category')) {
if ($model->id == setting('default.expense_category')) {
$relationships[] = strtolower(trans_choice('general.expenses', 1));
}
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);
}
}
}