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'), ]); 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() { request()->validate([ 'name' => ['required'], 'address' => ['required'], 'city' => ['required'], 'state' => ['required'], 'zip' => ['required'], ]); $school = School::create([ 'name' => request('name'), 'address' => request('address'), 'city' => request('city'), 'state' => request('state'), 'zip' => request('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; $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'), ]); 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')); } }