Files
alrahma_sunday_school/app/Controllers/Administrator/SchoolYearClosingController.php
T
root e06ccc9cc0
Tests / PHPUnit (push) Failing after 40s
ADD SCHOOL YEAR MANAGEMENT
2026-07-12 02:21:39 -04:00

83 lines
2.8 KiB
PHP

<?php
namespace App\Controllers\Administrator;
use App\Controllers\BaseController;
use App\Models\SchoolYearModel;
use Throwable;
class SchoolYearClosingController extends BaseController
{
public function preview(int $id)
{
try {
$targetId = $this->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;
}
}