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
+7 -92
View File
@@ -18,8 +18,8 @@ use \App\Models\EnrollmentModel;
use \App\Models\EventChargesModel;
use \App\Models\EventModel;
use CodeIgniter\Events\Events;
use App\Services\SchoolIdService;
use App\Services\FeeCalculationService;
use App\Services\SchoolIdService;
use App\Services\PhoneFormatterService;
use App\Support\Enrollment\DeliberationDecision;
use App\Support\Enrollment\EnrollmentEligibility;
@@ -441,8 +441,6 @@ class ParentController extends BaseController
public function enrollClassesHandler()
{
$refundService = new FeeCalculationService();
// Retrieve enrollment and withdrawal data from the POST request
$enroll = $this->request->getPost('enroll'); // Selected students for enrollment
$withdraw = $this->request->getPost('withdraw'); // Selected students for withdrawal
@@ -697,97 +695,14 @@ class ParentController extends BaseController
->getRowArray();
if ($enrollment !== null) {
// Update enrollment as withdrawn
$enrollmentStatusService = \Config\Services::enrollmentStatus(false);
$enrollmentStatusService->upsertStatus([
'id' => (int) $enrollment['id'],
'student_id' => (int) $studentId,
'parent_id' => (int) ($enrollment['parent_id'] ?? $parentId),
'school_year' => (string) $this->schoolYear,
'semester' => (string) ($enrollment['semester'] ?? $this->semester),
'withdrawal_date' => local_date(utc_now(), 'Y-m-d'),
'enrollment_status' => 'withdraw under review', // Withdrawal needs review
'updated_at' => utc_now()
], (int) $parentId, 'parent_withdrawal_requested');
$withdrawalRequestDate = local_date(utc_now(), 'Y-m-d');
service('withdrawalFinancial')->requestWithdrawal(
(int) $enrollment['id'],
$withdrawalRequestDate,
(int) $parentId
);
log_message('info', "Student ID $studentId has been withdrawn from enrollment ID {$enrollment['id']}.");
$withdrawalResultMessages[] = 'Student ID ' . $studentId . ': withdraw under review';
// === Trigger refund process ===
// Find the related invoice (you may need to adjust this based on your DB structure)
$invoice = $this->db->table('invoices')
->where('parent_id', $parentId)
->where('school_year', $this->schoolYear)
->orderBy('created_at', 'DESC')
->get()
->getRowArray();
if ($invoice !== null) {
$invoiceId = $invoice['id'];
$studentsForRefund = $this->enrollmentModel
->where('parent_id', $parentId)
->where('school_year', $this->schoolYear)
->findAll();
$refundAmount = $refundService->calculateRefund($studentsForRefund, (int) $parentId);
$refundCents = max(0, (int) round($refundAmount * 100));
$refundTable = $this->db->table('refunds');
$existingRefund = $refundTable
->where('parent_id', $parentId)
->where('invoice_id', $invoiceId)
->where('school_year', $this->schoolYear)
->whereIn('status', ['Pending', 'Approved', 'Partial', 'pending', 'requested', 'approved', 'partial', 'partially_paid'])
->get()
->getRow();
if ($existingRefund) {
// Only update the fields that should change
$updateData = [
'refund_amount' => $refundAmount,
'requested_amount_cents' => $refundCents,
'currency' => 'USD',
'reason' => 'Withdrawal under review for student ID ' . $studentId,
'note' => null,
'request' => 'tuition',
'source_type' => 'tuition_withdrawal',
'source_id' => (int) $invoiceId,
'status' => 'Pending',
'updated_by' => session()->get('user_id'), // optionally track updates
// Add other fields if *and only if* they must be changed
];
$refundTable
->where('id', $existingRefund->id)
->update($this->filterPayloadByTableColumns($updateData, 'refunds'));
log_message('info', "Refund record updated for invoice ID {$invoiceId}, student ID {$studentId}.");
} else {
// Only set these once, for new entries
$insertData = [
'parent_id' => $parentId,
'invoice_id' => $invoiceId,
'refund_amount' => $refundAmount,
'requested_amount_cents' => $refundCents,
'approved_amount_cents' => null,
'currency' => 'USD',
'requested_at' => utc_now(),
'school_year' => $this->schoolYear,
'status' => 'Pending',
'reason' => 'Withdrawal under review for student ID ' . $studentId,
'request' => 'tuition',
'source_type' => 'tuition_withdrawal',
'source_id' => (int) $invoiceId,
'semester' => $this->semester,
'refund_paid_amount' => 0.0,
];
$refundTable->insert($this->filterPayloadByTableColumns($insertData, 'refunds'));
log_message('info', "Refund record created for invoice ID {$invoiceId}, student ID {$studentId}.");
}
} else {
log_message('error', "No invoice found for parent ID {$parentId}, student ID {$studentId}.");
}
} else {
log_message('error', "No active enrollment found for student ID $studentId.");
$withdrawalErrors[] = 'Student ID ' . $studentId . ': no active enrollment was found.';