|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Meeting\Saving; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Siak\Tontine\Exception\MessageException; |
|
9
|
|
|
use Siak\Tontine\Model\Saving; |
|
10
|
|
|
use Siak\Tontine\Model\Member; |
|
11
|
|
|
use Siak\Tontine\Model\Session; |
|
12
|
|
|
use Siak\Tontine\Service\LocaleService; |
|
13
|
|
|
use Siak\Tontine\Service\TenantService; |
|
14
|
|
|
use Siak\Tontine\Service\Tontine\FundService; |
|
15
|
|
|
|
|
16
|
|
|
use function trans; |
|
17
|
|
|
|
|
18
|
|
|
class SavingService |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param LocaleService $localeService |
|
22
|
|
|
* @param TenantService $tenantService |
|
23
|
|
|
* @param FundService $fundService |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(private LocaleService $localeService, |
|
26
|
|
|
private TenantService $tenantService, private FundService $fundService) |
|
27
|
|
|
{} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param Session $session |
|
31
|
|
|
* @param int $fundId |
|
32
|
|
|
* |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
private function getSavingQuery(Session $session, int $fundId) |
|
36
|
|
|
{ |
|
37
|
|
|
return $session->savings() |
|
38
|
|
|
->when($fundId === 0, function(Builder $query) { |
|
39
|
|
|
$query->whereNull('fund_id'); |
|
40
|
|
|
}) |
|
41
|
|
|
->when($fundId > 0, function(Builder $query) use($fundId) { |
|
42
|
|
|
$query->where('fund_id', $fundId); |
|
43
|
|
|
}); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Count the savings for a given session. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Session $session |
|
50
|
|
|
* @param int $fundId |
|
51
|
|
|
* |
|
52
|
|
|
* @return int |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getSavingCount(Session $session, int $fundId): int |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->getSavingQuery($session, $fundId)->count(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the savings for a given session. |
|
61
|
|
|
* |
|
62
|
|
|
* @param Session $session |
|
63
|
|
|
* @param int $fundId |
|
64
|
|
|
* @param int $page |
|
65
|
|
|
* |
|
66
|
|
|
* @return Collection |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getSavings(Session $session, int $fundId, int $page = 0): Collection |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->getSavingQuery($session, $fundId) |
|
71
|
|
|
->with(['member', 'fund']) |
|
72
|
|
|
->page($page, $this->tenantService->getLimit()) |
|
73
|
|
|
->get(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get a saving for a given session. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Session $session |
|
80
|
|
|
* @param int $savingId |
|
81
|
|
|
* |
|
82
|
|
|
* @return Saving|null |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getSaving(Session $session, int $savingId): ?Saving |
|
85
|
|
|
{ |
|
86
|
|
|
return $session->savings()->with(['member', 'fund'])->find($savingId); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Create a saving. |
|
91
|
|
|
* |
|
92
|
|
|
* @param Member $member |
|
93
|
|
|
* @param Session $session The session |
|
94
|
|
|
* @param array $values |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function createSaving(Member $member, Session $session, array $values): void |
|
99
|
|
|
{ |
|
100
|
|
|
$saving = new Saving(); |
|
101
|
|
|
$saving->amount = $values['amount']; |
|
102
|
|
|
$saving->member()->associate($member); |
|
103
|
|
|
$saving->session()->associate($session); |
|
104
|
|
|
if($values['fund_id'] !== 0) |
|
105
|
|
|
{ |
|
106
|
|
|
$fund = $this->fundService->getFund($values['fund_id']); |
|
107
|
|
|
if($fund !== null) |
|
108
|
|
|
{ |
|
109
|
|
|
$saving->fund()->associate($fund); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
$saving->save(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Update a saving. |
|
117
|
|
|
* |
|
118
|
|
|
* @param Member $member |
|
119
|
|
|
* @param Session $session The session |
|
120
|
|
|
* @param array $values |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
|
|
public function updateSaving(Member $member, Session $session, int $savingId, array $values): void |
|
125
|
|
|
{ |
|
126
|
|
|
$saving = $session->savings()->find($savingId); |
|
127
|
|
|
if(!$saving) |
|
128
|
|
|
{ |
|
129
|
|
|
throw new MessageException(trans('meeting.saving.errors.not_found')); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$saving->amount = $values['amount']; |
|
133
|
|
|
$saving->member()->associate($member); |
|
134
|
|
|
if($values['fund_id'] !== 0) |
|
135
|
|
|
{ |
|
136
|
|
|
$fund = $this->fundService->getFund($values['fund_id']); |
|
137
|
|
|
if($fund !== null) |
|
138
|
|
|
{ |
|
139
|
|
|
$saving->fund()->associate($fund); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
else |
|
143
|
|
|
{ |
|
144
|
|
|
$saving->fund()->dissociate(); |
|
145
|
|
|
} |
|
146
|
|
|
$saving->save(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Delete a saving. |
|
151
|
|
|
* |
|
152
|
|
|
* @param Session $session The session |
|
153
|
|
|
* @param int $savingId |
|
154
|
|
|
* |
|
155
|
|
|
* @return void |
|
156
|
|
|
*/ |
|
157
|
|
|
public function deleteSaving(Session $session, int $savingId): void |
|
158
|
|
|
{ |
|
159
|
|
|
$session->savings()->where('id', $savingId)->delete(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get all closings for a given fund. |
|
164
|
|
|
* |
|
165
|
|
|
* @param int $fundId |
|
166
|
|
|
* |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getFundClosings(int $fundId): array |
|
170
|
|
|
{ |
|
171
|
|
|
$closings = $this->tenantService->tontine()->properties['closings'] ?? []; |
|
|
|
|
|
|
172
|
|
|
$closings = Arr::where($closings, fn($closing) => isset($closing[$fundId])); |
|
173
|
|
|
return Arr::map($closings, fn($closing) => $closing[$fundId]); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Save the given session as closing for the fund. |
|
178
|
|
|
* |
|
179
|
|
|
* @param Session $session |
|
180
|
|
|
* @param int $fundId |
|
181
|
|
|
* @param int $profitAmount |
|
182
|
|
|
* |
|
183
|
|
|
* @return void |
|
184
|
|
|
*/ |
|
185
|
|
|
public function saveFundClosing(Session $session, int $fundId, int $profitAmount) |
|
186
|
|
|
{ |
|
187
|
|
|
$tontine = $this->tenantService->tontine(); |
|
188
|
|
|
$properties = $tontine->properties; |
|
|
|
|
|
|
189
|
|
|
$properties['closings'][$session->id][$fundId] = $profitAmount; |
|
190
|
|
|
$tontine->saveProperties($properties); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Check if the given session is closing the fund. |
|
195
|
|
|
* |
|
196
|
|
|
* @param Session $session |
|
197
|
|
|
* @param int $fundId |
|
198
|
|
|
* |
|
199
|
|
|
* @return bool |
|
200
|
|
|
*/ |
|
201
|
|
|
public function hasFundClosing(Session $session, int $fundId): bool |
|
202
|
|
|
{ |
|
203
|
|
|
$properties = $this->tenantService->tontine()->properties; |
|
|
|
|
|
|
204
|
|
|
return isset($properties['closings'][$session->id][$fundId]); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Set the given session as closing the fund. |
|
209
|
|
|
* |
|
210
|
|
|
* @param Session $session |
|
211
|
|
|
* @param int $fundId |
|
212
|
|
|
* |
|
213
|
|
|
* @return void |
|
214
|
|
|
*/ |
|
215
|
|
|
public function deleteFundClosing(Session $session, int $fundId) |
|
216
|
|
|
{ |
|
217
|
|
|
$tontine = $this->tenantService->tontine(); |
|
218
|
|
|
$properties = $tontine->properties; |
|
|
|
|
|
|
219
|
|
|
if(isset($properties['closings'][$session->id][$fundId])) |
|
220
|
|
|
{ |
|
221
|
|
|
unset($properties['closings'][$session->id][$fundId]); |
|
222
|
|
|
if(count($properties['closings'][$session->id]) == 0) |
|
223
|
|
|
{ |
|
224
|
|
|
unset($properties['closings'][$session->id]); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
$tontine->saveProperties($properties); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Get the profit amount saved on a given session. |
|
232
|
|
|
* |
|
233
|
|
|
* @param Session $session |
|
234
|
|
|
* @param int $fundId |
|
235
|
|
|
* |
|
236
|
|
|
* @return int |
|
237
|
|
|
*/ |
|
238
|
|
|
public function getProfitAmount(Session $session, int $fundId): int |
|
239
|
|
|
{ |
|
240
|
|
|
$tontine = $this->tenantService->tontine(); |
|
241
|
|
|
return $tontine->properties['closings'][$session->id][$fundId] ?? 0; |
|
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Get all the fund closings on a given session. |
|
246
|
|
|
* |
|
247
|
|
|
* @param Session $session |
|
248
|
|
|
* |
|
249
|
|
|
* @return array |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getSessionClosings(Session $session): array |
|
252
|
|
|
{ |
|
253
|
|
|
$tontine = $this->tenantService->tontine(); |
|
254
|
|
|
return $tontine->properties['closings'][$session->id] ?? []; |
|
|
|
|
|
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.