fixed enrolled after first payment
This commit is contained in:
@@ -522,7 +522,7 @@ class PaymentController extends ResourceController
|
|||||||
return $this->response->setJSON(['items' => $items]);
|
return $this->response->setJSON(['items' => $items]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateEnrollmentStatusIfPaid(int $invoiceId): int
|
private function updateEnrollmentStatusAfterRecordedPayment(int $invoiceId): int
|
||||||
{
|
{
|
||||||
// 1) Fetch invoice
|
// 1) Fetch invoice
|
||||||
$invoice = $this->invoiceModel->find($invoiceId);
|
$invoice = $this->invoiceModel->find($invoiceId);
|
||||||
@@ -531,19 +531,19 @@ class PaymentController extends ResourceController
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2) Payment check: enrollment transitions only after the invoice is fully paid
|
// 2) Payment check: any successful payment, including the first installment, enrolls the student.
|
||||||
$currentBal = $this->getCurrentInvoiceBalance($invoiceId);
|
if ($this->getSuccessfulPaymentCount($invoiceId) < 1) {
|
||||||
if ($currentBal > 0.00001) {
|
log_message('info', 'No successful payment found. Skipping enrollment update. Invoice #{id}', ['id' => $invoiceId]);
|
||||||
log_message('info', 'Invoice not fully paid. Skipping enrollment update. Invoice #{id}', ['id' => $invoiceId]);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parentId = (int) ($invoice['parent_id'] ?? 0);
|
$parentId = (int) ($invoice['parent_id'] ?? 0);
|
||||||
$schoolYear = (string) ($invoice['school_year'] ?? $this->schoolYear);
|
$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 === '') {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -631,7 +631,7 @@ class PaymentController extends ResourceController
|
|||||||
'enrollment_status' => 'enrolled',
|
'enrollment_status' => 'enrolled',
|
||||||
'admission_status' => 'accepted',
|
'admission_status' => 'accepted',
|
||||||
'updated_at' => utc_now(),
|
'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);
|
$affected = count($rowsToUpdate);
|
||||||
@@ -639,7 +639,7 @@ class PaymentController extends ResourceController
|
|||||||
|
|
||||||
log_message(
|
log_message(
|
||||||
'info',
|
'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,
|
'n' => $affected,
|
||||||
'p' => $parentId,
|
'p' => $parentId,
|
||||||
@@ -652,7 +652,7 @@ class PaymentController extends ResourceController
|
|||||||
return $affected;
|
return $affected;
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
$db->transRollback();
|
$db->transRollback();
|
||||||
log_message('error', 'updateEnrollmentStatusIfPaid error: ' . $e->getMessage());
|
log_message('error', 'updateEnrollmentStatusAfterRecordedPayment error: ' . $e->getMessage());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1073,10 +1073,15 @@ class PaymentController extends ResourceController
|
|||||||
$this->syncEnrollmentFinanceAfterPayment($parentId, $invYear);
|
$this->syncEnrollmentFinanceAfterPayment($parentId, $invYear);
|
||||||
|
|
||||||
// Optional enrollment update
|
// Optional enrollment update
|
||||||
$enrollmentupdated = $this->updateEnrollmentStatusIfPaid($invoiceId);
|
$enrollmentupdated = $this->updateEnrollmentStatusAfterRecordedPayment($invoiceId);
|
||||||
if ($enrollmentupdated != 0) {
|
if ($enrollmentupdated != 0) {
|
||||||
$studentIds = $this->studentModel->getStudentIdsByParentId($parentId);
|
$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);
|
Events::trigger('studentEnrolled', $eventDataEnroll, $studentDataEnroll);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user