| Total Complexity | 46 |
| Total Lines | 393 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
Complex classes like RefundService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RefundService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class RefundService |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @param DebtCalculator $debtCalculator |
||
| 29 | * @param TenantService $tenantService |
||
| 30 | * @param LocaleService $localeService |
||
| 31 | * @param SessionService $sessionService |
||
| 32 | * @param FundService $fundService |
||
| 33 | * @param PaymentServiceInterface $paymentService; |
||
| 34 | */ |
||
| 35 | public function __construct(private DebtCalculator $debtCalculator, |
||
| 36 | private TenantService $tenantService, private LocaleService $localeService, |
||
| 37 | private SessionService $sessionService, private FundService $fundService, |
||
| 38 | private PaymentServiceInterface $paymentService) |
||
| 39 | {} |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param Session $session The session |
||
| 43 | * @param Fund $fund |
||
| 44 | * @param bool|null $onlyPaid |
||
| 45 | * |
||
| 46 | * @return Builder|Relation |
||
| 47 | */ |
||
| 48 | private function getDebtsQuery(Session $session, Fund $fund, ?bool $onlyPaid): Builder|Relation |
||
| 49 | { |
||
| 50 | $prevSessions = $this->fundService->getFundSessionIds($session, $fund) |
||
| 51 | ->filter(fn(int $sessionId) => $sessionId !== $session->id); |
||
| 52 | |||
| 53 | return Debt::whereHas('loan', function(Builder $query) use($fund) { |
||
|
|
|||
| 54 | $query->where('fund_id', $fund->id); |
||
| 55 | }) |
||
| 56 | ->when($onlyPaid === false, function(Builder $query) { |
||
| 57 | return $query->whereDoesntHave('refund'); |
||
| 58 | }) |
||
| 59 | ->when($onlyPaid === true, function(Builder $query) { |
||
| 60 | return $query->whereHas('refund'); |
||
| 61 | }) |
||
| 62 | ->where(function(Builder $query) use($session, $prevSessions) { |
||
| 63 | // Take all the debts in the current session |
||
| 64 | $query->where(function(Builder $query) use($session) { |
||
| 65 | $query->whereHas('loan', function(Builder $query) use($session) { |
||
| 66 | $query->where('session_id', $session->id); |
||
| 67 | }); |
||
| 68 | }); |
||
| 69 | if($prevSessions->count() === 0) |
||
| 70 | { |
||
| 71 | return; |
||
| 72 | } |
||
| 73 | // The debts in the previous sessions. |
||
| 74 | $query->orWhere(function(Builder $query) use($session, $prevSessions) { |
||
| 75 | $query->whereHas('loan', function(Builder $query) use($prevSessions) { |
||
| 76 | $query->whereIn('session_id', $prevSessions); |
||
| 77 | }) |
||
| 78 | ->where(function(Builder $query) use($session) { |
||
| 79 | // The debts that are not yet refunded. |
||
| 80 | $query->orWhereDoesntHave('refund'); |
||
| 81 | // The debts that are refunded in the current session. |
||
| 82 | $query->orWhereHas('refund', function(Builder $query) use($session) { |
||
| 83 | $query->where('session_id', $session->id); |
||
| 84 | }); |
||
| 85 | }); |
||
| 86 | }); |
||
| 87 | }); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get the number of debts. |
||
| 92 | * |
||
| 93 | * @param Session $session The session |
||
| 94 | * @param Fund $fund |
||
| 95 | * @param bool|null $onlyPaid |
||
| 96 | * |
||
| 97 | * @return int |
||
| 98 | */ |
||
| 99 | public function getDebtCount(Session $session, Fund $fund, ?bool $onlyPaid): int |
||
| 100 | { |
||
| 101 | return $this->getDebtsQuery($session, $fund, $onlyPaid)->count(); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get the debts. |
||
| 106 | * |
||
| 107 | * @param Session $session The session |
||
| 108 | * @param Fund $fund |
||
| 109 | * @param bool|null $onlyPaid |
||
| 110 | * @param int $page |
||
| 111 | * |
||
| 112 | * @return Collection |
||
| 113 | */ |
||
| 114 | public function getDebts(Session $session, Fund $fund, ?bool $onlyPaid, int $page = 0): Collection |
||
| 115 | { |
||
| 116 | return $this->getDebtsQuery($session, $fund, $onlyPaid) |
||
| 117 | ->page($page, $this->tenantService->getLimit()) |
||
| 118 | ->with(['loan', 'loan.member', 'loan.session', 'refund', 'refund.session', |
||
| 119 | 'partial_refunds', 'partial_refunds.session']) |
||
| 120 | ->get() |
||
| 121 | ->each(function(Debt $debt) use($session) { |
||
| 122 | $debt->isEditable = $debt->refund !== null ? |
||
| 123 | $this->canDeleteRefund($debt, $session) : $this->canCreateRefund($debt, $session); |
||
| 124 | }) |
||
| 125 | ->sortBy('loan.member.name', SORT_LOCALE_STRING) |
||
| 126 | ->values(); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the refunds for a given session. |
||
| 131 | * |
||
| 132 | * @param Session $session The session |
||
| 133 | * @param int $page |
||
| 134 | * |
||
| 135 | * @return Collection |
||
| 136 | */ |
||
| 137 | public function getRefunds(Session $session, int $page = 0): Collection |
||
| 138 | { |
||
| 139 | return $session->refunds() |
||
| 140 | ->page($page, $this->tenantService->getLimit()) |
||
| 141 | ->with('debt.loan.member') |
||
| 142 | ->get(); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param int $debtId |
||
| 147 | * |
||
| 148 | * @return Debt|null |
||
| 149 | */ |
||
| 150 | public function getDebt(int $debtId): ?Debt |
||
| 151 | { |
||
| 152 | return Debt::whereDoesntHave('refund') |
||
| 153 | ->whereHas('loan', function(Builder $query) { |
||
| 154 | $query->whereHas('member', function(Builder $query) { |
||
| 155 | $query->where('tontine_id', $this->tenantService->tontine()->id); |
||
| 156 | }); |
||
| 157 | }) |
||
| 158 | ->find($debtId); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param Debt $debt |
||
| 163 | * @param Session $session |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | private function canCreateRefund(Debt $debt, Session $session): bool |
||
| 168 | { |
||
| 169 | // Already refunded |
||
| 170 | // Cannot refund the principal debt in the same session. |
||
| 171 | if(!$session->opened || $debt->refund !== null || |
||
| 172 | $debt->is_principal && $debt->loan->session->id === $session->id) |
||
| 173 | { |
||
| 174 | return false; |
||
| 175 | } |
||
| 176 | // Cannot refund the interest debt before the principal. |
||
| 177 | if($debt->is_interest && !$debt->loan->fixed_interest) |
||
| 178 | { |
||
| 179 | return $debt->loan->principal_debt->refund !== null; |
||
| 180 | } |
||
| 181 | |||
| 182 | // Cannot be refunded before the last partial refund. |
||
| 183 | $lastRefund = $debt->partial_refunds->sortByDesc('session.start_at')->first(); |
||
| 184 | return !$lastRefund || $lastRefund->session->start_at < $session->start_at; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Create a refund. |
||
| 189 | * |
||
| 190 | * @param Debt $debt |
||
| 191 | * @param Session $session The session |
||
| 192 | * |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | public function createRefund(Debt $debt, Session $session): void |
||
| 196 | { |
||
| 197 | if(!$this->canCreateRefund($debt, $session)) |
||
| 198 | { |
||
| 199 | throw new MessageException(trans('meeting.refund.errors.cannot_refund')); |
||
| 200 | } |
||
| 201 | |||
| 202 | $refund = new Refund(); |
||
| 203 | $refund->debt()->associate($debt); |
||
| 204 | $refund->session()->associate($session); |
||
| 205 | DB::transaction(function() use($debt, $session, $refund) { |
||
| 206 | $refund->save(); |
||
| 207 | // For simple or compound interest, also save the final amount. |
||
| 208 | if($debt->is_interest && !$debt->loan->fixed_interest) |
||
| 209 | { |
||
| 210 | $debt->amount = $this->debtCalculator->getDebtDueAmount($debt, $session, true); |
||
| 211 | $debt->save(); |
||
| 212 | } |
||
| 213 | }); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param Debt $debt |
||
| 218 | * @param Session $session |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | private function canDeleteRefund(Debt $debt, Session $session): bool |
||
| 223 | { |
||
| 224 | // A refund can only be deleted in the same session it was created. |
||
| 225 | if(!$session->opened || !$debt->refund || $debt->refund->session_id !== $session->id) |
||
| 226 | { |
||
| 227 | return false; |
||
| 228 | } |
||
| 229 | |||
| 230 | return true; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Delete a refund. |
||
| 235 | * |
||
| 236 | * @param Debt $debt |
||
| 237 | * @param Session $session The session |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | public function deleteRefund(Debt $debt, Session $session): void |
||
| 242 | { |
||
| 243 | if(!$this->canDeleteRefund($debt, $session)) |
||
| 244 | { |
||
| 245 | throw new MessageException(trans('meeting.refund.errors.cannot_refund')); |
||
| 246 | } |
||
| 247 | if(!$this->paymentService->isEditable($debt->refund)) |
||
| 248 | { |
||
| 249 | throw new MessageException(trans('meeting.refund.errors.cannot_delete')); |
||
| 250 | } |
||
| 251 | $debt->refund->delete(); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get the number of partial refunds. |
||
| 256 | * |
||
| 257 | * @param Session $session The session |
||
| 258 | * |
||
| 259 | * @return int |
||
| 260 | */ |
||
| 261 | public function getPartialRefundCount(Session $session): int |
||
| 262 | { |
||
| 263 | return $session->partial_refunds()->count(); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get the partial refunds. |
||
| 268 | * |
||
| 269 | * @param Session $session The session |
||
| 270 | * @param int $page |
||
| 271 | * |
||
| 272 | * @return Collection |
||
| 273 | */ |
||
| 274 | public function getPartialRefunds(Session $session, int $page = 0): Collection |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the ids of all active funds. |
||
| 290 | * |
||
| 291 | * @return Collection |
||
| 292 | */ |
||
| 293 | public function getActiveFundIds(): Collection |
||
| 294 | { |
||
| 295 | $tontine = $this->tenantService->tontine(); |
||
| 296 | return $tontine->funds()->active() |
||
| 297 | ->pluck('id') |
||
| 298 | ->prepend($tontine->default_fund->id); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param Debt $debt |
||
| 303 | * @param Session $session |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | private function canCreatePartialRefund(Debt $debt, Session $session): bool |
||
| 308 | { |
||
| 309 | // Cannot refund the principal debt in the same session. |
||
| 310 | if(!$session->opened || $debt->refund !== null || |
||
| 311 | ($debt->is_principal && $debt->loan->session->id === $session->id)) |
||
| 312 | { |
||
| 313 | return false; |
||
| 314 | } |
||
| 315 | |||
| 316 | return true; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get debt list for dropdown. |
||
| 321 | * |
||
| 322 | * @param Session $session The session |
||
| 323 | * |
||
| 324 | * @return Collection |
||
| 325 | */ |
||
| 326 | public function getUnpaidDebtList(Session $session): Collection |
||
| 327 | { |
||
| 328 | return $this->fundService->getActiveFunds() |
||
| 329 | ->reduce(function(Collection $debts, Fund $fund) use($session) { |
||
| 330 | $fundDebts = $this->getDebtsQuery($session, $fund, false) |
||
| 331 | ->with(['loan', 'loan.member', 'loan.session', 'refund', 'refund.session']) |
||
| 332 | ->get() |
||
| 333 | ->sortBy('loan.member.name', SORT_LOCALE_STRING) |
||
| 334 | ->values(); |
||
| 335 | return $debts->concat($fundDebts); |
||
| 336 | }, collect()) |
||
| 337 | ->filter(function($debt) use($session) { |
||
| 338 | return $this->canCreatePartialRefund($debt, $session); |
||
| 339 | }) |
||
| 340 | ->keyBy('id') |
||
| 341 | ->map(function($debt) use($session) { |
||
| 342 | $loan = $debt->loan; |
||
| 343 | $fundTitle = $this->fundService->getFundTitle($loan->fund); |
||
| 344 | $unpaidAmount = $this->debtCalculator->getDebtUnpaidAmount($debt, $session); |
||
| 345 | |||
| 346 | return $loan->member->name . " - $fundTitle - " . $loan->session->title . |
||
| 347 | ' - ' . trans('meeting.loan.labels.' . $debt->type) . |
||
| 348 | ' - ' . $this->localeService->formatMoney($unpaidAmount, true); |
||
| 349 | }); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Create a refund. |
||
| 354 | * |
||
| 355 | * @param Debt $debt |
||
| 356 | * @param Session $session The session |
||
| 357 | * @param int $amount |
||
| 358 | * |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | public function createPartialRefund(Debt $debt, Session $session, int $amount): void |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param PartialRefund $refund |
||
| 382 | * @param Session $session |
||
| 383 | * |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | private function canDeletePartialRefund(PartialRefund $refund, Session $session): bool |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Delete a refund. |
||
| 399 | * |
||
| 400 | * @param Session $session The session |
||
| 401 | * @param int $refundId |
||
| 402 | * |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | public function deletePartialRefund(Session $session, int $refundId): void |
||
| 418 | } |
||
| 419 | } |
||
| 420 |