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