Passed
Push — main ( 0be37e...fc9941 )
by Thierry
06:01
created

ReportService::getSessionProfitAmounts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Service\Report;
4
5
use Siak\Tontine\Model\Loan;
6
use Siak\Tontine\Model\Round;
7
use Siak\Tontine\Model\Session;
8
use Siak\Tontine\Service\LocaleService;
9
use Siak\Tontine\Service\TenantService;
10
use Siak\Tontine\Service\Meeting\Credit\DebtCalculator;
11
use Siak\Tontine\Service\Meeting\FundService;
12
use Siak\Tontine\Service\Meeting\Saving\ProfitService;
13
use Siak\Tontine\Service\Meeting\SummaryService;
14
use Siak\Tontine\Service\Report\RoundService;
15
16
use function compact;
17
18
class ReportService
19
{
20
    /**
21
     * @param LocaleService $localeService
22
     * @param TenantService $tenantService
23
     * @param SessionService $sessionService
24
     * @param MemberService $memberService
25
     * @param RoundService $roundService
26
     * @param FundService $fundService
27
     * @param SummaryService $summaryService
28
     * @param ProfitService $profitService
29
     * @param DebtCalculator $debtCalculator
30
     */
31
    public function __construct(protected LocaleService $localeService,
32
        protected TenantService $tenantService, protected SessionService $sessionService,
33
        protected MemberService $memberService, protected RoundService $roundService,
34
        protected FundService $fundService, protected SummaryService $summaryService,
35
        protected ProfitService $profitService, protected DebtCalculator $debtCalculator)
36
    {}
37
38
    /**
39
     * @param Session $session
40
     *
41
     * @return array
42
     */
43
    public function getSessionReport(Session $session): array
44
    {
45
        $guild = $this->tenantService->guild();
46
        [$country] = $this->localeService->getNameFromGuild($guild);
0 ignored issues
show
Bug introduced by
It seems like $guild can also be of type null; however, parameter $guild of Siak\Tontine\Service\Loc...ice::getNameFromGuild() does only seem to accept Siak\Tontine\Model\Guild, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        [$country] = $this->localeService->getNameFromGuild(/** @scrutinizer ignore-type */ $guild);
Loading history...
47
48
        return [
49
            'guild' => $guild,
50
            'session' => $session,
51
            'country' => $country,
52
            'deposits' => [
53
                'session' => $session,
54
                'receivables' => $this->memberService->getReceivables($session),
55
                'pools' => $this->sessionService->getReceivables($session),
56
            ],
57
            'remitments' => [
58
                'session' => $session,
59
                'payables' => $this->memberService->getPayables($session),
60
                'pools' => $this->sessionService->getPayables($session),
61
                'auctions' => $this->memberService->getAuctions($session),
62
            ],
63
            'bills' => [
64
                'bills' => $this->memberService->getBills($session),
65
                'charges' => [
66
                    'session' => $this->sessionService->getSessionCharges($session),
67
                    'total' => $this->sessionService->getTotalCharges($session),
68
                ],
69
            ],
70
            'loans' => [
71
                'loans' => $this->memberService->getLoans($session),
72
                'total' => $this->sessionService->getLoan($session),
73
            ],
74
            'refunds' => [
75
                'refunds' => $this->memberService->getRefunds($session),
76
                'total' => $this->sessionService->getRefund($session),
77
            ],
78
            'savings' => [
79
                'savings' => $this->memberService->getSavings($session),
80
                'funds' => $this->fundService->getSessionFundList($session),
81
                'total' => $this->sessionService->getSaving($session),
82
            ],
83
            'outflows' => [
84
                'outflows' => $this->memberService->getOutflows($session),
85
                'total' => $this->sessionService->getOutflow($session),
86
            ],
87
        ];
88
    }
89
90
    /**
91
     * @param Session $session
92
     *
93
     * @return array
94
     */
95
    public function getSessionEntry(Session $session): array
96
    {
97
        $guild = $this->tenantService->guild();
98
        [$country] = $this->localeService->getNameFromGuild($guild);
0 ignored issues
show
Bug introduced by
It seems like $guild can also be of type null; however, parameter $guild of Siak\Tontine\Service\Loc...ice::getNameFromGuild() does only seem to accept Siak\Tontine\Model\Guild, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        [$country] = $this->localeService->getNameFromGuild(/** @scrutinizer ignore-type */ $guild);
Loading history...
99
100
        return [
101
            'guild' => $guild,
102
            'session' => $session,
103
            'country' => $country,
104
            'deposits' => [
105
                'session' => $session,
106
                'receivables' => $this->memberService->getReceivables($session),
107
                'pools' => $this->sessionService->getReceivables($session),
108
            ],
109
            'remitments' => [
110
                'session' => $session,
111
                'payables' => $this->memberService->getPayables($session),
112
                'pools' => $this->sessionService->getPayables($session),
113
            ],
114
            'bills' => [
115
                'bills' => $this->memberService->getBills($session),
116
                'charges' => $this->tenantService->guild()->charges()
117
                    ->active()->fixed()->get(),
118
            ],
119
        ];
120
    }
121
122
    /**
123
     * @param Session $session
124
     *
125
     * @return array
126
     */
127
    public function getSavingsReport(Session $session): array
128
    {
129
        $guild = $this->tenantService->guild();
130
        [$country] = $this->localeService->getNameFromGuild($guild);
0 ignored issues
show
Bug introduced by
It seems like $guild can also be of type null; however, parameter $guild of Siak\Tontine\Service\Loc...ice::getNameFromGuild() does only seem to accept Siak\Tontine\Model\Guild, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
        [$country] = $this->localeService->getNameFromGuild(/** @scrutinizer ignore-type */ $guild);
Loading history...
131
        $funds = $this->fundService->getSessionFunds($session);
132
        $profits = $session->funds->keyBy('id')->map(fn($fund) => $fund->profit_amount);
133
134
        $funds = $funds
135
            ->map(fn($fund) => [
136
                'fund' => $fund,
137
                'distribution' => $this->profitService->getDistribution($session,
138
                    $fund, $profits[$fund->id] ?? 0),
139
            ])
140
            ->filter(fn($report) => $report['distribution']->savings->count() > 0);
141
142
        return compact('guild', 'session', 'country', 'funds');
143
    }
144
145
    /**
146
     * @param Loan $loan
147
     * @param Session $session
148
     *
149
     * @return void
150
     */
151
    private function setDebtAmount(Loan $loan, Session $session)
152
    {
153
        if(($debt = $loan->i_debt) !== null)
0 ignored issues
show
Bug introduced by
The property i_debt does not seem to exist on Siak\Tontine\Model\Loan. 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...
154
        {
155
            $loan->iDebtAmount = $this->debtCalculator->getDebtAmount($debt, $session);
0 ignored issues
show
Bug introduced by
The property iDebtAmount does not seem to exist on Siak\Tontine\Model\Loan. 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...
156
        }
157
    }
158
159
    /**
160
     * @param Session $session
161
     *
162
     * @return array
163
     */
164
    public function getCreditReport(Session $session): array
165
    {
166
        $round = $session->round;
167
        $guild = $this->tenantService->guild();
168
        [$country, $currency] = $this->localeService->getNameFromGuild($guild);
0 ignored issues
show
Bug introduced by
It seems like $guild can also be of type null; however, parameter $guild of Siak\Tontine\Service\Loc...ice::getNameFromGuild() does only seem to accept Siak\Tontine\Model\Guild, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
        [$country, $currency] = $this->localeService->getNameFromGuild(/** @scrutinizer ignore-type */ $guild);
Loading history...
169
170
        $funds = $this->fundService->getSessionFunds($session)
171
            ->each(function($fund) use($session) {
172
                $sessionIds = $this->fundService->getFundSessionIds($fund, $session);
173
                $fund->loans = Loan::select('loans.*')
174
                    ->join('sessions', 'loans.session_id', '=', 'sessions.id')
175
                    ->where(fn($query) => $query->where('fund_id', $fund->id))
176
                    ->whereIn('loans.session_id', $sessionIds)
177
                    ->with(['member', 'session', 'debts.refund', 'debts.refund.session',
178
                        'debts.partial_refunds' => function($query) use($sessionIds) {
179
                            $query->whereIn('session_id', $sessionIds);
180
                        },
181
                        'debts.partial_refunds.session'])
182
                    ->orderBy('sessions.day_date')
0 ignored issues
show
Bug introduced by
'sessions.day_date' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderBy(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

182
                    ->orderBy(/** @scrutinizer ignore-type */ 'sessions.day_date')
Loading history...
183
                    ->get()
184
                    ->each(fn(Loan $loan) => $this->setDebtAmount($loan, $session))
185
                    ->groupBy('member.id');
186
            })
187
            ->filter(fn($fund) => $fund->loans->count() > 0);
188
189
        return compact('guild', 'round', 'session', 'country', 'currency', 'funds');
190
    }
191
192
    /**
193
     * @param Round $round
194
     *
195
     * @return array
196
     */
197
    public function getRoundReport(Round $round): array
198
    {
199
        $guild = $this->tenantService->guild();
200
        [$country, $currency] = $this->localeService->getNameFromGuild($guild);
0 ignored issues
show
Bug introduced by
It seems like $guild can also be of type null; however, parameter $guild of Siak\Tontine\Service\Loc...ice::getNameFromGuild() does only seem to accept Siak\Tontine\Model\Guild, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

200
        [$country, $currency] = $this->localeService->getNameFromGuild(/** @scrutinizer ignore-type */ $guild);
Loading history...
201
202
        $figures = $this->summaryService->getFigures($round);
203
      
204
        $sessions = $this->roundService->getRoundSessions($round);
205
        $sessionIds = $sessions->pluck('id');
206
        $balance = [
207
            'sessions' => $sessions,
208
            'settlements' => $this->roundService->getSettlementAmounts($sessionIds),
209
            'loans' => $this->roundService->getLoanAmounts($sessionIds),
210
            'refunds' => $this->roundService->getRefundAmounts($sessionIds),
211
            'savings' => $this->roundService->getSavingAmounts($sessionIds),
212
            'outflows' => $this->roundService->getOutflowAmounts($sessionIds),
213
            'pools' => $this->summaryService->getPoolsBalance($figures),
214
        ];
215
216
        return compact('guild', 'round', 'country', 'currency', 'figures', 'balance');
217
    }
218
}
219