fix enrollment and email send to parent
This commit is contained in:
@@ -514,6 +514,8 @@ class DiscountController extends BaseController
|
||||
$affected = count($rowsToUpdate);
|
||||
$db->transCommit();
|
||||
|
||||
$this->triggerStudentEnrolledNotification($parentId, $rowsToUpdate);
|
||||
|
||||
log_message(
|
||||
'info',
|
||||
'Enrollment status -> enrolled for {n} row(s). parent={p}, year={y}, sem={s}, ids=[{ids}]',
|
||||
@@ -534,10 +536,60 @@ class DiscountController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
private function triggerStudentEnrolledNotification(int $parentId, array $enrollmentRows): void
|
||||
{
|
||||
if ($parentId <= 0 || $enrollmentRows === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$parent = $this->userModel->find($parentId) ?? [];
|
||||
$studentIds = array_values(array_unique(array_filter(
|
||||
array_map(static fn (array $row): int => (int) ($row['student_id'] ?? 0), $enrollmentRows),
|
||||
static fn (int $studentId): bool => $studentId > 0
|
||||
)));
|
||||
|
||||
if ($studentIds === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$students = [];
|
||||
foreach ($this->db->table('students')->whereIn('id', $studentIds)->get()->getResultArray() as $student) {
|
||||
$studentId = (int) ($student['id'] ?? 0);
|
||||
if ($studentId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$students[] = [
|
||||
'student_id' => $studentId,
|
||||
'name' => trim((string) ($student['firstname'] ?? '') . ' ' . (string) ($student['lastname'] ?? '')) ?: 'Student #' . $studentId,
|
||||
];
|
||||
}
|
||||
|
||||
if ($students === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
Events::trigger('studentEnrolled', [
|
||||
'user_id' => $parent['id'] ?? $parentId,
|
||||
'email' => $parent['email'] ?? null,
|
||||
'firstname' => $parent['firstname'] ?? '',
|
||||
'lastname' => $parent['lastname'] ?? '',
|
||||
'school_year' => (string) $this->schoolYear,
|
||||
'portalLink' => base_url('/login'),
|
||||
], $students);
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Enrollment confirmation notification after discount failed for parent {parent_id}: {error}', [
|
||||
'parent_id' => $parentId,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function listVouchers()
|
||||
{
|
||||
$this->schoolYear = $this->activeSchoolYearName();
|
||||
$this->schoolYear = $this->currentSchoolYearName();
|
||||
|
||||
$vouchers = $this->voucherModel
|
||||
->where('school_year', $this->schoolYear)
|
||||
@@ -684,6 +736,19 @@ class DiscountController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
protected function currentSchoolYearName(?string $fallback = null): string
|
||||
{
|
||||
try {
|
||||
return service('schoolYearContext')->resolve($this->request)->yearName();
|
||||
} catch (\Throwable) {
|
||||
if ($fallback !== null && $fallback !== '') {
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
return (string) ($this->configModel->getConfig('school_year') ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 🔄 Helper: Current invoice balance (school-year scoped) = total - payments - discounts - refundsPaid
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user