fixed enrolled after first payment
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 48s
Tests / PHPUnit (push) Successful in 1m19s

This commit is contained in:
root
2026-08-29 18:47:18 -04:00
parent 6cf3a607a4
commit 22c4cf6fd2
+17 -12
View File
@@ -522,7 +522,7 @@ class PaymentController extends ResourceController
return $this->response->setJSON(['items' => $items]);
}
private function updateEnrollmentStatusIfPaid(int $invoiceId): int
private function updateEnrollmentStatusAfterRecordedPayment(int $invoiceId): int
{
// 1) Fetch invoice
$invoice = $this->invoiceModel->find($invoiceId);
@@ -531,19 +531,19 @@ class PaymentController extends ResourceController
return 0;
}
// 2) Payment check: enrollment transitions only after the invoice is fully paid
$currentBal = $this->getCurrentInvoiceBalance($invoiceId);
if ($currentBal > 0.00001) {
log_message('info', 'Invoice not fully paid. Skipping enrollment update. Invoice #{id}', ['id' => $invoiceId]);
// 2) Payment check: any successful payment, including the first installment, enrolls the student.
if ($this->getSuccessfulPaymentCount($invoiceId) < 1) {
log_message('info', 'No successful payment found. Skipping enrollment update. Invoice #{id}', ['id' => $invoiceId]);
return 0;
}
$parentId = (int) ($invoice['parent_id'] ?? 0);
$schoolYear = (string) ($invoice['school_year'] ?? $this->schoolYear);
$semester = isset($this->semester) && $this->semester !== '' ? (string)$this->semester : null;
$semester = trim((string) ($invoice['semester'] ?? ''));
$semester = $semester !== '' ? $semester : null;
if ($parentId <= 0 || $schoolYear === '') {
log_message('warning', 'updateEnrollmentStatusIfPaid: missing parent_id/school_year for invoice #{id}', ['id' => $invoiceId]);
log_message('warning', 'updateEnrollmentStatusAfterRecordedPayment: missing parent_id/school_year for invoice #{id}', ['id' => $invoiceId]);
return 0;
}
@@ -631,7 +631,7 @@ class PaymentController extends ResourceController
'enrollment_status' => 'enrolled',
'admission_status' => 'accepted',
'updated_at' => utc_now(),
], (int) (session()->get('user_id') ?? 0) ?: null, 'payment_completed');
], (int) (session()->get('user_id') ?? 0) ?: null, 'payment_recorded');
}
$affected = count($rowsToUpdate);
@@ -639,7 +639,7 @@ class PaymentController extends ResourceController
log_message(
'info',
'Enrollment status -> enrolled for {n} row(s). parent={p}, year={y}, sem={s}, ids=[{ids}]',
'Enrollment status -> enrolled after payment for {n} row(s). parent={p}, year={y}, sem={s}, ids=[{ids}]',
[
'n' => $affected,
'p' => $parentId,
@@ -652,7 +652,7 @@ class PaymentController extends ResourceController
return $affected;
} catch (\Throwable $e) {
$db->transRollback();
log_message('error', 'updateEnrollmentStatusIfPaid error: ' . $e->getMessage());
log_message('error', 'updateEnrollmentStatusAfterRecordedPayment error: ' . $e->getMessage());
return 0;
}
}
@@ -1073,10 +1073,15 @@ class PaymentController extends ResourceController
$this->syncEnrollmentFinanceAfterPayment($parentId, $invYear);
// Optional enrollment update
$enrollmentupdated = $this->updateEnrollmentStatusIfPaid($invoiceId);
$enrollmentupdated = $this->updateEnrollmentStatusAfterRecordedPayment($invoiceId);
if ($enrollmentupdated != 0) {
$studentIds = $this->studentModel->getStudentIdsByParentId($parentId);
[$eventDataEnroll, $studentDataEnroll] = $this->buildStudentEnrolledEventData($parentId, $studentIds);
[$eventDataEnroll, $studentDataEnroll] = $this->buildStudentEnrolledEventData(
$parentId,
$studentIds,
$invYear,
(string) ($row['semester'] ?? $this->semester)
);
Events::trigger('studentEnrolled', $eventDataEnroll, $studentDataEnroll);
}