diff --git a/app/Providers/InvoiceDataServiceProvider.php b/app/Providers/InvoiceDataServiceProvider.php index 5f75c06..2ed6784 100644 --- a/app/Providers/InvoiceDataServiceProvider.php +++ b/app/Providers/InvoiceDataServiceProvider.php @@ -6,6 +6,7 @@ use App\Services\EntryService; use App\Services\Invoice\InvoiceDataService; use App\Services\Invoice\InvoiceOneFeePerEntry; use App\Services\Invoice\InvoiceOneFeePerStudent; +use App\Services\Invoice\InvoiceOneFeePerStudentPerEvent; use Illuminate\Support\Facades\Schema; use Illuminate\Support\ServiceProvider; @@ -42,6 +43,7 @@ class InvoiceDataServiceProvider extends ServiceProvider return match (auditionSetting('fee_structure')) { 'oneFeePerEntry' => new InvoiceOneFeePerEntry($app->make(EntryService::class)), 'oneFeePerStudent' => new InvoiceOneFeePerStudent($app->make(EntryService::class)), + 'oneFeePerStudentPerEvent' => new InvoiceOneFeePerStudentPerEvent($app->make(EntryService::class)), default => throw new \Exception('Unknown Invoice Method'), }; }); diff --git a/app/Services/Invoice/InvoiceOneFeePerStudentPerEvent.php b/app/Services/Invoice/InvoiceOneFeePerStudentPerEvent.php new file mode 100644 index 0000000..a1ad22e --- /dev/null +++ b/app/Services/Invoice/InvoiceOneFeePerStudentPerEvent.php @@ -0,0 +1,101 @@ +entryService = $entryService; + } + + public function allData($schoolId) + { + static $schoolInvoiceData = []; + + if (Arr::has($schoolInvoiceData, $schoolId)) { + return $schoolInvoiceData[$schoolId]; + } + $school = School::findOrFail($schoolId); + + $invoiceData['lines'] = []; + $invoiceData['linesTotal'] = 0; + $invoiceData['lateFeesTotal'] = 0; + /** @noinspection PhpArrayIndexImmediatelyRewrittenInspection */ + $invoiceData['grandTotal'] = 0; + + $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) { + if ($firstEntryForStudent) { + $entryFee = $entry->audition->entry_fee / 100; + $lateFee = ($this->entryService->isEntryLate($entry) && ! $entry->hasFlag('late_fee_waived')) + ? auditionSetting('late_fee') / 100 : 0; + } else { + $entryFee = 0; + $lateFee = 0; + } + + $invoiceData['lines'][] = [ + 'student_name' => $student->full_name(true), + 'audition' => $entry->audition->name, + 'entry_timestamp' => $entry->created_at, + 'entry_fee' => $entryFee, + 'late_fee' => $lateFee, + ]; + $invoiceData['linesTotal'] += $entryFee; + $invoiceData['lateFeesTotal'] += $lateFee; + $firstEntryForStudent = false; + } + } + // School Fee Total + if (! auditionSetting('school_fee')) { + $invoiceData['schoolFeeTotal'] = 0; + } else { + $invoiceData['schoolFeeTotal'] = auditionSetting('school_fee') / 100; + } + + $invoiceData['grandTotal'] = $invoiceData['linesTotal'] + $invoiceData['lateFeesTotal'] + $invoiceData['schoolFeeTotal']; + $schoolInvoiceData[$school->id] = $invoiceData; + + return $invoiceData; + } + + public function getLines($schoolId) + { + return $this->allData($schoolId)['lines']; + } + + public function getLinesTotal($schoolId) + { + return $this->allData($schoolId)['linesTotal']; + } + + public function getLateFeesTotal($schoolId) + { + return $this->allData($schoolId)['lateFeesTotal']; + } + + public function getSchoolFeeTotal($schoolId) + { + return $this->allData($schoolId)['schoolFeeTotal']; + + } + + public function getGrandTotal($schoolId) + { + return $this->allData($schoolId)['grandTotal']; + } +} diff --git a/resources/views/admin/audition-settings.blade.php b/resources/views/admin/audition-settings.blade.php index adb0651..9264d21 100644 --- a/resources/views/admin/audition-settings.blade.php +++ b/resources/views/admin/audition-settings.blade.php @@ -85,6 +85,9 @@ +