invoiceService = $invoiceController;
}
public function index()
{
$schools = School::with(['users', 'students', 'entries'])->orderBy('name')->get();
$schoolTotalFees = [];
foreach ($schools as $school) {
$schoolTotalFees[$school->id] = $this->invoiceService->getGrandTotal($school->id);
}
return view('admin.schools.index', compact('schools', 'schoolTotalFees'));
}
public function show(School $school)
{
if (! Auth::user()->is_admin) {
abort(403);
}
return view('admin.schools.show', ['school' => $school]);
}
public function edit(School $school)
{
if (! Auth::user()->is_admin) {
abort(403);
}
$school->loadCount('students');
return view('admin.schools.edit', ['school' => $school]);
}
public function update(School $school)
{
request()->validate([
'name' => ['required'],
'address' => ['required'],
'city' => ['required'],
'state' => ['required'],
'zip' => ['required'],
]);
$school->update([
'name' => request('name'),
'address' => request('address'),
'city' => request('city'),
'state' => request('state'),
'zip' => request('zip'),
]);
$message = 'Modified school #'.$school->id.' - '.$school->name.' with address
'.$school->address.'
'.$school->city.', '.$school->state.' '.$school->zip;
AuditLogEntry::create([
'user' => auth()->user()->email,
'ip_address' => request()->ip(),
'message' => $message,
'affected' => ['schools' => [$school->id]],
]);
return redirect()->route('admin.schools.show', ['school' => $school->id])->with('success',
'School '.$school->name.' updated');
}
public function create()
{
if (! Auth::user()->is_admin) {
abort(403);
}
return view('admin.schools.create');
}
public function store()
{
$creator = app(CreateSchool::class);
$validData = request()->validate([
'name' => ['required', 'unique:schools,name'],
'address' => ['required'],
'city' => ['required'],
'state' => ['required'],
'zip' => ['required'],
]);
$school = $creator(
$validData['name'],
$validData['address'],
$validData['city'],
$validData['state'],
$validData['zip'],
);
return redirect('/admin/schools')->with('success', 'School '.$school->name.' created');
}
public function destroy(School $school)
{
if ($school->students()->count() > 0) {
return to_route('admin.schools.index')->with('error', 'You cannot delete a school with students.');
}
$name = $school->name;
$message = 'Delete school #'.$school->id.' - '.$school->name;
AuditLogEntry::create([
'user' => auth()->user()->email,
'ip_address' => request()->ip(),
'message' => $message,
'affected' => ['schools' => [$school->id]],
]);
$school->delete();
return to_route('admin.schools.index')->with('success', 'School '.$school->name.' deleted');
}
public function add_domain(School $school)
{
if (! Auth::user()->is_admin) {
abort(403);
}
request()->validate([
// validate that the combination of school and domain is unique on the school_email_domains table
'domain' => ['required'],
]);
SchoolEmailDomain::updateOrInsert([
'school_id' => $school->id,
'domain' => request('domain'),
]);
AuditLogEntry::create([
'user' => auth()->user()->email,
'ip_address' => request()->ip(),
'message' => 'Added '.request('domain').' as an email domain for school #'.$school->id.' - '.$school->name,
'affected' => ['schools' => [$school->id]],
]);
return redirect()->route('admin.schools.show', $school)->with('success', 'Domain Added');
}
public function destroy_domain(SchoolEmailDomain $domain)
{
// Destroy the $domain
$domain->delete();
// return a redirect to the previous URL
return redirect()->back();
}
public function viewInvoice(School $school)
{
$invoiceData = $this->invoiceService->allData($school->id);
return view('dashboard.invoice', compact('school', 'invoiceData'));
}
public function setHeadDirector(School $school, User $user, SetHeadDirector $headSetter)
{
if ($user->school_id !== $school->id) {
return redirect()->back()->with('error', 'That user is not at that school');
}
$headSetter->setHeadDirector($user);
return redirect()->back()->with('success', 'Head director set');
}
}