141 lines
3.8 KiB
PHP
141 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\School;
|
|
use App\Models\SchoolEmailDomain;
|
|
use App\Services\Invoice\InvoiceDataService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use function abort;
|
|
use function redirect;
|
|
use function request;
|
|
|
|
class SchoolController extends Controller
|
|
{
|
|
protected $invoiceService;
|
|
|
|
public function __construct(InvoiceDataService $invoiceController)
|
|
{
|
|
$this->invoiceService = $invoiceController;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
if (! Auth::user()->is_admin) {
|
|
abort(403);
|
|
}
|
|
$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(Request $request, 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);
|
|
}
|
|
|
|
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 $request)
|
|
{
|
|
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 add_domain(Request $request, 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('/admin/schools/'.$school->id.'/edit')->with('success', 'Domain Added');
|
|
|
|
}
|
|
|
|
public function destroy_domain(Request $request, SchoolEmailDomain $domain)
|
|
{
|
|
// Destroy the $domain
|
|
$domain->delete();
|
|
|
|
// return a redirect to the previous URL
|
|
return redirect()->back();
|
|
}
|
|
|
|
public function viewInvoice(Request $request, School $school)
|
|
{
|
|
$invoiceData = $this->invoiceService->allData($school->id);
|
|
|
|
return view('dashboard.invoice', compact('school', 'invoiceData'));
|
|
}
|
|
}
|