fix enrollment, invoice, payment and financila aid
This commit is contained in:
@@ -3,9 +3,12 @@
|
||||
namespace App\Controllers\Administrator;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\FinancialAidEstimateModel;
|
||||
use App\Models\FinancialAidRequestModel;
|
||||
use App\Models\InvoiceModel;
|
||||
use App\Models\StudentModel;
|
||||
use App\Models\UserModel;
|
||||
use InvalidArgumentException;
|
||||
use Throwable;
|
||||
|
||||
class FinancialAidController extends BaseController
|
||||
@@ -49,13 +52,68 @@ class FinancialAidController extends BaseController
|
||||
? (new StudentModel())->whereIn('id', $studentIds)->findAll()
|
||||
: [];
|
||||
|
||||
$schoolYear = (string) ($request['school_year'] ?? '');
|
||||
$invoiceBalance = $this->invoiceBalanceForParent((int) ($request['parent_id'] ?? 0), $schoolYear);
|
||||
$householdSize = max(1, (int) ($request['household_size'] ?? 1));
|
||||
$householdIncome = (float) ($request['household_income'] ?? 0);
|
||||
$requestedAmount = (float) ($request['requested_amount'] ?? 0);
|
||||
$estimateModel = new FinancialAidEstimateModel();
|
||||
$estimatedAid = $estimateModel->estimatedAid($invoiceBalance, $householdSize, $householdIncome);
|
||||
$finalRebate = $estimateModel->finalRebate($invoiceBalance, $householdSize, $householdIncome, $requestedAmount);
|
||||
|
||||
return view('administrator/financial_aid_review', [
|
||||
'requestRow' => $request,
|
||||
'parent' => $parent,
|
||||
'students' => $students,
|
||||
'schoolYear' => $schoolYear,
|
||||
'invoiceBalance' => $invoiceBalance,
|
||||
'estimatedAid' => $estimatedAid,
|
||||
'finalRebate' => $finalRebate,
|
||||
]);
|
||||
}
|
||||
|
||||
public function estimate(int $id)
|
||||
{
|
||||
$request = (new FinancialAidRequestModel())->find($id);
|
||||
if ($request === null) {
|
||||
return $this->response->setStatusCode(404)->setJSON(['error' => 'Financial aid request was not found.']);
|
||||
}
|
||||
|
||||
$householdIncomeRaw = trim((string) $this->request->getPost('household_income'));
|
||||
$householdSizeRaw = (int) $this->request->getPost('household_size');
|
||||
$schoolYear = (string) ($request['school_year'] ?? '');
|
||||
$invoiceBalance = $this->invoiceBalanceForParent((int) ($request['parent_id'] ?? 0), $schoolYear);
|
||||
|
||||
if ($householdIncomeRaw === '' || ! is_numeric($householdIncomeRaw) || (float) $householdIncomeRaw < 0) {
|
||||
return $this->response->setStatusCode(422)->setJSON(['error' => 'Enter a valid household income.']);
|
||||
}
|
||||
if ($householdSizeRaw < 1) {
|
||||
return $this->response->setStatusCode(422)->setJSON(['error' => 'Household size must be at least 1.']);
|
||||
}
|
||||
|
||||
try {
|
||||
$estimateModel = new FinancialAidEstimateModel();
|
||||
$householdIncome = round((float) $householdIncomeRaw, 2);
|
||||
$estimatedAid = $estimateModel->estimatedAid($invoiceBalance, $householdSizeRaw, $householdIncome);
|
||||
$finalRebate = $estimateModel->finalRebate(
|
||||
$invoiceBalance,
|
||||
$householdSizeRaw,
|
||||
$householdIncome,
|
||||
(float) ($request['requested_amount'] ?? 0)
|
||||
);
|
||||
|
||||
return $this->response->setJSON([
|
||||
'invoice_balance' => $invoiceBalance,
|
||||
'estimated_aid' => $estimatedAid,
|
||||
'final_rebate' => $finalRebate,
|
||||
'csrf_token' => csrf_token(),
|
||||
'csrf_hash' => csrf_hash(),
|
||||
]);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return $this->response->setStatusCode(422)->setJSON(['error' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
public function approve(int $id)
|
||||
{
|
||||
try {
|
||||
@@ -113,4 +171,23 @@ class FinancialAidController extends BaseController
|
||||
|
||||
return (float) ($request['requested_amount'] ?? 0);
|
||||
}
|
||||
|
||||
private function invoiceBalanceForParent(int $parentId, string $schoolYear): float
|
||||
{
|
||||
if ($parentId <= 0 || $schoolYear === '') {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
$invoice = (new InvoiceModel())
|
||||
->where('parent_id', $parentId)
|
||||
->where('school_year', $schoolYear)
|
||||
->orderBy('id', 'DESC')
|
||||
->first();
|
||||
|
||||
if ($invoice === null) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return round((float) ($invoice['balance'] ?? 0), 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user