akaunting/app/Jobs/Common/DeleteCompany.php

72 lines
1.8 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Traits\Users;
class DeleteCompany extends Job
{
use Users;
protected $company;
2020-12-25 12:08:15 +03:00
protected $active_company_id;
2019-11-16 10:21:14 +03:00
/**
* Create a new job instance.
*
* @param $request
*/
2020-12-25 12:08:15 +03:00
public function __construct($company, $active_company_id)
2019-11-16 10:21:14 +03:00
{
$this->company = $company;
2020-12-25 12:08:15 +03:00
$this->active_company_id = $active_company_id;
2019-11-16 10:21:14 +03:00
}
/**
* Execute the job.
*
2020-03-28 22:50:06 +03:00
* @return boolean|Exception
2019-11-16 10:21:14 +03:00
*/
public function handle()
{
$this->authorize();
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->deleteRelationships($this->company, [
'accounts', 'bills', 'bill_histories', 'bill_items', 'bill_item_taxes', 'bill_totals', 'categories',
'contacts', 'currencies', 'dashboards', 'email_templates', 'invoices', 'invoice_histories', 'invoice_items',
'invoice_item_taxes', 'invoice_totals', 'items', 'modules', 'module_histories', 'reconciliations',
'recurring', 'reports', 'settings', 'taxes', 'transactions', 'transfers', 'widgets',
]);
2019-12-22 15:58:48 +03:00
2020-06-26 13:40:19 +03:00
$this->company->delete();
});
2019-11-16 10:21:14 +03:00
return true;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
// Can't delete active company
2020-12-25 12:08:15 +03:00
if ($this->company->id == $this->active_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
if (!$this->isUserCompany($this->company->id)) {
$message = trans('companies.error.not_user_company');
throw new \Exception($message);
}
}
}