Passed
Push — main ( 1074e2...00bd25 )
by Thierry
05:52
created

PartialRefund::updateRefund()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 19
rs 9.9666
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Credit;
4
5
use App\Ajax\CallableSessionClass;
6
use Siak\Tontine\Model\Session as SessionModel;
7
use Siak\Tontine\Service\Meeting\Credit\RefundService;
8
use Siak\Tontine\Validation\Meeting\DebtValidator;
9
10
use function Jaxon\jq;
11
use function Jaxon\pm;
12
use function trans;
13
14
/**
15
 * @databag partial.refund
16
 */
17
class PartialRefund extends CallableSessionClass
18
{
19
    /**
20
     * @var DebtValidator
21
     */
22
    protected DebtValidator $validator;
23
24
    /**
25
     * The constructor
26
     *
27
     * @param RefundService $refundService
28
     */
29
    public function __construct(protected RefundService $refundService)
30
    {}
31
32
    /**
33
     * @exclude
34
     */
35
    public function show(SessionModel $session)
36
    {
37
        $this->session = $session;
38
39
        return $this->home();
40
    }
41
42
    public function home()
43
    {
44
        $html = $this->renderView('pages.meeting.refund.partial.home')
45
            ->with('session', $this->session);
46
        $this->response->html('meeting-partial-refunds', $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

46
        $this->response->html('meeting-partial-refunds', /** @scrutinizer ignore-type */ $html);
Loading history...
47
        $this->jq('#btn-partial-refunds-refresh')->click($this->rq()->home());
48
        $this->jq('#btn-partial-refunds-add')->click($this->rq()->addRefund());
49
50
        return $this->page();
51
    }
52
53
    /**
54
     * @param int $pageNumber
55
     *
56
     * @return mixed
57
     */
58
    public function page(int $pageNumber = 0)
59
    {
60
        $refundCount = $this->refundService->getPartialRefundCount($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...getPartialRefundCount() 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

60
        $refundCount = $this->refundService->getPartialRefundCount(/** @scrutinizer ignore-type */ $this->session);
Loading history...
61
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $refundCount,
62
            'partial.refund', 'principal.page');
63
        $refunds = $this->refundService->getPartialRefunds($this->session, $pageNumber);
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...ce::getPartialRefunds() 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

63
        $refunds = $this->refundService->getPartialRefunds(/** @scrutinizer ignore-type */ $this->session, $pageNumber);
Loading history...
64
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $refundCount);
65
66
        $html = $this->renderView('pages.meeting.refund.partial.page', [
67
            'session' => $this->session,
68
            'refunds' => $refunds,
69
            'pagination' => $pagination,
70
        ]);
71
        $this->response->html('meeting-partial-refunds-page', $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

71
        $this->response->html('meeting-partial-refunds-page', /** @scrutinizer ignore-type */ $html);
Loading history...
72
        $this->response->call('makeTableResponsive', 'meeting-partial-refunds-page');
73
74
        $refundId = jq()->parent()->attr('data-partial-refund-id')->toInt();
75
        $this->jq('.btn-partial-refund-edit')->click($this->rq()->editRefund($refundId));
76
        $this->jq('.btn-partial-refund-delete')->click($this->rq()->deleteRefund($refundId)
77
            ->confirm(trans('meeting.refund.questions.delete')));
78
        $this->jq('.btn-del-partial-refund')->click($this->rq()->deleteRefund($refundId)
79
            ->confirm(trans('meeting.refund.questions.delete')));
80
81
        return $this->response;
82
    }
83
84
    public function addRefund()
85
    {
86
        if($this->session->closed)
87
        {
88
            $this->notify->warning(trans('meeting.warnings.session.closed'));
89
            return $this->response;
90
        }
91
92
        $title = trans('meeting.refund.titles.add');
93
        $content = $this->renderView('pages.meeting.refund.partial.add', [
94
            'debts' => $this->refundService->getUnpaidDebtList($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...ce::getUnpaidDebtList() 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

94
            'debts' => $this->refundService->getUnpaidDebtList(/** @scrutinizer ignore-type */ $this->session),
Loading history...
95
        ]);
96
        $buttons = [[
97
            'title' => trans('common.actions.cancel'),
98
            'class' => 'btn btn-tertiary',
99
            'click' => 'close',
100
        ],[
101
            'title' => trans('common.actions.save'),
102
            'class' => 'btn btn-primary',
103
            'click' => $this->rq()->createRefund(pm()->form('refund-form')),
104
        ]];
105
        $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

105
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
106
107
        return $this->response;
108
    }
109
110
    /**
111
     * @di $validator
112
     * @after showBalanceAmounts
113
     */
114
    public function createRefund(array $formValues)
115
    {
116
        if($this->session->closed)
117
        {
118
            $this->notify->warning(trans('meeting.warnings.session.closed'));
119
            return $this->response;
120
        }
121
122
        $values = $this->validator->validateItem($formValues);
123
        $debt = $this->refundService->getDebt($values['debt']);
124
        if(!$debt)
0 ignored issues
show
introduced by
$debt is of type Siak\Tontine\Model\Debt, thus it always evaluated to true.
Loading history...
125
        {
126
            $this->notify->warning(trans('meeting.loan.errors.not_found'));
127
            return $this->response;
128
        }
129
130
        $this->refundService->createPartialRefund($debt, $this->session, $values['amount']);
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...::createPartialRefund() 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

130
        $this->refundService->createPartialRefund($debt, /** @scrutinizer ignore-type */ $this->session, $values['amount']);
Loading history...
131
132
        $this->dialog->hide();
133
134
        // Refresh the refunds pages
135
        $this->cl(Refund::class)->show($this->session);
136
137
        return $this->page();
138
    }
139
140
    public function editRefund(int $refundId)
141
    {
142
        if($this->session->closed)
143
        {
144
            $this->notify->warning(trans('meeting.warnings.session.closed'));
145
            return $this->response;
146
        }
147
148
        $refund = $this->refundService->getPartialRefund($this->session, $refundId);
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...ice::getPartialRefund() 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

148
        $refund = $this->refundService->getPartialRefund(/** @scrutinizer ignore-type */ $this->session, $refundId);
Loading history...
149
        $title = trans('meeting.refund.titles.edit');
150
        $content = $this->renderView('pages.meeting.refund.partial.edit', [
151
            'refund' => $refund,
152
            'debtLabel' => $this->refundService->getDebtLabel($refund->debt, $this->session),
153
        ]);
154
        $buttons = [[
155
            'title' => trans('common.actions.cancel'),
156
            'class' => 'btn btn-tertiary',
157
            'click' => 'close',
158
        ],[
159
            'title' => trans('common.actions.save'),
160
            'class' => 'btn btn-primary',
161
            'click' => $this->rq()->updateRefund($refundId, pm()->form('refund-form')),
162
        ]];
163
        $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

163
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
164
165
        return $this->response;
166
    }
167
168
    /**
169
     * @di $validator
170
     * @after showBalanceAmounts
171
     */
172
    public function updateRefund(int $refundId, array $formValues)
173
    {
174
        if($this->session->closed)
175
        {
176
            $this->notify->warning(trans('meeting.warnings.session.closed'));
177
            return $this->response;
178
        }
179
180
        $formValues['debt'] = $refundId;
181
        $values = $this->validator->validateItem($formValues);
182
183
        $this->refundService->updatePartialRefund($this->session, $refundId, $values['amount']);
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...::updatePartialRefund() 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

183
        $this->refundService->updatePartialRefund(/** @scrutinizer ignore-type */ $this->session, $refundId, $values['amount']);
Loading history...
184
185
        $this->dialog->hide();
186
187
        // Refresh the refunds pages
188
        $this->cl(Refund::class)->show($this->session);
189
190
        return $this->page();
191
    }
192
193
    /**
194
     * @after showBalanceAmounts
195
     */
196
    public function deleteRefund(int $refundId)
197
    {
198
        if($this->session->closed)
199
        {
200
            $this->notify->warning(trans('meeting.warnings.session.closed'));
201
            return $this->response;
202
        }
203
204
        $this->refundService->deletePartialRefund($this->session, $refundId);
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...::deletePartialRefund() 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

204
        $this->refundService->deletePartialRefund(/** @scrutinizer ignore-type */ $this->session, $refundId);
Loading history...
205
206
        // Refresh the refunds pages
207
        $this->cl(Refund::class)->show($this->session);
208
209
        return $this->page();
210
    }
211
}
212