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); |
|
|
|
|
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); |
|
|
|
|
61
|
|
|
$html = $this->render('pages.meeting.deposit.pool.total', [ |
62
|
|
|
'depositCount' => $depositCount, |
63
|
|
|
'depositAmount' => $this->balanceCalculator |
64
|
|
|
->getPoolDepositAmount($this->pool, $this->session), |
|
|
|
|
65
|
|
|
]); |
66
|
|
|
$this->response->html('meeting-deposits-total', $html); |
|
|
|
|
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), |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
104
|
|
|
[$pageNumber, $perPage] = $this->pageNumber($pageNumber, $receivableCount, 'meeting', 'deposit.page'); |
105
|
|
|
$receivables = $this->depositService->getReceivables($this->pool, $this->session, $pageNumber); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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): |
|
|
|
|
207
|
|
|
$this->depositService->deleteDeposit($this->pool, $this->session, $receivableId); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
266
|
|
|
|
267
|
|
|
return $this->page(); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|