Passed
Push — main ( e5c1fd...5eb91d )
by Thierry
06:13
created

Saving::page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 1
dl 0
loc 22
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Saving;
4
5
use App\Ajax\CallableSessionClass;
6
use Siak\Tontine\Model\Session as SessionModel;
7
use Siak\Tontine\Service\Meeting\Saving\SavingService;
8
use Siak\Tontine\Service\Tontine\FundService;
9
use Siak\Tontine\Service\Tontine\MemberService;
10
use Siak\Tontine\Validation\Meeting\SavingValidator;
11
12
use function Jaxon\jq;
13
use function Jaxon\pm;
14
use function trans;
15
16
/**
17
 * @databag meeting.saving
18
 */
19
class Saving extends CallableSessionClass
20
{
21
    /**
22
     * @var SavingValidator
23
     */
24
    protected SavingValidator $validator;
25
26
    /**
27
     * The constructor
28
     *
29
     * @param SavingService $savingService
30
     * @param FundService $fundService
31
     * @param MemberService $memberService
32
     */
33
    public function __construct(protected SavingService $savingService,
34
        protected FundService $fundService, protected MemberService $memberService)
35
    {}
36
37
    /**
38
     * @exclude
39
     */
40
    public function show(SessionModel $session)
41
    {
42
        $this->session = $session;
43
44
        return $this->home();
45
    }
46
47
    public function home()
48
    {
49
        $html = $this->render('pages.meeting.saving.home', [
50
            'session' => $this->session,
51
            'fundId' => (int)$this->bag('meeting.saving')->get('fund.id', -1),
52
            'funds' => $this->fundService->getFundList()->prepend('', -1),
53
        ]);
54
        $this->response->html('meeting-savings', $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-savings', /** @scrutinizer ignore-type */ $html);
Loading history...
55
56
        $this->jq('#btn-savings-refresh')->click($this->rq()->home());
57
        $fundId = pm()->select('savings-fund-id')->toInt();
58
        $this->jq('#btn-saving-add')->click($this->rq()->addSaving($fundId));
59
        $this->jq('#btn-savings-fund')->click($this->rq()->filter($fundId));
60
61
        return $this->page();
62
    }
63
64
    public function page(int $pageNumber = 0)
65
    {
66
        $fundId = (int)$this->bag('meeting.saving')->get('fund.id', -1);
67
        $savingCount = $this->savingService->getSavingCount($this->session, $fundId);
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::getSavingCount() 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

67
        $savingCount = $this->savingService->getSavingCount(/** @scrutinizer ignore-type */ $this->session, $fundId);
Loading history...
68
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $savingCount,
69
            'meeting.saving', 'page');
70
        $savings = $this->savingService->getSavings($this->session, $fundId, $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...ngService::getSavings() 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

70
        $savings = $this->savingService->getSavings(/** @scrutinizer ignore-type */ $this->session, $fundId, $pageNumber);
Loading history...
71
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $savingCount);
72
73
        $html = $this->render('pages.meeting.saving.page', [
74
            'session' => $this->session,
75
            'savings' => $savings,
76
            'pagination' => $pagination,
77
        ]);
78
        $this->response->html('meeting-savings-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

78
        $this->response->html('meeting-savings-page', /** @scrutinizer ignore-type */ $html);
Loading history...
79
80
        $savingId = jq()->parent()->attr('data-saving-id')->toInt();
81
        $this->jq('.btn-saving-edit')->click($this->rq()->editSaving($savingId));
82
        $this->jq('.btn-saving-delete')->click($this->rq()->deleteSaving($savingId)
83
            ->confirm(trans('meeting.saving.questions.delete')));
84
85
        return $this->response;
86
    }
87
88
    public function filter(int $fundId)
89
    {
90
        $this->bag('meeting.saving')->set('fund.id', $fundId);
91
92
        return $this->page();
93
    }
94
95
    public function addSaving(int $fundId)
96
    {
97
        if($this->session->closed)
98
        {
99
            $this->notify->warning(trans('meeting.warnings.session.closed'));
100
            return $this->response;
101
        }
102
103
        $title = trans('meeting.saving.titles.add');
104
        $content = $this->render('pages.meeting.saving.add', [
105
            'members' => $this->memberService->getMemberList(),
106
            'fundId' => $fundId,
107
            'funds' => $this->fundService->getFundList(),
108
        ]);
109
        $buttons = [[
110
            'title' => trans('common.actions.cancel'),
111
            'class' => 'btn btn-tertiary',
112
            'click' => 'close',
113
        ],[
114
            'title' => trans('common.actions.save'),
115
            'class' => 'btn btn-primary',
116
            'click' => $this->rq()->createSaving(pm()->form('saving-form')),
117
        ]];
118
        $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

118
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
119
120
        return $this->response;
121
    }
122
123
    /**
124
     * @di $validator
125
     * @after showBalanceAmounts
126
     */
127
    public function createSaving(array $formValues)
128
    {
129
        if($this->session->closed)
130
        {
131
            $this->notify->warning(trans('meeting.warnings.session.closed'));
132
            return $this->response;
133
        }
134
135
        $values = $this->validator->validateItem($formValues);
136
        if(!($member = $this->memberService->getMember($values['member'])))
137
        {
138
            $this->notify->warning(trans('tontine.member.errors.not_found'));
139
            return $this->response;
140
        }
141
142
        $this->savingService->createSaving($member, $this->session, $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...Service::createSaving() 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

142
        $this->savingService->createSaving($member, /** @scrutinizer ignore-type */ $this->session, $values);
Loading history...
143
144
        $this->dialog->hide();
145
146
        return $this->home();
147
    }
148
149
    public function editSaving(int $savingId)
150
    {
151
        if($this->session->closed)
152
        {
153
            $this->notify->warning(trans('meeting.warnings.session.closed'));
154
            return $this->response;
155
        }
156
157
        $saving = $this->savingService->getSaving($this->session, $savingId);
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...ingService::getSaving() 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

157
        $saving = $this->savingService->getSaving(/** @scrutinizer ignore-type */ $this->session, $savingId);
Loading history...
158
        $title = trans('meeting.saving.titles.edit');
159
        $content = $this->render('pages.meeting.saving.edit', [
160
            'saving' => $saving,
161
            'funds' => $this->fundService->getFundList(),
162
        ]);
163
        $buttons = [[
164
            'title' => trans('common.actions.cancel'),
165
            'class' => 'btn btn-tertiary',
166
            'click' => 'close',
167
        ],[
168
            'title' => trans('common.actions.save'),
169
            'class' => 'btn btn-primary',
170
            'click' => $this->rq()->updateSaving($savingId, pm()->form('saving-form')),
171
        ]];
172
        $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

172
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
173
174
        return $this->response;
175
    }
176
177
    /**
178
     * @di $validator
179
     * @after showBalanceAmounts
180
     */
181
    public function updateSaving(int $savingId, array $formValues)
182
    {
183
        if($this->session->closed)
184
        {
185
            $this->notify->warning(trans('meeting.warnings.session.closed'));
186
            return $this->response;
187
        }
188
189
        $values = $this->validator->validateItem($formValues);
190
        if(!($member = $this->memberService->getMember($values['member'])))
191
        {
192
            $this->notify->warning(trans('tontine.member.errors.not_found'));
193
            return $this->response;
194
        }
195
196
        $this->savingService->updateSaving($member, $this->session, $savingId, $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...Service::updateSaving() 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

196
        $this->savingService->updateSaving($member, /** @scrutinizer ignore-type */ $this->session, $savingId, $values);
Loading history...
197
198
        $this->dialog->hide();
199
200
        return $this->home();
201
    }
202
203
    /**
204
     * @after showBalanceAmounts
205
     */
206
    public function deleteSaving(int $savingId)
207
    {
208
        if($this->session->closed)
209
        {
210
            $this->notify->warning(trans('meeting.warnings.session.closed'));
211
            return $this->response;
212
        }
213
214
        $this->savingService->deleteSaving($this->session, $savingId);
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...Service::deleteSaving() 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

214
        $this->savingService->deleteSaving(/** @scrutinizer ignore-type */ $this->session, $savingId);
Loading history...
215
216
        return $this->home();
217
    }
218
}
219