From c08a8cfc4ea5fd37b1b598a21cd33d206341a2cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Mon, 6 Sep 2021 11:53:57 +0300 Subject: [PATCH] added pointer interfaces for jobs --- app/Abstracts/Job.php | 85 +++++++++++++++++++ app/Interfaces/Job/HasOwner.php | 8 ++ app/Interfaces/Job/ShouldCreate.php | 8 ++ app/Interfaces/Job/ShouldDelete.php | 8 ++ app/Interfaces/Job/ShouldUpdate.php | 8 ++ app/Jobs/Auth/CreatePermission.php | 28 ++---- app/Jobs/Auth/CreateRole.php | 30 ++----- app/Jobs/Auth/CreateUser.php | 50 ++++------- app/Jobs/Auth/DeletePermission.php | 24 +----- app/Jobs/Auth/DeleteRole.php | 26 ++---- app/Jobs/Auth/DeleteUser.php | 32 ++----- app/Jobs/Auth/UpdatePermission.php | 30 ++----- app/Jobs/Auth/UpdateRole.php | 32 ++----- app/Jobs/Auth/UpdateUser.php | 52 ++++-------- app/Jobs/Banking/CreateAccount.php | 32 ++----- .../CreateBankingDocumentTransaction.php | 35 +++----- app/Jobs/Banking/CreateReconciliation.php | 30 ++----- app/Jobs/Banking/CreateTransaction.php | 36 ++------ app/Jobs/Banking/CreateTransfer.php | 42 +++------ app/Jobs/Banking/DeleteAccount.php | 36 ++------ app/Jobs/Banking/DeleteReconciliation.php | 28 ++---- app/Jobs/Banking/DeleteTransaction.php | 34 ++------ app/Jobs/Banking/DeleteTransfer.php | 28 ++---- app/Jobs/Banking/UpdateAccount.php | 48 +++-------- app/Jobs/Banking/UpdateReconciliation.php | 32 ++----- app/Jobs/Banking/UpdateTransaction.php | 46 +++------- app/Jobs/Banking/UpdateTransfer.php | 43 +++------- app/Jobs/Common/CreateCompany.php | 50 ++++------- app/Jobs/Common/CreateContact.php | 36 ++------ app/Jobs/Common/CreateDashboard.php | 46 +++------- app/Jobs/Common/CreateItem.php | 34 ++------ app/Jobs/Common/CreateItemTaxes.php | 15 ++-- app/Jobs/Common/CreateReport.php | 30 ++----- app/Jobs/Common/CreateWidget.php | 26 +----- app/Jobs/Common/DeleteCompany.php | 40 +++------ app/Jobs/Common/DeleteContact.php | 36 ++------ app/Jobs/Common/DeleteDashboard.php | 36 ++------ app/Jobs/Common/DeleteItem.php | 36 ++------ app/Jobs/Common/DeleteReport.php | 24 +----- app/Jobs/Common/DeleteWidget.php | 24 +----- app/Jobs/Common/UpdateCompany.php | 46 +++------- app/Jobs/Common/UpdateContact.php | 52 ++++-------- app/Jobs/Common/UpdateDashboard.php | 40 ++------- app/Jobs/Common/UpdateItem.php | 36 ++------ app/Jobs/Common/UpdateReport.php | 30 ++----- app/Jobs/Common/UpdateWidget.php | 30 ++----- app/Jobs/Document/CancelDocument.php | 28 +++--- app/Jobs/Document/CreateDocument.php | 42 +++------ app/Jobs/Document/CreateDocumentHistory.php | 22 ++--- app/Jobs/Document/CreateDocumentItem.php | 21 ++--- .../Document/CreateDocumentItemsAndTotals.php | 29 +++---- app/Jobs/Document/DeleteDocument.php | 34 ++------ app/Jobs/Document/DuplicateDocument.php | 22 ++--- app/Jobs/Document/UpdateDocument.php | 63 +++++--------- app/Jobs/Setting/CreateCategory.php | 30 ++----- app/Jobs/Setting/CreateCurrency.php | 30 ++----- app/Jobs/Setting/CreateTax.php | 30 ++----- app/Jobs/Setting/DeleteCategory.php | 38 +++------ app/Jobs/Setting/DeleteCurrency.php | 36 ++------ app/Jobs/Setting/DeleteTax.php | 34 ++------ app/Jobs/Setting/UpdateCategory.php | 46 +++------- app/Jobs/Setting/UpdateCurrency.php | 50 +++-------- app/Jobs/Setting/UpdateTax.php | 46 +++------- 63 files changed, 635 insertions(+), 1524 deletions(-) create mode 100644 app/Interfaces/Job/HasOwner.php create mode 100644 app/Interfaces/Job/ShouldCreate.php create mode 100644 app/Interfaces/Job/ShouldDelete.php create mode 100644 app/Interfaces/Job/ShouldUpdate.php diff --git a/app/Abstracts/Job.php b/app/Abstracts/Job.php index 2d3c41de4..73c99b82e 100644 --- a/app/Abstracts/Job.php +++ b/app/Abstracts/Job.php @@ -3,14 +3,86 @@ namespace App\Abstracts; use App\Abstracts\Http\FormRequest; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; +use App\Interfaces\Job\ShouldDelete; +use App\Interfaces\Job\ShouldUpdate; use App\Traits\Jobs; use App\Traits\Relationships; use App\Traits\Uploads; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Http\Request; abstract class Job { use Jobs, Relationships, Uploads; + protected $model; + + protected $request; + + public function __construct(...$arguments) + { + $this->booting(...$arguments); + $this->bootCreate(...$arguments); + $this->bootUpdate(...$arguments); + $this->bootDelete(...$arguments); + $this->booted(...$arguments); + } + + public function booting(...$arguments): void + { + // + } + + public function bootCreate(...$arguments): void + { + if (! $this instanceof ShouldCreate) { + return; + } + + $request = $this->getRequestInstance($arguments[0]); + if ($request instanceof Request) { + $this->request = $request; + } + + if ($this instanceof HasOwner) { + $this->setOwner(); + } + } + + public function bootUpdate(...$arguments): void + { + if (! $this instanceof ShouldUpdate) { + return; + } + + if ($arguments[0] instanceof Model) { + $this->model = $arguments[0]; + } + + $request = $this->getRequestInstance($arguments[1]); + if ($request instanceof Request) { + $this->request = $request; + } + } + + public function bootDelete(...$arguments): void + { + if (! $this instanceof ShouldDelete) { + return; + } + + if ($arguments[0] instanceof Model) { + $this->model = $arguments[0]; + } + } + + public function booted(...$arguments): void + { + // + } + public function getRequestInstance($request) { if (!is_array($request)) { @@ -21,4 +93,17 @@ abstract class Job return $class->merge($request); } + + public function setOwner(): void + { + if (! $this->request instanceof Request) { + return; + } + + if ($this->request->has('created_by')) { + return; + } + + $this->request->merge(['created_by' => user_id()]); + } } diff --git a/app/Interfaces/Job/HasOwner.php b/app/Interfaces/Job/HasOwner.php new file mode 100644 index 000000000..430b6eed6 --- /dev/null +++ b/app/Interfaces/Job/HasOwner.php @@ -0,0 +1,8 @@ +request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Permission - */ - public function handle() + public function handle(): Permission { \DB::transaction(function () { - $this->permission = Permission::create($this->request->all()); + $this->model = Permission::create($this->request->all()); }); - return $this->permission; + return $this->model; } } diff --git a/app/Jobs/Auth/CreateRole.php b/app/Jobs/Auth/CreateRole.php index 576f376ed..45a317a3a 100644 --- a/app/Jobs/Auth/CreateRole.php +++ b/app/Jobs/Auth/CreateRole.php @@ -3,39 +3,21 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldCreate; use App\Models\Auth\Role; -class CreateRole extends Job +class CreateRole extends Job implements ShouldCreate { - protected $role; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Permission - */ - public function handle() + public function handle(): Role { \DB::transaction(function () { - $this->role = Role::create($this->request->input()); + $this->model = Role::create($this->request->input()); if ($this->request->has('permissions')) { - $this->role->permissions()->attach($this->request->get('permissions')); + $this->model->permissions()->attach($this->request->get('permissions')); } }); - return $this->role; + return $this->model; } } diff --git a/app/Jobs/Auth/CreateUser.php b/app/Jobs/Auth/CreateUser.php index c797780b0..c6acdd460 100644 --- a/app/Jobs/Auth/CreateUser.php +++ b/app/Jobs/Auth/CreateUser.php @@ -3,61 +3,43 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldCreate; use App\Events\Auth\UserCreated; use App\Events\Auth\UserCreating; use App\Models\Auth\User; -use Artisan; +use Illuminate\Support\Facades\Artisan; -class CreateUser extends Job +class CreateUser extends Job implements ShouldCreate { - protected $user; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Permission - */ - public function handle() + public function handle(): User { event(new UserCreating($this->request)); \DB::transaction(function () { - $this->user = User::create($this->request->input()); + $this->model = User::create($this->request->input()); // Upload picture if ($this->request->file('picture')) { $media = $this->getMedia($this->request->file('picture'), 'users'); - $this->user->attachMedia($media, 'picture'); + $this->model->attachMedia($media, 'picture'); } if ($this->request->has('dashboards')) { - $this->user->dashboards()->attach($this->request->get('dashboards')); + $this->model->dashboards()->attach($this->request->get('dashboards')); } if ($this->request->has('permissions')) { - $this->user->permissions()->attach($this->request->get('permissions')); + $this->model->permissions()->attach($this->request->get('permissions')); } if ($this->request->has('roles')) { - $this->user->roles()->attach($this->request->get('roles')); + $this->model->roles()->attach($this->request->get('roles')); } if ($this->request->has('companies')) { if (app()->runningInConsole() || request()->isInstall()) { - $this->user->companies()->attach($this->request->get('companies')); + $this->model->companies()->attach($this->request->get('companies')); } else { $user = user(); @@ -66,25 +48,25 @@ class CreateUser extends Job }); if ($companies->isNotEmpty()) { - $this->user->companies()->attach($companies->toArray()); + $this->model->companies()->attach($companies->toArray()); } } } - if (empty($this->user->companies)) { + if (empty($this->model->companies)) { return; } - foreach ($this->user->companies as $company) { + foreach ($this->model->companies as $company) { Artisan::call('user:seed', [ - 'user' => $this->user->id, + 'user' => $this->model->id, 'company' => $company->id, ]); } }); - event(new UserCreated($this->user, $this->request)); + event(new UserCreated($this->model, $this->request)); - return $this->user; + return $this->model; } } diff --git a/app/Jobs/Auth/DeletePermission.php b/app/Jobs/Auth/DeletePermission.php index a9939a77d..a3c7ecb27 100644 --- a/app/Jobs/Auth/DeletePermission.php +++ b/app/Jobs/Auth/DeletePermission.php @@ -3,30 +3,14 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeletePermission extends Job +class DeletePermission extends Job implements ShouldDelete { - protected $permission; - - /** - * Create a new job instance. - * - * @param $permission - */ - public function __construct($permission) - { - $this->permission = $permission; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { \DB::transaction(function () { - $this->permission->delete(); + $this->model->delete(); }); return true; diff --git a/app/Jobs/Auth/DeleteRole.php b/app/Jobs/Auth/DeleteRole.php index c7298075a..470d90efe 100644 --- a/app/Jobs/Auth/DeleteRole.php +++ b/app/Jobs/Auth/DeleteRole.php @@ -3,32 +3,16 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeleteRole extends Job +class DeleteRole extends Job implements ShouldDelete { - protected $role; - - /** - * Create a new job instance. - * - * @param $role - */ - public function __construct($role) - { - $this->role = $role; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { \DB::transaction(function () { - $this->role->delete(); + $this->model->delete(); - $this->role->flushCache(); + $this->model->flushCache(); }); return true; diff --git a/app/Jobs/Auth/DeleteUser.php b/app/Jobs/Auth/DeleteUser.php index 384b2f6da..759302e58 100644 --- a/app/Jobs/Auth/DeleteUser.php +++ b/app/Jobs/Auth/DeleteUser.php @@ -3,34 +3,18 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeleteUser extends Job +class DeleteUser extends Job implements ShouldDelete { - protected $user; - - /** - * Create a new job instance. - * - * @param $user - */ - public function __construct($user) - { - $this->user = $user; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { $this->authorize(); \DB::transaction(function () { - $this->user->delete(); + $this->model->delete(); - $this->user->flushCache(); + $this->model->flushCache(); }); return true; @@ -38,13 +22,11 @@ class DeleteUser extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { // Can't delete yourself - if ($this->user->id == user()->id) { + if ($this->model->id == user()->id) { $message = trans('auth.error.self_delete'); throw new \Exception($message); diff --git a/app/Jobs/Auth/UpdatePermission.php b/app/Jobs/Auth/UpdatePermission.php index 5f6acbd6e..c8f8f0ebf 100644 --- a/app/Jobs/Auth/UpdatePermission.php +++ b/app/Jobs/Auth/UpdatePermission.php @@ -3,37 +3,17 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Auth\Permission; -class UpdatePermission extends Job +class UpdatePermission extends Job implements ShouldUpdate { - protected $permission; - - protected $request; - - /** - * Create a new job instance. - * - * @param $permission - * @param $request - */ - public function __construct($permission, $request) - { - $this->permission = $permission; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Permission - */ - public function handle() + public function handle(): Permission { \DB::transaction(function () { - $this->permission->update($this->request->all()); + $this->model->update($this->request->all()); }); - return $this->permission; + return $this->model; } } diff --git a/app/Jobs/Auth/UpdateRole.php b/app/Jobs/Auth/UpdateRole.php index 16ce9184c..2335e0cb4 100644 --- a/app/Jobs/Auth/UpdateRole.php +++ b/app/Jobs/Auth/UpdateRole.php @@ -3,41 +3,21 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Auth\Role; -class UpdateRole extends Job +class UpdateRole extends Job implements ShouldUpdate { - protected $role; - - protected $request; - - /** - * Create a new job instance. - * - * @param $role - * @param $request - */ - public function __construct($role, $request) - { - $this->role = $role; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Role - */ - public function handle() + public function handle(): Role { \DB::transaction(function () { - $this->role->update($this->request->all()); + $this->model->update($this->request->all()); if ($this->request->has('permissions')) { - $this->role->permissions()->sync($this->request->get('permissions')); + $this->model->permissions()->sync($this->request->get('permissions')); } }); - return $this->role; + return $this->model; } } diff --git a/app/Jobs/Auth/UpdateUser.php b/app/Jobs/Auth/UpdateUser.php index e003406c2..8d1e7bcae 100644 --- a/app/Jobs/Auth/UpdateUser.php +++ b/app/Jobs/Auth/UpdateUser.php @@ -3,34 +3,14 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Events\Auth\UserUpdated; use App\Events\Auth\UserUpdating; use App\Models\Auth\User; -class UpdateUser extends Job +class UpdateUser extends Job implements ShouldUpdate { - protected $user; - - protected $request; - - /** - * Create a new job instance. - * - * @param $user - * @param $request - */ - public function __construct($user, $request) - { - $this->user = $user; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return User - */ - public function handle() + public function handle(): User { $this->authorize(); @@ -40,25 +20,25 @@ class UpdateUser extends Job unset($this->request['password_confirmation']); } - event(new UserUpdating($this->user, $this->request)); + event(new UserUpdating($this->model, $this->request)); \DB::transaction(function () { - $this->user->update($this->request->input()); + $this->model->update($this->request->input()); // Upload picture if ($this->request->file('picture')) { $media = $this->getMedia($this->request->file('picture'), 'users'); - $this->user->attachMedia($media, 'picture'); + $this->model->attachMedia($media, 'picture'); } if ($this->request->has('roles')) { - $this->user->roles()->sync($this->request->get('roles')); + $this->model->roles()->sync($this->request->get('roles')); } if ($this->request->has('companies')) { if (app()->runningInConsole() || request()->isInstall()) { - $this->user->companies()->sync($this->request->get('companies')); + $this->model->companies()->sync($this->request->get('companies')); } else { $user = user(); @@ -67,30 +47,28 @@ class UpdateUser extends Job }); if ($companies->isNotEmpty()) { - $this->user->companies()->sync($companies->toArray()); + $this->model->companies()->sync($companies->toArray()); } } } - if ($this->user->contact) { - $this->user->contact->update($this->request->input()); + if ($this->model->contact) { + $this->model->contact->update($this->request->input()); } }); - event(new UserUpdated($this->user, $this->request)); + event(new UserUpdated($this->model, $this->request)); - return $this->user; + return $this->model; } /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { // Can't disable yourself - if (($this->request->get('enabled', 1) == 0) && ($this->user->id == user()->id)) { + if (($this->request->get('enabled', 1) == 0) && ($this->model->id == user()->id)) { $message = trans('auth.error.self_disable'); throw new \Exception($message); diff --git a/app/Jobs/Banking/CreateAccount.php b/app/Jobs/Banking/CreateAccount.php index 0676983e7..b5f626557 100644 --- a/app/Jobs/Banking/CreateAccount.php +++ b/app/Jobs/Banking/CreateAccount.php @@ -3,42 +3,24 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Banking\Account; -class CreateAccount extends Job +class CreateAccount extends Job implements HasOwner, ShouldCreate { - protected $account; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Account - */ - public function handle() + public function handle(): Account { \DB::transaction(function () { - $this->account = Account::create($this->request->all()); + $this->model = Account::create($this->request->all()); // Set default account if ($this->request['default_account']) { - setting()->set('default.account', $this->account->id); + setting()->set('default.account', $this->model->id); setting()->save(); } }); - return $this->account; + return $this->model; } } diff --git a/app/Jobs/Banking/CreateBankingDocumentTransaction.php b/app/Jobs/Banking/CreateBankingDocumentTransaction.php index 90b84d475..57d43d7cd 100644 --- a/app/Jobs/Banking/CreateBankingDocumentTransaction.php +++ b/app/Jobs/Banking/CreateBankingDocumentTransaction.php @@ -6,40 +6,27 @@ use App\Abstracts\Job; use App\Jobs\Banking\CreateTransaction; use App\Jobs\Document\CreateDocumentHistory; use App\Events\Document\PaidAmountCalculated; +use App\Interfaces\Job\ShouldCreate; use App\Models\Banking\Account; use App\Models\Banking\Transaction; +use App\Models\Document\Document; use App\Traits\Currencies; use App\Utilities\Date; -class CreateBankingDocumentTransaction extends Job +class CreateBankingDocumentTransaction extends Job implements ShouldCreate { use Currencies; - protected $model; - - protected $request; - protected $transaction; - /** - * Create a new job instance. - * - * @param $model - * @param $request - */ - public function __construct($model, $request) + public function __construct(Document $model, $request) { $this->model = $model; - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); + + parent::__construct($request); } - /** - * Execute the job. - * - * @return Transaction - */ - public function handle() + public function handle(): Transaction { $this->prepareRequest(); @@ -63,7 +50,7 @@ class CreateBankingDocumentTransaction extends Job return $this->transaction; } - protected function prepareRequest() + protected function prepareRequest(): void { if (!isset($this->request['amount'])) { $this->model->paid_amount = $this->model->paid; @@ -85,7 +72,7 @@ class CreateBankingDocumentTransaction extends Job $this->request['payment_method'] = isset($this->request['payment_method']) ? $this->request['payment_method'] : setting('default.payment_method'); $this->request['notify'] = isset($this->request['notify']) ? $this->request['notify'] : 0; - if ($this->request['mark_paid'] || $this->request['account_id'] == setting('default.account')) { + if ($this->request['mark_paid'] || ($this->request['account_id'] == setting('default.account'))) { $account = Account::find((int) $this->request['account_id']); $code = $account->currency_code; @@ -100,7 +87,7 @@ class CreateBankingDocumentTransaction extends Job } } - protected function checkAmount() + protected function checkAmount(): bool { $code = $this->request['currency_code']; $rate = $this->request['currency_rate']; @@ -144,7 +131,7 @@ class CreateBankingDocumentTransaction extends Job return true; } - protected function createHistory() + protected function createHistory(): void { $history_desc = money((double) $this->transaction->amount, (string) $this->transaction->currency_code, true)->format() . ' ' . trans_choice('general.payments', 1); diff --git a/app/Jobs/Banking/CreateReconciliation.php b/app/Jobs/Banking/CreateReconciliation.php index 995e25529..5dfe9322a 100644 --- a/app/Jobs/Banking/CreateReconciliation.php +++ b/app/Jobs/Banking/CreateReconciliation.php @@ -3,32 +3,14 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Banking\Reconciliation; use App\Models\Banking\Transaction; -class CreateReconciliation extends Job +class CreateReconciliation extends Job implements HasOwner, ShouldCreate { - protected $reconciliation; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Reconciliation - */ - public function handle() + public function handle(): Reconciliation { \DB::transaction(function () { $reconcile = (int) $this->request->get('reconcile'); @@ -36,7 +18,7 @@ class CreateReconciliation extends Job $this->request->merge(['reconciled' => $reconcile]); - $this->reconciliation = Reconciliation::create($this->request->all()); + $this->model = Reconciliation::create($this->request->all()); if ($reconcile && $transactions) { foreach ($transactions as $key => $value) { @@ -53,6 +35,6 @@ class CreateReconciliation extends Job } }); - return $this->reconciliation; + return $this->model; } } diff --git a/app/Jobs/Banking/CreateTransaction.php b/app/Jobs/Banking/CreateTransaction.php index db5b4f0b7..9ae1cafb1 100644 --- a/app/Jobs/Banking/CreateTransaction.php +++ b/app/Jobs/Banking/CreateTransaction.php @@ -5,52 +5,34 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; use App\Events\Banking\TransactionCreated; use App\Events\Banking\TransactionCreating; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Banking\Transaction; -class CreateTransaction extends Job +class CreateTransaction extends Job implements HasOwner, ShouldCreate { - protected $transaction; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Transaction - */ - public function handle() + public function handle(): Transaction { event(new TransactionCreating($this->request)); \DB::transaction(function () { - $this->transaction = Transaction::create($this->request->all()); + $this->model = Transaction::create($this->request->all()); // Upload attachment if ($this->request->file('attachment')) { foreach ($this->request->file('attachment') as $attachment) { $media = $this->getMedia($attachment, 'transactions'); - $this->transaction->attachMedia($media, 'attachment'); + $this->model->attachMedia($media, 'attachment'); } } // Recurring - $this->transaction->createRecurring($this->request->all()); + $this->model->createRecurring($this->request->all()); }); - event(new TransactionCreated($this->transaction)); + event(new TransactionCreated($this->model)); - return $this->transaction; + return $this->model; } } diff --git a/app/Jobs/Banking/CreateTransfer.php b/app/Jobs/Banking/CreateTransfer.php index 6203a0a98..2c12b840f 100644 --- a/app/Jobs/Banking/CreateTransfer.php +++ b/app/Jobs/Banking/CreateTransfer.php @@ -3,37 +3,19 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; +use App\Jobs\Banking\CreateTransaction; use App\Models\Banking\Account; -use App\Models\Banking\Transaction; use App\Models\Banking\Transfer; use App\Models\Setting\Category; use App\Traits\Currencies; -class CreateTransfer extends Job +class CreateTransfer extends Job implements HasOwner, ShouldCreate { use Currencies; - protected $transfer; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Transfer - */ - public function handle() + public function handle(): Transfer { \DB::transaction(function () { $expense_currency_code = $this->getCurrencyCode('from'); @@ -42,7 +24,7 @@ class CreateTransfer extends Job $expense_currency_rate = $this->getCurrencyRate('from'); $income_currency_rate = $this->getCurrencyRate('to'); - $expense_transaction = Transaction::create([ + $expense_transaction = $this->dispatch(new CreateTransaction([ 'company_id' => $this->request['company_id'], 'type' => 'expense', 'account_id' => $this->request->get('from_account_id'), @@ -56,7 +38,7 @@ class CreateTransfer extends Job 'payment_method' => $this->request->get('payment_method'), 'reference' => $this->request->get('reference'), 'created_by' => $this->request->get('created_by'), - ]); + ])); $amount = $this->request->get('amount'); @@ -65,7 +47,7 @@ class CreateTransfer extends Job $amount = $this->convertBetween($amount, $expense_currency_code, $expense_currency_rate, $income_currency_code, $income_currency_rate); } - $income_transaction = Transaction::create([ + $income_transaction = $this->dispatch(new CreateTransaction([ 'company_id' => $this->request['company_id'], 'type' => 'income', 'account_id' => $this->request->get('to_account_id'), @@ -79,9 +61,9 @@ class CreateTransfer extends Job 'payment_method' => $this->request->get('payment_method'), 'reference' => $this->request->get('reference'), 'created_by' => $this->request->get('created_by'), - ]); + ])); - $this->transfer = Transfer::create([ + $this->model = Transfer::create([ 'company_id' => $this->request['company_id'], 'expense_transaction_id' => $expense_transaction->id, 'income_transaction_id' => $income_transaction->id, @@ -93,12 +75,12 @@ class CreateTransfer extends Job foreach ($this->request->file('attachment') as $attachment) { $media = $this->getMedia($attachment, 'transfers'); - $this->transfer->attachMedia($media, 'attachment'); + $this->model->attachMedia($media, 'attachment'); } } }); - return $this->transfer; + return $this->model; } protected function getCurrencyCode($type) diff --git a/app/Jobs/Banking/DeleteAccount.php b/app/Jobs/Banking/DeleteAccount.php index be9e67027..a4fb6ce61 100644 --- a/app/Jobs/Banking/DeleteAccount.php +++ b/app/Jobs/Banking/DeleteAccount.php @@ -3,32 +3,16 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeleteAccount extends Job +class DeleteAccount extends Job implements ShouldDelete { - protected $account; - - /** - * Create a new job instance. - * - * @param $account - */ - public function __construct($account) - { - $this->account = $account; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { $this->authorize(); \DB::transaction(function () { - $this->account->delete(); + $this->model->delete(); }); return true; @@ -36,27 +20,25 @@ class DeleteAccount extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { if ($relationships = $this->getRelationships()) { - $message = trans('messages.warning.deleted', ['name' => $this->account->name, 'text' => implode(', ', $relationships)]); + $message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'transactions' => 'transactions', ]; - $relationships = $this->countRelationships($this->account, $rels); + $relationships = $this->countRelationships($this->model, $rels); - if ($this->account->id == setting('default.account')) { + if ($this->model->id == setting('default.account')) { $relationships[] = strtolower(trans_choice('general.companies', 1)); } diff --git a/app/Jobs/Banking/DeleteReconciliation.php b/app/Jobs/Banking/DeleteReconciliation.php index 98de26b5c..f9bc830dd 100644 --- a/app/Jobs/Banking/DeleteReconciliation.php +++ b/app/Jobs/Banking/DeleteReconciliation.php @@ -3,35 +3,19 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; use App\Models\Banking\Transaction; -class DeleteReconciliation extends Job +class DeleteReconciliation extends Job implements ShouldDelete { - protected $reconciliation; - - /** - * Create a new job instance. - * - * @param $reconciliation - */ - public function __construct($reconciliation) - { - $this->reconciliation = $reconciliation; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { \DB::transaction(function () { - $this->reconciliation->delete(); + $this->model->delete(); - Transaction::where('account_id', $this->reconciliation->account_id) + Transaction::where('account_id', $this->model->account_id) ->isReconciled() - ->whereBetween('paid_at', [$this->reconciliation->started_at, $this->reconciliation->ended_at])->each(function ($transaction) { + ->whereBetween('paid_at', [$this->model->started_at, $this->model->ended_at])->each(function ($transaction) { $transaction->reconciled = 0; $transaction->save(); }); diff --git a/app/Jobs/Banking/DeleteTransaction.php b/app/Jobs/Banking/DeleteTransaction.php index 69a36ab51..b5beac0fd 100644 --- a/app/Jobs/Banking/DeleteTransaction.php +++ b/app/Jobs/Banking/DeleteTransaction.php @@ -3,34 +3,18 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; use App\Models\Setting\Category; -class DeleteTransaction extends Job +class DeleteTransaction extends Job implements ShouldDelete { - protected $transaction; - - /** - * Create a new job instance. - * - * @param $transaction - */ - public function __construct($transaction) - { - $this->transaction = $transaction; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { $this->authorize(); \DB::transaction(function () { - $this->transaction->recurring()->delete(); - $this->transaction->delete(); + $this->model->recurring()->delete(); + $this->model->delete(); }); return true; @@ -38,18 +22,16 @@ class DeleteTransaction extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { - if ($this->transaction->reconciled) { + if ($this->model->reconciled) { $message = trans('messages.warning.reconciled_tran'); throw new \Exception($message); } - if ($this->transaction->category->id == Category::transfer()) { + if ($this->model->category->id == Category::transfer()) { throw new \Exception('Unauthorized'); } } diff --git a/app/Jobs/Banking/DeleteTransfer.php b/app/Jobs/Banking/DeleteTransfer.php index 6bbd58ac5..695fb6f00 100644 --- a/app/Jobs/Banking/DeleteTransfer.php +++ b/app/Jobs/Banking/DeleteTransfer.php @@ -3,32 +3,16 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeleteTransfer extends Job +class DeleteTransfer extends Job implements ShouldDelete { - protected $transfer; - - /** - * Create a new job instance. - * - * @param $transfer - */ - public function __construct($transfer) - { - $this->transfer = $transfer; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { \DB::transaction(function () { - $this->transfer->expense_transaction->delete(); - $this->transfer->income_transaction->delete(); - $this->transfer->delete(); + $this->model->expense_transaction->delete(); + $this->model->income_transaction->delete(); + $this->model->delete(); }); return true; diff --git a/app/Jobs/Banking/UpdateAccount.php b/app/Jobs/Banking/UpdateAccount.php index e2593f056..b37a7b512 100644 --- a/app/Jobs/Banking/UpdateAccount.php +++ b/app/Jobs/Banking/UpdateAccount.php @@ -3,61 +3,39 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Banking\Account; -class UpdateAccount extends Job +class UpdateAccount extends Job implements ShouldUpdate { - protected $account; - - protected $request; - - /** - * Create a new job instance. - * - * @param $account - * @param $request - */ - public function __construct($account, $request) - { - $this->account = $account; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Account - */ - public function handle() + public function handle(): Account { $this->authorize(); \DB::transaction(function () { - $this->account->update($this->request->all()); + $this->model->update($this->request->all()); // Set default account if ($this->request['default_account']) { - setting()->set('default.account', $this->account->id); + setting()->set('default.account', $this->model->id); setting()->save(); } }); - return $this->account; + return $this->model; } /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { $relationships = $this->getRelationships(); - if (!$this->request->get('enabled') && ($this->account->id == setting('default.account'))) { + if (!$this->request->get('enabled') && ($this->model->id == setting('default.account'))) { $relationships[] = strtolower(trans_choice('general.companies', 1)); - $message = trans('messages.warning.disabled', ['name' => $this->account->name, 'text' => implode(', ', $relationships)]); + $message = trans('messages.warning.disabled', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } @@ -66,19 +44,19 @@ class UpdateAccount extends Job return; } - if ($this->account->currency_code != $this->request->get('currency_code')) { - $message = trans('messages.warning.disable_code', ['name' => $this->account->name, 'text' => implode(', ', $relationships)]); + if ($this->model->currency_code != $this->request->get('currency_code')) { + $message = trans('messages.warning.disable_code', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'transactions' => 'transactions', ]; - return $this->countRelationships($this->account, $rels); + return $this->countRelationships($this->model, $rels); } } diff --git a/app/Jobs/Banking/UpdateReconciliation.php b/app/Jobs/Banking/UpdateReconciliation.php index 6e62424ec..8825f2b61 100644 --- a/app/Jobs/Banking/UpdateReconciliation.php +++ b/app/Jobs/Banking/UpdateReconciliation.php @@ -3,40 +3,20 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Banking\Reconciliation; use App\Models\Banking\Transaction; -class UpdateReconciliation extends Job +class UpdateReconciliation extends Job implements ShouldUpdate { - protected $reconciliation; - - protected $request; - - /** - * Create a new job instance. - * - * @param $reconciliation - * @param $request - */ - public function __construct($reconciliation, $request) - { - $this->reconciliation = $reconciliation; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Reconciliation - */ - public function handle() + public function handle(): Reconciliation { \DB::transaction(function () { $reconcile = (int) $this->request->get('reconcile'); $transactions = $this->request->get('transactions'); - $this->reconciliation->reconciled = $reconcile; - $this->reconciliation->save(); + $this->model->reconciled = $reconcile; + $this->model->save(); if ($transactions) { foreach ($transactions as $key => $value) { @@ -53,6 +33,6 @@ class UpdateReconciliation extends Job } }); - return $this->reconciliation; + return $this->model; } } diff --git a/app/Jobs/Banking/UpdateTransaction.php b/app/Jobs/Banking/UpdateTransaction.php index 98df2031b..210c8e73e 100644 --- a/app/Jobs/Banking/UpdateTransaction.php +++ b/app/Jobs/Banking/UpdateTransaction.php @@ -3,66 +3,44 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Banking\Transaction; -class UpdateTransaction extends Job +class UpdateTransaction extends Job implements ShouldUpdate { - protected $transaction; - - protected $request; - - /** - * Create a new job instance. - * - * @param $transaction - * @param $request - */ - public function __construct($transaction, $request) - { - $this->transaction = $transaction; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Transaction - */ - public function handle() + public function handle(): Transaction { $this->authorize(); \DB::transaction(function () { - $this->transaction->update($this->request->all()); + $this->model->update($this->request->all()); // Upload attachment if ($this->request->file('attachment')) { - $this->deleteMediaModel($this->transaction, 'attachment', $this->request); + $this->deleteMediaModel($this->model, 'attachment', $this->request); foreach ($this->request->file('attachment') as $attachment) { $media = $this->getMedia($attachment, 'transactions'); - $this->transaction->attachMedia($media, 'attachment'); + $this->model->attachMedia($media, 'attachment'); } - } elseif (!$this->request->file('attachment') && $this->transaction->attachment) { - $this->deleteMediaModel($this->transaction, 'attachment', $this->request); + } elseif (!$this->request->file('attachment') && $this->model->attachment) { + $this->deleteMediaModel($this->model, 'attachment', $this->request); } // Recurring - $this->transaction->updateRecurring($this->request->all()); + $this->model->updateRecurring($this->request->all()); }); - return $this->transaction; + return $this->model; } /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { - if ($this->transaction->reconciled) { + if ($this->model->reconciled) { $message = trans('messages.warning.reconciled_tran'); throw new \Exception($message); diff --git a/app/Jobs/Banking/UpdateTransfer.php b/app/Jobs/Banking/UpdateTransfer.php index 23fb96b51..2d49be9b3 100644 --- a/app/Jobs/Banking/UpdateTransfer.php +++ b/app/Jobs/Banking/UpdateTransfer.php @@ -3,52 +3,31 @@ namespace App\Jobs\Banking; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Banking\Account; use App\Models\Banking\Transaction; use App\Models\Banking\Transfer; use App\Models\Setting\Category; -use App\Models\Setting\Currency; use App\Traits\Currencies; -class UpdateTransfer extends Job +class UpdateTransfer extends Job implements ShouldUpdate { use Currencies; - protected $transfer; - - protected $request; - - /** - * Create a new job instance. - * - * @param $transfer - * @param $request - */ - public function __construct($transfer, $request) - { - $this->transfer = $transfer; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Transfer - */ - public function handle() + public function handle(): Transfer { \DB::transaction(function () { // Upload attachment if ($this->request->file('attachment')) { - $this->deleteMediaModel($this->transfer, 'attachment', $this->request); + $this->deleteMediaModel($this->model, 'attachment', $this->request); foreach ($this->request->file('attachment') as $attachment) { $media = $this->getMedia($attachment, 'transfers'); - $this->transfer->attachMedia($media, 'attachment'); + $this->model->attachMedia($media, 'attachment'); } - } elseif (!$this->request->file('attachment') && $this->transfer->attachment) { - $this->deleteMediaModel($this->transfer, 'attachment', $this->request); + } elseif (!$this->request->file('attachment') && $this->model->attachment) { + $this->deleteMediaModel($this->model, 'attachment', $this->request); } $expense_currency_code = $this->getCurrencyCode('from'); @@ -57,8 +36,8 @@ class UpdateTransfer extends Job $expense_currency_rate = $this->getCurrencyRate('from'); $income_currency_rate = $this->getCurrencyRate('to'); - $expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id); - $income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id); + $expense_transaction = Transaction::findOrFail($this->model->expense_transaction_id); + $income_transaction = Transaction::findOrFail($this->model->income_transaction_id); $expense_transaction->update([ 'company_id' => $this->request['company_id'], @@ -97,14 +76,14 @@ class UpdateTransfer extends Job 'reference' => $this->request->get('reference'), ]); - $this->transfer->update([ + $this->model->update([ 'company_id' => $this->request['company_id'], 'expense_transaction_id' => $expense_transaction->id, 'income_transaction_id' => $income_transaction->id, ]); }); - return $this->transfer; + return $this->model; } protected function getCurrencyCode($type) diff --git a/app/Jobs/Common/CreateCompany.php b/app/Jobs/Common/CreateCompany.php index 90f0eabc7..59f781aba 100644 --- a/app/Jobs/Common/CreateCompany.php +++ b/app/Jobs/Common/CreateCompany.php @@ -5,57 +5,39 @@ namespace App\Jobs\Common; use App\Abstracts\Job; use App\Events\Common\CompanyCreated; use App\Events\Common\CompanyCreating; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Common\Company; -use Artisan; +use Illuminate\Support\Facades\Artisan; -class CreateCompany extends Job +class CreateCompany extends Job implements HasOwner, ShouldCreate { - protected $company; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Company - */ - public function handle() + public function handle(): Company { $current_company_id = company_id(); event(new CompanyCreating($this->request)); \DB::transaction(function () { - $this->company = Company::create($this->request->all()); + $this->model = Company::create($this->request->all()); - $this->company->makeCurrent(); + $this->model->makeCurrent(); $this->callSeeds(); $this->updateSettings(); }); - event(new CompanyCreated($this->company)); + event(new CompanyCreated($this->model)); if (!empty($current_company_id)) { company($current_company_id)->makeCurrent(); } - return $this->company; + return $this->model; } - protected function callSeeds() + protected function callSeeds(): void { // Set custom locale if ($this->request->has('locale')) { @@ -64,7 +46,7 @@ class CreateCompany extends Job // Company seeds Artisan::call('company:seed', [ - 'company' => $this->company->id + 'company' => $this->model->id ]); if (!$user = user()) { @@ -72,22 +54,22 @@ class CreateCompany extends Job } // Attach company to user logged in - $user->companies()->attach($this->company->id); + $user->companies()->attach($this->model->id); // User seeds Artisan::call('user:seed', [ 'user' => $user->id, - 'company' => $this->company->id, + 'company' => $this->model->id, ]); } - protected function updateSettings() + protected function updateSettings(): void { if ($this->request->file('logo')) { - $company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id); + $company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->model->id); if ($company_logo) { - $this->company->attachMedia($company_logo, 'company_logo'); + $this->model->attachMedia($company_logo, 'company_logo'); setting()->set('company.logo', $company_logo->id); } diff --git a/app/Jobs/Common/CreateContact.php b/app/Jobs/Common/CreateContact.php index 14fd1c1c0..74a0220f1 100644 --- a/app/Jobs/Common/CreateContact.php +++ b/app/Jobs/Common/CreateContact.php @@ -3,54 +3,36 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Auth\User; use App\Models\Auth\Role; use App\Models\Common\Contact; use Illuminate\Support\Str; -class CreateContact extends Job +class CreateContact extends Job implements HasOwner, ShouldCreate { - protected $contact; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Contact - */ - public function handle() + public function handle(): Contact { \DB::transaction(function () { if ($this->request->get('create_user', 'false') === 'true') { $this->createUser(); } - $this->contact = Contact::create($this->request->all()); + $this->model = Contact::create($this->request->all()); // Upload logo if ($this->request->file('logo')) { - $media = $this->getMedia($this->request->file('logo'), Str::plural($this->contact->type)); + $media = $this->getMedia($this->request->file('logo'), Str::plural($this->model->type)); - $this->contact->attachMedia($media, 'logo'); + $this->model->attachMedia($media, 'logo'); } }); - return $this->contact; + return $this->model; } - public function createUser() + public function createUser(): void { // Check if user exist if ($user = User::where('email', $this->request['email'])->first()) { diff --git a/app/Jobs/Common/CreateDashboard.php b/app/Jobs/Common/CreateDashboard.php index eb9edd4f8..241bd4faf 100644 --- a/app/Jobs/Common/CreateDashboard.php +++ b/app/Jobs/Common/CreateDashboard.php @@ -3,6 +3,8 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Auth\User; use App\Models\Common\Company; use App\Models\Common\Dashboard; @@ -10,29 +12,9 @@ use App\Models\Common\Widget; use App\Utilities\Widgets; use Illuminate\Support\Arr; -class CreateDashboard extends Job +class CreateDashboard extends Job implements HasOwner, ShouldCreate { - protected $dashboard; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Item - */ - public function handle() + public function handle(): Dashboard { $this->request['enabled'] = $this->request['enabled'] ?? 1; @@ -43,17 +25,17 @@ class CreateDashboard extends Job return; } - $this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled'])); + $this->model = Dashboard::create($this->request->only(['company_id', 'name', 'enabled'])); - $this->dashboard->users()->attach($users); + $this->model->users()->attach($users); $this->checkAndCreateWidgets(); }); - return $this->dashboard; + return $this->model; } - protected function getUsers() + protected function getUsers(): array { $list = []; @@ -88,7 +70,7 @@ class CreateDashboard extends Job return $list; } - protected function shouldCreateDashboardFor($user) + protected function shouldCreateDashboardFor($user): bool { if (empty($user)) { return false; @@ -102,7 +84,7 @@ class CreateDashboard extends Job return true; } - protected function checkAndCreateWidgets() + protected function checkAndCreateWidgets(): void { $sort = 1; @@ -119,7 +101,7 @@ class CreateDashboard extends Job } } - protected function createWidgets($widgets, &$sort) + protected function createWidgets($widgets, &$sort): void { foreach ($widgets as $class => $name) { // It's just an array of classes @@ -129,14 +111,14 @@ class CreateDashboard extends Job } Widget::firstOrCreate([ - 'company_id' => $this->dashboard->company_id, - 'dashboard_id' => $this->dashboard->id, + 'company_id' => $this->model->company_id, + 'dashboard_id' => $this->model->id, 'class' => $class, ], [ 'name' => $name, 'sort' => $sort, 'settings' => (new $class())->getDefaultSettings(), - 'created_by' => $this->dashboard->created_by, + 'created_by' => $this->model->created_by, ]); $sort++; diff --git a/app/Jobs/Common/CreateItem.php b/app/Jobs/Common/CreateItem.php index be4b27981..b1a0aac03 100644 --- a/app/Jobs/Common/CreateItem.php +++ b/app/Jobs/Common/CreateItem.php @@ -3,46 +3,28 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Jobs\Common\CreateItemTaxes; use App\Models\Common\Item; -class CreateItem extends Job +class CreateItem extends Job implements HasOwner, ShouldCreate { - protected $item; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Item - */ - public function handle() + public function handle(): Item { \DB::transaction(function () { - $this->item = Item::create($this->request->all()); + $this->model = Item::create($this->request->all()); // Upload picture if ($this->request->file('picture')) { $media = $this->getMedia($this->request->file('picture'), 'items'); - $this->item->attachMedia($media, 'picture'); + $this->model->attachMedia($media, 'picture'); } - $this->dispatch(new CreateItemTaxes($this->item, $this->request)); + $this->dispatch(new CreateItemTaxes($this->model, $this->request)); }); - return $this->item; + return $this->model; } } diff --git a/app/Jobs/Common/CreateItemTaxes.php b/app/Jobs/Common/CreateItemTaxes.php index ec7b93231..22c8a9835 100644 --- a/app/Jobs/Common/CreateItemTaxes.php +++ b/app/Jobs/Common/CreateItemTaxes.php @@ -3,30 +3,29 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldCreate; +use App\Models\Common\Item; use App\Models\Common\ItemTax; -class CreateItemTaxes extends Job +class CreateItemTaxes extends Job implements ShouldCreate { protected $item; protected $request; - /** - * Create a new job instance. - * - * @param $item - * @param $request - */ - public function __construct($item, $request) + public function __construct(Item $item, $request) { $this->item = $item; $this->request = $request; + + parent::__construct($item, $request); } /** * Execute the job. * * @return mixed + * @todo type hint after upgrading to PHP 8 */ public function handle() { diff --git a/app/Jobs/Common/CreateReport.php b/app/Jobs/Common/CreateReport.php index ce6f6f892..d830ec00e 100644 --- a/app/Jobs/Common/CreateReport.php +++ b/app/Jobs/Common/CreateReport.php @@ -3,36 +3,18 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Common\Report; -class CreateReport extends Job +class CreateReport extends Job implements HasOwner, ShouldCreate { - protected $report; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Report - */ - public function handle() + public function handle(): Report { \DB::transaction(function () { - $this->report = Report::create($this->request->all()); + $this->model = Report::create($this->request->all()); }); - return $this->report; + return $this->model; } } diff --git a/app/Jobs/Common/CreateWidget.php b/app/Jobs/Common/CreateWidget.php index 3e636f69a..8dbb87061 100644 --- a/app/Jobs/Common/CreateWidget.php +++ b/app/Jobs/Common/CreateWidget.php @@ -3,31 +3,13 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Common\Widget; -class CreateWidget extends Job +class CreateWidget extends Job implements HasOwner, ShouldCreate { - protected $widget; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Widget - */ - public function handle() + public function handle(): Widget { $this->request['enabled'] = $this->request['enabled'] ?? 1; diff --git a/app/Jobs/Common/DeleteCompany.php b/app/Jobs/Common/DeleteCompany.php index c3cba7242..2a3647ff7 100644 --- a/app/Jobs/Common/DeleteCompany.php +++ b/app/Jobs/Common/DeleteCompany.php @@ -5,53 +5,41 @@ namespace App\Jobs\Common; use App\Abstracts\Job; use App\Events\Common\CompanyDeleted; use App\Events\Common\CompanyDeleting; +use App\Interfaces\Job\ShouldDelete; use App\Traits\Users; -class DeleteCompany extends Job +class DeleteCompany extends Job implements ShouldDelete { use Users; - protected $company; - protected $current_company_id; - /** - * Create a new job instance. - * - * @param $company - */ - public function __construct($company) + public function booted(...$arguments): void { - $this->company = $company; $this->current_company_id = company_id(); } - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { $this->authorize(); - $this->company->makeCurrent(); + $this->model->makeCurrent(); - event(new CompanyDeleting($this->company, $this->current_company_id)); + event(new CompanyDeleting($this->model, $this->current_company_id)); \DB::transaction(function () { - $this->deleteRelationships($this->company, [ + $this->deleteRelationships($this->model, [ 'accounts', 'document_histories', 'document_item_taxes', 'document_items', 'document_totals', 'documents', 'categories', 'contacts', 'currencies', 'dashboards', 'email_templates', 'items', 'module_histories', 'modules', 'reconciliations', 'recurring', 'reports', 'settings', 'taxes', 'transactions', 'transfers', 'widgets', ]); - $this->company->users()->detach(); + $this->model->users()->detach(); - $this->company->delete(); + $this->model->delete(); }); - event(new CompanyDeleted($this->company, $this->current_company_id)); + event(new CompanyDeleted($this->model, $this->current_company_id)); company($this->current_company_id)->makeCurrent(); @@ -60,20 +48,18 @@ class DeleteCompany extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { // Can't delete active company - if ($this->company->id == $this->current_company_id) { + if ($this->model->id == $this->current_company_id) { $message = trans('companies.error.delete_active'); throw new \Exception($message); } // Check if user can access company - if ($this->isNotUserCompany($this->company->id)) { + if ($this->isNotUserCompany($this->model->id)) { $message = trans('companies.error.not_user_company'); throw new \Exception($message); diff --git a/app/Jobs/Common/DeleteContact.php b/app/Jobs/Common/DeleteContact.php index ba7bc19f7..b58fb5a17 100644 --- a/app/Jobs/Common/DeleteContact.php +++ b/app/Jobs/Common/DeleteContact.php @@ -3,40 +3,24 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; use App\Jobs\Auth\DeleteUser; use App\Traits\Contacts; -class DeleteContact extends Job +class DeleteContact extends Job implements ShouldDelete { use Contacts; - 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() + public function handle() :bool { $this->authorize(); \DB::transaction(function () { - if ($user = $this->contact->user) { + if ($user = $this->model->user) { $this->dispatch(new DeleteUser($user)); } - $this->contact->delete(); + $this->model->delete(); }); return true; @@ -44,19 +28,17 @@ class DeleteContact extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { if ($relationships = $this->getRelationships()) { - $message = trans('messages.warning.deleted', ['name' => $this->contact->name, 'text' => implode(', ', $relationships)]); + $message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'transactions' => 'transactions', @@ -68,6 +50,6 @@ class DeleteContact extends Job $rels['bills'] = 'bills'; } - return $this->countRelationships($this->contact, $rels); + return $this->countRelationships($this->model, $rels); } } diff --git a/app/Jobs/Common/DeleteDashboard.php b/app/Jobs/Common/DeleteDashboard.php index 77bd86884..67a5d42e5 100644 --- a/app/Jobs/Common/DeleteDashboard.php +++ b/app/Jobs/Common/DeleteDashboard.php @@ -5,39 +5,23 @@ namespace App\Jobs\Common; use App\Abstracts\Job; use App\Exceptions\Common\LastDashboard; use App\Exceptions\Common\NotUserDashboard; +use App\Interfaces\Job\ShouldDelete; use App\Traits\Users; -class DeleteDashboard extends Job +class DeleteDashboard extends Job implements ShouldDelete { use Users; - protected $dashboard; - - /** - * Create a new job instance. - * - * @param $dashboard - */ - public function __construct($dashboard) - { - $this->dashboard = $dashboard; - } - - /** - * Execute the job. - * - * @return boolean - */ - public function handle() + public function handle(): bool { $this->authorize(); \DB::transaction(function () { - $this->deleteRelationships($this->dashboard, ['widgets']); + $this->deleteRelationships($this->model, ['widgets']); - $this->dashboard->users()->detach(); + $this->model->users()->detach(); - $this->dashboard->delete(); + $this->model->delete(); }); return true; @@ -45,13 +29,11 @@ class DeleteDashboard extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { // Can't delete last dashboard for any shared user - foreach ($this->dashboard->users as $user) { + foreach ($this->model->users as $user) { if ($user->dashboards()->enabled()->count() > 1) { continue; } @@ -62,7 +44,7 @@ class DeleteDashboard extends Job } // Check if user can access dashboard - if ($this->isNotUserDashboard($this->dashboard->id)) { + if ($this->isNotUserDashboard($this->model->id)) { $message = trans('dashboards.error.not_user_dashboard'); throw new NotUserDashboard($message); diff --git a/app/Jobs/Common/DeleteItem.php b/app/Jobs/Common/DeleteItem.php index 51e5a9423..6bd21b709 100644 --- a/app/Jobs/Common/DeleteItem.php +++ b/app/Jobs/Common/DeleteItem.php @@ -3,34 +3,18 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeleteItem extends Job +class DeleteItem extends Job implements ShouldDelete { - protected $item; - - /** - * Create a new job instance. - * - * @param $item - */ - public function __construct($item) - { - $this->item = $item; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { $this->authorize(); \DB::transaction(function () { - $this->deleteRelationships($this->item, ['taxes']); + $this->deleteRelationships($this->model, ['taxes']); - $this->item->delete(); + $this->model->delete(); }); return true; @@ -38,25 +22,23 @@ class DeleteItem extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { if ($relationships = $this->getRelationships()) { - $message = trans('messages.warning.deleted', ['name' => $this->item->name, 'text' => implode(', ', $relationships)]); + $message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'invoice_items' => 'invoices', 'bill_items' => 'bills', ]; - return $this->countRelationships($this->item, $rels); + return $this->countRelationships($this->model, $rels); } } diff --git a/app/Jobs/Common/DeleteReport.php b/app/Jobs/Common/DeleteReport.php index 303b04f9d..0a7c7c1ad 100644 --- a/app/Jobs/Common/DeleteReport.php +++ b/app/Jobs/Common/DeleteReport.php @@ -3,30 +3,14 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeleteReport extends Job +class DeleteReport extends Job implements ShouldDelete { - protected $report; - - /** - * Create a new job instance. - * - * @param $report - */ - public function __construct($report) - { - $this->report = $report; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { \DB::transaction(function () { - $this->report->delete(); + $this->model->delete(); }); return true; diff --git a/app/Jobs/Common/DeleteWidget.php b/app/Jobs/Common/DeleteWidget.php index 3b74dfa05..60c67a305 100644 --- a/app/Jobs/Common/DeleteWidget.php +++ b/app/Jobs/Common/DeleteWidget.php @@ -3,30 +3,14 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeleteWidget extends Job +class DeleteWidget extends Job implements ShouldDelete { - protected $widget; - - /** - * Create a new job instance. - * - * @param $widget - */ - public function __construct($widget) - { - $this->widget = $widget; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { \DB::transaction(function () { - $this->widget->delete(); + $this->model->delete(); }); return true; diff --git a/app/Jobs/Common/UpdateCompany.php b/app/Jobs/Common/UpdateCompany.php index 890df7ddb..901766769 100644 --- a/app/Jobs/Common/UpdateCompany.php +++ b/app/Jobs/Common/UpdateCompany.php @@ -5,47 +5,31 @@ namespace App\Jobs\Common; use App\Abstracts\Job; use App\Events\Common\CompanyUpdated; use App\Events\Common\CompanyUpdating; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Common\Company; use App\Traits\Users; -class UpdateCompany extends Job +class UpdateCompany extends Job implements ShouldUpdate { use Users; - protected $company; - - protected $request; - protected $current_company_id; - /** - * Create a new job instance. - * - * @param $company - * @param $request - */ - public function __construct($company, $request) + public function booted(...$arguments): void { - $this->company = $company; - $this->request = $this->getRequestInstance($request); $this->current_company_id = company_id(); } - /** - * Execute the job. - * - * @return Company - */ - public function handle() + public function handle(): Company { $this->authorize(); - event(new CompanyUpdating($this->company, $this->request)); + event(new CompanyUpdating($this->model, $this->request)); \DB::transaction(function () { - $this->company->update($this->request->all()); + $this->model->update($this->request->all()); - $this->company->makeCurrent(); + $this->model->makeCurrent(); if ($this->request->has('name')) { setting()->set('company.name', $this->request->get('name')); @@ -92,10 +76,10 @@ class UpdateCompany extends Job } if ($this->request->file('logo')) { - $company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id); + $company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->model->id); if ($company_logo) { - $this->company->attachMedia($company_logo, 'company_logo'); + $this->model->attachMedia($company_logo, 'company_logo'); setting()->set('company.logo', $company_logo->id); } @@ -104,31 +88,29 @@ class UpdateCompany extends Job setting()->save(); }); - event(new CompanyUpdated($this->company, $this->request)); + event(new CompanyUpdated($this->model, $this->request)); if (!empty($this->current_company_id)) { company($this->current_company_id)->makeCurrent(); } - return $this->company; + return $this->model; } /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { // Can't disable active company - if (($this->request->get('enabled', 1) == 0) && ($this->company->id == $this->current_company_id)) { + if (($this->request->get('enabled', 1) == 0) && ($this->model->id == $this->current_company_id)) { $message = trans('companies.error.disable_active'); throw new \Exception($message); } // Check if user can access company - if ($this->isNotUserCompany($this->company->id)) { + if ($this->isNotUserCompany($this->model->id)) { $message = trans('companies.error.not_user_company'); throw new \Exception($message); diff --git a/app/Jobs/Common/UpdateContact.php b/app/Jobs/Common/UpdateContact.php index a64a1a202..8eecba0f6 100644 --- a/app/Jobs/Common/UpdateContact.php +++ b/app/Jobs/Common/UpdateContact.php @@ -3,73 +3,51 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Auth\Role; use App\Models\Auth\User; use App\Models\Common\Contact; use Illuminate\Support\Str; -class UpdateContact extends Job +class UpdateContact extends Job implements ShouldUpdate { - protected $contact; - - protected $request; - - /** - * Create a new job instance. - * - * @param $contact - * @param $request - */ - public function __construct($contact, $request) - { - $this->contact = $contact; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Contact - */ - public function handle() + public function handle(): Contact { $this->authorize(); \DB::transaction(function () { if ($this->request->get('create_user', 'false') === 'true') { $this->createUser(); - } elseif ($this->contact->user) { - $this->contact->user->update($this->request->all()); + } elseif ($this->model->user) { + $this->model->user->update($this->request->all()); } // Upload logo if ($this->request->file('logo')) { - $media = $this->getMedia($this->request->file('logo'), Str::plural($this->contact->type)); + $media = $this->getMedia($this->request->file('logo'), Str::plural($this->model->type)); - $this->contact->attachMedia($media, 'logo'); + $this->model->attachMedia($media, 'logo'); } - $this->contact->update($this->request->all()); + $this->model->update($this->request->all()); }); - return $this->contact; + return $this->model; } /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { if (($this->request['enabled'] == 0) && ($relationships = $this->getRelationships())) { - $message = trans('messages.warning.disabled', ['name' => $this->contact->name, 'text' => implode(', ', $relationships)]); + $message = trans('messages.warning.disabled', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function createUser() + public function createUser(): void { // Check if user exist if ($user = User::where('email', $this->request['email'])->first()) { @@ -92,18 +70,18 @@ class UpdateContact extends Job $this->request['user_id'] = $user->id; } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'transactions' => 'transactions', ]; - if ($this->contact->type == 'customer') { + if ($this->model->type == 'customer') { $rels['invoices'] = 'invoices'; } else { $rels['bills'] = 'bills'; } - return $this->countRelationships($this->contact, $rels); + return $this->countRelationships($this->model, $rels); } } diff --git a/app/Jobs/Common/UpdateDashboard.php b/app/Jobs/Common/UpdateDashboard.php index 6fef36ac7..1d18c5c88 100644 --- a/app/Jobs/Common/UpdateDashboard.php +++ b/app/Jobs/Common/UpdateDashboard.php @@ -3,59 +3,37 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Common\Dashboard; use App\Traits\Users; -class UpdateDashboard extends Job +class UpdateDashboard extends Job implements ShouldUpdate { use Users; - protected $dashboard; - - protected $request; - - /** - * Create a new job instance. - * - * @param $dashboard - * @param $request - */ - public function __construct($dashboard, $request) - { - $this->dashboard = $dashboard; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Dashboard - */ - public function handle() + public function handle(): Dashboard { $this->authorize(); \DB::transaction(function () { - $this->dashboard->update($this->request->all()); + $this->model->update($this->request->all()); if ($this->request->has('users')) { - $this->dashboard->users()->sync($this->request->get('users')); + $this->model->users()->sync($this->request->get('users')); } }); - return $this->dashboard; + return $this->model; } /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { // Can't disable last dashboard for any shared user if ($this->request->has('enabled') && !$this->request->get('enabled')) { - foreach ($this->dashboard->users as $user) { + foreach ($this->model->users as $user) { if ($user->dashboards()->enabled()->count() > 1) { continue; } @@ -77,7 +55,7 @@ class UpdateDashboard extends Job } // Check if user can access dashboard - if ($this->isNotUserDashboard($this->dashboard->id)) { + if ($this->isNotUserDashboard($this->model->id)) { $message = trans('dashboards.error.not_user_dashboard'); throw new \Exception($message); diff --git a/app/Jobs/Common/UpdateItem.php b/app/Jobs/Common/UpdateItem.php index ee81220e0..a5eceb42f 100644 --- a/app/Jobs/Common/UpdateItem.php +++ b/app/Jobs/Common/UpdateItem.php @@ -3,49 +3,29 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Jobs\Common\CreateItemTaxes; use App\Models\Common\Item; -class UpdateItem extends Job +class UpdateItem extends Job implements ShouldUpdate { - protected $item; - - protected $request; - - /** - * Create a new job instance. - * - * @param $item - * @param $request - */ - public function __construct($item, $request) - { - $this->item = $item; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Item - */ - public function handle() + public function handle(): Item { \DB::transaction(function () { - $this->item->update($this->request->all()); + $this->model->update($this->request->all()); // Upload picture if ($this->request->file('picture')) { $media = $this->getMedia($this->request->file('picture'), 'items'); - $this->item->attachMedia($media, 'picture'); + $this->model->attachMedia($media, 'picture'); } - $this->deleteRelationships($this->item, ['taxes']); + $this->deleteRelationships($this->model, ['taxes']); - $this->dispatch(new CreateItemTaxes($this->item, $this->request)); + $this->dispatch(new CreateItemTaxes($this->model, $this->request)); }); - return $this->item; + return $this->model; } } diff --git a/app/Jobs/Common/UpdateReport.php b/app/Jobs/Common/UpdateReport.php index 20cae5f0a..55f5cdcf8 100644 --- a/app/Jobs/Common/UpdateReport.php +++ b/app/Jobs/Common/UpdateReport.php @@ -3,37 +3,17 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Common\Report; -class UpdateReport extends Job +class UpdateReport extends Job implements ShouldUpdate { - protected $report; - - protected $request; - - /** - * Create a new job instance. - * - * @param $report - * @param $request - */ - public function __construct($report, $request) - { - $this->report = $report; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Report - */ - public function handle() + public function handle(): Report { \DB::transaction(function () { - $this->report->update($this->request->all()); + $this->model->update($this->request->all()); }); - return $this->report; + return $this->model; } } diff --git a/app/Jobs/Common/UpdateWidget.php b/app/Jobs/Common/UpdateWidget.php index 710c1e119..5b9868254 100644 --- a/app/Jobs/Common/UpdateWidget.php +++ b/app/Jobs/Common/UpdateWidget.php @@ -3,37 +3,17 @@ namespace App\Jobs\Common; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Common\Widget; -class UpdateWidget extends Job +class UpdateWidget extends Job implements ShouldUpdate { - protected $widget; - - protected $request; - - /** - * Create a new job instance. - * - * @param $widget - * @param $request - */ - public function __construct($widget, $request) - { - $this->widget = $widget; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Item - */ - public function handle() + public function handle(): Widget { \DB::transaction(function () { - $this->widget->update($this->request->all()); + $this->model->update($this->request->all()); }); - return $this->widget; + return $this->model; } } diff --git a/app/Jobs/Document/CancelDocument.php b/app/Jobs/Document/CancelDocument.php index 746b23ef8..5e64ae5c3 100644 --- a/app/Jobs/Document/CancelDocument.php +++ b/app/Jobs/Document/CancelDocument.php @@ -7,34 +7,26 @@ use App\Models\Document\Document; class CancelDocument extends Job { - protected $document; + protected $model; - /** - * Create a new job instance. - * - * @param $document - */ - public function __construct($document) + public function __construct(Document $model) { - $this->document = $document; + $this->model = $model; + + parent::__construct($model); } - /** - * Execute the job. - * - * @return Document - */ - public function handle() + public function handle(): Document { \DB::transaction(function () { - $this->deleteRelationships($this->document, [ + $this->deleteRelationships($this->model, [ 'transactions', 'recurring' ]); - $this->document->status = 'cancelled'; - $this->document->save(); + $this->model->status = 'cancelled'; + $this->model->save(); }); - return $this->document; + return $this->model; } } diff --git a/app/Jobs/Document/CreateDocument.php b/app/Jobs/Document/CreateDocument.php index 647a8b507..d45a08e80 100644 --- a/app/Jobs/Document/CreateDocument.php +++ b/app/Jobs/Document/CreateDocument.php @@ -5,33 +5,15 @@ namespace App\Jobs\Document; use App\Abstracts\Job; use App\Events\Document\DocumentCreated; use App\Events\Document\DocumentCreating; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Jobs\Document\CreateDocumentItemsAndTotals; use App\Models\Document\Document; use Illuminate\Support\Str; -class CreateDocument extends Job +class CreateDocument extends Job implements HasOwner, ShouldCreate { - protected $document; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Document - */ - public function handle() + public function handle(): Document { if (empty($this->request['amount'])) { $this->request['amount'] = 0; @@ -40,26 +22,26 @@ class CreateDocument extends Job event(new DocumentCreating($this->request)); \DB::transaction(function () { - $this->document = Document::create($this->request->all()); + $this->model = Document::create($this->request->all()); // Upload attachment if ($this->request->file('attachment')) { foreach ($this->request->file('attachment') as $attachment) { - $media = $this->getMedia($attachment, Str::plural($this->document->type)); + $media = $this->getMedia($attachment, Str::plural($this->model->type)); - $this->document->attachMedia($media, 'attachment'); + $this->model->attachMedia($media, 'attachment'); } } - $this->dispatch(new CreateDocumentItemsAndTotals($this->document, $this->request)); + $this->dispatch(new CreateDocumentItemsAndTotals($this->model, $this->request)); - $this->document->update($this->request->all()); + $this->model->update($this->request->all()); - $this->document->createRecurring($this->request->all()); + $this->model->createRecurring($this->request->all()); }); - event(new DocumentCreated($this->document, $this->request)); + event(new DocumentCreated($this->model, $this->request)); - return $this->document; + return $this->model; } } diff --git a/app/Jobs/Document/CreateDocumentHistory.php b/app/Jobs/Document/CreateDocumentHistory.php index 33c04a3e2..b710a3d49 100644 --- a/app/Jobs/Document/CreateDocumentHistory.php +++ b/app/Jobs/Document/CreateDocumentHistory.php @@ -3,9 +3,11 @@ namespace App\Jobs\Document; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldCreate; +use App\Models\Document\Document; use App\Models\Document\DocumentHistory; -class CreateDocumentHistory extends Job +class CreateDocumentHistory extends Job implements ShouldCreate { protected $document; @@ -13,26 +15,16 @@ class CreateDocumentHistory extends Job protected $description; - /** - * Create a new job instance. - * - * @param $document - * @param $notify - * @param $description - */ - public function __construct($document, $notify = 0, $description = null) + public function __construct(Document $document, $notify = 0, $description = null) { $this->document = $document; $this->notify = $notify; $this->description = $description; + + parent::__construct($document, $notify, $description); } - /** - * Execute the job. - * - * @return DocumentHistory - */ - public function handle() + public function handle(): DocumentHistory { $description = $this->description ?: trans_choice('general.payments', 1); diff --git a/app/Jobs/Document/CreateDocumentItem.php b/app/Jobs/Document/CreateDocumentItem.php index c04244708..89db2a16e 100644 --- a/app/Jobs/Document/CreateDocumentItem.php +++ b/app/Jobs/Document/CreateDocumentItem.php @@ -3,35 +3,28 @@ namespace App\Jobs\Document; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldCreate; +use App\Models\Document\Document; use App\Models\Document\DocumentItem; use App\Models\Document\DocumentItemTax; use App\Models\Setting\Tax; use Illuminate\Support\Str; -class CreateDocumentItem extends Job +class CreateDocumentItem extends Job implements ShouldCreate { protected $document; protected $request; - /** - * Create a new job instance. - * - * @param $document - * @param $request - */ - public function __construct($document, $request) + public function __construct(Document $document, $request) { $this->document = $document; $this->request = $request; + + parent::__construct($document, $request); } - /** - * Execute the job. - * - * @return DocumentItem - */ - public function handle() + public function handle(): DocumentItem { $item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0; $precision = config('money.' . $this->document->currency_code . '.precision'); diff --git a/app/Jobs/Document/CreateDocumentItemsAndTotals.php b/app/Jobs/Document/CreateDocumentItemsAndTotals.php index 4d971ebf4..bed1acb89 100644 --- a/app/Jobs/Document/CreateDocumentItemsAndTotals.php +++ b/app/Jobs/Document/CreateDocumentItemsAndTotals.php @@ -3,36 +3,27 @@ namespace App\Jobs\Document; use App\Abstracts\Job; -use App\Traits\DateTime; -use App\Traits\Currencies; +use App\Interfaces\Job\ShouldCreate; use App\Jobs\Common\CreateItem; +use App\Models\Document\Document; use App\Models\Document\DocumentTotal; +use App\Traits\Currencies; +use App\Traits\DateTime; -class CreateDocumentItemsAndTotals extends Job +class CreateDocumentItemsAndTotals extends Job implements ShouldCreate { use Currencies, DateTime; protected $document; - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($document, $request) + public function __construct(Document $document, $request) { $this->document = $document; - $this->request = $this->getRequestInstance($request); + + parent::__construct($request); } - /** - * Execute the job. - * - * @return void - */ - public function handle() + public function handle(): void { $precision = config('money.' . $this->document->currency_code . '.precision'); @@ -152,7 +143,7 @@ class CreateDocumentItemsAndTotals extends Job ]); } - protected function createItems() + protected function createItems(): array { $sub_total = $discount_amount = $discount_amount_total = 0; diff --git a/app/Jobs/Document/DeleteDocument.php b/app/Jobs/Document/DeleteDocument.php index c72600da4..05265c2d5 100644 --- a/app/Jobs/Document/DeleteDocument.php +++ b/app/Jobs/Document/DeleteDocument.php @@ -3,40 +3,24 @@ namespace App\Jobs\Document; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; use App\Observers\Transaction; use Illuminate\Support\Str; -class DeleteDocument extends Job +class DeleteDocument extends Job implements ShouldDelete { - protected $document; - - /** - * Create a new job instance. - * - * @param $document - */ - public function __construct($document) - { - $this->document = $document; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { $this->authorize(); \DB::transaction(function () { Transaction::mute(); - $this->deleteRelationships($this->document, [ + $this->deleteRelationships($this->model, [ 'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals' ]); - $this->document->delete(); + $this->model->delete(); Transaction::unmute(); }); @@ -46,13 +30,11 @@ class DeleteDocument extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { - if ($this->document->transactions()->isReconciled()->count()) { - $type = Str::plural($this->document->type); + if ($this->model->transactions()->isReconciled()->count()) { + $type = Str::plural($this->model->type); $message = trans('messages.warning.reconciled_doc', ['type' => trans_choice("general.$type", 1)]); throw new \Exception($message); diff --git a/app/Jobs/Document/DuplicateDocument.php b/app/Jobs/Document/DuplicateDocument.php index 498669cdb..972b55d62 100644 --- a/app/Jobs/Document/DuplicateDocument.php +++ b/app/Jobs/Document/DuplicateDocument.php @@ -8,29 +8,19 @@ use App\Models\Document\Document; class DuplicateDocument extends Job { - protected $document; - protected $clone; - /** - * Create a new job instance. - * - * @param $document - */ - public function __construct($document) + public function __construct(Document $model) { - $this->document = $document; + $this->model = $model; + + parent::__construct($model); } - /** - * Execute the job. - * - * @return Document - */ - public function handle() + public function handle(): Document { \DB::transaction(function () { - $this->clone = $this->document->duplicate(); + $this->clone = $this->model->duplicate(); }); event(new DocumentCreated($this->clone, request())); diff --git a/app/Jobs/Document/UpdateDocument.php b/app/Jobs/Document/UpdateDocument.php index 7c144cd81..6295968ba 100644 --- a/app/Jobs/Document/UpdateDocument.php +++ b/app/Jobs/Document/UpdateDocument.php @@ -6,85 +6,66 @@ use App\Abstracts\Job; use App\Events\Document\PaidAmountCalculated; use App\Events\Document\DocumentUpdated; use App\Events\Document\DocumentUpdating; +use App\Interfaces\Job\ShouldUpdate; use App\Jobs\Document\CreateDocumentItemsAndTotals; use App\Models\Document\Document; use App\Traits\Relationships; use Illuminate\Support\Str; -class UpdateDocument extends Job +class UpdateDocument extends Job implements ShouldUpdate { use Relationships; - protected $document; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($document, $request) - { - $this->document = $document; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Document - */ - public function handle() + public function handle(): Document { if (empty($this->request['amount'])) { $this->request['amount'] = 0; } - event(new DocumentUpdating($this->document, $this->request)); + event(new DocumentUpdating($this->model, $this->request)); \DB::transaction(function () { // Upload attachment if ($this->request->file('attachment')) { - $this->deleteMediaModel($this->document, 'attachment', $this->request); + $this->deleteMediaModel($this->model, 'attachment', $this->request); foreach ($this->request->file('attachment') as $attachment) { - $media = $this->getMedia($attachment, Str::plural($this->document->type)); + $media = $this->getMedia($attachment, Str::plural($this->model->type)); - $this->document->attachMedia($media, 'attachment'); + $this->model->attachMedia($media, 'attachment'); } - } elseif (!$this->request->file('attachment') && $this->document->attachment) { - $this->deleteMediaModel($this->document, 'attachment', $this->request); + } elseif (!$this->request->file('attachment') && $this->model->attachment) { + $this->deleteMediaModel($this->model, 'attachment', $this->request); } - $this->deleteRelationships($this->document, ['items', 'item_taxes', 'totals']); + $this->deleteRelationships($this->model, ['items', 'item_taxes', 'totals']); - $this->dispatch(new CreateDocumentItemsAndTotals($this->document, $this->request)); + $this->dispatch(new CreateDocumentItemsAndTotals($this->model, $this->request)); - $this->document->paid_amount = $this->document->paid; + $this->model->paid_amount = $this->model->paid; - event(new PaidAmountCalculated($this->document)); + event(new PaidAmountCalculated($this->model)); - if ($this->document->paid_amount > 0) { - if ($this->request['amount'] == $this->document->paid_amount) { + if ($this->model->paid_amount > 0) { + if ($this->request['amount'] == $this->model->paid_amount) { $this->request['status'] = 'paid'; } - if ($this->request['amount'] > $this->document->paid_amount) { + if ($this->request['amount'] > $this->model->paid_amount) { $this->request['status'] = 'partial'; } } - unset($this->document->reconciled); - unset($this->document->paid_amount); + unset($this->model->reconciled); + unset($this->model->paid_amount); - $this->document->update($this->request->all()); + $this->model->update($this->request->all()); - $this->document->updateRecurring($this->request->all()); + $this->model->updateRecurring($this->request->all()); }); - event(new DocumentUpdated($this->document, $this->request)); + event(new DocumentUpdated($this->model, $this->request)); - return $this->document; + return $this->model; } } diff --git a/app/Jobs/Setting/CreateCategory.php b/app/Jobs/Setting/CreateCategory.php index 2d415004f..2c20cf733 100644 --- a/app/Jobs/Setting/CreateCategory.php +++ b/app/Jobs/Setting/CreateCategory.php @@ -3,36 +3,18 @@ namespace App\Jobs\Setting; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Setting\Category; -class CreateCategory extends Job +class CreateCategory extends Job implements HasOwner, ShouldCreate { - protected $category; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Category - */ - public function handle() + public function handle(): Category { \DB::transaction(function () { - $this->category = Category::create($this->request->all()); + $this->model = Category::create($this->request->all()); }); - return $this->category; + return $this->model; } } diff --git a/app/Jobs/Setting/CreateCurrency.php b/app/Jobs/Setting/CreateCurrency.php index 716eb0deb..0487d7485 100644 --- a/app/Jobs/Setting/CreateCurrency.php +++ b/app/Jobs/Setting/CreateCurrency.php @@ -3,31 +3,13 @@ namespace App\Jobs\Setting; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Setting\Currency; -class CreateCurrency extends Job +class CreateCurrency extends Job implements HasOwner, ShouldCreate { - protected $currency; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Currency - */ - public function handle() + public function handle(): Currency { // Force the rate to be 1 for default currency if ($this->request->get('default_currency')) { @@ -35,7 +17,7 @@ class CreateCurrency extends Job } \DB::transaction(function () { - $this->currency = Currency::create($this->request->all()); + $this->model = Currency::create($this->request->all()); // Update default currency setting if ($this->request->get('default_currency')) { @@ -44,6 +26,6 @@ class CreateCurrency extends Job } }); - return $this->currency; + return $this->model; } } diff --git a/app/Jobs/Setting/CreateTax.php b/app/Jobs/Setting/CreateTax.php index 810cc6caa..276cff0d6 100644 --- a/app/Jobs/Setting/CreateTax.php +++ b/app/Jobs/Setting/CreateTax.php @@ -3,36 +3,18 @@ namespace App\Jobs\Setting; use App\Abstracts\Job; +use App\Interfaces\Job\HasOwner; +use App\Interfaces\Job\ShouldCreate; use App\Models\Setting\Tax; -class CreateTax extends Job +class CreateTax extends Job implements HasOwner, ShouldCreate { - protected $tax; - - protected $request; - - /** - * Create a new job instance. - * - * @param $request - */ - public function __construct($request) - { - $this->request = $this->getRequestInstance($request); - $this->request->merge(['created_by' => user_id()]); - } - - /** - * Execute the job. - * - * @return Tax - */ - public function handle() + public function handle(): Tax { \DB::transaction(function () { - $this->tax = Tax::create($this->request->all()); + $this->model = Tax::create($this->request->all()); }); - return $this->tax; + return $this->model; } } diff --git a/app/Jobs/Setting/DeleteCategory.php b/app/Jobs/Setting/DeleteCategory.php index 68a55e5f7..5018718ab 100644 --- a/app/Jobs/Setting/DeleteCategory.php +++ b/app/Jobs/Setting/DeleteCategory.php @@ -3,33 +3,17 @@ namespace App\Jobs\Setting; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; use App\Models\Setting\Category; -class DeleteCategory extends Job +class DeleteCategory extends Job implements ShouldDelete { - protected $category; - - /** - * Create a new job instance. - * - * @param $category - */ - public function __construct($category) - { - $this->category = $category; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { $this->authorize(); \DB::transaction(function () { - $this->category->delete(); + $this->model->delete(); }); return true; @@ -37,26 +21,24 @@ class DeleteCategory extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { // Can not delete the last category by type - if (Category::where('type', $this->category->type)->count() == 1) { - $message = trans('messages.error.last_category', ['type' => strtolower(trans_choice('general.' . $this->category->type . 's', 1))]); + if (Category::where('type', $this->model->type)->count() == 1) { + $message = trans('messages.error.last_category', ['type' => strtolower(trans_choice('general.' . $this->model->type . 's', 1))]); throw new \Exception($message); } if ($relationships = $this->getRelationships()) { - $message = trans('messages.warning.deleted', ['name' => $this->category->name, 'text' => implode(', ', $relationships)]); + $message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'items' => 'items', @@ -65,6 +47,6 @@ class DeleteCategory extends Job 'transactions' => 'transactions', ]; - return $this->countRelationships($this->category, $rels); + return $this->countRelationships($this->model, $rels); } } diff --git a/app/Jobs/Setting/DeleteCurrency.php b/app/Jobs/Setting/DeleteCurrency.php index 9ff5e844f..c3e542712 100644 --- a/app/Jobs/Setting/DeleteCurrency.php +++ b/app/Jobs/Setting/DeleteCurrency.php @@ -3,32 +3,16 @@ namespace App\Jobs\Setting; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeleteCurrency extends Job +class DeleteCurrency extends Job implements ShouldDelete { - protected $currency; - - /** - * Create a new job instance. - * - * @param $currency - */ - public function __construct($currency) - { - $this->currency = $currency; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { $this->authorize(); \DB::transaction(function () { - $this->currency->delete(); + $this->model->delete(); }); return true; @@ -36,19 +20,17 @@ class DeleteCurrency extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { if ($relationships = $this->getRelationships()) { - $message = trans('messages.warning.deleted', ['name' => $this->currency->name, 'text' => implode(', ', $relationships)]); + $message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'accounts' => 'accounts', @@ -58,9 +40,9 @@ class DeleteCurrency extends Job 'transactions' => 'transactions', ]; - $relationships = $this->countRelationships($this->currency, $rels); + $relationships = $this->countRelationships($this->model, $rels); - if ($this->currency->code == setting('default.currency')) { + if ($this->model->code == setting('default.currency')) { $relationships[] = strtolower(trans_choice('general.companies', 1)); } diff --git a/app/Jobs/Setting/DeleteTax.php b/app/Jobs/Setting/DeleteTax.php index 21bf97ef3..34fccd728 100644 --- a/app/Jobs/Setting/DeleteTax.php +++ b/app/Jobs/Setting/DeleteTax.php @@ -3,32 +3,16 @@ namespace App\Jobs\Setting; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldDelete; -class DeleteTax extends Job +class DeleteTax extends Job implements ShouldDelete { - protected $tax; - - /** - * Create a new job instance. - * - * @param $tax - */ - public function __construct($tax) - { - $this->tax = $tax; - } - - /** - * Execute the job. - * - * @return boolean|Exception - */ - public function handle() + public function handle(): bool { $this->authorize(); \DB::transaction(function () { - $this->tax->delete(); + $this->model->delete(); }); return true; @@ -36,19 +20,17 @@ class DeleteTax extends Job /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { if ($relationships = $this->getRelationships()) { - $message = trans('messages.warning.deleted', ['name' => $this->tax->name, 'text' => implode(', ', $relationships)]); + $message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'items' => 'items', @@ -56,6 +38,6 @@ class DeleteTax extends Job 'bill_items' => 'bills', ]; - return $this->countRelationships($this->tax, $rels); + return $this->countRelationships($this->model, $rels); } } diff --git a/app/Jobs/Setting/UpdateCategory.php b/app/Jobs/Setting/UpdateCategory.php index 360df3aed..8c1eefade 100644 --- a/app/Jobs/Setting/UpdateCategory.php +++ b/app/Jobs/Setting/UpdateCategory.php @@ -3,67 +3,45 @@ namespace App\Jobs\Setting; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Setting\Category; -class UpdateCategory extends Job +class UpdateCategory extends Job implements ShouldUpdate { - protected $category; - - protected $request; - - /** - * Create a new job instance. - * - * @param $category - * @param $request - */ - public function __construct($category, $request) - { - $this->category = $category; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Category - */ - public function handle() + public function handle(): Category { $this->authorize(); \DB::transaction(function () { - $this->category->update($this->request->all()); + $this->model->update($this->request->all()); }); - return $this->category; + return $this->model; } /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { - if (!$relationships = $this->getRelationships()) { + if (! $relationships = $this->getRelationships()) { return; } - if ($this->request->has('type') && ($this->request->get('type') != $this->category->type)) { + if ($this->request->has('type') && ($this->request->get('type') != $this->model->type)) { $message = trans('messages.error.change_type', ['text' => implode(', ', $relationships)]); throw new \Exception($message); } - if (!$this->request->get('enabled')) { - $message = trans('messages.warning.disabled', ['name' => $this->category->name, 'text' => implode(', ', $relationships)]); + if (! $this->request->get('enabled')) { + $message = trans('messages.warning.disabled', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'items' => 'items', @@ -72,6 +50,6 @@ class UpdateCategory extends Job 'transactions' => 'transactions', ]; - return $this->countRelationships($this->category, $rels); + return $this->countRelationships($this->model, $rels); } } diff --git a/app/Jobs/Setting/UpdateCurrency.php b/app/Jobs/Setting/UpdateCurrency.php index 808215aae..ae0930844 100644 --- a/app/Jobs/Setting/UpdateCurrency.php +++ b/app/Jobs/Setting/UpdateCurrency.php @@ -3,32 +3,12 @@ namespace App\Jobs\Setting; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Setting\Currency; -class UpdateCurrency extends Job +class UpdateCurrency extends Job implements ShouldUpdate { - protected $currency; - - protected $request; - - /** - * Create a new job instance. - * - * @param $currency - * @param $request - */ - public function __construct($currency, $request) - { - $this->currency = $currency; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Currency - */ - public function handle() + public function handle(): Currency { $this->authorize(); @@ -38,7 +18,7 @@ class UpdateCurrency extends Job } \DB::transaction(function () { - $this->currency->update($this->request->all()); + $this->model->update($this->request->all()); // Update default currency setting if ($this->request->get('default_currency')) { @@ -47,34 +27,32 @@ class UpdateCurrency extends Job } }); - return $this->currency; + return $this->model; } /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { - if (!$relationships = $this->getRelationships()) { + if (! $relationships = $this->getRelationships()) { return; } - if ($this->currency->code != $this->request->get('code')) { - $message = trans('messages.warning.disable_code', ['name' => $this->currency->name, 'text' => implode(', ', $relationships)]); + if ($this->model->code != $this->request->get('code')) { + $message = trans('messages.warning.disable_code', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } - if (!$this->request->get('enabled')) { - $message = trans('messages.warning.disable_code', ['name' => $this->currency->name, 'text' => implode(', ', $relationships)]); + if (! $this->request->get('enabled')) { + $message = trans('messages.warning.disable_code', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'accounts' => 'accounts', @@ -84,9 +62,9 @@ class UpdateCurrency extends Job 'transactions' => 'transactions', ]; - $relationships = $this->countRelationships($this->currency, $rels); + $relationships = $this->countRelationships($this->model, $rels); - if ($this->currency->code == setting('default.currency')) { + if ($this->model->code == setting('default.currency')) { $relationships[] = strtolower(trans_choice('general.companies', 1)); } diff --git a/app/Jobs/Setting/UpdateTax.php b/app/Jobs/Setting/UpdateTax.php index 4c833e20f..4f01cf2ef 100644 --- a/app/Jobs/Setting/UpdateTax.php +++ b/app/Jobs/Setting/UpdateTax.php @@ -3,67 +3,45 @@ namespace App\Jobs\Setting; use App\Abstracts\Job; +use App\Interfaces\Job\ShouldUpdate; use App\Models\Setting\Tax; -class UpdateTax extends Job +class UpdateTax extends Job implements ShouldUpdate { - protected $tax; - - protected $request; - - /** - * Create a new job instance. - * - * @param $tax - * @param $request - */ - public function __construct($tax, $request) - { - $this->tax = $tax; - $this->request = $this->getRequestInstance($request); - } - - /** - * Execute the job. - * - * @return Tax - */ - public function handle() + public function handle(): Tax { $this->authorize(); \DB::transaction(function () { - $this->tax->update($this->request->all()); + $this->model->update($this->request->all()); }); - return $this->tax; + return $this->model; } /** * Determine if this action is applicable. - * - * @return void */ - public function authorize() + public function authorize(): void { - if (!$relationships = $this->getRelationships()) { + if (! $relationships = $this->getRelationships()) { return; } - if ($this->request->has('type') && ($this->request->get('type') != $this->tax->type)) { + if ($this->request->has('type') && ($this->request->get('type') != $this->model->type)) { $message = trans('messages.error.change_type', ['text' => implode(', ', $relationships)]); throw new \Exception($message); } - if (!$this->request->get('enabled')) { - $message = trans('messages.warning.disabled', ['name' => $this->tax->name, 'text' => implode(', ', $relationships)]); + if (! $this->request->get('enabled')) { + $message = trans('messages.warning.disabled', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]); throw new \Exception($message); } } - public function getRelationships() + public function getRelationships(): array { $rels = [ 'items' => 'items', @@ -71,6 +49,6 @@ class UpdateTax extends Job 'bill_items' => 'bills', ]; - return $this->countRelationships($this->tax, $rels); + return $this->countRelationships($this->model, $rels); } }