2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2021-05-06 00:13:52 +03:00
|
|
|
use App\Events\Common\CompanyDeleted;
|
|
|
|
use App\Events\Common\CompanyDeleting;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldDelete;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Traits\Users;
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
class DeleteCompany extends Job implements ShouldDelete
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
use Users;
|
|
|
|
|
2021-04-16 00:59:43 +03:00
|
|
|
protected $current_company_id;
|
2020-12-25 12:08:15 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function booted(...$arguments): void
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-04-16 00:59:43 +03:00
|
|
|
$this->current_company_id = company_id();
|
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();
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->makeCurrent();
|
2021-05-06 00:13:52 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
event(new CompanyDeleting($this->model, $this->current_company_id));
|
2021-05-06 00:13:52 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->deleteRelationships($this->model, [
|
2021-05-12 17:17:22 +03:00
|
|
|
'accounts', 'document_histories', 'document_item_taxes', 'document_items', 'document_totals', 'documents', 'categories',
|
|
|
|
'contacts', 'currencies', 'dashboards', 'email_templates', 'items', 'module_histories', 'modules', 'reconciliations',
|
2020-06-26 13:40:19 +03:00
|
|
|
'recurring', 'reports', 'settings', 'taxes', 'transactions', 'transfers', 'widgets',
|
|
|
|
]);
|
2019-12-22 15:58:48 +03:00
|
|
|
|
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
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
event(new CompanyDeleted($this->model, $this->current_company_id));
|
2021-05-06 00:13:52 +03:00
|
|
|
|
|
|
|
company($this->current_company_id)->makeCurrent();
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// Can't delete active company
|
2021-09-06 11:53:57 +03:00
|
|
|
if ($this->model->id == $this->current_company_id) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = trans('companies.error.delete_active');
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user can access company
|
2021-09-06 11:53:57 +03:00
|
|
|
if ($this->isNotUserCompany($this->model->id)) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = trans('companies.error.not_user_company');
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|