1 | <?php |
||
2 | |||
3 | namespace Siak\Tontine\Service\Planning; |
||
4 | |||
5 | use Illuminate\Support\Facades\DB; |
||
6 | use Illuminate\Support\Collection; |
||
7 | use Siak\Tontine\Model\Pool; |
||
8 | use Siak\Tontine\Model\Receivable; |
||
9 | use Siak\Tontine\Model\Round; |
||
10 | use Siak\Tontine\Model\Session; |
||
11 | use Siak\Tontine\Model\Subscription; |
||
12 | |||
13 | use function count; |
||
14 | |||
15 | class PoolSyncService |
||
16 | { |
||
17 | use SessionSyncTrait; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
18 | |||
19 | /** |
||
20 | * @param Round $round |
||
21 | * @param Collection|array $sessions |
||
22 | * |
||
23 | * @return void |
||
24 | */ |
||
25 | public function sessionsCreated(Round $round, Collection|array $sessions): void |
||
26 | { |
||
27 | // Create the receivables. |
||
28 | $receivables = []; |
||
29 | $poolFilter = fn($query, $session) => $query->ofRound($round) |
||
30 | ->whereHas('start', fn($qs) => $qs->where('day_date', '<', $session->day_date)) |
||
31 | ->whereHas('end', fn($qe) => $qe->where('day_date', '>', $session->day_date)); |
||
32 | foreach($sessions as $session) |
||
33 | { |
||
34 | // Take all the subscriptions to a pool this session belongs to. |
||
35 | $subscriptions = Subscription::whereNotNull('id') |
||
36 | ->whereHas('pool', fn($query) => |
||
37 | $poolFilter($query, $session)); |
||
38 | foreach($subscriptions->get() as $subscription) |
||
39 | { |
||
40 | $receivables[] = [ |
||
41 | 'subscription_id' => $subscription->id, |
||
42 | 'session_id' => $session->id, |
||
43 | ]; |
||
44 | } |
||
45 | } |
||
46 | if(count($receivables) > 0) |
||
47 | { |
||
48 | DB::table('receivables')->insert($receivables); |
||
49 | } |
||
50 | |||
51 | // Update the start and end sessions. |
||
52 | $round->pools()->update([ |
||
53 | 'start_sid' => $round->start->id, |
||
0 ignored issues
–
show
|
|||
54 | 'end_sid' => $round->end->id, |
||
0 ignored issues
–
show
|
|||
55 | ]); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param Session $session |
||
60 | * |
||
61 | * @return void |
||
62 | */ |
||
63 | public function sessionDeleted(Session $session) |
||
64 | { |
||
65 | // Detach the payables and delete the receivables. |
||
66 | $session->payables()->update(['session_id' => null]); |
||
67 | $session->receivables()->delete(); |
||
68 | |||
69 | // Delete the disabled sessions rows. |
||
70 | DB::table('pool_session_disabled as psd') |
||
71 | ->where('session_id', $session->id) |
||
72 | ->delete(); |
||
73 | |||
74 | $round = $session->round; |
||
75 | if($round->sessions()->count() === 1) |
||
76 | { |
||
77 | // The last session is being deleted. |
||
78 | $round->pools()->delete(); |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | // Update the start sessions. |
||
83 | $nextSession = $this->getNextSession($round, $session); |
||
84 | if($nextSession !== null) |
||
85 | { |
||
86 | $round->pools() |
||
87 | ->where('start_sid', $session->id) |
||
88 | ->update(['start_sid' => $nextSession->id]); |
||
89 | } |
||
90 | |||
91 | // Update the end sessions. |
||
92 | $prevSession = $this->getPrevSession($round, $session); |
||
93 | if($prevSession !== null) |
||
94 | { |
||
95 | $round->pools() |
||
96 | ->where('end_sid', $session->id) |
||
97 | ->update(['end_sid' => $prevSession->id]); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param Pool $pool |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | public function sessionsChanged(Pool $pool): void |
||
107 | { |
||
108 | // The relations need to be reloaded. |
||
109 | $pool->refresh(); |
||
110 | |||
111 | // Delete the disabled sessions removed from the pool. |
||
112 | DB::table(DB::raw('pool_session_disabled as psd')) |
||
113 | ->where('pool_id', $pool->id) |
||
114 | ->whereExists(fn($query) => $query->select(DB::raw(1)) |
||
115 | ->from(DB::raw('sessions s')) |
||
116 | ->whereColumn('s.id', '=', 'psd.session_id') |
||
117 | ->where(fn($qw) => $qw->where(fn($qs) => $qs |
||
118 | ->orWhere('day_date', '<', $pool->start->day_date) |
||
119 | ->orWhere('day_date', '>', $pool->end->day_date)))) |
||
120 | ->delete(); |
||
121 | |||
122 | // Delete the receivables for the sessions removed from the pool. |
||
123 | Receivable::whereHas('subscription', fn($qs) => |
||
124 | $qs->where('pool_id', $pool->id)) |
||
125 | ->whereHas('session', fn($qw) => |
||
126 | $qw->where(fn($qs) => $qs |
||
127 | ->orWhere('day_date', '<', $pool->start->day_date) |
||
128 | ->orWhere('day_date', '>', $pool->end->day_date))) |
||
129 | ->delete(); |
||
130 | |||
131 | // Create the receivables for the sessions added to the pool. |
||
132 | $receivables = []; |
||
133 | foreach($pool->sessions as $session) |
||
134 | { |
||
135 | $subscriptions = $pool->subscriptions() |
||
136 | ->whereDoesntHave('receivables', fn($qr) => |
||
137 | $qr->where('session_id', $session->id)); |
||
138 | foreach($subscriptions->get() as $subscription) |
||
139 | { |
||
140 | $receivables[] = [ |
||
141 | 'subscription_id' => $subscription->id, |
||
142 | 'session_id' => $session->id, |
||
143 | ]; |
||
144 | } |
||
145 | } |
||
146 | if(count($receivables) > 0) |
||
147 | { |
||
148 | DB::table('receivables')->insert($receivables); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param Pool $pool |
||
154 | * @param Session $session |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | public function sessionEnabled(Pool $pool, Session $session): void |
||
159 | { |
||
160 | // Create the receivables for the pool subscriptions. |
||
161 | $receivables = []; |
||
162 | foreach($pool->subscriptions as $subscription) |
||
163 | { |
||
164 | $receivables[] = [ |
||
165 | 'subscription_id' => $subscription->id, |
||
166 | 'session_id' => $session->id, |
||
167 | ]; |
||
168 | } |
||
169 | if(count($receivables) > 0) |
||
170 | { |
||
171 | DB::table('receivables')->insert($receivables); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param Pool $pool |
||
177 | * @param Session $session |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | public function sessionDisabled(Pool $pool, Session $session): void |
||
182 | { |
||
183 | // Detach the payables. |
||
184 | $session->payables() |
||
185 | ->whereHas('subscription', fn($qs) => |
||
186 | $qs->where('pool_id', $pool->id)) |
||
187 | ->update(['session_id' => null]); |
||
188 | |||
189 | // Delete the receivables |
||
190 | $session->receivables() |
||
191 | ->whereHas('subscription', fn($qs) => |
||
192 | $qs->where('pool_id', $pool->id)) |
||
193 | ->delete(); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param Subscription $subscription |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | public function subscriptionCreated(Subscription $subscription) |
||
202 | { |
||
203 | // Create the payable |
||
204 | $subscription->payable()->create([]); |
||
205 | |||
206 | // Create the receivables for the pool sessions. |
||
207 | $receivables = []; |
||
208 | foreach($subscription->pool->sessions as $session) |
||
209 | { |
||
210 | $receivables[] = [ |
||
211 | 'subscription_id' => $subscription->id, |
||
212 | 'session_id' => $session->id, |
||
213 | ]; |
||
214 | } |
||
215 | if(count($receivables) > 0) |
||
216 | { |
||
217 | DB::table('receivables')->insert($receivables); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param Subscription $subscription |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | public function subscriptionDeleted(Subscription $subscription) |
||
227 | { |
||
228 | // Delete the payable |
||
229 | $subscription->payable()->delete(); |
||
230 | |||
231 | // Delete the receivables |
||
232 | $subscription->receivables()->delete(); |
||
233 | } |
||
234 | } |
||
235 |