From c3b48bf89cdc1df7ad91fe081cad55a80cf0373e Mon Sep 17 00:00:00 2001 From: Matt Young Date: Sat, 29 Jun 2024 10:22:25 -0500 Subject: [PATCH] Allow admin to access any schools invoice from the schools admin page --- .../Controllers/Admin/SchoolController.php | 21 ++++++++++++++++++- .../Invoice/InvoiceOneFeePerEntry.php | 2 +- .../Invoice/InvoiceOneFeePerStudent.php | 2 +- resources/views/admin/schools/index.blade.php | 8 ++++++- routes/admin.php | 1 + 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Admin/SchoolController.php b/app/Http/Controllers/Admin/SchoolController.php index d34e7d7..af0ea3c 100644 --- a/app/Http/Controllers/Admin/SchoolController.php +++ b/app/Http/Controllers/Admin/SchoolController.php @@ -5,6 +5,7 @@ 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; @@ -14,14 +15,25 @@ 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', ['schools' => $schools]); + return view('admin.schools.index', compact('schools', 'schoolTotalFees')); } public function show(Request $request, School $school) @@ -122,4 +134,11 @@ class SchoolController extends Controller // 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')); + } } diff --git a/app/Services/Invoice/InvoiceOneFeePerEntry.php b/app/Services/Invoice/InvoiceOneFeePerEntry.php index 16e4665..b08b27d 100644 --- a/app/Services/Invoice/InvoiceOneFeePerEntry.php +++ b/app/Services/Invoice/InvoiceOneFeePerEntry.php @@ -37,7 +37,7 @@ class InvoiceOneFeePerEntry implements InvoiceDataService $entries = $school->entries()->with('audition')->orderBy('created_at', 'desc')->get()->groupBy('student_id'); foreach ($school->students as $student) { - foreach ($entries[$student->id] as $entry) { + foreach ($entries[$student->id] ?? [] as $entry) { $entryFee = $entry->audition->entry_fee / 100; $lateFee = $this->entryService->entryIsLate($entry) ? auditionSetting('late_fee') / 100 : 0; diff --git a/app/Services/Invoice/InvoiceOneFeePerStudent.php b/app/Services/Invoice/InvoiceOneFeePerStudent.php index 3bf9f99..c5090ac 100644 --- a/app/Services/Invoice/InvoiceOneFeePerStudent.php +++ b/app/Services/Invoice/InvoiceOneFeePerStudent.php @@ -38,7 +38,7 @@ class InvoiceOneFeePerStudent implements InvoiceDataService $entries = $school->entries()->with('audition')->orderBy('created_at', 'desc')->get()->groupBy('student_id'); foreach ($school->students as $student) { $firstEntryForStudent = true; - foreach ($entries[$student->id] as $entry) { + foreach ($entries[$student->id] ?? [] as $entry) { if ($firstEntryForStudent) { $entryFee = $entry->audition->entry_fee / 100; $lateFee = $this->entryService->entryIsLate($entry) ? auditionSetting('late_fee') / 100 : 0; diff --git a/resources/views/admin/schools/index.blade.php b/resources/views/admin/schools/index.blade.php index b640084..9ffc01e 100644 --- a/resources/views/admin/schools/index.blade.php +++ b/resources/views/admin/schools/index.blade.php @@ -4,7 +4,7 @@ Schools - Click school name to edit + Click school name to edit
Click total fees for invoice
New School @@ -12,6 +12,7 @@ Name + Total Fees Directors Students Entries @@ -22,6 +23,11 @@ @foreach($schools as $school) {{ $school->name }} + + + ${{ number_format($schoolTotalFees[$school->id],2) }} + + {{ $school->users->count() }} {{ $school->students->count() }} {{ $school->entries->count() }} diff --git a/routes/admin.php b/routes/admin.php index c318db7..4f7d721 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -99,6 +99,7 @@ Route::middleware(['auth', 'verified', CheckIfAdmin::class])->prefix('admin/')-> Route::get('/create', 'create')->name('admin.schools.create'); Route::get('/{school}', 'show')->name('admin.schools.show'); Route::get('/{school}/edit', 'edit')->name('admin.schools.edit'); + Route::get('/{school}/invoice', 'viewInvoice')->name('admin.schools.invoice'); Route::patch('/{school}', 'update')->name('admin.schools.update'); Route::post('/', 'store')->name('admin.schools.store'); Route::delete('/domain/{domain}', 'destroy_domain')->name('admin.schools.destroy_domain');