This commit is contained in:
@@ -25,8 +25,14 @@ final class SchoolYearManagementService
|
||||
|
||||
public function createDraft(array $payload, ?int $userId = null): int
|
||||
{
|
||||
$nextDraft = $this->nextDraftDefaults();
|
||||
if ($nextDraft['name'] === null) {
|
||||
throw new InvalidArgumentException('Create an initial school year before using automatic next-year draft creation.');
|
||||
}
|
||||
|
||||
$payload['name'] = $nextDraft['name'];
|
||||
$payload = $this->metadataPayload($payload);
|
||||
$payload['previous_school_year_id'] = $this->previousYearIdForDraft((string) $payload['name']);
|
||||
$payload['previous_school_year_id'] = (int) ($nextDraft['previous_year']['id'] ?? 0) ?: $this->previousYearIdForDraft((string) $payload['name']);
|
||||
$payload['status'] = SchoolYearStatus::DRAFT;
|
||||
$payload['created_by'] = $userId;
|
||||
$payload['updated_by'] = $userId;
|
||||
@@ -48,6 +54,19 @@ final class SchoolYearManagementService
|
||||
return (int) $id;
|
||||
}
|
||||
|
||||
public function nextDraftDefaults(): array
|
||||
{
|
||||
$previousYear = $this->sourceYearForNextDraft();
|
||||
$name = $previousYear !== null
|
||||
? $this->nextSchoolYearName((string) ($previousYear['name'] ?? ''))
|
||||
: null;
|
||||
|
||||
return [
|
||||
'name' => $name,
|
||||
'previous_year' => $previousYear,
|
||||
];
|
||||
}
|
||||
|
||||
public function updateMetadata(int $id, array $payload, ?int $userId = null): void
|
||||
{
|
||||
$year = $this->requireYear($id);
|
||||
@@ -321,6 +340,29 @@ final class SchoolYearManagementService
|
||||
return $latestYear !== null ? ((int) ($latestYear['id'] ?? 0) ?: null) : null;
|
||||
}
|
||||
|
||||
private function sourceYearForNextDraft(): ?array
|
||||
{
|
||||
$activeYear = $this->schoolYearModel->active();
|
||||
if ($activeYear !== null) {
|
||||
return $activeYear;
|
||||
}
|
||||
|
||||
return $this->schoolYearModel
|
||||
->orderBy('name', 'DESC')
|
||||
->first();
|
||||
}
|
||||
|
||||
private function nextSchoolYearName(string $name): ?string
|
||||
{
|
||||
if (! preg_match('/^(\d{4})-(\d{4})$/', $name, $matches)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$start = (int) $matches[2];
|
||||
|
||||
return $start . '-' . ($start + 1);
|
||||
}
|
||||
|
||||
private function nullableDate(mixed $value): ?string
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
|
||||
Reference in New Issue
Block a user