1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Planning; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Siak\Tontine\Model\Fund; |
7
|
|
|
use Siak\Tontine\Model\Pool; |
8
|
|
|
use Siak\Tontine\Model\Round; |
9
|
|
|
use Siak\Tontine\Model\Session; |
10
|
|
|
|
11
|
|
|
class FundSyncService |
12
|
|
|
{ |
13
|
|
|
use SessionSyncTrait; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Round $round |
17
|
|
|
* @param Pool $pool |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function savePoolFund(Round $round, Pool $pool): void |
22
|
|
|
{ |
23
|
|
|
Fund::updateOrCreate([ |
24
|
|
|
'pool_id' => $pool->id, |
25
|
|
|
], [ |
26
|
|
|
'def_id' => $round->guild->default_fund->id, |
27
|
|
|
'round_id' => $round->id, |
28
|
|
|
'start_sid' => $round->start->id, |
|
|
|
|
29
|
|
|
'end_sid' => $round->end->id, |
|
|
|
|
30
|
|
|
'interest_sid' => $round->end->id, |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Round $round |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
private function updateDefaultFund(Round $round): void |
40
|
|
|
{ |
41
|
|
|
$fundDef = $round->guild->default_fund; |
42
|
|
|
if(!$round->add_default_fund) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$fundDef->funds()->real()->where('round_id', $round->id)->delete(); |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
Fund::updateOrCreate([ |
49
|
|
|
'def_id' => $fundDef->id, |
50
|
|
|
'round_id' => $round->id, |
51
|
|
|
], [ |
52
|
|
|
'start_sid' => $round->start->id, |
|
|
|
|
53
|
|
|
'end_sid' => $round->end->id, |
|
|
|
|
54
|
|
|
'interest_sid' => $round->end->id, |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Round $round |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
private function updateFunds(Round $round): void |
64
|
|
|
{ |
65
|
|
|
if(!$round->start || !$round->end) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Create the fund that will be used to lend the money in the pools. |
71
|
|
|
Pool::ofRound($round) |
72
|
|
|
->whereHas('def', fn($q) => $q->depositLendable()) |
73
|
|
|
->get() |
74
|
|
|
->each(fn($pool) => $this->savePoolFund($round, $pool)); |
75
|
|
|
|
76
|
|
|
// Create the default savings fund. |
77
|
|
|
$this->updateDefaultFund($round); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Round $round |
82
|
|
|
* @param Collection|array $sessions |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function sessionCreated(Round $round, Collection|array $sessions): void |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$this->updateFunds($round); |
89
|
|
|
|
90
|
|
|
// Update the start and end sessions. |
91
|
|
|
$round->funds()->update([ |
92
|
|
|
'start_sid' => $round->start->id, |
|
|
|
|
93
|
|
|
'end_sid' => $round->end->id, |
|
|
|
|
94
|
|
|
'interest_sid' => $round->end->id, |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Round $round |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function sessionUpdated(Round $round): void |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
// Not necessary |
106
|
|
|
// $this->updateFunds($round); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Session $session |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function sessionDeleted(Session $session): void |
115
|
|
|
{ |
116
|
|
|
$round = $session->round; |
117
|
|
|
if($round->sessions()->count() === 1) |
118
|
|
|
{ |
119
|
|
|
// The last session is being deleted. |
120
|
|
|
$round->funds()->delete(); |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Update the start sessions. |
125
|
|
|
$nextSession = $this->getNextSession($round, $session); |
126
|
|
|
if($nextSession !== null) |
127
|
|
|
{ |
128
|
|
|
$round->funds() |
129
|
|
|
->where('start_sid', $session->id) |
130
|
|
|
->update(['start_sid' => $nextSession->id]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Update the end sessions. |
134
|
|
|
$prevSession = $this->getPrevSession($round, $session); |
135
|
|
|
if($prevSession !== null) |
136
|
|
|
{ |
137
|
|
|
$round->funds() |
138
|
|
|
->where('end_sid', $session->id) |
139
|
|
|
->update(['end_sid' => $prevSession->id]); |
140
|
|
|
|
141
|
|
|
$round->funds() |
142
|
|
|
->where('interest_sid', $session->id) |
143
|
|
|
->update(['interest_sid' => $prevSession->id]); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param Round $round |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
public function roundDeleted(Round $round): void |
153
|
|
|
{ |
154
|
|
|
// Delete the funds (default and for pools) that was automatically created. |
155
|
|
|
$round->guild->default_fund |
156
|
|
|
->funds() |
157
|
|
|
->where('round_id', $round->id) |
158
|
|
|
->delete(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|