-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
UpdateTax.php
60 lines (46 loc) · 1.55 KB
/
UpdateTax.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
<?php
namespace App\Jobs\Setting;
use App\Abstracts\Job;
use App\Events\Setting\TaxUpdated;
use App\Events\Setting\TaxUpdating;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Setting\Tax;
class UpdateTax extends Job implements ShouldUpdate
{
public function handle(): Tax
{
$this->authorize();
event(new TaxUpdating($this->model, $this->request));
\DB::transaction(function () {
$this->model->update($this->request->all());
});
event(new TaxUpdated($this->model, $this->request));
return $this->model;
}
/**
* Determine if this action is applicable.
*/
public function authorize(): void
{
if (! $relationships = $this->getRelationships()) {
return;
}
if ($this->request->has('type') && ($this->request->get('type') != $this->model->type)) {
$message = trans('messages.error.change_type', ['text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
if ($this->request->has('enabled') && ! $this->request->get('enabled')) {
$message = trans('messages.warning.disabled', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
}
public function getRelationships(): array
{
$rels = [
'items' => 'items',
'invoice_items' => 'invoices',
'bill_items' => 'bills',
];
return $this->countRelationships($this->model, $rels);
}
}