1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Planning; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Siak\Tontine\Model\Fund; |
8
|
|
|
use Siak\Tontine\Model\Round; |
9
|
|
|
use Siak\Tontine\Service\TenantService; |
10
|
|
|
|
11
|
|
|
class FundService |
12
|
|
|
{ |
13
|
|
|
use SessionTrait; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param TenantService $tenantService |
17
|
|
|
*/ |
18
|
|
|
public function __construct(protected TenantService $tenantService) |
19
|
|
|
{} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Round $round |
23
|
|
|
* |
24
|
|
|
* @return Relation |
25
|
|
|
*/ |
26
|
|
|
private function getQuery(Round $round): Relation |
27
|
|
|
{ |
28
|
|
|
return $this->tenantService->guild() |
29
|
|
|
->funds() |
30
|
|
|
->when(!$round->add_default_fund, fn($query) => $query->user()); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get a paginated list of funds. |
35
|
|
|
* |
36
|
|
|
* @param Round $round |
37
|
|
|
* @param int $page |
38
|
|
|
* |
39
|
|
|
* @return Collection |
40
|
|
|
*/ |
41
|
|
|
public function getFundDefs(Round $round, int $page = 0): Collection |
42
|
|
|
{ |
43
|
|
|
return $this->getQuery($round) |
44
|
|
|
->with(['funds' => fn($query) => $query->ofRound($round)]) |
45
|
|
|
->page($page, $this->tenantService->getLimit()) |
46
|
|
|
->get(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the number of funds. |
51
|
|
|
* |
52
|
|
|
* @param Round $round |
53
|
|
|
* |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
|
public function getFundDefCount(Round $round): int |
57
|
|
|
{ |
58
|
|
|
return $this->getQuery($round)->count(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get a fund. |
63
|
|
|
* |
64
|
|
|
* @param Round $round |
65
|
|
|
* @param int $fundId |
66
|
|
|
* |
67
|
|
|
* @return Fund|null |
68
|
|
|
*/ |
69
|
|
|
public function getFund(Round $round, int $fundId): ?Fund |
70
|
|
|
{ |
71
|
|
|
return Fund::ofRound($round)->with(['sessions'])->find($fundId); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Round $round |
76
|
|
|
* @param int $defId |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function enableFund(Round $round, int $defId): void |
81
|
|
|
{ |
82
|
|
|
// Can't use the getQuery() method here because |
83
|
|
|
// the default fund can't be enabled or disabled. |
84
|
|
|
$def = $this->tenantService->guild() |
85
|
|
|
->funds() |
86
|
|
|
->user() |
87
|
|
|
->withCount(['funds' => fn($query) => $query->ofRound($round)]) |
88
|
|
|
->find($defId); |
89
|
|
|
if(!$def || $def->funds_count > 0) |
90
|
|
|
{ |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Create the fund |
95
|
|
|
$def->funds()->create([ |
96
|
|
|
'round_id' => $round->id, |
97
|
|
|
'start_sid' => $round->start->id, |
|
|
|
|
98
|
|
|
'end_sid' => $round->end->id, |
|
|
|
|
99
|
|
|
'interest_sid' => $round->end->id, |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Round $round |
105
|
|
|
* @param int $defId |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function disableFund(Round $round, int $defId): void |
110
|
|
|
{ |
111
|
|
|
// Can't use the getQuery() method here because |
112
|
|
|
// the default fund can't be enabled or disabled. |
113
|
|
|
$def = $this->tenantService->guild() |
114
|
|
|
->funds() |
115
|
|
|
->user() |
116
|
|
|
->withCount(['funds' => fn($query) => $query->ofRound($round)]) |
117
|
|
|
->find($defId); |
118
|
|
|
if(!$def || $def->funds_count === 0) |
119
|
|
|
{ |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Delete the fund |
124
|
|
|
$def->funds()->ofRound($round)->delete(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Save the fund sessions. |
129
|
|
|
* |
130
|
|
|
* @param Fund $fund |
131
|
|
|
* @param array $values |
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
public function saveSessions(Fund $fund, array $values) |
136
|
|
|
{ |
137
|
|
|
$fund->update($values); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|