Passed
Pull Request — main (#57)
by Thierry
15:28 queued 13s
created

Member::showTotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
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\Service\LocaleService;
7
use Siak\Tontine\Service\Meeting\Saving\SavingService;
8
use Siak\Tontine\Service\Tontine\FundService;
9
use Siak\Tontine\Validation\Meeting\SavingValidator;
10
11
use function Jaxon\jq;
12
use function str_replace;
13
use function trans;
14
use function trim;
15
16
/**
17
 * @databag meeting.saving
18
 * @before getFundId
19
 */
20
class Member extends CallableSessionClass
21
{
22
    /**
23
     * @var LocaleService
24
     */
25
    protected LocaleService $localeService;
26
27
    /**
28
     * @var SavingValidator
29
     */
30
    protected SavingValidator $validator;
31
32
    /**
33
     * @var int
34
     */
35
    private $fundId = 0;
36
37
    /**
38
     * The constructor
39
     *
40
     * @param FundService $fundService
41
     * @param SavingService $savingService
42
     */
43
    public function __construct(private FundService $fundService,
44
        private SavingService $savingService)
45
    {}
46
47
    protected function getFundId()
48
    {
49
        $this->fundId = (int)($this->target()->method() === 'home' ?
50
            $this->target()->args()[0] :
51
            $this->bag('meeting.saving')->get('fund.id', 0));
52
    }
53
54
    /**
55
     * @param int $fundId
56
     *
57
     * @return mixed
58
     */
59
    public function home(int $fundId)
60
    {
61
        $fund = $fundId > 0 ? $this->fundService->getFund($fundId) : null;
62
        $fundId = !$fund ? 0 : $fund->id;
63
        $this->bag('meeting.saving')->set('fund.id', $fundId);
64
        $this->bag('meeting.saving')->set('member.filter', null);
65
        $this->bag('meeting.saving')->set('member.search', '');
66
67
        $html = $this->render('pages.meeting.saving.member.home', [
68
            'fund' => $fund,
69
        ]);
70
        $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

70
        $this->response->html('meeting-savings', /** @scrutinizer ignore-type */ $html);
Loading history...
71
72
        $this->jq('#btn-saving-back')->click($this->rq(Saving::class)->home());
73
        $this->jq('#btn-saving-filter')->click($this->rq()->toggleFilter());
74
        $this->jq('#btn-saving-search')
75
            ->click($this->rq()->search(jq('#txt-fee-member-search')->val()));
76
77
        return $this->page(1);
78
    }
79
80
    private function showTotal()
81
    {
82
        $savingCount = $this->savingService->getSavingCount($this->session, $this->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

82
        $savingCount = $this->savingService->getSavingCount(/** @scrutinizer ignore-type */ $this->session, $this->fundId);
Loading history...
83
        $savingTotal = $this->savingService->getSavingTotal($this->session, $this->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::getSavingTotal() 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

83
        $savingTotal = $this->savingService->getSavingTotal(/** @scrutinizer ignore-type */ $this->session, $this->fundId);
Loading history...
84
        $html = $this->render('pages.meeting.saving.total', [
85
            'savingCount' => $savingCount,
86
            'savingTotal' => $savingTotal,
87
        ]);
88
        $this->response->html('meeting-saving-members-total', $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

88
        $this->response->html('meeting-saving-members-total', /** @scrutinizer ignore-type */ $html);
Loading history...
89
    }
90
91
    /**
92
     * @param int $pageNumber
93
     *
94
     * @return mixed
95
     */
96
    public function page(int $pageNumber = 0)
97
    {
98
        $search = trim($this->bag('meeting.saving')->get('member.search', ''));
99
        $filter = $this->bag('meeting.saving')->get('member.filter', null);
100
        $memberCount = $this->savingService->getMemberCount($this->session, $this->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::getMemberCount() 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

100
        $memberCount = $this->savingService->getMemberCount(/** @scrutinizer ignore-type */ $this->session, $this->fundId,
Loading history...
101
            $search, $filter);
102
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $memberCount,
103
            'meeting', 'member.page');
104
        $members = $this->savingService->getMembers($this->session, $this->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...ngService::getMembers() 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

104
        $members = $this->savingService->getMembers(/** @scrutinizer ignore-type */ $this->session, $this->fundId,
Loading history...
105
            $search, $filter, $pageNumber);
106
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $memberCount);
107
108
        $this->showTotal();
109
110
        $html = $this->render('pages.meeting.saving.member.page', [
111
            'session' => $this->session,
112
            'members' => $members,
113
            'pagination' => $pagination,
114
        ]);
115
        $this->response->html('meeting-saving-members', $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

115
        $this->response->html('meeting-saving-members', /** @scrutinizer ignore-type */ $html);
Loading history...
116
        $this->response->call('makeTableResponsive', 'meeting-saving-members');
117
118
        $memberId = jq()->parent()->attr('data-member-id')->toInt();
119
        $amount = jq('input', jq()->parent()->parent())->val()->toInt();
120
        $this->jq('.btn-save-saving')->click($this->rq()->saveSaving($memberId, $amount));
121
        $this->jq('.btn-edit-saving')->click($this->rq()->editSaving($memberId));
122
123
        return $this->response;
124
    }
125
126
    public function search(string $search)
127
    {
128
        $this->bag('meeting.saving')->set('member.search', trim($search));
129
130
        return $this->page();
131
    }
132
133
    public function toggleFilter()
134
    {
135
        $filter = $this->bag('meeting.saving')->get('member.filter', null);
136
        // Switch between null, true and false
137
        $filter = $filter === null ? true : ($filter === true ? false : null);
138
        $this->bag('meeting.saving')->set('member.filter', $filter);
139
140
        return $this->page();
141
    }
142
143
    /**
144
     * @di $localeService
145
     * @param int $memberId
146
     *
147
     * @return mixed
148
     */
149
    public function editSaving(int $memberId)
150
    {
151
        if($this->session->closed)
152
        {
153
            $this->notify->warning(trans('meeting.warnings.session.closed'));
154
            return $this->response;
155
        }
156
        $saving = $this->savingService->findSaving($this->session, $this->fundId, $memberId);
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::findSaving() 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

156
        $saving = $this->savingService->findSaving(/** @scrutinizer ignore-type */ $this->session, $this->fundId, $memberId);
Loading history...
157
        $amount = !$saving ? '' : $this->localeService->getMoneyValue($saving->amount);
158
159
        $html = $this->render('pages.meeting.saving.member.edit', [
160
            'memberId' => $memberId,
161
            'amount' => $amount,
162
        ]);
163
        $fieldId = 'saving-member-' . $memberId;
164
        $this->response->html($fieldId, $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

164
        $this->response->html($fieldId, /** @scrutinizer ignore-type */ $html);
Loading history...
165
166
        $memberId = jq()->parent()->attr('data-member-id')->toInt();
167
        $amount = jq('input', jq()->parent()->parent())->val();
168
        $this->jq('.btn-save-saving', "#$fieldId")->click($this->rq()->saveSaving($memberId, $amount));
169
170
        return $this->response;
171
    }
172
173
    /**
174
     * @di $validator
175
     * @after showBalanceAmounts
176
     *
177
     * @param int $memberId
178
     * @param string $amount
179
     *
180
     * @return mixed
181
     */
182
    public function saveSaving(int $memberId, string $amount)
183
    {
184
        if($this->session->closed)
185
        {
186
            $this->notify->warning(trans('meeting.warnings.session.closed'));
187
            return $this->response;
188
        }
189
190
        $amount = str_replace(',', '.', trim($amount));
191
        if($amount === '')
192
        {
193
            $this->savingService->deleteSaving($this->session, 0, $memberId);
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

193
            $this->savingService->deleteSaving(/** @scrutinizer ignore-type */ $this->session, 0, $memberId);
Loading history...
194
195
            $this->notify->success(trans('meeting.messages.deleted'));
196
            return $this->page();
197
        }
198
199
        $values = ['member' => $memberId, 'amount' => $amount, 'fund' => $this->fundId];
200
        $values = $this->validator->validateItem($values);
201
        $amount = $values['amount'];
202
        $this->savingService->saveSaving($this->session, $this->fundId, $memberId, $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...ngService::saveSaving() 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

202
        $this->savingService->saveSaving(/** @scrutinizer ignore-type */ $this->session, $this->fundId, $memberId, $amount);
Loading history...
203
204
        $this->notify->success(trans('meeting.messages.saved'), trans('common.titles.success'));
205
        return $this->page();
206
    }
207
}
208