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

Settlement   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 63
c 0
b 0
f 0
dl 0
loc 146
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A delSettlement() 0 15 2
A getCharge() 0 7 2
A toggleFilter() 0 8 3
A page() 0 32 1
A addSettlement() 0 15 2
A home() 0 15 1
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Charge\Libre;
4
5
use App\Ajax\CallableClass;
6
use App\Ajax\Web\Meeting\Cash\Disbursement;
7
use App\Ajax\Web\Meeting\Credit\Loan;
8
use App\Ajax\Web\Meeting\Charge\LibreFee as Charge;
9
use Siak\Tontine\Service\Meeting\Charge\BillService;
10
use Siak\Tontine\Service\Meeting\Charge\SettlementService;
11
use Siak\Tontine\Model\Session as SessionModel;
12
use Siak\Tontine\Model\Charge as ChargeModel;
13
14
use function Jaxon\jq;
15
use function trans;
16
use function trim;
17
18
/**
19
 * @databag meeting
20
 * @before getCharge
21
 */
22
class Settlement extends CallableClass
23
{
24
    /**
25
     * @di
26
     * @var BillService
27
     */
28
    protected BillService $billService;
29
30
    /**
31
     * @di
32
     * @var SettlementService
33
     */
34
    protected SettlementService $settlementService;
35
36
    /**
37
     * @var SessionModel|null
38
     */
39
    protected ?SessionModel $session;
40
41
    /**
42
     * @var ChargeModel|null
43
     */
44
    protected ?ChargeModel $charge;
45
46
    protected function getCharge()
47
    {
48
        $sessionId = $this->bag('meeting')->get('session.id');
49
        $chargeId = $this->target()->method() === 'home' ?
50
            $this->target()->args()[0] : $this->bag('meeting')->get('fee.libre.id');
51
        $this->session = $this->settlementService->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\Mee...ntService::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

51
        $this->session = $this->settlementService->getSession(/** @scrutinizer ignore-type */ $sessionId);
Loading history...
52
        $this->charge = $this->settlementService->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\Mee...entService::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

52
        $this->charge = $this->settlementService->getCharge(/** @scrutinizer ignore-type */ $chargeId);
Loading history...
53
    }
54
55
    /**
56
     * @param int $chargeId
57
     *
58
     * @return mixed
59
     */
60
    public function home(int $chargeId)
61
    {
62
        $this->bag('meeting')->set('fee.libre.id', $chargeId);
63
        $this->bag('meeting')->set('settlement.libre.search', '');
64
        $this->bag('meeting')->set('settlement.libre.filter', null);
65
66
        $html = $this->view()->render('tontine.pages.meeting.settlement.home', [
67
            'charge' => $this->charge,
68
            'type' => 'libre',
69
        ]);
70
        $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

70
        $this->response->html('meeting-fees-libre', /** @scrutinizer ignore-type */ $html);
Loading history...
71
        $this->jq('#btn-fee-libre-settlements-back')->click($this->cl(Charge::class)->rq()->home());
72
        $this->jq('#btn-fee-libre-settlements-filter')->click($this->rq()->toggleFilter());
73
74
        return $this->page(1);
75
    }
76
77
    /**
78
     * @param int $pageNumber
79
     *
80
     * @return mixed
81
     */
82
    public function page(int $pageNumber = 0)
83
    {
84
        $search = trim($this->bag('meeting')->get('settlement.libre.search', ''));
85
        $onlyUnpaid = $this->bag('meeting')->get('settlement.libre.filter', null);
86
        $billCount = $this->billService->getBillCount($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...Service::getBillCount() 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

86
        $billCount = $this->billService->getBillCount(/** @scrutinizer ignore-type */ $this->charge,
Loading history...
87
            $this->session, $search, $onlyUnpaid);
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::getBillCount() 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

87
            /** @scrutinizer ignore-type */ $this->session, $search, $onlyUnpaid);
Loading history...
88
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber,
89
            $billCount, 'meeting', 'settlement.page');
90
        $bills = $this->billService->getBills($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...BillService::getBills() 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

90
        $bills = $this->billService->getBills($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...BillService::getBills() 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

90
        $bills = $this->billService->getBills(/** @scrutinizer ignore-type */ $this->charge, $this->session,
Loading history...
91
            $search, $onlyUnpaid, $pageNumber);
92
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $billCount);
93
        $settlement = $this->settlementService->getSettlement($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...ervice::getSettlement() 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

93
        $settlement = $this->settlementService->getSettlement($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...ervice::getSettlement() 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

93
        $settlement = $this->settlementService->getSettlement(/** @scrutinizer ignore-type */ $this->charge, $this->session);
Loading history...
94
95
        $html = $this->view()->render('tontine.pages.meeting.settlement.page', [
96
            'session' => $this->session,
97
            'charge' => $this->charge,
98
            'billCount' => $billCount,
99
            'settlement' => $settlement,
100
            'bills' => $bills,
101
            'pagination' => $pagination,
102
        ]);
103
        $this->response->html('meeting-fee-libre-bills', $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

103
        $this->response->html('meeting-fee-libre-bills', /** @scrutinizer ignore-type */ $html);
Loading history...
104
105
        $billId = jq()->parent()->attr('data-bill-id')->toInt();
106
        $this->jq('.btn-add-settlement', '#meeting-fee-libre-bills')
107
            ->click($this->rq()->addSettlement($billId));
108
        $this->jq('.btn-del-settlement', '#meeting-fee-libre-bills')
109
            ->click($this->rq()->delSettlement($billId));
110
        $this->jq('.btn-edit-notes', '#meeting-fee-libre-bills')
111
            ->click($this->rq()->editNotes($billId));
112
113
        return $this->response;
114
    }
115
116
    public function toggleFilter()
117
    {
118
        $onlyUnpaid = $this->bag('meeting')->get('settlement.libre.filter', null);
119
        // Switch between null, true and false
120
        $onlyUnpaid = $onlyUnpaid === null ? true : ($onlyUnpaid === true ? false : null);
121
        $this->bag('meeting')->set('settlement.libre.filter', $onlyUnpaid);
122
123
        return $this->page();
124
    }
125
126
    /**
127
     * @param int $billId
128
     *
129
     * @return mixed
130
     */
131
    public function addSettlement(int $billId)
132
    {
133
        if($this->session->closed)
134
        {
135
            $this->notify->warning(trans('meeting.warnings.session.closed'));
136
            return $this->response;
137
        }
138
139
        $this->settlementService->createSettlement($this->charge, $this->session, $billId);
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...ice::createSettlement() 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

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

139
        $this->settlementService->createSettlement($this->charge, /** @scrutinizer ignore-type */ $this->session, $billId);
Loading history...
140
141
        // Refresh the amounts available
142
        $this->cl(Loan::class)->refreshAmount($this->session);
143
        $this->cl(Disbursement::class)->refreshAmount($this->session);
144
145
        return $this->page();
146
    }
147
148
    /**
149
     * @param int $billId
150
     *
151
     * @return mixed
152
     */
153
    public function delSettlement(int $billId)
154
    {
155
        if($this->session->closed)
156
        {
157
            $this->notify->warning(trans('meeting.warnings.session.closed'));
158
            return $this->response;
159
        }
160
161
        $this->settlementService->deleteSettlement($this->charge, $this->session, $billId);
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...ice::deleteSettlement() 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->settlementService->deleteSettlement($this->charge, /** @scrutinizer ignore-type */ $this->session, $billId);
Loading history...
Bug introduced by
It seems like $this->charge can also be of type null; however, parameter $charge of Siak\Tontine\Service\Mee...ice::deleteSettlement() 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->settlementService->deleteSettlement(/** @scrutinizer ignore-type */ $this->charge, $this->session, $billId);
Loading history...
162
163
        // Refresh the amounts available
164
        $this->cl(Loan::class)->refreshAmount($this->session);
165
        $this->cl(Disbursement::class)->refreshAmount($this->session);
166
167
        return $this->page();
168
    }
169
}
170