add refund logic and fix books inventory logic
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 47s
Tests / PHPUnit (push) Failing after 1m20s

This commit is contained in:
root
2026-08-22 13:44:25 -04:00
parent 23d1cbb64c
commit d906a915d6
45 changed files with 4711 additions and 442 deletions
+26 -96
View File
@@ -2,8 +2,6 @@
namespace App\Services;
use App\Models\ConfigurationModel;
use App\Models\PaymentModel;
use App\Models\InvoiceModel;
use App\Models\ClassSectionModel;
class FeeCalculationService
@@ -15,112 +13,44 @@ class FeeCalculationService
public function calculateRefund(array $students, int $parentId): float
{
$configModel = new ConfigurationModel();
$paymentModel = new PaymentModel();
$invoiceModel = new InvoiceModel();
$classSectionModel = new ClassSectionModel();
$totalCents = 0;
$seenEnrollmentIds = [];
$schoolYear = $configModel->getConfig('school_year');
$refundDeadline = date('Y-m-d', strtotime($configModel->getConfig('refund_deadline')));
$weekOfStudy = (float) ($configModel->getConfig('weeks_study') ?? 8);
$schoolEndDate = date('Y-m-d', strtotime($configModel->getConfig('last_day_of_school')));
$totalPaid = $paymentModel->getTotalPaidByParentId($parentId, $schoolYear);
if ($totalPaid <= 0) {
log_message('info', "No payments made. Refund = 0.");
return 0;
}
// Classify and enrich student data
$registeredStudents = [];
$withdrawnStudents = [];
foreach ($students as &$student) {
$gradeName = $classSectionModel->getClassSectionNameBySectionId($student['class_section_id']);
$student['grade'] = strtoupper(trim($gradeName));
if (in_array($student['enrollment_status'], ['withdrawn', 'refund pending', 'withdraw under review'])) {
$withdrawnStudents[] = $student;
} elseif (
in_array($student['enrollment_status'], ['enrolled', 'payment pending']) &&
$student['admission_status'] === 'accepted'
) {
$registeredStudents[] = $student;
}
}
unset($student);
if (empty($withdrawnStudents)) {
log_message('info', "No withdrawn students found. Refund = 0.");
return 0;
}
usort($withdrawnStudents, function ($a, $b) {
$leftDate = strtotime((string)($a['withdrawal_date'] ?? '')) ?: PHP_INT_MAX;
$rightDate = strtotime((string)($b['withdrawal_date'] ?? '')) ?: PHP_INT_MAX;
if ($leftDate !== $rightDate) {
return $leftDate <=> $rightDate;
}
return $this->compareGrades($a['grade'], $b['grade']);
});
// Combine all students for proper fee tiering before withdrawal.
$allStudents = array_merge($registeredStudents, $withdrawnStudents);
// Sort all students by grade for correct tiering
usort($allStudents, function ($a, $b) {
return $this->compareGrades($a['grade'], $b['grade']);
});
// Retrieve fee configs
$firstStudentFee = (float) ($configModel->getConfig('first_student_fee') ?? 380);
$secondStudentFee = (float) ($configModel->getConfig('second_student_fee') ?? 280);
$refundFeeStack = $this->reverseTuitionRefundFeeStack(
count($allStudents),
count($registeredStudents),
$firstStudentFee,
$secondStudentFee
);
// Calculate refund for withdrawn students
$refundAmount = 0;
$withdrawnRefundIndex = 0;
foreach ($withdrawnStudents as $student) {
if (empty($student['withdrawal_date'])) {
log_message('warning', "Missing withdraw date for student ID: {$student['student_id']}");
foreach ($students as $student) {
if ((int) ($student['parent_id'] ?? $parentId) !== $parentId) {
continue;
}
$withdrawDate = date('Y-m-d', strtotime($student['withdrawal_date']));
if (strtotime($withdrawDate) > strtotime($refundDeadline)) {
log_message('info', "Withdraw date {$withdrawDate} is after refund deadline {$refundDeadline}. No refund for this student.");
$status = strtolower(trim((string) ($student['enrollment_status'] ?? '')));
if (! in_array($status, ['withdrawn', 'refund pending', 'withdraw under review'], true)) {
continue;
}
$withdrawDateObj = new \DateTime($withdrawDate);
$schoolEndDateObj = new \DateTime($schoolEndDate);
$daysRemaining = $withdrawDateObj->diff($schoolEndDateObj)->days;
$weeksRemaining = min($weekOfStudy, max(0, ceil($daysRemaining / 7)));
$enrollmentId = (int) ($student['enrollment_id'] ?? $student['id'] ?? 0);
if ($enrollmentId <= 0 || isset($seenEnrollmentIds[$enrollmentId])) {
continue;
}
$seenEnrollmentIds[$enrollmentId] = true;
$studentFee = (float) ($refundFeeStack[$withdrawnRefundIndex] ?? 0);
$withdrawnRefundIndex++;
$proportionalRefund = ($studentFee / $weekOfStudy) * $weeksRemaining;
$refundAmount += $proportionalRefund;
$calculation = $this->latestWithdrawalCalculation($enrollmentId);
if ($calculation === null || ($calculation['status'] ?? '') === 'superseded') {
$calculation = $this->previewWithdrawalCalculation($enrollmentId);
}
log_message('info', "Student ID {$student['student_id']} refund portion: {$proportionalRefund} of {$studentFee} for {$weeksRemaining} weeks.");
$totalCents += max(0, (int) ($calculation['new_refund_request_cents'] ?? 0));
}
if ($refundAmount > $totalPaid) {
log_message('info', "Refund capped at total paid amount: {$totalPaid}");
return $totalPaid;
}
return round($totalCents / 100, 2);
}
log_message('info', "Final calculated refund: {$refundAmount}");
return $refundAmount;
protected function latestWithdrawalCalculation(int $enrollmentId): ?array
{
return service('withdrawalFinancial')->latestForEnrollment($enrollmentId);
}
protected function previewWithdrawalCalculation(int $enrollmentId): array
{
return service('withdrawalFinancial')->preview($enrollmentId, (int) (session()->get('user_id') ?? 0) ?: null);
}
/**