Akaunting/app/Jobs/Common/DeleteCompany.php

67 lines
1.8 KiB
PHP
Raw Normal View History

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