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

Member::delBill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Charge\Libre;
4
5
use App\Ajax\CallableChargeClass;
6
use App\Ajax\Web\Meeting\Charge\LibreFee as Charge;
7
use Siak\Tontine\Exception\MessageException;
8
use Siak\Tontine\Model\Charge as ChargeModel;
9
use Siak\Tontine\Service\LocaleService;
10
11
use function filter_var;
12
use function Jaxon\jq;
13
use function Jaxon\pm;
14
use function str_replace;
15
use function trans;
16
use function trim;
17
18
class Member extends CallableChargeClass
19
{
20
    /**
21
     * @var LocaleService
22
     */
23
    protected LocaleService $localeService;
24
25
    /**
26
     * @var ChargeModel|null
27
     */
28
    protected ?ChargeModel $charge;
29
30
    /**
31
     * @param int $chargeId
32
     *
33
     * @return mixed
34
     */
35
    public function home(int $chargeId)
36
    {
37
        $this->bag('meeting')->set('charge.id', $chargeId);
38
        $this->bag('meeting')->set('fee.member.filter', null);
39
        $this->bag('meeting')->set('fee.member.search', '');
40
41
        $html = $this->render('pages.meeting.charge.libre.member.home', [
42
            'charge' => $this->charge,
43
            'paid' => $this->charge->is_fee,
44
        ]);
45
        $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

45
        $this->response->html('meeting-fees-libre', /** @scrutinizer ignore-type */ $html);
Loading history...
46
47
        $this->jq('#btn-fee-libre-back')->click($this->rq(Charge::class)->home());
48
        $this->jq('#btn-fee-libre-filter')->click($this->rq()->toggleFilter());
49
        $this->jq('#btn-fee-libre-search')
50
            ->click($this->rq()->search(jq('#txt-fee-member-search')->val()));
51
52
        return $this->page(1);
53
    }
54
55
    private function showTotal()
56
    {
57
        $settlement = $this->settlementService->getSettlementCount($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...e::getSettlementCount() 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

57
        $settlement = $this->settlementService->getSettlementCount($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...e::getSettlementCount() 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

57
        $settlement = $this->settlementService->getSettlementCount(/** @scrutinizer ignore-type */ $this->charge, $this->session);
Loading history...
58
        $settlementCount = $settlement->total ?? 0;
0 ignored issues
show
Bug introduced by
The property total does not seem to exist on Siak\Tontine\Model\Bill. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
59
        $settlementAmount = $settlement->amount ?? 0;
60
61
        $html = $this->render('pages.meeting.charge.libre.member.total', [
62
            'settlementCount' => $settlementCount,
63
            'settlementAmount' => $settlementAmount,
64
        ]);
65
        $this->response->html('member-libre-settlements-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

65
        $this->response->html('member-libre-settlements-total', /** @scrutinizer ignore-type */ $html);
Loading history...
66
    }
67
68
    /**
69
     * @param int $pageNumber
70
     *
71
     * @return mixed
72
     */
73
    public function page(int $pageNumber = 0)
74
    {
75
        $search = trim($this->bag('meeting')->get('fee.member.search', ''));
76
        $filter = $this->bag('meeting')->get('fee.member.filter', null);
77
        $memberCount = $this->billService->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

77
        $memberCount = $this->billService->getMemberCount(/** @scrutinizer ignore-type */ $this->charge,
Loading history...
78
            $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

78
            /** @scrutinizer ignore-type */ $this->session, $search, $filter);
Loading history...
79
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $memberCount,
80
            'meeting', 'member.page');
81
        $members = $this->billService->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...llService::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

81
        $members = $this->billService->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...llService::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

81
        $members = $this->billService->getMembers(/** @scrutinizer ignore-type */ $this->charge, $this->session,
Loading history...
82
            $search, $filter, $pageNumber);
83
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $memberCount);
84
85
        $this->showTotal();
86
87
        $html = $this->render('pages.meeting.charge.libre.member.page', [
88
            'session' => $this->session,
89
            'charge' => $this->charge,
90
            'members' => $members,
91
            'pagination' => $pagination,
92
        ]);
93
        $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

93
        $this->response->html('meeting-fee-libre-members', /** @scrutinizer ignore-type */ $html);
Loading history...
94
        $this->response->call('makeTableResponsive', 'meeting-fee-libre-members');
95
96
        $memberId = jq()->parent()->attr('data-member-id')->toInt();
97
        $paid = pm()->checked('check-fee-libre-paid');
98
        $amount = jq('input', jq()->parent()->parent())->val()->toInt();
99
        $this->jq('.btn-add-bill')->click($this->rq()->addBill($memberId, $paid));
100
        $this->jq('.btn-del-bill')->click($this->rq()->delBill($memberId));
101
        $this->jq('.btn-save-bill')->click($this->rq()->addBill($memberId, $paid, $amount));
102
        $this->jq('.btn-edit-bill')->click($this->rq()->editBill($memberId));
103
104
        return $this->response;
105
    }
106
107
    public function toggleFilter()
108
    {
109
        $filter = $this->bag('meeting')->get('fee.member.filter', null);
110
        // Switch between null, true and false
111
        $filter = $filter === null ? true : ($filter === true ? false : null);
112
        $this->bag('meeting')->set('fee.member.filter', $filter);
113
114
        return $this->page();
115
    }
116
117
    public function search(string $search)
118
    {
119
        $this->bag('meeting')->set('fee.member.search', trim($search));
120
121
        return $this->page();
122
    }
123
124
    private function convertAmount(string $amount): float
125
    {
126
        $amount = str_replace(',', '.', trim($amount));
127
        if($amount !== '' && filter_var($amount, FILTER_VALIDATE_FLOAT) === false)
128
        {
129
            throw new MessageException(trans('meeting.errors.amount.invalid', [
130
                'amount' => $amount,
131
            ]));
132
        }
133
        return $amount === '' ? 0 : (float)$amount;
134
    }
135
136
    /**
137
     * @before checkChargeEdit
138
     * @param int $memberId
139
     * @param string $amount
140
     *
141
     * @return mixed
142
     */
143
    public function addBill(int $memberId, bool $paid, string $amount = '')
144
    {
145
        $this->billService->createBill($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...llService::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

145
        $this->billService->createBill($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...llService::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

145
        $this->billService->createBill(/** @scrutinizer ignore-type */ $this->charge, $this->session, $memberId,
Loading history...
146
            $paid, $this->convertAmount($amount));
147
148
        return $this->page();
149
    }
150
151
    /**
152
     * @before checkChargeEdit
153
     * @param int $memberId
154
     *
155
     * @return mixed
156
     */
157
    public function delBill(int $memberId)
158
    {
159
        $this->billService->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...llService::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

159
        $this->billService->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...llService::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

159
        $this->billService->deleteBill(/** @scrutinizer ignore-type */ $this->charge, $this->session, $memberId);
Loading history...
160
161
        return $this->page();
162
    }
163
164
    /**
165
     * @di $localeService
166
     * @before checkChargeEdit
167
     * @param int $memberId
168
     *
169
     * @return mixed
170
     */
171
    public function editBill(int $memberId)
172
    {
173
        $bill = $this->billService->getMemberBill($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...ervice::getMemberBill() 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

173
        $bill = $this->billService->getMemberBill($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...ervice::getMemberBill() 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

173
        $bill = $this->billService->getMemberBill(/** @scrutinizer ignore-type */ $this->charge, $this->session, $memberId);
Loading history...
174
        if($bill === null)
175
        {
176
            return $this->response;
177
        }
178
179
        $html = $this->render('pages.meeting.charge.libre.member.edit', [
180
            'id' => $memberId,
181
            'amount' => $this->localeService->getMoneyValue($bill->bill->amount),
182
        ]);
183
        $fieldId = 'member-' . $memberId;
184
        $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

184
        $this->response->html($fieldId, /** @scrutinizer ignore-type */ $html);
Loading history...
185
186
        $memberId = jq()->parent()->attr('data-member-id')->toInt();
187
        $amount = jq('input', jq()->parent()->parent())->val();
188
        $this->jq('.btn-save-bill', "#$fieldId")->click($this->rq()->saveBill($memberId, $amount));
189
190
        return $this->response;
191
    }
192
193
    /**
194
     * @before checkChargeEdit
195
     * @param int $memberId
196
     * @param string $amount
197
     *
198
     * @return mixed
199
     */
200
    public function saveBill(int $memberId, string $amount)
201
    {
202
        $amount = $this->convertAmount($amount);
203
        if(!$amount)
204
        {
205
            $this->billService->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...llService::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

205
            $this->billService->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...llService::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

205
            $this->billService->deleteBill($this->charge, /** @scrutinizer ignore-type */ $this->session, $memberId);
Loading history...
206
            return $this->page();
207
        }
208
209
        $this->billService->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...llService::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

209
        $this->billService->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...llService::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

209
        $this->billService->updateBill(/** @scrutinizer ignore-type */ $this->charge, $this->session, $memberId, $amount);
Loading history...
210
        return $this->page();
211
    }
212
}
213