Passed
Push — main ( c89920...e11349 )
by Thierry
05:00
created

Pool::delAllDeposits()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Ajax\Web\Meeting\Pool\Deposit;
4
5
use App\Ajax\CallableClass;
6
use App\Ajax\Web\Meeting\Pool\Deposit;
7
use Siak\Tontine\Service\LocaleService;
8
use Siak\Tontine\Service\Meeting\Pool\DepositService;
9
use Siak\Tontine\Service\Meeting\Pool\PoolService;
10
use Siak\Tontine\Model\Session as SessionModel;
11
use Siak\Tontine\Model\Pool as PoolModel;
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
 * @databag meeting
21
 * @before getPool
22
 */
23
class Pool extends CallableClass
24
{
25
    /**
26
     * @var LocaleService
27
     */
28
    protected LocaleService $localeService;
29
30
    /**
31
     * @var DepositService
32
     */
33
    protected DepositService $depositService;
34
35
    /**
36
     * @var PoolService
37
     */
38
    protected PoolService $poolService;
39
40
    /**
41
     * @var SessionModel|null
42
     */
43
    protected ?SessionModel $session;
44
45
    /**
46
     * @var PoolModel|null
47
     */
48
    protected ?PoolModel $pool;
49
50
    /**
51
     * The constructor
52
     *
53
     * @param PoolService $poolService
54
     * @param DepositService $depositService
55
     */
56
    public function __construct(PoolService $poolService, DepositService $depositService)
57
    {
58
        $this->poolService = $poolService;
59
        $this->depositService = $depositService;
60
    }
61
62
    protected function getPool()
63
    {
64
        $sessionId = $this->bag('meeting')->get('session.id');
65
        $this->session = $this->depositService->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...itService::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

65
        $this->session = $this->depositService->getSession(/** @scrutinizer ignore-type */ $sessionId);
Loading history...
66
67
        $poolId = $this->target()->method() === 'home' ?
68
            $this->target()->args()[0] : $this->bag('meeting')->get('pool.id');
69
        $this->pool = $this->depositService->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...positService::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

69
        $this->pool = $this->depositService->getPool(/** @scrutinizer ignore-type */ $poolId);
Loading history...
70
71
        if(!$this->session || !$this->pool || $this->session->disabled($this->pool))
72
        {
73
            $this->notify->error(trans('tontine.session.errors.disabled'), trans('common.titles.error'));
74
            $this->pool = null;
75
        }
76
    }
77
78
    /**
79
     * @param int $poolId
80
     *
81
     * @return mixed
82
     */
83
    public function home(int $poolId)
84
    {
85
        $this->bag('meeting')->set('pool.id', $poolId);
86
87
        $html = $this->view()->render('tontine.pages.meeting.deposit.pool.home', [
88
            'pool' => $this->pool,
89
        ]);
90
        $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

90
        $this->response->html('meeting-deposits', /** @scrutinizer ignore-type */ $html);
Loading history...
91
        $this->jq('#btn-deposits-back')->click($this->cl(Deposit::class)->rq()->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->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...
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...
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->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...
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...
106
        $pagination = $this->rq()->page()->paginate($pageNumber, $perPage, $receivableCount);
107
108
        $html = $this->view()->render('tontine.pages.meeting.deposit.pool.page', [
109
            'pool' => $this->pool,
110
            'session' => $this->session,
111
            'receivableCount' => $receivableCount,
112
            'depositCount' => $this->depositService->countDeposits($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...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

112
            'depositCount' => $this->depositService->countDeposits(/** @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...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

112
            'depositCount' => $this->depositService->countDeposits($this->pool, /** @scrutinizer ignore-type */ $this->session),
Loading history...
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
118
        $this->jq('.btn-add-all-deposits')->click($this->rq()->addAllDeposits());
119
        $this->jq('.btn-del-all-deposits')->click($this->rq()->delAllDeposits());
120
        $receivableId = jq()->parent()->attr('data-receivable-id')->toInt();
121
        $amount = jq('input', jq()->parent()->parent())->val()->toInt();
122
        $this->jq('.btn-add-deposit')->click($this->rq()->addDeposit($receivableId));
123
        $this->jq('.btn-del-deposit')->click($this->rq()->delDeposit($receivableId));
124
        $this->jq('.btn-save-deposit')->click($this->rq()->saveAmount($receivableId, $amount));
125
        $this->jq('.btn-edit-deposit')->click($this->rq()->editAmount($receivableId));
126
127
        return $this->response;
128
    }
129
130
    /**
131
     * @param int $receivableId
132
     *
133
     * @return mixed
134
     */
135
    public function addDeposit(int $receivableId)
136
    {
137
        if($this->session->closed)
138
        {
139
            $this->notify->warning(trans('meeting.warnings.session.closed'));
140
            return $this->response;
141
        }
142
143
        $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

143
        $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

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

161
        $receivable = $this->depositService->getReceivable(/** @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::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

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

173
        $this->response->html($fieldId, /** @scrutinizer ignore-type */ $html);
Loading history...
174
175
        $receivableId = jq()->parent()->attr('data-receivable-id')->toInt();
176
        $amount = jq('input', jq()->parent()->parent())->val();
177
        $this->jq('.btn-save-deposit', "#$fieldId")->click($this->rq()->saveAmount($receivableId, $amount));
178
179
        return $this->response;
180
    }
181
182
    /**
183
     * @di $localeService
184
     * @param int $receivableId
185
     * @param string $amount
186
     *
187
     * @return mixed
188
     */
189
    public function saveAmount(int $receivableId, string $amount)
190
    {
191
        if($this->session->closed)
192
        {
193
            $this->notify->warning(trans('meeting.warnings.session.closed'));
194
            return $this->response;
195
        }
196
        $amount = str_replace(',', '.', trim($amount));
197
        if($amount !== '' && filter_var($amount, FILTER_VALIDATE_FLOAT) === false)
198
        {
199
            $this->notify->error(trans('meeting.errors.amount.invalid', ['amount' => $amount]));
200
            return $this->response;
201
        }
202
        $amount = $amount === '' ? 0 : $this->localeService->convertMoneyToInt((float)$amount);
203
204
        $amount > 0 ?
205
            $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

205
            $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

205
            $this->depositService->saveDepositAmount(/** @scrutinizer ignore-type */ $this->pool, $this->session, $receivableId, $amount):
Loading history...
206
            $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

206
            $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

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

224
        $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

224
        $this->depositService->deleteDeposit(/** @scrutinizer ignore-type */ $this->pool, $this->session, $receivableId);
Loading history...
225
226
        return $this->page();
227
    }
228
229
    /**
230
     * @return mixed
231
     */
232
    public function addAllDeposits()
233
    {
234
        if($this->session->closed)
235
        {
236
            $this->notify->warning(trans('meeting.warnings.session.closed'));
237
            return $this->response;
238
        }
239
        if(!$this->pool->deposit_fixed)
240
        {
241
            return $this->response;
242
        }
243
244
        $this->depositService->createAllDeposits($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::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

244
        $this->depositService->createAllDeposits($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::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

244
        $this->depositService->createAllDeposits(/** @scrutinizer ignore-type */ $this->pool, $this->session);
Loading history...
245
246
        return $this->page();
247
    }
248
249
    /**
250
     * @return mixed
251
     */
252
    public function delAllDeposits()
253
    {
254
        if($this->session->closed)
255
        {
256
            $this->notify->warning(trans('meeting.warnings.session.closed'));
257
            return $this->response;
258
        }
259
        if(!$this->pool->deposit_fixed)
260
        {
261
            return $this->response;
262
        }
263
264
        $this->depositService->deleteAllDeposits($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::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

264
        $this->depositService->deleteAllDeposits(/** @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::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

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