Passed
Pull Request — main (#49)
by Thierry
14:14
created

Member::saveBill()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 24
rs 9.2222
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\Service\Meeting\Charge\LibreFeeService;
9
use Siak\Tontine\Service\Tontine\ChargeService;
10
use Siak\Tontine\Model\Session as SessionModel;
11
use Siak\Tontine\Model\Charge as ChargeModel;
12
13
use function filter_var;
14
use function Jaxon\jq;
15
use function Jaxon\pm;
16
use function str_replace;
17
use function trans;
18
use function trim;
19
20
/**
21
 * @databag meeting
22
 * @before getCharge
23
 */
24
class Member 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 LibreFeeService
40
     */
41
    protected LibreFeeService $feeService;
42
43
    /**
44
     * @var SessionModel|null
45
     */
46
    protected ?SessionModel $session;
47
48
    /**
49
     * @var ChargeModel|null
50
     */
51
    protected ?ChargeModel $charge;
52
53
    protected function getCharge()
54
    {
55
        $sessionId = $this->bag('meeting')->get('session.id');
56
        $chargeId = $this->target()->method() === 'home' ?
57
            $this->target()->args()[0] : $this->bag('meeting')->get('charge.id');
58
        $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

58
        $this->session = $this->chargeService->getSession(/** @scrutinizer ignore-type */ $sessionId);
Loading history...
59
        $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

59
        $this->charge = $this->chargeService->getCharge(/** @scrutinizer ignore-type */ $chargeId);
Loading history...
60
    }
61
62
    /**
63
     * @param int $chargeId
64
     *
65
     * @return mixed
66
     */
67
    public function home(int $chargeId)
68
    {
69
        $this->bag('meeting')->set('charge.id', $chargeId);
70
        $this->bag('meeting')->set('fee.member.search', '');
71
        $this->bag('meeting')->set('fee.member.filter', null);
72
73
        $html = $this->view()->render('tontine.pages.meeting.charge.libre.member.home', [
74
            'charge' => $this->charge,
75
        ]);
76
        $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

76
        $this->response->html('meeting-fees-libre', /** @scrutinizer ignore-type */ $html);
Loading history...
77
        $this->jq('#btn-fee-libre-back')->click($this->cl(Charge::class)->rq()->home());
78
        $this->jq('#btn-fee-libre-filter')->click($this->rq()->toggleFilter());
79
        $this->jq('#btn-fee-libre-search')
80
            ->click($this->rq()->search(jq('#txt-fee-member-search')->val()));
81
82
        return $this->page(1);
83
    }
84
85
    /**
86
     * @param int $pageNumber
87
     *
88
     * @return mixed
89
     */
90
    public function page(int $pageNumber = 0)
91
    {
92
        $search = trim($this->bag('meeting')->get('fee.member.search', ''));
93
        $filter = $this->bag('meeting')->get('fee.member.filter', null);
94
        $memberCount = $this->feeService->getMemberCount($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...rvice::getMemberCount() 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

94
        $memberCount = $this->feeService->getMemberCount(/** @scrutinizer ignore-type */ $this->charge,
Loading history...
95
            $this->session, $search, $filter);
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

95
            /** @scrutinizer ignore-type */ $this->session, $search, $filter);
Loading history...
96
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $memberCount,
97
            'meeting', 'member.page');
98
        $members = $this->feeService->getMembers($this->charge, $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...eeService::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

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

98
        $members = $this->feeService->getMembers(/** @scrutinizer ignore-type */ $this->charge, $this->session,
Loading history...
99
            $search, $filter, $pageNumber);
100
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $memberCount);
101
102
        $html = $this->view()->render('tontine.pages.meeting.charge.libre.member.page', [
103
            'session' => $this->session,
104
            'charge' => $this->charge,
105
            'members' => $members,
106
            'pagination' => $pagination,
107
        ]);
108
        $this->response->html('meeting-fee-libre-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

108
        $this->response->html('meeting-fee-libre-members', /** @scrutinizer ignore-type */ $html);
Loading history...
109
110
        $memberId = jq()->parent()->attr('data-member-id')->toInt();
111
        $paid = pm()->checked('check-fee-libre-paid');
112
        $amount = jq('input', jq()->parent()->parent())->val()->toInt();
113
        $this->jq('.btn-add-bill')->click($this->rq()->addBill($memberId, $paid));
114
        $this->jq('.btn-del-bill')->click($this->rq()->delBill($memberId));
115
        $this->jq('.btn-save-bill')->click($this->rq()->addBill($memberId, $paid, $amount));
116
        $this->jq('.btn-edit-bill')->click($this->rq()->editBill($memberId));
117
        // $this->jq('.btn-edit-notes')->click($this->rq()->editNotes($memberId));
118
119
        return $this->response;
120
    }
121
122
    public function toggleFilter()
123
    {
124
        $filter = $this->bag('meeting')->get('fee.member.filter', null);
125
        // Switch between null, true and false
126
        $filter = $filter === null ? true : ($filter === true ? false : null);
127
        $this->bag('meeting')->set('fee.member.filter', $filter);
128
129
        return $this->page();
130
    }
131
132
    public function search(string $search)
133
    {
134
        $this->bag('meeting')->set('fee.member.search', trim($search));
135
136
        return $this->page();
137
    }
138
139
    /**
140
     * @param int $memberId
141
     * @param bool $paid
142
     * @param string $amount
143
     *
144
     * @return mixed
145
     */
146
    public function addBill(int $memberId, bool $paid, string $amount = '')
147
    {
148
        if($this->session->closed)
149
        {
150
            $this->notify->warning(trans('meeting.warnings.session.closed'));
151
            return $this->response;
152
        }
153
        $amount = str_replace(',', '.', trim($amount));
154
        if($amount !== '' && filter_var($amount, FILTER_VALIDATE_FLOAT) === false)
155
        {
156
            $this->notify->error(trans('meeting.errors.amount.invalid', ['amount' => $amount]));
157
            return $this->response;
158
        }
159
        $amount = $amount === '' ? 0 : (float)$amount;
160
161
        $this->feeService->createBill($this->charge, $this->session, $memberId, $paid, $amount);
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...eeService::createBill() 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

161
        $this->feeService->createBill(/** @scrutinizer ignore-type */ $this->charge, $this->session, $memberId, $paid, $amount);
Loading history...
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...eeService::createBill() 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

161
        $this->feeService->createBill($this->charge, /** @scrutinizer ignore-type */ $this->session, $memberId, $paid, $amount);
Loading history...
162
163
        return $this->page();
164
    }
165
166
    /**
167
     * @param int $memberId
168
     *
169
     * @return mixed
170
     */
171
    public function delBill(int $memberId)
172
    {
173
        if($this->session->closed)
174
        {
175
            $this->notify->warning(trans('meeting.warnings.session.closed'));
176
            return $this->response;
177
        }
178
179
        $this->feeService->deleteBill($this->charge, $this->session, $memberId);
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...eeService::deleteBill() 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

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

179
        $this->feeService->deleteBill($this->charge, /** @scrutinizer ignore-type */ $this->session, $memberId);
Loading history...
180
181
        return $this->page();
182
    }
183
184
    /**
185
     * @di $localeService
186
     * @param int $memberId
187
     *
188
     * @return mixed
189
     */
190
    public function editBill(int $memberId)
191
    {
192
        if($this->session->closed)
193
        {
194
            $this->notify->warning(trans('meeting.warnings.session.closed'));
195
            return $this->response;
196
        }
197
        $bill = $this->feeService->getBill($this->charge, $this->session, $memberId);
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...reFeeService::getBill() 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

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

197
        $bill = $this->feeService->getBill($this->charge, /** @scrutinizer ignore-type */ $this->session, $memberId);
Loading history...
198
        if($bill === null || $bill->bill->settlement !== null)
199
        {
200
            return $this->page();
201
        }
202
203
        $html = $this->view()->render('tontine.pages.meeting.charge.libre.member.edit', [
204
            'id' => $memberId,
205
            'amount' => $this->localeService->getMoneyValue($bill->bill->amount),
206
        ]);
207
        $fieldId = 'member-' . $memberId;
208
        $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

208
        $this->response->html($fieldId, /** @scrutinizer ignore-type */ $html);
Loading history...
209
210
        $memberId = jq()->parent()->attr('data-member-id')->toInt();
211
        $amount = jq('input', jq()->parent()->parent())->val();
212
        $this->jq('.btn-save-bill', "#$fieldId")->click($this->rq()->saveBill($memberId, $amount));
213
214
        return $this->response;
215
    }
216
217
    /**
218
     * @param int $memberId
219
     * @param string $amount
220
     *
221
     * @return mixed
222
     */
223
    public function saveBill(int $memberId, string $amount)
224
    {
225
        if($this->session->closed)
226
        {
227
            $this->notify->warning(trans('meeting.warnings.session.closed'));
228
            return $this->response;
229
        }
230
231
        $amount = str_replace(',', '.', trim($amount));
232
        if($amount !== '' && filter_var($amount, FILTER_VALIDATE_FLOAT) === false)
233
        {
234
            $this->notify->error(trans('meeting.errors.amount.invalid', ['amount' => $amount]));
235
            return $this->response;
236
        }
237
        $amount = $amount === '' ? 0 : (float)$amount;
238
239
        if(!$amount)
240
        {
241
            $this->feeService->deleteBill($this->charge, $this->session, $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...eeService::deleteBill() 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

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

241
            $this->feeService->deleteBill(/** @scrutinizer ignore-type */ $this->charge, $this->session, $memberId);
Loading history...
242
            return $this->page();
243
        }
244
245
        $this->feeService->updateBill($this->charge, $this->session, $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...eeService::updateBill() 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

245
        $this->feeService->updateBill($this->charge, /** @scrutinizer ignore-type */ $this->session, $memberId, $amount);
Loading history...
Bug introduced by
It seems like $this->charge can also be of type null; however, parameter $charge of Siak\Tontine\Service\Mee...eeService::updateBill() 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

245
        $this->feeService->updateBill(/** @scrutinizer ignore-type */ $this->charge, $this->session, $memberId, $amount);
Loading history...
246
        return $this->page();
247
    }
248
}
249