added pointer interfaces for jobs

This commit is contained in:
Denis Duliçi 2021-09-06 11:53:57 +03:00
parent abd55133f1
commit c08a8cfc4e
63 changed files with 635 additions and 1524 deletions

View File

@ -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()]);
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Interfaces\Job;
interface HasOwner
{
//
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Interfaces\Job;
interface ShouldCreate
{
//
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Interfaces\Job;
interface ShouldDelete
{
//
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Interfaces\Job;
interface ShouldUpdate
{
//
}

View File

@ -3,35 +3,17 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Auth\Permission;
class CreatePermission extends Job
class CreatePermission extends Job implements ShouldCreate
{
protected $permission;
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(): Permission
{
\DB::transaction(function () {
$this->permission = Permission::create($this->request->all());
$this->model = Permission::create($this->request->all());
});
return $this->permission;
return $this->model;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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)

View File

@ -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));
}

View File

@ -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();
});

View File

@ -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');
}
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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)

View File

@ -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);
}

View File

@ -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()) {

View File

@ -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++;

View File

@ -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;
}
}

View File

@ -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()
{

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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');

View File

@ -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;

View File

@ -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);

View File

@ -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()));

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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));
}

View File

@ -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);
}
}

View File

@ -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()) {
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)]);
$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);
}
}

View File

@ -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()) {
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)]);
$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));
}

View File

@ -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()) {
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)]);
$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);
}
}