|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Guild; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Siak\Tontine\Exception\MessageException; |
|
8
|
|
|
use Siak\Tontine\Model\ChargeDef; |
|
9
|
|
|
use Siak\Tontine\Model\Guild; |
|
10
|
|
|
use Siak\Tontine\Service\TenantService; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
|
|
13
|
|
|
class ChargeService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param TenantService $tenantService |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(private TenantService $tenantService) |
|
19
|
|
|
{} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get a paginated list of charges. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Guild $guild |
|
25
|
|
|
* @param bool|null $filter |
|
26
|
|
|
* @param int $page |
|
27
|
|
|
* |
|
28
|
|
|
* @return Collection |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getCharges(Guild $guild, bool $filter = null, int $page = 0): Collection |
|
31
|
|
|
{ |
|
32
|
|
|
return $guild->charges() |
|
33
|
|
|
->when($filter !== null, fn(Builder $query) => $query->active($filter)) |
|
34
|
|
|
->page($page, $this->tenantService->getLimit()) |
|
35
|
|
|
->orderBy('type', 'asc') |
|
36
|
|
|
->orderBy('period', 'desc') |
|
37
|
|
|
->orderBy('name', 'asc') |
|
38
|
|
|
->get(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the number of charges. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Guild $guild |
|
45
|
|
|
* @param bool|null $filter |
|
46
|
|
|
* |
|
47
|
|
|
* @return int |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getChargeCount(Guild $guild, bool $filter = null): int |
|
50
|
|
|
{ |
|
51
|
|
|
return $guild->charges() |
|
52
|
|
|
->when($filter !== null, fn(Builder $query) => $query->active($filter)) |
|
53
|
|
|
->count(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get a single charge. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Guild $guild |
|
60
|
|
|
* @param int $chargeId The charge id |
|
61
|
|
|
* |
|
62
|
|
|
* @return ChargeDef|null |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getCharge(Guild $guild, int $chargeId): ?ChargeDef |
|
65
|
|
|
{ |
|
66
|
|
|
return $guild->charges()->find($chargeId); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Add new charge. |
|
71
|
|
|
* |
|
72
|
|
|
* @param Guild $guild |
|
73
|
|
|
* @param array $values |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function createCharge(Guild $guild, array $values): bool |
|
78
|
|
|
{ |
|
79
|
|
|
$guild->charges()->create($values); |
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Update a charge. |
|
85
|
|
|
* |
|
86
|
|
|
* @param ChargeDef $charge |
|
87
|
|
|
* @param array $values |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function updateCharge(ChargeDef $charge, array $values): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return $charge->update($values); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Toggle a charge. |
|
98
|
|
|
* |
|
99
|
|
|
* @param ChargeDef $charge |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function toggleCharge(ChargeDef $charge) |
|
104
|
|
|
{ |
|
105
|
|
|
$charge->update(['active' => !$charge->active]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Delete a charge. |
|
110
|
|
|
* |
|
111
|
|
|
* @param ChargeDef $charge |
|
112
|
|
|
* |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
public function deleteCharge(ChargeDef $charge) |
|
116
|
|
|
{ |
|
117
|
|
|
try |
|
118
|
|
|
{ |
|
119
|
|
|
$charge->delete(); |
|
120
|
|
|
} |
|
121
|
|
|
catch(Exception) |
|
122
|
|
|
{ |
|
123
|
|
|
throw new MessageException(trans('tontine.charge.errors.cannot_delete')); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|