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); |
|
|
|
|
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); |
|
|
|
|
58
|
|
|
$settlementCount = $settlement->total ?? 0; |
|
|
|
|
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); |
|
|
|
|
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, |
|
|
|
|
78
|
|
|
$this->session, $search, $filter); |
|
|
|
|
79
|
|
|
[$pageNumber, $perPage] = $this->pageNumber($pageNumber, $memberCount, |
80
|
|
|
'meeting', 'member.page'); |
81
|
|
|
$members = $this->billService->getMembers($this->charge, $this->session, |
|
|
|
|
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); |
|
|
|
|
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, |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
206
|
|
|
return $this->page(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$this->billService->updateBill($this->charge, $this->session, $memberId, $amount); |
|
|
|
|
210
|
|
|
return $this->page(); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|