This commit is contained in:
@@ -26,6 +26,7 @@ final class SchoolYearManagementService
|
||||
public function createDraft(array $payload, ?int $userId = null): int
|
||||
{
|
||||
$payload = $this->metadataPayload($payload);
|
||||
$payload['previous_school_year_id'] = $this->previousYearIdForDraft((string) $payload['name']);
|
||||
$payload['status'] = SchoolYearStatus::DRAFT;
|
||||
$payload['created_by'] = $userId;
|
||||
$payload['updated_by'] = $userId;
|
||||
@@ -35,6 +36,7 @@ final class SchoolYearManagementService
|
||||
$this->db->transStart();
|
||||
$id = $this->schoolYearModel->insert($payload, true);
|
||||
if ($id !== false) {
|
||||
$this->syncConfigurationFromSchoolYear($payload);
|
||||
$this->log((int) $id, null, SchoolYearStatus::DRAFT, 'create', $userId);
|
||||
}
|
||||
$this->db->transComplete();
|
||||
@@ -62,6 +64,7 @@ final class SchoolYearManagementService
|
||||
$this->db->transStart();
|
||||
$updated = $this->schoolYearModel->update($id, $payload);
|
||||
if ($updated !== false) {
|
||||
$this->syncConfigurationFromSchoolYear($payload);
|
||||
$this->log($id, $status, $status, 'metadata_update', $userId);
|
||||
}
|
||||
$this->db->transComplete();
|
||||
@@ -104,7 +107,7 @@ final class SchoolYearManagementService
|
||||
'activated_at' => $now,
|
||||
'updated_by' => $userId,
|
||||
]);
|
||||
$this->configurationModel->setConfigValueByKey('school_year', (string) $year['name']);
|
||||
$this->syncConfigurationFromSchoolYear($year);
|
||||
$this->syncActiveYearSession((string) $year['name']);
|
||||
$this->log($id, $from, SchoolYearStatus::ACTIVE, 'activate', $userId);
|
||||
|
||||
@@ -255,10 +258,69 @@ final class SchoolYearManagementService
|
||||
'description' => trim((string) ($payload['description'] ?? '')) ?: null,
|
||||
'registration_starts_on' => $this->nullableDate($payload['registration_starts_on'] ?? null),
|
||||
'registration_ends_on' => $this->nullableDate($payload['registration_ends_on'] ?? null),
|
||||
'fall_makeup_exam_on' => $this->nullableDate($payload['fall_makeup_exam_on'] ?? null),
|
||||
'previous_school_year_id' => $this->nullableInt($payload['previous_school_year_id'] ?? null),
|
||||
];
|
||||
}
|
||||
|
||||
private function syncConfigurationFromSchoolYear(array $schoolYear): void
|
||||
{
|
||||
$name = trim((string) ($schoolYear['name'] ?? ''));
|
||||
if ($name === '') {
|
||||
throw new RuntimeException('Unable to update school-year configuration: school year name is missing.');
|
||||
}
|
||||
|
||||
$ageReferenceDate = $this->ageReferenceDateForSchoolYear($name);
|
||||
$configValues = [
|
||||
'school_year' => $name,
|
||||
'date_age_reference' => $ageReferenceDate,
|
||||
'refund_deadline' => $ageReferenceDate,
|
||||
'enrollment_deadline' => (string) ($schoolYear['registration_ends_on'] ?? ''),
|
||||
'fall_semester_start' => (string) ($schoolYear['starts_on'] ?? ''),
|
||||
'last_day_of_school' => (string) ($schoolYear['ends_on'] ?? ''),
|
||||
];
|
||||
|
||||
foreach ($configValues as $key => $value) {
|
||||
if (! $this->configurationModel->setConfigValueByKey($key, $value)) {
|
||||
throw new RuntimeException("Unable to update configuration value for {$key}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function ageReferenceDateForSchoolYear(string $schoolYearName): string
|
||||
{
|
||||
if (! preg_match('/^(\d{4})-\d{4}$/', $schoolYearName, $matches)) {
|
||||
throw new RuntimeException('Unable to update school-year configuration: invalid school year format.');
|
||||
}
|
||||
|
||||
return $matches[1] . '-12-31';
|
||||
}
|
||||
|
||||
private function previousYearIdForDraft(string $name): ?int
|
||||
{
|
||||
if (preg_match('/^(\d{4})-(\d{4})$/', $name, $matches)) {
|
||||
$previousName = ((int) $matches[1] - 1) . '-' . (int) $matches[1];
|
||||
$previousYear = $this->schoolYearModel
|
||||
->where('name', $previousName)
|
||||
->first();
|
||||
|
||||
if ($previousYear !== null) {
|
||||
return (int) ($previousYear['id'] ?? 0) ?: null;
|
||||
}
|
||||
}
|
||||
|
||||
$activeYear = $this->schoolYearModel->active();
|
||||
if ($activeYear !== null) {
|
||||
return (int) ($activeYear['id'] ?? 0) ?: null;
|
||||
}
|
||||
|
||||
$latestYear = $this->schoolYearModel
|
||||
->orderBy('name', 'DESC')
|
||||
->first();
|
||||
|
||||
return $latestYear !== null ? ((int) ($latestYear['id'] ?? 0) ?: null) : null;
|
||||
}
|
||||
|
||||
private function nullableDate(mixed $value): ?string
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
|
||||
Reference in New Issue
Block a user