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

Pool::showTotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Pool\Deposit;
4
5
use App\Ajax\CallableSessionClass;
6
use App\Ajax\Web\Meeting\Pool\Deposit;
7
use Siak\Tontine\Model\Pool as PoolModel;
8
use Siak\Tontine\Service\BalanceCalculator;
9
use Siak\Tontine\Service\LocaleService;
10
use Siak\Tontine\Service\Meeting\Pool\DepositService;
11
use Siak\Tontine\Service\Meeting\Pool\PoolService;
12
13
use function Jaxon\jq;
14
use function filter_var;
15
use function str_replace;
16
use function trans;
17
use function trim;
18
19
/**
20
 * @before getPool
21
 */
22
class Pool extends CallableSessionClass
23
{
24
    /**
25
     * @var LocaleService
26
     */
27
    protected LocaleService $localeService;
28
29
    /**
30
     * @var PoolModel|null
31
     */
32
    protected ?PoolModel $pool;
33
34
    /**
35
     * The constructor
36
     *
37
     * @param BalanceCalculator $balanceCalculator
38
     * @param PoolService $poolService
39
     * @param DepositService $depositService
40
     */
41
    public function __construct(private BalanceCalculator $balanceCalculator,
42
        protected PoolService $poolService, protected DepositService $depositService)
43
    {}
44
45
    protected function getPool()
46
    {
47
        $poolId = $this->target()->method() === 'home' ?
48
            $this->target()->args()[0] : $this->bag('meeting')->get('pool.id');
49
        $this->pool = $this->poolService->getPool($poolId);
0 ignored issues
show
Bug introduced by
It seems like $poolId can also be of type null; however, parameter $poolId of Siak\Tontine\Service\Mee...\PoolService::getPool() 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

49
        $this->pool = $this->poolService->getPool(/** @scrutinizer ignore-type */ $poolId);
Loading history...
50
51
        if(!$this->session || !$this->pool || $this->session->disabled($this->pool))
52
        {
53
            $this->notify->error(trans('tontine.session.errors.disabled'), trans('common.titles.error'));
54
            $this->pool = null;
55
        }
56
    }
57
58
    private function showTotal()
59
    {
60
        $depositCount = $this->depositService->countDeposits($this->pool, $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::countDeposits() 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

60
        $depositCount = $this->depositService->countDeposits($this->pool, /** @scrutinizer ignore-type */ $this->session);
Loading history...
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...ervice::countDeposits() does only seem to accept Siak\Tontine\Model\Pool, 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

60
        $depositCount = $this->depositService->countDeposits(/** @scrutinizer ignore-type */ $this->pool, $this->session);
Loading history...
61
        $html = $this->render('pages.meeting.deposit.pool.total', [
62
            'depositCount' => $depositCount,
63
            'depositAmount' => $this->balanceCalculator
64
                ->getPoolDepositAmount($this->pool, $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\Bal...:getPoolDepositAmount() 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

64
                ->getPoolDepositAmount($this->pool, /** @scrutinizer ignore-type */ $this->session),
Loading history...
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Bal...:getPoolDepositAmount() does only seem to accept Siak\Tontine\Model\Pool, 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

64
                ->getPoolDepositAmount(/** @scrutinizer ignore-type */ $this->pool, $this->session),
Loading history...
65
        ]);
66
        $this->response->html('meeting-deposits-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

66
        $this->response->html('meeting-deposits-total', /** @scrutinizer ignore-type */ $html);
Loading history...
67
68
        $html = $this->render('pages.meeting.deposit.pool.action', [
69
            'session' => $this->session,
70
            'depositCount' => $depositCount,
71
            'receivableCount' => $this->depositService
72
                ->getReceivableCount($this->pool, $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::getReceivableCount() 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

72
                ->getReceivableCount($this->pool, /** @scrutinizer ignore-type */ $this->session),
Loading history...
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...e::getReceivableCount() does only seem to accept Siak\Tontine\Model\Pool, 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

72
                ->getReceivableCount(/** @scrutinizer ignore-type */ $this->pool, $this->session),
Loading history...
73
        ]);
74
        $this->response->html('meeting-deposits-action', $html);
75
    }
76
77
    /**
78
     * @param int $poolId
79
     *
80
     * @return mixed
81
     */
82
    public function home(int $poolId)
83
    {
84
        $this->bag('meeting')->set('pool.id', $poolId);
85
86
        $html = $this->render('pages.meeting.deposit.pool.home', [
87
            'pool' => $this->pool,
88
        ]);
89
        $this->response->html('meeting-deposits', $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-deposits', /** @scrutinizer ignore-type */ $html);
Loading history...
90
91
        $this->jq('#btn-deposits-back')->click($this->rq(Deposit::class)->home());
92
93
        return $this->page(1);
94
    }
95
96
    /**
97
     * @param int $pageNumber
98
     *
99
     * @return mixed
100
     */
101
    public function page(int $pageNumber = 0)
102
    {
103
        $receivableCount = $this->depositService->getReceivableCount($this->pool, $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::getReceivableCount() 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

103
        $receivableCount = $this->depositService->getReceivableCount($this->pool, /** @scrutinizer ignore-type */ $this->session);
Loading history...
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...e::getReceivableCount() does only seem to accept Siak\Tontine\Model\Pool, 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
        $receivableCount = $this->depositService->getReceivableCount(/** @scrutinizer ignore-type */ $this->pool, $this->session);
Loading history...
104
        [$pageNumber, $perPage] = $this->pageNumber($pageNumber, $receivableCount, 'meeting', 'deposit.page');
105
        $receivables = $this->depositService->getReceivables($this->pool, $this->session, $pageNumber);
0 ignored issues
show
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...rvice::getReceivables() does only seem to accept Siak\Tontine\Model\Pool, 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

105
        $receivables = $this->depositService->getReceivables(/** @scrutinizer ignore-type */ $this->pool, $this->session, $pageNumber);
Loading history...
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...rvice::getReceivables() 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

105
        $receivables = $this->depositService->getReceivables($this->pool, /** @scrutinizer ignore-type */ $this->session, $pageNumber);
Loading history...
106
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $receivableCount);
107
108
        $this->showTotal();
109
110
        $html = $this->render('pages.meeting.deposit.pool.page', [
111
            'pool' => $this->pool,
112
            'session' => $this->session,
113
            'receivables' => $receivables,
114
            'pagination' => $pagination,
115
        ]);
116
        $this->response->html('meeting-pool-deposits', $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

116
        $this->response->html('meeting-pool-deposits', /** @scrutinizer ignore-type */ $html);
Loading history...
117
        $this->response->call('makeTableResponsive', 'meeting-pool-deposits');
118
119
        $this->jq('.btn-add-all-deposits')->click($this->rq()->addAllDeposits());
120
        $this->jq('.btn-del-all-deposits')->click($this->rq()->delAllDeposits());
121
        $receivableId = jq()->parent()->attr('data-receivable-id')->toInt();
122
        $amount = jq('input', jq()->parent()->parent())->val()->toInt();
123
        $this->jq('.btn-add-deposit')->click($this->rq()->addDeposit($receivableId));
124
        $this->jq('.btn-del-deposit')->click($this->rq()->delDeposit($receivableId));
125
        $this->jq('.btn-save-deposit')->click($this->rq()->saveAmount($receivableId, $amount));
126
        $this->jq('.btn-edit-deposit')->click($this->rq()->editAmount($receivableId));
127
128
        return $this->response;
129
    }
130
131
    /**
132
     * @param int $receivableId
133
     *
134
     * @return mixed
135
     */
136
    public function addDeposit(int $receivableId)
137
    {
138
        if($this->session->closed)
139
        {
140
            $this->notify->warning(trans('meeting.warnings.session.closed'));
141
            return $this->response;
142
        }
143
144
        $this->depositService->createDeposit($this->pool, $this->session, $receivableId);
0 ignored issues
show
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...ervice::createDeposit() does only seem to accept Siak\Tontine\Model\Pool, 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

144
        $this->depositService->createDeposit(/** @scrutinizer ignore-type */ $this->pool, $this->session, $receivableId);
Loading history...
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...ervice::createDeposit() 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

144
        $this->depositService->createDeposit($this->pool, /** @scrutinizer ignore-type */ $this->session, $receivableId);
Loading history...
145
146
        return $this->page();
147
    }
148
149
    /**
150
     * @di $localeService
151
     * @param int $receivableId
152
     *
153
     * @return mixed
154
     */
155
    public function editAmount(int $receivableId)
156
    {
157
        if($this->session->closed)
158
        {
159
            $this->notify->warning(trans('meeting.warnings.session.closed'));
160
            return $this->response;
161
        }
162
        $receivable = $this->depositService->getReceivable($this->pool, $this->session, $receivableId);
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::getReceivable() 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

162
        $receivable = $this->depositService->getReceivable($this->pool, /** @scrutinizer ignore-type */ $this->session, $receivableId);
Loading history...
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...ervice::getReceivable() does only seem to accept Siak\Tontine\Model\Pool, 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

162
        $receivable = $this->depositService->getReceivable(/** @scrutinizer ignore-type */ $this->pool, $this->session, $receivableId);
Loading history...
163
        if(!$receivable || !$receivable->deposit)
164
        {
165
            return $this->page();
166
        }
167
168
        $html = $this->render('pages.meeting.deposit.libre.edit', [
169
            'id' => $receivable->id,
170
            'amount' => !$receivable->deposit ? '' :
171
                $this->localeService->getMoneyValue($receivable->deposit->amount),
172
        ]);
173
        $fieldId = 'receivable-' . $receivable->id;
174
        $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

174
        $this->response->html($fieldId, /** @scrutinizer ignore-type */ $html);
Loading history...
175
176
        $receivableId = jq()->parent()->attr('data-receivable-id')->toInt();
177
        $amount = jq('input', jq()->parent()->parent())->val();
178
        $this->jq('.btn-save-deposit', "#$fieldId")->click($this->rq()->saveAmount($receivableId, $amount));
179
180
        return $this->response;
181
    }
182
183
    /**
184
     * @di $localeService
185
     * @param int $receivableId
186
     * @param string $amount
187
     *
188
     * @return mixed
189
     */
190
    public function saveAmount(int $receivableId, string $amount)
191
    {
192
        if($this->session->closed)
193
        {
194
            $this->notify->warning(trans('meeting.warnings.session.closed'));
195
            return $this->response;
196
        }
197
        $amount = str_replace(',', '.', trim($amount));
198
        if($amount !== '' && filter_var($amount, FILTER_VALIDATE_FLOAT) === false)
199
        {
200
            $this->notify->error(trans('meeting.errors.amount.invalid', ['amount' => $amount]));
201
            return $this->response;
202
        }
203
        $amount = $amount === '' ? 0 : $this->localeService->convertMoneyToInt((float)$amount);
204
205
        $amount > 0 ?
206
            $this->depositService->saveDepositAmount($this->pool, $this->session, $receivableId, $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...ce::saveDepositAmount() 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
            $this->depositService->saveDepositAmount($this->pool, /** @scrutinizer ignore-type */ $this->session, $receivableId, $amount):
Loading history...
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...ce::saveDepositAmount() does only seem to accept Siak\Tontine\Model\Pool, 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
            $this->depositService->saveDepositAmount(/** @scrutinizer ignore-type */ $this->pool, $this->session, $receivableId, $amount):
Loading history...
207
            $this->depositService->deleteDeposit($this->pool, $this->session, $receivableId);
0 ignored issues
show
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...ervice::deleteDeposit() does only seem to accept Siak\Tontine\Model\Pool, 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

207
            $this->depositService->deleteDeposit(/** @scrutinizer ignore-type */ $this->pool, $this->session, $receivableId);
Loading history...
Bug introduced by
It seems like $this->session can also be of type null; however, parameter $session of Siak\Tontine\Service\Mee...ervice::deleteDeposit() 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

207
            $this->depositService->deleteDeposit($this->pool, /** @scrutinizer ignore-type */ $this->session, $receivableId);
Loading history...
208
209
        return $this->page();
210
    }
211
212
    /**
213
     * @param int $receivableId
214
     *
215
     * @return mixed
216
     */
217
    public function delDeposit(int $receivableId)
218
    {
219
        if($this->session->closed)
220
        {
221
            $this->notify->warning(trans('meeting.warnings.session.closed'));
222
            return $this->response;
223
        }
224
225
        $this->depositService->deleteDeposit($this->pool, $this->session, $receivableId);
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::deleteDeposit() 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

225
        $this->depositService->deleteDeposit($this->pool, /** @scrutinizer ignore-type */ $this->session, $receivableId);
Loading history...
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...ervice::deleteDeposit() does only seem to accept Siak\Tontine\Model\Pool, 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

225
        $this->depositService->deleteDeposit(/** @scrutinizer ignore-type */ $this->pool, $this->session, $receivableId);
Loading history...
226
227
        return $this->page();
228
    }
229
230
    /**
231
     * @return mixed
232
     */
233
    public function addAllDeposits()
234
    {
235
        if($this->session->closed)
236
        {
237
            $this->notify->warning(trans('meeting.warnings.session.closed'));
238
            return $this->response;
239
        }
240
        if(!$this->pool->deposit_fixed)
241
        {
242
            return $this->response;
243
        }
244
245
        $this->depositService->createAllDeposits($this->pool, $this->session);
0 ignored issues
show
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...ce::createAllDeposits() does only seem to accept Siak\Tontine\Model\Pool, 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->depositService->createAllDeposits(/** @scrutinizer ignore-type */ $this->pool, $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...ce::createAllDeposits() 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->depositService->createAllDeposits($this->pool, /** @scrutinizer ignore-type */ $this->session);
Loading history...
246
247
        return $this->page();
248
    }
249
250
    /**
251
     * @return mixed
252
     */
253
    public function delAllDeposits()
254
    {
255
        if($this->session->closed)
256
        {
257
            $this->notify->warning(trans('meeting.warnings.session.closed'));
258
            return $this->response;
259
        }
260
        if(!$this->pool->deposit_fixed)
261
        {
262
            return $this->response;
263
        }
264
265
        $this->depositService->deleteAllDeposits($this->pool, $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...ce::deleteAllDeposits() 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

265
        $this->depositService->deleteAllDeposits($this->pool, /** @scrutinizer ignore-type */ $this->session);
Loading history...
Bug introduced by
It seems like $this->pool can also be of type null; however, parameter $pool of Siak\Tontine\Service\Mee...ce::deleteAllDeposits() does only seem to accept Siak\Tontine\Model\Pool, 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

265
        $this->depositService->deleteAllDeposits(/** @scrutinizer ignore-type */ $this->pool, $this->session);
Loading history...
266
267
        return $this->page();
268
    }
269
}
270