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->entryIsLate($entry) ? 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']; } }