akaunting/app/Jobs/Common/UpdateContact.php

115 lines
3.5 KiB
PHP
Raw Normal View History

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;
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;
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
// 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);
}
$this->updateRecurringDocument();
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);
}
$customer_role_id = Role::all()->filter(function ($role) {
return $role->hasPermission('read-client-portal');
})->pluck('id')->first();
2022-06-02 20:40:49 +03:00
$this->request->merge([
'locale' => setting('default.locale', 'en-GB'),
'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;
}
public function updateRecurringDocument(): void
{
$recurring = $this->model->document_recurring;
if ($recurring) {
foreach ($recurring as $recur) {
$recur->update([
'contact_name' => $this->request['name'],
'contact_email' => $this->request['email'],
'contact_tax_number' => $this->request['tax_number'],
'contact_phone' => $this->request['phone'],
'contact_address' => $this->request['address'],
'contact_city' => $this->request['city'],
'contact_state' => $this->request['state'],
'contact_zip_code' => $this->request['zip_code'],
'contact_country' => $this->request['country'],
]);
}
}
}
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
}
}