2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
|
|
|
use App\Models\Auth\User;
|
2020-06-05 16:18:46 +03:00
|
|
|
use App\Models\Auth\Role;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Common\Contact;
|
|
|
|
|
|
|
|
class CreateContact extends Job
|
|
|
|
{
|
2020-06-26 13:40:19 +03:00
|
|
|
protected $contact;
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @param $request
|
|
|
|
*/
|
|
|
|
public function __construct($request)
|
|
|
|
{
|
|
|
|
$this->request = $this->getRequestInstance($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return Contact
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
|
|
|
if ($this->request->get('create_user', 'false') === 'true') {
|
|
|
|
$this->createUser();
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
$this->contact = Contact::create($this->request->all());
|
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
return $this->contact;
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function createUser()
|
|
|
|
{
|
|
|
|
// Check if user exist
|
|
|
|
if ($user = User::where('email', $this->request['email'])->first()) {
|
|
|
|
$message = trans('messages.error.customer', ['name' => $user->name]);
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->request->all();
|
|
|
|
$data['locale'] = setting('default.locale', 'en-GB');
|
|
|
|
|
2020-06-05 16:18:46 +03:00
|
|
|
$customer_role = Role::all()->filter(function ($role) {
|
|
|
|
return $role->hasPermission('read-client-portal');
|
|
|
|
})->first();
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$user = User::create($data);
|
2020-06-05 16:18:46 +03:00
|
|
|
$user->roles()->attach($customer_role);
|
2020-12-25 12:08:15 +03:00
|
|
|
$user->companies()->attach($data['company_id']);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
$this->request['user_id'] = $user->id;
|
|
|
|
}
|
|
|
|
}
|