akaunting/app/Jobs/Banking/UpdateTransaction.php
2021-02-09 11:54:53 +03:00

72 lines
1.7 KiB
PHP

<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
use App\Models\Banking\Transaction;
class UpdateTransaction extends Job
{
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()
{
$this->authorize();
\DB::transaction(function () {
$this->transaction->update($this->request->all());
// Upload attachment
if ($this->request->file('attachment')) {
$this->transaction->delete_attachment();
foreach ($this->request->file('attachment') as $attachment) {
$media = $this->getMedia($attachment, 'transactions');
$this->transaction->attachMedia($media, 'attachment');
}
} elseif (!$this->request->file('attachment') && $this->transaction->attachment) {
$this->transaction->delete_attachment();
}
// Recurring
$this->transaction->updateRecurring();
});
return $this->transaction;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
if ($this->transaction->reconciled) {
$message = trans('messages.warning.reconciled_tran');
throw new \Exception($message);
}
}
}