2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldUpdate;
|
2022-06-02 20:40:49 +03:00
|
|
|
use App\Jobs\Auth\CreateUser;
|
2020-08-19 17:46:48 +03:00
|
|
|
use App\Models\Auth\Role;
|
2019-12-23 12:46:00 +03:00
|
|
|
use App\Models\Auth\User;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Common\Contact;
|
2021-04-20 21:44:20 +03:00
|
|
|
use Illuminate\Support\Str;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
class UpdateContact extends Job implements ShouldUpdate
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle(): Contact
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$this->authorize();
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
|
|
|
if ($this->request->get('create_user', 'false') === 'true') {
|
|
|
|
$this->createUser();
|
2021-09-06 11:53:57 +03:00
|
|
|
} elseif ($this->model->user) {
|
|
|
|
$this->model->user->update($this->request->all());
|
2020-06-26 13:40:19 +03:00
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-04-20 21:44:20 +03:00
|
|
|
// Upload logo
|
|
|
|
if ($this->request->file('logo')) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$media = $this->getMedia($this->request->file('logo'), Str::plural($this->model->type));
|
2021-04-29 11:43:52 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->attachMedia($media, 'logo');
|
2022-08-05 19:15:33 +03:00
|
|
|
} elseif (! $this->request->file('logo') && $this->model->logo) {
|
|
|
|
$this->deleteMediaModel($this->model, 'logo', $this->request);
|
2021-04-20 21:44:20 +03:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->update($this->request->all());
|
2020-06-26 13:40:19 +03:00
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
return $this->model;
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
if (($this->request['enabled'] == 0) && ($relationships = $this->getRelationships())) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$message = trans('messages.warning.disabled', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function createUser(): void
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2022-06-29 14:43:32 +03:00
|
|
|
$customer_role_id = Role::all()->filter(function ($role) {
|
2020-08-19 17:46:48 +03:00
|
|
|
return $role->hasPermission('read-client-portal');
|
2022-06-29 14:43:32 +03:00
|
|
|
})->pluck('id')->first();
|
2020-08-19 17:46:48 +03:00
|
|
|
|
2022-06-02 20:40:49 +03:00
|
|
|
$this->request->merge([
|
|
|
|
'locale' => setting('default.locale', 'en-GB'),
|
2022-06-29 14:43:32 +03:00
|
|
|
'roles' => $customer_role_id,
|
2022-06-02 20:40:49 +03:00
|
|
|
'companies' => [$this->request->get('company_id')],
|
|
|
|
]);
|
2022-06-01 10:15:55 +03:00
|
|
|
|
2022-06-02 20:40:49 +03:00
|
|
|
$user = $this->dispatch(new CreateUser($this->request));
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
$this->request['user_id'] = $user->id;
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function getRelationships(): array
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$rels = [
|
|
|
|
'transactions' => 'transactions',
|
|
|
|
];
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
if ($this->model->type == 'customer') {
|
2019-11-16 10:21:14 +03:00
|
|
|
$rels['invoices'] = 'invoices';
|
|
|
|
} else {
|
|
|
|
$rels['bills'] = 'bills';
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
return $this->countRelationships($this->model, $rels);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
}
|