2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2020-06-05 16:04:54 +03:00
|
|
|
use App\Jobs\Auth\DeleteUser;
|
2020-01-22 21:23:20 +03:00
|
|
|
use App\Traits\Contacts;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
class DeleteContact extends Job
|
|
|
|
{
|
2020-01-22 21:23:20 +03:00
|
|
|
use Contacts;
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
protected $contact;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @param $contact
|
|
|
|
*/
|
|
|
|
public function __construct($contact)
|
|
|
|
{
|
|
|
|
$this->contact = $contact;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return boolean|Exception
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$this->authorize();
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
|
|
|
if ($user = $this->contact->user) {
|
|
|
|
$this->dispatch(new DeleteUser($user));
|
|
|
|
}
|
2020-06-05 16:04:54 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
$this->contact->delete();
|
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this action is applicable.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
if ($relationships = $this->getRelationships()) {
|
|
|
|
$message = trans('messages.warning.deleted', ['name' => $this->contact->name, 'text' => implode(', ', $relationships)]);
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRelationships()
|
|
|
|
{
|
|
|
|
$rels = [
|
|
|
|
'transactions' => 'transactions',
|
|
|
|
];
|
|
|
|
|
2020-06-08 23:09:43 +03:00
|
|
|
if ($this->isCustomer()) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$rels['invoices'] = 'invoices';
|
|
|
|
} else {
|
|
|
|
$rels['bills'] = 'bills';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->countRelationships($this->contact, $rels);
|
|
|
|
}
|
|
|
|
}
|