Passed
Pull Request — main (#51)
by Thierry
13:39
created

Target::add()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
nc 4
nop 0
dl 0
loc 33
rs 9.3222
c 1
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Charge\Libre;
4
5
use App\Ajax\CallableClass;
6
use App\Ajax\Web\Meeting\Charge\LibreFee as Charge;
7
use Siak\Tontine\Service\LocaleService;
8
use Siak\Tontine\Model\Charge as ChargeModel;
9
use Siak\Tontine\Model\Session as SessionModel;
10
use Siak\Tontine\Model\SettlementTarget as TargetModel;
11
use Siak\Tontine\Service\Meeting\Charge\SettlementTargetService;
12
use Siak\Tontine\Service\Tontine\ChargeService;
13
use Siak\Tontine\Validation\Meeting\TargetValidator;
14
15
use function Jaxon\jq;
16
use function Jaxon\pm;
17
use function trans;
18
use function trim;
19
20
/**
21
 * @databag meeting
22
 * @before getTarget
23
 */
24
class Target extends CallableClass
25
{
26
    /**
27
     * @var LocaleService
28
     */
29
    protected LocaleService $localeService;
30
31
    /**
32
     * @di
33
     * @var ChargeService
34
     */
35
    protected ChargeService $chargeService;
36
37
    /**
38
     * @di
39
     * @var SettlementTargetService
40
     */
41
    protected SettlementTargetService $targetService;
42
43
    /**
44
     * @var TargetValidator
45
     */
46
    protected TargetValidator $validator;
47
48
    /**
49
     * @var SessionModel|null
50
     */
51
    protected ?SessionModel $session;
52
53
    /**
54
     * @var ChargeModel|null
55
     */
56
    protected ?ChargeModel $charge;
57
58
    /**
59
     * @var TargetModel|null
60
     */
61
    protected ?TargetModel $target = null;
62
63
    protected function getTarget()
64
    {
65
        $sessionId = $this->bag('meeting')->get('session.id');
66
        $chargeId = $this->target()->method() === 'home' ?
67
            $this->target()->args()[0] : $this->bag('meeting')->get('charge.id');
68
        $this->session = $this->chargeService->getSession($sessionId);
0 ignored issues
show
Bug introduced by
It seems like $sessionId can also be of type null; however, parameter $sessionId of Siak\Tontine\Service\Ton...geService::getSession() does only seem to accept integer, 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

68
        $this->session = $this->chargeService->getSession(/** @scrutinizer ignore-type */ $sessionId);
Loading history...
69
        $this->charge = $this->chargeService->getCharge($chargeId);
0 ignored issues
show
Bug introduced by
It seems like $chargeId can also be of type null; however, parameter $chargeId of Siak\Tontine\Service\Ton...rgeService::getCharge() does only seem to accept integer, 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

69
        $this->charge = $this->chargeService->getCharge(/** @scrutinizer ignore-type */ $chargeId);
Loading history...
70
        if($this->session !== null && $this->charge !== null)
71
        {
72
            $this->target = $this->targetService->getTarget($this->charge, $this->session);
73
        }
74
    }
75
76
    /**
77
     * @param int $chargeId
78
     *
79
     * @return mixed
80
     */
81
    public function home(int $chargeId)
82
    {
83
        $this->bag('meeting')->set('charge.id', $chargeId);
84
85
        $html = $this->view()->render('tontine.pages.meeting.charge.libre.target.home', [
86
            'charge' => $this->charge,
87
            'target' => $this->target,
88
        ]);
89
        $this->response->html('meeting-fees-libre', $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

89
        $this->response->html('meeting-fees-libre', /** @scrutinizer ignore-type */ $html);
Loading history...
90
        $this->jq('#btn-fee-target-add')->click($this->rq()->add());
91
        $this->jq('#btn-fee-target-edit')->click($this->rq()->edit());
92
        $this->jq('#btn-fee-target-remove')->click($this->rq()->remove()
93
            ->confirm(trans('meeting.target.questions.remove')));
94
        $this->jq('#btn-fee-target-back')->click($this->cl(Charge::class)->rq()->home());
95
96
        return $this->page();
97
    }
98
99
    /**
100
     * @param int $pageNumber
101
     *
102
     * @return mixed
103
     */
104
    public function page(int $pageNumber = 0)
105
    {
106
        if($this->target === null)
107
        {
108
            return $this->response;
109
        }
110
111
        $search = trim($this->bag('meeting')->get('fee.member.search', ''));
112
        $memberCount = $this->targetService->getMemberCount($search);
113
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $memberCount,
114
            'meeting', 'target.page');
115
        $members = $this->targetService->getMembersWithSettlements($this->charge,
0 ignored issues
show
Bug introduced by
It seems like $this->charge can also be of type null; however, parameter $charge of Siak\Tontine\Service\Mee...embersWithSettlements() does only seem to accept Siak\Tontine\Model\Charge, 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

115
        $members = $this->targetService->getMembersWithSettlements(/** @scrutinizer ignore-type */ $this->charge,
Loading history...
116
            $this->target, $search, $pageNumber);
117
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $memberCount);
118
119
        $html = $this->view()->render('tontine.pages.meeting.charge.libre.target.page', [
120
            'search' => $search,
121
            'session' => $this->session,
122
            'target' => $this->target,
123
            'charge' => $this->charge,
124
            'members' => $members,
125
            'pagination' => $pagination,
126
        ]);
127
        $this->response->html('meeting-fee-libre-target', $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

127
        $this->response->html('meeting-fee-libre-target', /** @scrutinizer ignore-type */ $html);
Loading history...
128
        $this->jq('#btn-fee-libre-search')
129
            ->click($this->rq()->search(jq('#txt-fee-member-search')->val()));
130
131
        return $this->response;
132
    }
133
134
    public function search(string $search)
135
    {
136
        $this->bag('meeting')->set('fee.member.search', trim($search));
137
138
        return $this->page();
139
    }
140
141
    /**
142
     * @return mixed
143
     */
144
    public function add()
145
    {
146
        if($this->session->closed)
147
        {
148
            $this->notify->warning(trans('meeting.warnings.session.closed'));
149
            return $this->response;
150
        }
151
        if($this->session === null || $this->charge === null)
152
        {
153
            return $this->response;
154
        }
155
        if($this->target !== null)
156
        {
157
            return $this->response;
158
        }
159
160
        $title = trans('meeting.target.titles.set');
161
        $content = $this->view()->render('tontine.pages.meeting.charge.libre.target.add', [
162
            'sessions' => $this->targetService->getDeadlineSessions($this->session),
163
        ]);
164
        $buttons = [[
165
            'title' => trans('common.actions.cancel'),
166
            'class' => 'btn btn-tertiary',
167
            'click' => 'close',
168
        ],[
169
            'title' => trans('common.actions.save'),
170
            'class' => 'btn btn-primary',
171
            'click' => $this->rq()->create(pm()->form('target-form')),
172
        ]];
173
174
        $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

174
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
175
176
        return $this->response;
177
    }
178
179
    /**
180
     * @di $validator
181
     * @param array $formValues
182
     *
183
     * @return mixed
184
     */
185
    public function create(array $formValues)
186
    {
187
        if($this->session->closed)
188
        {
189
            $this->notify->warning(trans('meeting.warnings.session.closed'));
190
            return $this->response;
191
        }
192
        if($this->session === null || $this->charge === null)
193
        {
194
            return $this->response;
195
        }
196
        if($this->target !== null)
197
        {
198
            return $this->response;
199
        }
200
201
        $formValues['global'] = isset($formValues['global']);
202
        $values = $this->validator->validateItem($formValues);
203
        $deadlineSession = $this->chargeService->getSession($values['deadline']);
204
205
        $this->targetService->createTarget($this->charge, $this->session,
206
            $deadlineSession, $values['amount'], $values['global']);
0 ignored issues
show
Bug introduced by
It seems like $deadlineSession can also be of type null; however, parameter $deadline of Siak\Tontine\Service\Mee...Service::createTarget() 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

206
            /** @scrutinizer ignore-type */ $deadlineSession, $values['amount'], $values['global']);
Loading history...
207
        $this->dialog->hide();
208
209
        $this->target = $this->targetService->getTarget($this->charge, $this->session);
210
        return $this->home($this->charge->id);
211
    }
212
213
    /**
214
     * @return mixed
215
     */
216
    public function edit()
217
    {
218
        if($this->session->closed)
219
        {
220
            $this->notify->warning(trans('meeting.warnings.session.closed'));
221
            return $this->response;
222
        }
223
        if($this->target === null)
224
        {
225
            return $this->response;
226
        }
227
228
        $title = trans('meeting.target.titles.set');
229
        $content = $this->view()->render('tontine.pages.meeting.charge.libre.target.edit', [
230
            'target' => $this->target,
231
            'sessions' => $this->targetService->getDeadlineSessions($this->session),
0 ignored issues
show
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $currentSession of Siak\Tontine\Service\Mee...::getDeadlineSessions() 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

231
            'sessions' => $this->targetService->getDeadlineSessions(/** @scrutinizer ignore-type */ $this->session),
Loading history...
232
        ]);
233
        $buttons = [[
234
            'title' => trans('common.actions.cancel'),
235
            'class' => 'btn btn-tertiary',
236
            'click' => 'close',
237
        ],[
238
            'title' => trans('common.actions.save'),
239
            'class' => 'btn btn-primary',
240
            'click' => $this->rq()->update(pm()->form('target-form')),
241
        ]];
242
243
        $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

243
        $this->dialog->show($title, /** @scrutinizer ignore-type */ $content, $buttons);
Loading history...
244
245
        return $this->response;
246
    }
247
248
    /**
249
     * @di $validator
250
     * @param int $memberId
251
     * @param string $amount
252
     *
253
     * @return mixed
254
     */
255
    public function update(array $formValues)
256
    {
257
        if($this->session->closed)
258
        {
259
            $this->notify->warning(trans('meeting.warnings.session.closed'));
260
            return $this->response;
261
        }
262
        if($this->target === null)
263
        {
264
            return $this->response;
265
        }
266
267
        $formValues['global'] = isset($formValues['global']);
268
        $values = $this->validator->validateItem($formValues);
269
        $deadlineSession = $this->chargeService->getSession($values['deadline']);
270
271
        $this->targetService->updateTarget($this->target, $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...Service::updateTarget() 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

271
        $this->targetService->updateTarget($this->target, /** @scrutinizer ignore-type */ $this->session,
Loading history...
272
            $deadlineSession, $values['amount'], $values['global']);
0 ignored issues
show
Bug introduced by
It seems like $deadlineSession can also be of type null; however, parameter $deadline of Siak\Tontine\Service\Mee...Service::updateTarget() 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

272
            /** @scrutinizer ignore-type */ $deadlineSession, $values['amount'], $values['global']);
Loading history...
273
        $this->dialog->hide();
274
275
        $this->target = $this->targetService->getTarget($this->charge, $this->session);
0 ignored issues
show
Bug introduced by
It seems like $this->charge can also be of type null; however, parameter $charge of Siak\Tontine\Service\Mee...getService::getTarget() does only seem to accept Siak\Tontine\Model\Charge, 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

275
        $this->target = $this->targetService->getTarget(/** @scrutinizer ignore-type */ $this->charge, $this->session);
Loading history...
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...getService::getTarget() 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

275
        $this->target = $this->targetService->getTarget($this->charge, /** @scrutinizer ignore-type */ $this->session);
Loading history...
276
        return $this->home($this->charge->id);
277
    }
278
279
    /**
280
     * @return mixed
281
     */
282
    public function remove()
283
    {
284
        if($this->session === null || $this->charge === null)
285
        {
286
            return $this->response;
287
        }
288
        if($this->target === null)
289
        {
290
            return $this->response;
291
        }
292
293
        $this->targetService->deleteTarget($this->target);
294
        $this->notify->success(trans('meeting.target.messages.removed'), trans('common.titles.success'));
295
296
        $this->target = $this->targetService->getTarget($this->charge, $this->session);
297
        return $this->home($this->charge->id);
298
    }
299
}
300