fix test and add invocie to parent enrollment
This commit is contained in:
@@ -481,6 +481,8 @@ class ParentController extends BaseController
|
||||
// Handle enrollments
|
||||
$studentData = [];
|
||||
$enrollmentResultMessages = [];
|
||||
$invoiceResultMessage = null;
|
||||
$invoiceErrorMessage = null;
|
||||
|
||||
if (!empty($enroll)) {
|
||||
$selectedYear = $this->currentSchoolYearName((string) ($this->schoolYear ?? ''));
|
||||
@@ -713,17 +715,34 @@ class ParentController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($enroll) && empty($withdraw)) {
|
||||
$invoiceResult = $this->generateInvoiceForParentEnrollment((int) $parentId);
|
||||
if (! empty($invoiceResult['ok'])) {
|
||||
$invoiceResultMessage = (string) ($invoiceResult['message'] ?? 'Invoice generated.');
|
||||
} else {
|
||||
$invoiceErrorMessage = (string) ($invoiceResult['message'] ?? 'Enrollment was submitted, but the invoice could not be generated.');
|
||||
}
|
||||
}
|
||||
|
||||
$parentData = $this->userModel->getUserInfoById($parentId);
|
||||
$parentData['user_id'] = $parentId;
|
||||
// Redirect to the success page after processing enrollment and withdrawal
|
||||
if (!empty($withdraw)) {
|
||||
// Redirect to withdrawal success page if there are withdrawals //parent/enroll_classes
|
||||
$redirect = redirect()->to('/parent/enroll_classes');
|
||||
$successParts = [];
|
||||
$errorParts = [];
|
||||
if ($withdrawalResultMessages !== []) {
|
||||
$redirect = $redirect->with('success', 'Withdrawal request submitted. ' . implode(' ', $withdrawalResultMessages));
|
||||
$successParts[] = 'Withdrawal request submitted. ' . implode(' ', $withdrawalResultMessages);
|
||||
}
|
||||
if ($withdrawalErrors !== []) {
|
||||
$redirect = $redirect->with('error', 'Some withdrawal requests failed. ' . implode(' ', $withdrawalErrors));
|
||||
$errorParts[] = 'Some withdrawal requests failed. ' . implode(' ', $withdrawalErrors);
|
||||
}
|
||||
if ($successParts !== []) {
|
||||
$redirect = $redirect->with('success', implode(' ', $successParts));
|
||||
}
|
||||
if ($errorParts !== []) {
|
||||
$redirect = $redirect->with('error', implode(' ', $errorParts));
|
||||
}
|
||||
|
||||
return $redirect;
|
||||
@@ -742,11 +761,76 @@ class ParentController extends BaseController
|
||||
if ($enrollmentResultMessages !== []) {
|
||||
$successMessage .= ' ' . implode(' ', $enrollmentResultMessages);
|
||||
}
|
||||
if ($invoiceResultMessage !== null) {
|
||||
$successMessage .= ' ' . $invoiceResultMessage;
|
||||
}
|
||||
|
||||
return redirect()->to('/parent/enroll_success')->with('success', $successMessage);
|
||||
$redirect = redirect()->to('/parent/enroll_success')->with('success', $successMessage);
|
||||
if ($invoiceErrorMessage !== null) {
|
||||
$redirect = $redirect->with('error', $invoiceErrorMessage);
|
||||
}
|
||||
|
||||
return $redirect;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate or refresh the parent's school-year invoice after parent-submitted enrollment.
|
||||
*
|
||||
* The invoice engine is intentionally authoritative for billable statuses. For example,
|
||||
* first-time students still under admission review may not produce billable lines yet.
|
||||
*
|
||||
* @return array{ok: bool, message: string}
|
||||
*/
|
||||
private function generateInvoiceForParentEnrollment(int $parentId): array
|
||||
{
|
||||
if ($parentId <= 0) {
|
||||
return ['ok' => false, 'message' => 'Enrollment was submitted, but the parent invoice could not be generated.'];
|
||||
}
|
||||
|
||||
$schoolYear = $this->currentSchoolYearName((string) ($this->schoolYear ?? ''));
|
||||
$semester = (string) ($this->semester ?? getSemester());
|
||||
|
||||
try {
|
||||
$result = $this->eventController->generateInvoice((string) $parentId, $schoolYear, $semester);
|
||||
} catch (Throwable $e) {
|
||||
log_message('error', 'Invoice generation failed after parent enrollment: {message}', [
|
||||
'message' => $e->getMessage(),
|
||||
'parentId' => $parentId,
|
||||
'schoolYear' => $schoolYear,
|
||||
'semester' => $semester,
|
||||
]);
|
||||
|
||||
return ['ok' => false, 'message' => 'Enrollment was submitted, but the invoice could not be generated. Please contact the school administration.'];
|
||||
}
|
||||
|
||||
if (is_array($result) && ! empty($result['ok'])) {
|
||||
return [
|
||||
'ok' => true,
|
||||
'message' => ! empty($result['updated']) ? 'Invoice updated.' : 'Invoice generated.',
|
||||
];
|
||||
}
|
||||
|
||||
$message = is_array($result) ? (string) ($result['message'] ?? '') : '';
|
||||
if ($message === 'Invoice requires at least one non-zero line.') {
|
||||
log_message('info', 'No invoice generated after parent enrollment because no billable invoice lines exist yet for parent {parentId}, year {schoolYear}.', [
|
||||
'parentId' => $parentId,
|
||||
'schoolYear' => $schoolYear,
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'message' => 'No invoice was generated yet because there are no billable enrollment charges.'];
|
||||
}
|
||||
|
||||
log_message('error', 'Invoice generation returned an unsuccessful result after parent enrollment: {result}', [
|
||||
'result' => json_encode($result),
|
||||
'parentId' => $parentId,
|
||||
'schoolYear' => $schoolYear,
|
||||
'semester' => $semester,
|
||||
]);
|
||||
|
||||
return ['ok' => false, 'message' => 'Enrollment was submitted, but the invoice could not be generated. Please contact the school administration.'];
|
||||
}
|
||||
|
||||
private function enrollmentPayloadFromEvaluation(array $evaluation, array $base): array
|
||||
{
|
||||
$payload = array_merge($base, [
|
||||
|
||||
Reference in New Issue
Block a user