2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Setting;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2022-12-07 11:43:38 +03:00
|
|
|
use App\Events\Setting\CategoryDeleted;
|
|
|
|
use App\Events\Setting\CategoryDeleting;
|
2022-12-06 14:24:38 +03:00
|
|
|
use App\Exceptions\Settings\LastCategoryDelete;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldDelete;
|
2019-11-17 16:29:36 +03:00
|
|
|
use App\Models\Setting\Category;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
class DeleteCategory extends Job implements ShouldDelete
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle(): bool
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$this->authorize();
|
|
|
|
|
2022-12-07 11:43:38 +03:00
|
|
|
event(new CategoryDeleting($this->model));
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->delete();
|
2020-06-26 13:40:19 +03:00
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2022-12-07 11:43:38 +03:00
|
|
|
event(new CategoryDeleted($this->model));
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this action is applicable.
|
|
|
|
*/
|
2021-09-06 11:53:57 +03:00
|
|
|
public function authorize(): void
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2023-04-05 14:40:51 +03:00
|
|
|
// Can not delete transfer category
|
|
|
|
if ($this->model->isTransferCategory()) {
|
|
|
|
$message = trans('messages.error.transfer_category', ['type' => $this->model->name]);
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
// Can not delete the last category by type
|
2021-09-06 11:53:57 +03:00
|
|
|
if (Category::where('type', $this->model->type)->count() == 1) {
|
|
|
|
$message = trans('messages.error.last_category', ['type' => strtolower(trans_choice('general.' . $this->model->type . 's', 1))]);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2022-12-06 14:24:38 +03:00
|
|
|
throw new LastCategoryDelete($message);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($relationships = $this->getRelationships()) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function getRelationships(): array
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$rels = [
|
|
|
|
'items' => 'items',
|
|
|
|
'invoices' => 'invoices',
|
|
|
|
'bills' => 'bills',
|
|
|
|
'transactions' => 'transactions',
|
|
|
|
];
|
|
|
|
|
2021-10-07 12:09:45 +03:00
|
|
|
$relationships = $this->countRelationships($this->model, $rels);
|
|
|
|
|
|
|
|
if ($this->model->id == setting('default.income_category')) {
|
|
|
|
$relationships[] = strtolower(trans_choice('general.incomes', 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->model->id == setting('default.expense_category')) {
|
2021-10-07 12:12:13 +03:00
|
|
|
$relationships[] = strtolower(trans_choice('general.expenses', 1));
|
2021-10-07 12:09:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $relationships;
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
}
|