Passed
Push — main ( 1848b7...f24d21 )
by Thierry
06:50
created

Loan::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Session\Credit;
4
5
use App\Ajax\OpenedSessionCallable;
6
use App\Ajax\Web\Meeting\Session\Misc;
7
use Siak\Tontine\Model\Session as SessionModel;
8
use Siak\Tontine\Service\Meeting\Credit\LoanService;
9
use Siak\Tontine\Service\Tontine\FundService;
10
use Siak\Tontine\Service\Tontine\MemberService;
11
use Siak\Tontine\Validation\Meeting\LoanValidator;
12
13
use function Jaxon\jq;
14
use function Jaxon\pm;
15
use function trans;
16
17
class Loan extends OpenedSessionCallable
18
{
19
    /**
20
     * @var LoanValidator
21
     */
22
    protected LoanValidator $validator;
23
24
    /**
25
     * The constructor
26
     *
27
     * @param LoanService $loanService
28
     * @param FundService $fundService
29
     * @param MemberService $memberService
30
     */
31
    public function __construct(protected LoanService $loanService,
32
        protected FundService $fundService, protected MemberService $memberService)
33
    {}
34
35
    /**
36
     * @exclude
37
     */
38
    public function show(SessionModel $session)
39
    {
40
        $this->session = $session;
41
42
        return $this->home();
43
    }
44
45
    public function home()
46
    {
47
        $loans = $this->loanService->getSessionLoans($this->session);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...vice::getSessionLoans() does only seem to accept Siak\Tontine\Model\Session, 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

47
        $loans = $this->loanService->getSessionLoans(/** @scrutinizer ignore-type */ $this->session);
Loading history...
48
49
        $html = $this->renderView('pages.meeting.loan.home', [
50
            'session' => $this->session,
51
            'loans' => $loans,
52
            'defaultFund' => $this->fundService->getDefaultFund(),
53
        ]);
54
        $this->response->html('meeting-loans', $html);
0 ignored issues
show
Bug introduced by
It seems like $html can also be of type null; however, parameter $sData of Jaxon\Response\Response::html() does only seem to accept string, 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

54
        $this->response->html('meeting-loans', /** @scrutinizer ignore-type */ $html);
Loading history...
55
        $this->response->call('makeTableResponsive', 'meeting-loans');
56
57
        $this->response->call('showBalanceAmountsWithDelay');
58
59
        $this->jq('#btn-loans-refresh')->click($this->rq()->home());
60
        $this->jq('#btn-loan-add')->click($this->rq()->addLoan());
61
        $this->jq('#btn-loan-balances')
62
            ->click($this->rq(Misc::class)->showBalanceDetails(true));
63
        $loanId = jq()->parent()->attr('data-loan-id')->toInt();
64
        $this->jq('.btn-loan-edit')->click($this->rq()->editLoan($loanId));
65
        $this->jq('.btn-loan-delete')->click($this->rq()->deleteLoan($loanId)
66
            ->confirm(trans('meeting.loan.questions.delete')));
67
68
        return $this->response;
69
    }
70
71
    public function addLoan()
72
    {
73
        $title = trans('meeting.loan.titles.add');
74
        $content = $this->renderView('pages.meeting.loan.add', [
75
            'amountAvailable' => $this->loanService->getAmountAvailableValue($this->session),
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...tAmountAvailableValue() does only seem to accept Siak\Tontine\Model\Session, 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

75
            'amountAvailable' => $this->loanService->getAmountAvailableValue(/** @scrutinizer ignore-type */ $this->session),
Loading history...
76
            'interestTypes' => $this->loanService->getInterestTypes(),
77
            'funds' => $this->fundService->getFundList(),
78
            'members' => $this->memberService->getMemberList(),
79
        ]);
80
        $buttons = [[
81
            'title' => trans('common.actions.cancel'),
82
            'class' => 'btn btn-tertiary',
83
            'click' => 'close',
84
        ],[
85
            'title' => trans('common.actions.save'),
86
            'class' => 'btn btn-primary',
87
            'click' => $this->rq()->createLoan(pm()->form('loan-form')),
88
        ]];
89
        $this->dialog->show($title, $content, $buttons);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $sContent of Jaxon\App\Dialog\ModalInterface::show() does only seem to accept string, 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

89
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
90
        $this->response->script('setLoanInterestLabel()');
91
92
        return $this->response;
93
    }
94
95
    /**
96
     * @di $validator
97
     * @after showBalanceAmounts
98
     */
99
    public function createLoan(array $formValues)
100
    {
101
        $values = $this->validator->validateItem($formValues);
102
        if(!($member = $this->memberService->getMember($values['member'])))
103
        {
104
            $this->notify->warning(trans('tontine.member.errors.not_found'));
105
            return $this->response;
106
        }
107
        if(!($fund = $this->fundService->getFund($values['fund'], true, true)))
108
        {
109
            $this->notify->warning(trans('tontine.fund.errors.not_found'));
110
            return $this->response;
111
        }
112
113
        $this->loanService->createLoan($this->session, $member, $fund, $values);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...anService::createLoan() does only seem to accept Siak\Tontine\Model\Session, 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

113
        $this->loanService->createLoan(/** @scrutinizer ignore-type */ $this->session, $member, $fund, $values);
Loading history...
114
115
        $this->dialog->hide();
116
117
        return $this->home();
118
    }
119
120
    public function editLoan(int $loanId)
121
    {
122
        $loan = $this->loanService->getSessionLoan($this->session, $loanId);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...rvice::getSessionLoan() does only seem to accept Siak\Tontine\Model\Session, 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

122
        $loan = $this->loanService->getSessionLoan(/** @scrutinizer ignore-type */ $this->session, $loanId);
Loading history...
123
        if(!$loan)
124
        {
125
            $this->notify->warning(trans('meeting.loan.errors.not_found'));
126
            return $this->response;
127
        }
128
        // A refunded loan cannot be updated.
129
        if($loan->refunds_count > 0)
130
        {
131
            $this->notify->warning(trans('meeting.loan.errors.update'));
132
            return $this->response;
133
        }
134
135
        $title = trans('meeting.loan.titles.edit');
136
        $content = $this->renderView('pages.meeting.loan.edit', [
137
            'loan' => $loan,
138
            'interestTypes' => $this->loanService->getInterestTypes(),
139
            'funds' => $this->fundService->getFundList(),
140
            'members' => $this->memberService->getMemberList(),
141
        ]);
142
        $buttons = [[
143
            'title' => trans('common.actions.cancel'),
144
            'class' => 'btn btn-tertiary',
145
            'click' => 'close',
146
        ],[
147
            'title' => trans('common.actions.save'),
148
            'class' => 'btn btn-primary',
149
            'click' => $this->rq()->updateLoan($loanId, pm()->form('loan-form')),
150
        ]];
151
        $this->dialog->show($title, $content, $buttons);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $sContent of Jaxon\App\Dialog\ModalInterface::show() does only seem to accept string, 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

151
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
152
        $this->response->script('setLoanInterestLabel()');
153
154
        return $this->response;
155
    }
156
157
    /**
158
     * @di $validator
159
     * @after showBalanceAmounts
160
     */
161
    public function updateLoan(int $loanId, array $formValues)
162
    {
163
        $loan = $this->loanService->getSessionLoan($this->session, $loanId);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...rvice::getSessionLoan() does only seem to accept Siak\Tontine\Model\Session, 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

163
        $loan = $this->loanService->getSessionLoan(/** @scrutinizer ignore-type */ $this->session, $loanId);
Loading history...
164
        if(!$loan)
165
        {
166
            $this->notify->warning(trans('meeting.loan.errors.not_found'));
167
            return $this->response;
168
        }
169
        // A refunded loan cannot be updated.
170
        if($loan->refunds_count > 0)
171
        {
172
            $this->notify->warning(trans('meeting.loan.errors.update'));
173
            return $this->response;
174
        }
175
176
        $values = $this->validator->validateItem($formValues);
177
        if(!($member = $this->memberService->getMember($values['member'])))
178
        {
179
            $this->notify->warning(trans('tontine.member.errors.not_found'));
180
            return $this->response;
181
        }
182
        if(!($fund = $this->fundService->getFund($values['fund'], true, true)))
183
        {
184
            $this->notify->warning(trans('tontine.fund.errors.not_found'));
185
            return $this->response;
186
        }
187
188
        $this->loanService->updateLoan($member, $fund, $loan, $values);
189
190
        $this->dialog->hide();
191
192
        return $this->home();
193
    }
194
195
    /**
196
     * @after showBalanceAmounts
197
     */
198
    public function deleteLoan(int $loanId)
199
    {
200
        $this->loanService->deleteLoan($this->session, $loanId);
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...anService::deleteLoan() does only seem to accept Siak\Tontine\Model\Session, 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
        $this->loanService->deleteLoan(/** @scrutinizer ignore-type */ $this->session, $loanId);
Loading history...
201
202
        return $this->home();
203
    }
204
}
205