akaunting/app/Jobs/Banking/UpdateTransaction.php

54 lines
1.5 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\ShouldUpdate;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Transaction;
2021-09-06 11:53:57 +03:00
class UpdateTransaction extends Job implements ShouldUpdate
2019-11-16 10:21:14 +03:00
{
2021-09-06 11:53:57 +03:00
public function handle(): Transaction
2019-11-16 10:21:14 +03:00
{
$this->authorize();
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
2021-09-06 11:53:57 +03:00
$this->model->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')) {
2021-09-06 11:53:57 +03:00
$this->deleteMediaModel($this->model, 'attachment', $this->request);
2019-11-16 10:21:14 +03:00
foreach ($this->request->file('attachment') as $attachment) {
$media = $this->getMedia($attachment, 'transactions');
2021-09-06 11:53:57 +03:00
$this->model->attachMedia($media, 'attachment');
}
2022-08-05 19:04:38 +03:00
} elseif (! $this->request->file('attachment') && $this->model->attachment) {
2021-09-06 11:53:57 +03:00
$this->deleteMediaModel($this->model, 'attachment', $this->request);
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
2021-09-06 11:53:57 +03:00
$this->model->updateRecurring($this->request->all());
2020-06-26 13:40:19 +03:00
});
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
return $this->model;
2019-11-16 10:21:14 +03:00
}
/**
* Determine if this action is applicable.
*/
2021-09-06 11:53:57 +03:00
public function authorize(): void
{
2021-09-06 11:53:57 +03:00
if ($this->model->reconciled) {
$message = trans('messages.warning.reconciled_tran');
throw new \Exception($message);
}
2022-08-04 09:54:27 +03:00
if ($this->model->isTransferTransaction()) {
throw new \Exception('Unauthorized');
}
}
2019-11-16 10:21:14 +03:00
}