add more controllers and fix tests
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Fees;
|
||||
|
||||
use App\Models\Configuration;
|
||||
|
||||
class FeeConfigService
|
||||
{
|
||||
public function getSchoolYear(): string
|
||||
{
|
||||
return (string) (Configuration::getConfig('school_year') ?? '');
|
||||
}
|
||||
|
||||
public function getRefundDeadline(): ?string
|
||||
{
|
||||
$raw = (string) (Configuration::getConfig('refund_deadline') ?? '');
|
||||
if ($raw === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$timestamp = strtotime($raw);
|
||||
if ($timestamp === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return date('Y-m-d', $timestamp);
|
||||
}
|
||||
|
||||
public function getWeeksOfStudy(): float
|
||||
{
|
||||
return (float) (Configuration::getConfig('weeks_study') ?? 8);
|
||||
}
|
||||
|
||||
public function getSchoolEndDate(): ?string
|
||||
{
|
||||
$raw = (string) (Configuration::getConfig('last_school_day') ?? '');
|
||||
if ($raw === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$timestamp = strtotime($raw);
|
||||
if ($timestamp === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return date('Y-m-d', $timestamp);
|
||||
}
|
||||
|
||||
public function getFees(): array
|
||||
{
|
||||
return [
|
||||
'first_student_fee' => (float) (Configuration::getConfig('first_student_fee') ?? 350),
|
||||
'second_student_fee' => (float) (Configuration::getConfig('second_student_fee') ?? 200),
|
||||
'youth_fee' => (float) (Configuration::getConfig('youth_fee') ?? 180),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Fees;
|
||||
|
||||
class FeeGradeService
|
||||
{
|
||||
public function normalizeGrade(?string $grade): string
|
||||
{
|
||||
return strtoupper(trim((string) $grade));
|
||||
}
|
||||
|
||||
public function compareGrades(string $gradeA, string $gradeB): int
|
||||
{
|
||||
$valA = $this->getGradeLevel($gradeA);
|
||||
$valB = $this->getGradeLevel($gradeB);
|
||||
|
||||
if ($valA !== $valB) {
|
||||
return $valA <=> $valB;
|
||||
}
|
||||
|
||||
preg_match('/\d+([A-Z]*)$/i', strtoupper($gradeA), $suffixA);
|
||||
preg_match('/\d+([A-Z]*)$/i', strtoupper($gradeB), $suffixB);
|
||||
|
||||
return strcmp($suffixA[1] ?? '', $suffixB[1] ?? '');
|
||||
}
|
||||
|
||||
public function getGradeLevel(string $grade): int
|
||||
{
|
||||
$grade = $this->normalizeGrade($grade);
|
||||
|
||||
if ($grade === 'K') {
|
||||
return 0;
|
||||
}
|
||||
if ($grade === 'Y') {
|
||||
return 99;
|
||||
}
|
||||
|
||||
if (preg_match('/^(\d+)([A-Z]*)$/i', $grade, $matches)) {
|
||||
return (int) $matches[1];
|
||||
}
|
||||
|
||||
return 999;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Fees;
|
||||
|
||||
use App\Models\Payment;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class FeeRefundCalculatorService
|
||||
{
|
||||
public function __construct(
|
||||
private FeeConfigService $config,
|
||||
private FeeStudentFeeService $studentFees
|
||||
) {
|
||||
}
|
||||
|
||||
public function calculateRefund(array $students, int $parentId): array
|
||||
{
|
||||
$schoolYear = $this->config->getSchoolYear();
|
||||
$refundDeadline = $this->config->getRefundDeadline();
|
||||
$weeksOfStudy = $this->config->getWeeksOfStudy();
|
||||
$schoolEndDate = $this->config->getSchoolEndDate();
|
||||
|
||||
$totalPaid = Payment::getTotalPaidByParentId($parentId, $schoolYear);
|
||||
if ($totalPaid <= 0) {
|
||||
return $this->resultPayload(0.0, $totalPaid, $refundDeadline, $schoolEndDate, $weeksOfStudy, 0);
|
||||
}
|
||||
|
||||
$registered = [];
|
||||
$withdrawn = [];
|
||||
|
||||
foreach ($students as $student) {
|
||||
$status = strtolower((string) ($student['enrollment_status'] ?? ''));
|
||||
$admission = strtolower((string) ($student['admission_status'] ?? ''));
|
||||
|
||||
if (in_array($status, ['withdrawn', 'refund pending', 'withdraw under review'], true)) {
|
||||
$withdrawn[] = $student;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($status, ['enrolled', 'payment pending'], true) && $admission === 'accepted') {
|
||||
$registered[] = $student;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($withdrawn)) {
|
||||
return $this->resultPayload(0.0, $totalPaid, $refundDeadline, $schoolEndDate, $weeksOfStudy, 0);
|
||||
}
|
||||
|
||||
$allStudents = array_merge($registered, $withdrawn);
|
||||
$allStudents = $this->studentFees->assignFees($allStudents);
|
||||
|
||||
$feeByStudentId = [];
|
||||
foreach ($allStudents as $student) {
|
||||
$key = $student['student_id'] ?? null;
|
||||
if ($key === null) {
|
||||
continue;
|
||||
}
|
||||
$feeByStudentId[(string) $key] = (float) ($student['tuition_fee'] ?? 0);
|
||||
}
|
||||
|
||||
$refundAmount = 0.0;
|
||||
$withdrawCount = 0;
|
||||
|
||||
foreach ($withdrawn as $student) {
|
||||
$withdrawalDate = $student['withdrawal_date'] ?? null;
|
||||
if (!$withdrawalDate) {
|
||||
Log::warning('Missing withdraw date for student', ['student_id' => $student['student_id'] ?? null]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$refundDeadline || strtotime($withdrawalDate) > strtotime($refundDeadline)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$schoolEndDate) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$withdrawDateObj = new \DateTime(date('Y-m-d', strtotime($withdrawalDate)));
|
||||
$schoolEndDateObj = new \DateTime($schoolEndDate);
|
||||
$daysRemaining = $withdrawDateObj->diff($schoolEndDateObj)->days;
|
||||
$weeksRemaining = min($weeksOfStudy, max(0, (int) ceil($daysRemaining / 7)));
|
||||
|
||||
$studentId = (string) ($student['student_id'] ?? '');
|
||||
$studentFee = $feeByStudentId[$studentId] ?? 0.0;
|
||||
|
||||
if ($studentFee <= 0 || $weeksOfStudy <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$proportionalRefund = ($studentFee / $weeksOfStudy) * $weeksRemaining;
|
||||
$refundAmount += $proportionalRefund;
|
||||
$withdrawCount++;
|
||||
}
|
||||
|
||||
if ($refundAmount > $totalPaid) {
|
||||
$refundAmount = $totalPaid;
|
||||
}
|
||||
|
||||
return $this->resultPayload($refundAmount, $totalPaid, $refundDeadline, $schoolEndDate, $weeksOfStudy, $withdrawCount);
|
||||
}
|
||||
|
||||
private function resultPayload(
|
||||
float $refund,
|
||||
float $totalPaid,
|
||||
?string $refundDeadline,
|
||||
?string $schoolEndDate,
|
||||
float $weeksOfStudy,
|
||||
int $withdrawCount
|
||||
): array {
|
||||
return [
|
||||
'refund_amount' => round($refund, 2),
|
||||
'total_paid' => round($totalPaid, 2),
|
||||
'refund_deadline' => $refundDeadline,
|
||||
'school_end_date' => $schoolEndDate,
|
||||
'weeks_of_study' => $weeksOfStudy,
|
||||
'withdrawn_students' => $withdrawCount,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Fees;
|
||||
|
||||
use App\Models\ClassSection;
|
||||
|
||||
class FeeStudentFeeService
|
||||
{
|
||||
public function __construct(
|
||||
private FeeGradeService $grades,
|
||||
private FeeConfigService $config
|
||||
) {
|
||||
}
|
||||
|
||||
public function assignFees(array $students): array
|
||||
{
|
||||
$fees = $this->config->getFees();
|
||||
$firstFee = $fees['first_student_fee'];
|
||||
$secondFee = $fees['second_student_fee'];
|
||||
$youthFee = $fees['youth_fee'];
|
||||
|
||||
foreach ($students as &$student) {
|
||||
$grade = $student['grade'] ?? null;
|
||||
if ($grade === null || trim((string) $grade) === '') {
|
||||
$sectionId = $student['class_section_id'] ?? null;
|
||||
$grade = $sectionId ? ClassSection::getClassSectionNameBySectionId($sectionId) : null;
|
||||
}
|
||||
$student['grade'] = $this->grades->normalizeGrade($grade);
|
||||
}
|
||||
unset($student);
|
||||
|
||||
usort($students, function ($a, $b) {
|
||||
return $this->grades->compareGrades($a['grade'] ?? '', $b['grade'] ?? '');
|
||||
});
|
||||
|
||||
$regularCount = 0;
|
||||
foreach ($students as &$student) {
|
||||
$gradeLevel = $this->grades->getGradeLevel($student['grade'] ?? '');
|
||||
if ($gradeLevel > 9) {
|
||||
$student['tuition_fee'] = $youthFee;
|
||||
} else {
|
||||
$student['tuition_fee'] = ($regularCount === 0) ? $firstFee : $secondFee;
|
||||
$regularCount++;
|
||||
}
|
||||
}
|
||||
unset($student);
|
||||
|
||||
return $students;
|
||||
}
|
||||
|
||||
public function totalTuitionFee(array $students): float
|
||||
{
|
||||
$students = $this->assignFees($students);
|
||||
|
||||
$total = 0.0;
|
||||
foreach ($students as $student) {
|
||||
$total += (float) ($student['tuition_fee'] ?? 0);
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user