-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
UpdateTransaction.php
65 lines (49 loc) · 2.03 KB
/
UpdateTransaction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
use App\Events\Banking\TransactionUpdated;
use App\Events\Banking\TransactionUpdating;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Banking\Transaction;
class UpdateTransaction extends Job implements ShouldUpdate
{
public function handle(): Transaction
{
$this->authorize();
event(new TransactionUpdating($this->model, $this->request));
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]);
}
\DB::transaction(function () {
$this->model->update($this->request->all());
// Upload attachment
if ($this->request->file('attachment')) {
$this->deleteMediaModel($this->model, 'attachment', $this->request);
foreach ($this->request->file('attachment') as $attachment) {
$media = $this->getMedia($attachment, 'transactions');
$this->model->attachMedia($media, 'attachment');
}
} elseif (! $this->request->file('attachment') && $this->model->attachment) {
$this->deleteMediaModel($this->model, 'attachment', $this->request);
}
// Recurring
$this->model->updateRecurring($this->request->all());
});
event(new TransactionUpdated($this->model, $this->request));
return $this->model;
}
/**
* Determine if this action is applicable.
*/
public function authorize(): void
{
if ($this->model->reconciled) {
$message = trans('messages.warning.reconciled_tran');
throw new \Exception($message);
}
if ($this->model->isTransferTransaction()) {
throw new \Exception('Unauthorized');
}
}
}