auditionadmin/app/Services/Invoice/InvoiceOneFeePerEntry.php

95 lines
2.8 KiB
PHP

<?php
namespace App\Services\Invoice;
use App\Models\School;
use App\Services\EntryService;
use Illuminate\Support\Arr;
use function auditionSetting;
class InvoiceOneFeePerEntry implements InvoiceDataService
{
/**
* Create a new class instance.
*/
protected $entryService;
public function __construct(EntryService $entryService)
{
$this->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) {
foreach ($entries[$student->id] ?? [] as $entry) {
$entryFee = $entry->audition->entry_fee / 100;
$lateFee = ($this->entryService->isEntryLate($entry) && ! $entry->hasFlag('late_fee_waived'))
? auditionSetting('late_fee') / 100 : 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;
}
}
// 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'];
}
}