|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Ajax\Web\Meeting\Credit; |
|
4
|
|
|
|
|
5
|
|
|
use App\Ajax\CallableSessionClass; |
|
6
|
|
|
use App\Ajax\Web\Meeting\Session\Session; |
|
7
|
|
|
use Siak\Tontine\Model\Session as SessionModel; |
|
8
|
|
|
use Siak\Tontine\Service\Meeting\Credit\LoanService; |
|
9
|
|
|
use Siak\Tontine\Service\Tontine\FundService; |
|
10
|
|
|
use Siak\Tontine\Service\Tontine\MemberService; |
|
11
|
|
|
use Siak\Tontine\Validation\Meeting\LoanValidator; |
|
12
|
|
|
|
|
13
|
|
|
use function Jaxon\jq; |
|
14
|
|
|
use function Jaxon\pm; |
|
15
|
|
|
use function trans; |
|
16
|
|
|
|
|
17
|
|
|
class Loan extends CallableSessionClass |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var LoanValidator |
|
21
|
|
|
*/ |
|
22
|
|
|
protected LoanValidator $validator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The constructor |
|
26
|
|
|
* |
|
27
|
|
|
* @param LoanService $loanService |
|
28
|
|
|
* @param FundService $fundService |
|
29
|
|
|
* @param MemberService $memberService |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(protected LoanService $loanService, |
|
32
|
|
|
protected FundService $fundService, protected MemberService $memberService) |
|
33
|
|
|
{} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @exclude |
|
37
|
|
|
*/ |
|
38
|
|
|
public function show(SessionModel $session) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->session = $session; |
|
41
|
|
|
|
|
42
|
|
|
return $this->home(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function home() |
|
46
|
|
|
{ |
|
47
|
|
|
$loans = $this->loanService->getSessionLoans($this->session); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$html = $this->render('pages.meeting.loan.home', [ |
|
50
|
|
|
'session' => $this->session, |
|
51
|
|
|
'loans' => $loans, |
|
52
|
|
|
'defaultFund' => $this->fundService->getDefaultFund(), |
|
53
|
|
|
]); |
|
54
|
|
|
$this->response->html('meeting-loans', $html); |
|
|
|
|
|
|
55
|
|
|
$this->response->call('makeTableResponsive', 'meeting-loans'); |
|
56
|
|
|
|
|
57
|
|
|
$this->jq('#btn-loans-refresh')->click($this->rq()->home()); |
|
58
|
|
|
$this->jq('#btn-loan-add')->click($this->rq()->addLoan()); |
|
59
|
|
|
$this->jq('#btn-loan-balances') |
|
60
|
|
|
->click($this->rq(Session::class)->showBalanceDetails(true)); |
|
61
|
|
|
$loanId = jq()->parent()->attr('data-loan-id')->toInt(); |
|
62
|
|
|
$this->jq('.btn-loan-edit')->click($this->rq()->editLoan($loanId)); |
|
63
|
|
|
$this->jq('.btn-loan-delete')->click($this->rq()->deleteLoan($loanId) |
|
64
|
|
|
->confirm(trans('meeting.loan.questions.delete'))); |
|
65
|
|
|
|
|
66
|
|
|
return $this->response; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function addLoan() |
|
70
|
|
|
{ |
|
71
|
|
|
if($this->session->closed) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->notify->warning(trans('meeting.warnings.session.closed')); |
|
74
|
|
|
return $this->response; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$title = trans('meeting.loan.titles.add'); |
|
78
|
|
|
$content = $this->render('pages.meeting.loan.add', [ |
|
79
|
|
|
'amountAvailable' => $this->loanService->getAmountAvailableValue($this->session), |
|
|
|
|
|
|
80
|
|
|
'interestTypes' => $this->loanService->getInterestTypes(), |
|
81
|
|
|
'funds' => $this->fundService->getFundList(), |
|
82
|
|
|
'members' => $this->memberService->getMemberList(), |
|
83
|
|
|
]); |
|
84
|
|
|
$buttons = [[ |
|
85
|
|
|
'title' => trans('common.actions.cancel'), |
|
86
|
|
|
'class' => 'btn btn-tertiary', |
|
87
|
|
|
'click' => 'close', |
|
88
|
|
|
],[ |
|
89
|
|
|
'title' => trans('common.actions.save'), |
|
90
|
|
|
'class' => 'btn btn-primary', |
|
91
|
|
|
'click' => $this->rq()->createLoan(pm()->form('loan-form')), |
|
92
|
|
|
]]; |
|
93
|
|
|
$this->dialog->show($title, $content, $buttons); |
|
|
|
|
|
|
94
|
|
|
$this->response->script('setLoanInterestLabel()'); |
|
95
|
|
|
|
|
96
|
|
|
return $this->response; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @di $validator |
|
101
|
|
|
* @after showBalanceAmounts |
|
102
|
|
|
*/ |
|
103
|
|
|
public function createLoan(array $formValues) |
|
104
|
|
|
{ |
|
105
|
|
|
if($this->session->closed) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->notify->warning(trans('meeting.warnings.session.closed')); |
|
108
|
|
|
return $this->response; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$values = $this->validator->validateItem($formValues); |
|
112
|
|
|
if(!($member = $this->memberService->getMember($values['member']))) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->notify->warning(trans('tontine.member.errors.not_found')); |
|
115
|
|
|
return $this->response; |
|
116
|
|
|
} |
|
117
|
|
|
if(!($fund = $this->fundService->getFund($values['fund'], true, true))) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->notify->warning(trans('tontine.fund.errors.not_found')); |
|
120
|
|
|
return $this->response; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->loanService->createLoan($this->session, $member, $fund, $values); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$this->dialog->hide(); |
|
126
|
|
|
|
|
127
|
|
|
return $this->home(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function editLoan(int $loanId) |
|
131
|
|
|
{ |
|
132
|
|
|
if($this->session->closed) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->notify->warning(trans('meeting.warnings.session.closed')); |
|
135
|
|
|
return $this->response; |
|
136
|
|
|
} |
|
137
|
|
|
$loan = $this->loanService->getSessionLoan($this->session, $loanId); |
|
|
|
|
|
|
138
|
|
|
if(!$loan) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->notify->warning(trans('meeting.loan.errors.not_found')); |
|
141
|
|
|
return $this->response; |
|
142
|
|
|
} |
|
143
|
|
|
// A refunded loan cannot be updated. |
|
144
|
|
|
if($loan->refunds_count > 0) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->notify->warning(trans('meeting.loan.errors.update')); |
|
147
|
|
|
return $this->response; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$title = trans('meeting.loan.titles.edit'); |
|
151
|
|
|
$content = $this->render('pages.meeting.loan.edit', [ |
|
152
|
|
|
'loan' => $loan, |
|
153
|
|
|
'interestTypes' => $this->loanService->getInterestTypes(), |
|
154
|
|
|
'funds' => $this->fundService->getFundList(), |
|
155
|
|
|
'members' => $this->memberService->getMemberList(), |
|
156
|
|
|
]); |
|
157
|
|
|
$buttons = [[ |
|
158
|
|
|
'title' => trans('common.actions.cancel'), |
|
159
|
|
|
'class' => 'btn btn-tertiary', |
|
160
|
|
|
'click' => 'close', |
|
161
|
|
|
],[ |
|
162
|
|
|
'title' => trans('common.actions.save'), |
|
163
|
|
|
'class' => 'btn btn-primary', |
|
164
|
|
|
'click' => $this->rq()->updateLoan($loanId, pm()->form('loan-form')), |
|
165
|
|
|
]]; |
|
166
|
|
|
$this->dialog->show($title, $content, $buttons); |
|
|
|
|
|
|
167
|
|
|
$this->response->script('setLoanInterestLabel()'); |
|
168
|
|
|
|
|
169
|
|
|
return $this->response; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @di $validator |
|
174
|
|
|
* @after showBalanceAmounts |
|
175
|
|
|
*/ |
|
176
|
|
|
public function updateLoan(int $loanId, array $formValues) |
|
177
|
|
|
{ |
|
178
|
|
|
if($this->session->closed) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->notify->warning(trans('meeting.warnings.session.closed')); |
|
181
|
|
|
return $this->response; |
|
182
|
|
|
} |
|
183
|
|
|
$loan = $this->loanService->getSessionLoan($this->session, $loanId); |
|
|
|
|
|
|
184
|
|
|
if(!$loan) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->notify->warning(trans('meeting.loan.errors.not_found')); |
|
187
|
|
|
return $this->response; |
|
188
|
|
|
} |
|
189
|
|
|
// A refunded loan cannot be updated. |
|
190
|
|
|
if($loan->refunds_count > 0) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->notify->warning(trans('meeting.loan.errors.update')); |
|
193
|
|
|
return $this->response; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$values = $this->validator->validateItem($formValues); |
|
197
|
|
|
if(!($member = $this->memberService->getMember($values['member']))) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->notify->warning(trans('tontine.member.errors.not_found')); |
|
200
|
|
|
return $this->response; |
|
201
|
|
|
} |
|
202
|
|
|
if(!($fund = $this->fundService->getFund($values['fund'], true, true))) |
|
203
|
|
|
{ |
|
204
|
|
|
$this->notify->warning(trans('tontine.fund.errors.not_found')); |
|
205
|
|
|
return $this->response; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$this->loanService->updateLoan($member, $fund, $loan, $values); |
|
209
|
|
|
|
|
210
|
|
|
$this->dialog->hide(); |
|
211
|
|
|
|
|
212
|
|
|
return $this->home(); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @after showBalanceAmounts |
|
217
|
|
|
*/ |
|
218
|
|
|
public function deleteLoan(int $loanId) |
|
219
|
|
|
{ |
|
220
|
|
|
if($this->session->closed) |
|
221
|
|
|
{ |
|
222
|
|
|
$this->notify->warning(trans('meeting.warnings.session.closed')); |
|
223
|
|
|
return $this->response; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
$this->loanService->deleteLoan($this->session, $loanId); |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
return $this->home(); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|