Passed
Pull Request — main (#67)
by Thierry
06:31
created

PoolService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Siak\Tontine\Service\Meeting\Pool;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\DB;
8
use Siak\Tontine\Model\Deposit;
9
use Siak\Tontine\Model\Pool;
10
use Siak\Tontine\Model\Session;
11
use Siak\Tontine\Service\Meeting\Session\SummaryService;
12
use Siak\Tontine\Service\Payment\BalanceCalculator;
13
use Siak\Tontine\Service\LocaleService;
14
use Siak\Tontine\Service\TenantService;
15
16
class PoolService
17
{
18
    /**
19
     * @param LocaleService $localeService
20
     * @param TenantService $tenantService
21
     * @param SummaryService $summaryService
22
     * @param BalanceCalculator $balanceCalculator
23
     */
24
    public function __construct(protected LocaleService $localeService,
25
        protected TenantService $tenantService,
26
        protected SummaryService $summaryService,
27
        protected BalanceCalculator $balanceCalculator)
28
    {}
29
30
    /**
31
     * @param Session $session
32
     * @param int $poolId    The pool id
33
     *
34
     * @return Pool|null
35
     */
36
    public function getPool(Session $session, int $poolId): ?Pool
37
    {
38
        return $session->pools()
39
            ->withCount(['sessions', 'disabled_sessions'])
40
            ->find($poolId);
41
    }
42
43
    /**
44
     * Get a list of pools with receivables.
45
     *
46
     * @param Session $session
47
     *
48
     * @return Collection
49
     */
50
    public function getPoolsWithReceivables(Session $session): Collection
51
    {
52
        return $session->pools()
53
            ->addSelect([
54
                'recv_amount' => Deposit::select(DB::raw('sum(amount)'))
55
                    ->whereColumn('pool_id', 'pools.id')
56
                    ->whereHas('receivable', fn(Builder $qr) =>
57
                        $qr->whereSession($session)),
58
                'extra_amount' => Deposit::select(DB::raw('sum(amount)'))
59
                    ->whereColumn('pool_id', 'pools.id')
60
                    ->whereSession($session)
61
                    ->whereHas('receivable', fn(Builder $qr) =>
62
                        $qr->where('session_id', '!=', $session->id)),
63
            ])
64
            ->withCount([
65
                'receivables as recv_count' => fn(Builder $query) =>
66
                    $query->whereSession($session),
67
                'receivables as paid_count' => fn(Builder $query) =>
68
                    $query->whereSession($session)->paid(),
69
                'receivables as paid_here' => fn(Builder $query) =>
70
                    $query->whereSession($session)->paidHere($session),
71
                'receivables as paid_early' => fn(Builder $query) =>
72
                    $query->whereSession($session)->paidEarlier($session),
73
                'receivables as paid_late' => fn(Builder $query) =>
74
                    $query->whereSession($session)->paidLater($session),
75
                'receivables as next_early' => fn(Builder $query) =>
76
                    $query->succeedes($session)->paidHere($session),
77
                'receivables as prev_late' => fn(Builder $query) =>
78
                    $query->precedes($session)->paidHere($session),
79
            ])
80
            ->get()
81
            ->each(fn(Pool $pool) => $pool->recv_amount ??= 0);
0 ignored issues
show
Bug introduced by
The property recv_amount does not exist on Siak\Tontine\Model\Pool. Did you mean amount?
Loading history...
82
    }
83
84
    /**
85
     * Get a list of pools with payables.
86
     *
87
     * @param Session $session
88
     *
89
     * @return Collection
90
     */
91
    public function getPoolsWithPayables(Session $session): Collection
92
    {
93
        return $session->pools()
94
            ->withCount([
95
                'sessions',
96
                'disabled_sessions',
97
                'payables as pay_paid' => fn(Builder $query) =>
98
                    $query->whereSession($session)->whereHas('remitment'),
99
            ])
100
            ->get()
101
            ->each(function(Pool $pool) use($session) {
102
                // Expected
103
                $pool->pay_count = $this->summaryService->getSessionRemitmentCount($pool, $session);
0 ignored issues
show
Bug introduced by
The property pay_count does not seem to exist on Siak\Tontine\Model\Pool. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
104
                // Amount paid
105
                $pool->amount_paid = $this->balanceCalculator->getPoolRemitmentAmount($pool, $session);
0 ignored issues
show
Bug introduced by
The property amount_paid does not exist on Siak\Tontine\Model\Pool. Did you mean amount?
Loading history...
106
            });
107
    }
108
109
    /**
110
     * Check if the current round has at least one pool with auctions.
111
     *
112
     * @param Session $session
113
     *
114
     * @return bool
115
     */
116
    public function hasPoolWithAuction(Session $session): bool
117
    {
118
        return $session->pools->contains(fn($pool) => $pool->remit_auction);
119
    }
120
121
    /**
122
     * @param Session $session
123
     * @param int $poolId    The pool id
124
     *
125
     * @return Pool|null
126
     */
127
    public function getRoundPool(Session $session, int $poolId): ?Pool
128
    {
129
        return $session->round->pools()
130
            ->withCount(['sessions', 'disabled_sessions'])
131
            ->find($poolId);
132
    }
133
134
    /**
135
     * Get a list of pools with late deposits.
136
     *
137
     * @param Session $session
138
     *
139
     * @return Collection
140
     */
141
    public function getPoolsWithLateDeposits(Session $session): Collection
142
    {
143
        // All the round pools are returned here.
144
        return $session->round->pools()
145
            ->addSelect([
146
                'amount_recv' => Deposit::select(DB::raw('sum(amount)'))
147
                    ->whereColumn('pool_id', 'pools.id')
148
                    ->whereSession($session)
149
                    ->whereHas('receivable', fn(Builder $rq) => $rq->precedes($session)),
150
            ])
151
            ->withCount([
152
                'receivables as late_count' => fn(Builder $query) =>
153
                    $query->late($session),
154
                'receivables as late_paid' => fn(Builder $query) =>
155
                    $query->precedes($session)->paidHere($session),
156
            ])
157
            ->get()
158
            ->each(fn(Pool $pool) => $pool->amount_recv ??= 0);
0 ignored issues
show
Bug introduced by
The property amount_recv does not exist on Siak\Tontine\Model\Pool. Did you mean amount?
Loading history...
159
    }
160
161
    /**
162
     * Get a list of pools with early deposits.
163
     *
164
     * @param Session $session
165
     *
166
     * @return Collection
167
     */
168
    public function getPoolsWithEarlyDeposits(Session $session): Collection
169
    {
170
        // All the round pools are returned here.
171
        return $session->round->pools()
172
            ->addSelect([
173
                'amount_recv' => Deposit::select(DB::raw('sum(amount)'))
174
                    ->whereColumn('pool_id', 'pools.id')
175
                    ->whereSession($session)
176
                    ->whereHas('receivable', fn(Builder $rq) => $rq->succeedes($session)),
177
            ])
178
            ->withCount([
179
                'receivables as early_count' => fn(Builder $query) =>
180
                    $query->succeedes($session)->paidHere($session),
181
            ])
182
            ->get()
183
            ->each(fn(Pool $pool) => $pool->amount_recv ??= 0);
0 ignored issues
show
Bug introduced by
The property amount_recv does not exist on Siak\Tontine\Model\Pool. Did you mean amount?
Loading history...
184
    }
185
186
    /**
187
     * @param Session $session
188
     * @param  bool $strictly
189
     *
190
     * @return Collection
191
     */
192
    public function getNextSessions(Session $session, bool $strictly = true): Collection
193
    {
194
        return $session->round->sessions()
195
            ->opened()
196
            ->succeedes($session, $strictly)
197
            ->orderBy('day_date', 'asc')
198
            ->pluck('title', 'id');
199
    }
200
201
    /**
202
     * @param Session $session
203
     * @param int $nextSessionId
204
     *
205
     * @return Session|null
206
     */
207
    public function getNextSession(Session $session, int $nextSessionId): ?Session
208
    {
209
        return $session->round->sessions()->opened()
210
            ->where('day_date', '>', $session->day_date)
211
            ->find($nextSessionId);
212
    }
213
}
214