normalizeInt($this->request->getGet('target_school_year_id')); return view('school_years/closing_preview', [ 'preview' => service('schoolYearClosing')->preview($id, $targetId), 'latestBatch' => service('schoolYearClosing')->latestBatch($id), 'schoolYears' => (new SchoolYearModel())->orderBy('name', 'DESC')->findAll(), ]); } catch (Throwable $e) { return redirect()->to('/administrator/school-years')->with('error', $e->getMessage()); } } public function start(int $id) { try { $targetId = $this->normalizeInt($this->request->getPost('target_school_year_id')); if ($targetId === null) { return redirect()->back()->with('error', 'Select a target school year.'); } service('schoolYearClosing')->start($id, $targetId, $this->userId()); return redirect()->to('/administrator/school-years/' . $id . '/closing/preview')->with('success', 'Closing started.'); } catch (Throwable $e) { return redirect()->back()->with('error', $e->getMessage()); } } public function execute(int $id) { try { service('schoolYearClosing')->execute($id, $this->userId()); return redirect()->to('/administrator/school-years/' . $id . '/closing/preview')->with('success', 'Carry-forward batch executed.'); } catch (Throwable $e) { return redirect()->back()->with('error', $e->getMessage()); } } public function complete(int $id) { try { service('schoolYearClosing')->complete($id, $this->userId()); return redirect()->to('/administrator/school-years')->with('success', 'School year closed.'); } catch (Throwable $e) { return redirect()->back()->with('error', $e->getMessage()); } } public function cancel(int $id) { try { service('schoolYearClosing')->cancel($id, $this->userId()); return redirect()->to('/administrator/school-years')->with('success', 'Closing cancelled.'); } catch (Throwable $e) { return redirect()->back()->with('error', $e->getMessage()); } } private function normalizeInt(mixed $value): ?int { return is_numeric($value) && (int) $value > 0 ? (int) $value : null; } private function userId(): ?int { $id = session('user_id') ?? session('id'); return is_numeric($id) ? (int) $id : null; } }