akaunting/app/Jobs/Banking/UpdateTransaction.php

72 lines
1.7 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?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();
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->transaction->update($this->request->all());
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
// Upload attachment
if ($this->request->file('attachment')) {
$this->transaction->delete_attachment();
2019-11-16 10:21:14 +03:00
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();
2020-06-26 13:40:19 +03:00
}
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
// Recurring
$this->transaction->updateRecurring();
});
2019-11-16 10:21:14 +03:00
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);
}
}
2019-11-16 10:21:14 +03:00
}