2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Banking;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2022-08-09 18:13:12 +03:00
|
|
|
use App\Events\Banking\TransactionUpdated;
|
|
|
|
use App\Events\Banking\TransactionUpdating;
|
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
|
|
|
{
|
2020-03-13 14:55:50 +03:00
|
|
|
$this->authorize();
|
|
|
|
|
2022-08-09 18:13:12 +03:00
|
|
|
event(new TransactionUpdating($this->model, $this->request));
|
|
|
|
|
2023-06-16 14:18:52 +03:00
|
|
|
if (! array_key_exists($this->request->get('type'), config('type.transaction'))) {
|
|
|
|
$type = (empty($this->request->get('recurring_frequency')) || ($this->request->get('recurring_frequency') == 'no')) ? Transaction::INCOME_TYPE : Transaction::INCOME_RECURRING_TYPE;
|
|
|
|
|
|
|
|
$this->request->merge(['type' => $type]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-02-09 11:54:53 +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');
|
2021-02-09 11:54:53 +03:00
|
|
|
}
|
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
|
|
|
|
2022-08-09 18:13:12 +03:00
|
|
|
event(new TransactionUpdated($this->model, $this->request));
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
return $this->model;
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
2020-03-13 14:55:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this action is applicable.
|
|
|
|
*/
|
2021-09-06 11:53:57 +03:00
|
|
|
public function authorize(): void
|
2020-03-13 14:55:50 +03:00
|
|
|
{
|
2021-09-06 11:53:57 +03:00
|
|
|
if ($this->model->reconciled) {
|
2020-03-13 14:55:50 +03:00
|
|
|
$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');
|
|
|
|
}
|
2020-03-13 14:55:50 +03:00
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|