0ac3a8375e
- load global semester helpers consistently and use date-based semester defaults - fix grading and daily attendance duplicate student/section rows - keep attendance violations scoped to the current semester by default - update invoice, refund, discount, payment, and financial aid flows - add configuration cleanup migrations for duplicate calendar/semester keys - refresh parent registration/report-card and print request handling - update related models, services, views, cron notes, and test coverage
330 lines
12 KiB
PHP
330 lines
12 KiB
PHP
<?= $this->extend('layout/management_layout') ?>
|
|
<?= $this->section('content') ?>
|
|
|
|
<div class="container-fluid mt-4">
|
|
<h2 class="text-center mt-4 mb-3">Refunds</h2>
|
|
|
|
<!-- Flash messages -->
|
|
<?php if (session()->getFlashdata('success')): ?>
|
|
<div class="alert alert-success"><?= session()->getFlashdata('success') ?></div>
|
|
<?php endif; ?>
|
|
<?php if (session()->getFlashdata('error')): ?>
|
|
<div class="alert alert-danger"><?= session()->getFlashdata('error') ?></div>
|
|
<?php endif; ?>
|
|
<?php if (session()->getFlashdata('info')): ?>
|
|
<div class="alert alert-info mb-3 d-inline-block"><?= session()->getFlashdata('info') ?></div>
|
|
<?php endif; ?>
|
|
|
|
<!-- (Optional) Bulk calculation form kept for future use -->
|
|
<form method="post" action="/refunds/processRefunds">
|
|
<?= csrf_field() ?>
|
|
<?php
|
|
$submitted = [];
|
|
foreach ($refunds as $refund):
|
|
if (!empty($refund['request']) && !in_array($refund['parent_id'], $submitted, true)):
|
|
$submitted[] = $refund['parent_id'];
|
|
?>
|
|
<input type="hidden" name="parent_ids[]" value="<?= esc($refund['parent_id']) ?>">
|
|
<?php
|
|
endif;
|
|
endforeach;
|
|
?>
|
|
<?php if (empty($submitted)): ?>
|
|
<div class="alert alert-info mb-3 d-inline-block">No eligible parents for refund calculation.</div>
|
|
<?php endif; ?>
|
|
</form>
|
|
|
|
<div class="table-responsive">
|
|
<table id="refundsTable" class="table table-bordered table-striped align-middle w-100">
|
|
<thead>
|
|
<tr>
|
|
<th>Parent</th>
|
|
<th>Invoice #</th>
|
|
<th>Requested</th>
|
|
<th>Status</th>
|
|
<th>Refund Details</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($refunds as $r): ?>
|
|
<?php
|
|
// Date formatting (keep raw if null)
|
|
$fmt = function($dt) {
|
|
if (empty($dt) || $dt === '0000-00-00 00:00:00') return '-';
|
|
// show date only; assumes DB is UTC
|
|
return local_date($dt, 'm-d-Y');
|
|
};
|
|
$statusRaw = (string)($r['status'] ?? '');
|
|
$statusKey = strtolower(str_replace(' ', '_', trim($statusRaw)));
|
|
if ($statusKey === 'requested') {
|
|
$statusKey = 'pending';
|
|
} elseif ($statusKey === 'partially_paid') {
|
|
$statusKey = 'partial';
|
|
}
|
|
$statusLabel = [
|
|
'pending' => 'Pending',
|
|
'approved' => 'Approved',
|
|
'rejected' => 'Rejected',
|
|
'partial' => 'Partial',
|
|
'paid' => 'Paid',
|
|
][$statusKey] ?? ($statusRaw !== '' ? $statusRaw : 'Pending');
|
|
$statusClass = [
|
|
'pending' => 'warning text-dark',
|
|
'approved' => 'primary',
|
|
'rejected' => 'danger',
|
|
'partial' => 'info text-dark',
|
|
'paid' => 'success',
|
|
][$statusKey] ?? 'secondary';
|
|
$refundAmount = (float)($r['refund_amount'] ?? 0);
|
|
$paidAmount = (float)($r['refund_paid_amount'] ?? 0);
|
|
$remainingAmount = max(0, $refundAmount - $paidAmount);
|
|
$hasSource = !empty($r['source_type']) && !empty($r['source_id']);
|
|
$canApprove = $statusKey === 'pending' && $refundAmount > 0 && $hasSource;
|
|
$canReject = $statusKey === 'pending';
|
|
$canRecord = in_array($statusKey, ['approved', 'partial'], true) && $remainingAmount > 0;
|
|
?>
|
|
<tr>
|
|
<td><?= esc(($r['firstname'] ?? '').' '.($r['lastname'] ?? '')) ?></td>
|
|
<td><?= esc($r['invoice_number'] ?? '-') ?></td>
|
|
<td>
|
|
<div class="fw-semibold">$<?= esc(number_format($refundAmount, 2)) ?></div>
|
|
<div class="text-muted small"><?= esc($fmt($r['requested_at'] ?? null)) ?></div>
|
|
</td>
|
|
<td>
|
|
<div><span class="badge bg-<?= esc($statusClass) ?>"><?= esc($statusLabel) ?></span></div>
|
|
<?php if (!empty($r['approved_at']) && $r['approved_at'] !== '0000-00-00 00:00:00'): ?>
|
|
<div class="text-muted small mt-1">
|
|
<?= esc($fmt($r['approved_at'])) ?>
|
|
<?php if (!empty($r['approved_by_name']) && $r['approved_by_name'] !== '-'): ?>
|
|
by <?= esc($r['approved_by_name']) ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<div><span class="text-muted small">Check #:</span> <?= esc($r['check_nbr'] ?? '-') ?></div>
|
|
<div>
|
|
<span class="text-muted small">Check File:</span>
|
|
<?php if (!empty($r['check_file'])): ?>
|
|
<a href="<?= base_url('refunds/file/' . (int) $r['id'] . '/inline') ?>" target="_blank">View</a>
|
|
<?php else: ?>
|
|
-
|
|
<?php endif; ?>
|
|
</div>
|
|
<div><span class="text-muted small">Paid:</span> $<?= esc(number_format($paidAmount, 2)) ?></div>
|
|
</td>
|
|
<td>
|
|
<div class="d-flex gap-2 flex-wrap">
|
|
<button class="btn btn-success btn-sm"
|
|
<?= $canRecord ? '' : 'disabled' ?>
|
|
onclick="handleRecordRefundClick(<?= (int)$r['id'] ?>, '<?= esc($statusLabel) ?>', <?= $remainingAmount ?>)">
|
|
Record
|
|
</button>
|
|
<button class="btn btn-primary btn-sm"
|
|
<?= $canApprove ? '' : 'disabled' ?>
|
|
onclick="handleStatusClick(<?= (int)$r['id'] ?>, 'Approved', <?= $refundAmount ?>)">
|
|
Approve
|
|
</button>
|
|
<button class="btn btn-danger btn-sm"
|
|
<?= $canReject ? '' : 'disabled' ?>
|
|
onclick="handleStatusClick(<?= (int)$r['id'] ?>, 'Rejected', <?= max($refundAmount, 0.01) ?>)">
|
|
Reject
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal -->
|
|
<div class="modal fade" id="statusModal" tabindex="-1" aria-labelledby="statusModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
|
|
<!-- APPROVE / REJECT FORM -->
|
|
<form id="statusForm" method="post" action="/refunds/updateStatus" class="d-none">
|
|
<?= csrf_field() ?>
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Update Refund Status</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" name="refund_id" id="refundIdStatus">
|
|
<input type="hidden" name="status" id="newStatus">
|
|
<p id="confirmationText" class="mb-2">
|
|
Please provide a reason to <strong id="statusText"></strong> this refund.
|
|
</p>
|
|
<div class="mb-3">
|
|
<label for="reason" class="form-label">Reason <span class="text-danger">*</span></label>
|
|
<textarea class="form-control" name="reason" id="reason" rows="3" required></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-primary">Confirm</button>
|
|
</div>
|
|
</form>
|
|
|
|
<!-- PAYMENT FORM -->
|
|
<form id="paymentForm" method="post" action="/refunds/updatePayment" enctype="multipart/form-data" class="d-none">
|
|
<?= csrf_field() ?>
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Record Refund</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" name="refund_id" id="refundIdPayment">
|
|
<input type="hidden" name="idempotency_key" id="refundPayoutIdempotencyKey">
|
|
<div class="mb-3">
|
|
<label for="paidAmount" class="form-label">Paid Amount</label>
|
|
<input type="number" step="0.01" min="0.01" class="form-control" name="paid_amount" id="paidAmount" required>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="paymentMethod" class="form-label">Payment Method</label>
|
|
<select class="form-select" name="payment_method" id="paymentMethod" required>
|
|
<option value="">-- Select Method --</option>
|
|
<option value="Check">Check</option>
|
|
<option value="Online">Online</option>
|
|
<option value="Cash">Cash</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div id="checkDetails" class="d-none">
|
|
<div class="mb-3">
|
|
<label for="checkNumber" class="form-label">Check Number</label>
|
|
<input type="text" class="form-control" name="check_number" id="checkNumber">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="checkFile" class="form-label">Upload Check Image</label>
|
|
<input type="file" class="form-control" name="check_file" id="checkFile" accept=".jpg,.jpeg,.png,.pdf">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-primary">Submit Payment</button>
|
|
</div>
|
|
</form>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?= $this->endSection() ?>
|
|
|
|
<?= $this->section('scripts') ?>
|
|
<script>
|
|
$(function () {
|
|
// DataTable
|
|
$('#refundsTable').DataTable({
|
|
pageLength: 25,
|
|
order: [[2, 'desc']], // order by Requested desc
|
|
scrollX: true,
|
|
autoWidth: false,
|
|
});
|
|
|
|
// Status form (approve/reject)
|
|
$('#statusForm').on('submit', function (e) {
|
|
e.preventDefault();
|
|
const reason = $('#reason').val().trim();
|
|
if (!reason) {
|
|
alert('Reason is required.');
|
|
return;
|
|
}
|
|
$.post($(this).attr('action'), $(this).serialize())
|
|
.done(function (resp) { alert(resp.success || resp.error); location.reload(); })
|
|
.fail(function () { alert('An error occurred. Please try again.'); });
|
|
});
|
|
|
|
// Payment form
|
|
$('#paymentForm').on('submit', function (e) {
|
|
e.preventDefault();
|
|
const fd = new FormData(this);
|
|
$.ajax({
|
|
url: $(this).attr('action'),
|
|
method: 'POST',
|
|
data: fd,
|
|
processData: false,
|
|
contentType: false
|
|
})
|
|
.done(function (resp) { alert(resp.success || resp.error); location.reload(); })
|
|
.fail(function () { alert('An error occurred. Please try again.'); });
|
|
});
|
|
|
|
// Toggle check details by method
|
|
$('#paymentMethod').on('change', function () {
|
|
if ($(this).val() === 'Check') {
|
|
$('#checkDetails').removeClass('d-none');
|
|
} else {
|
|
$('#checkDetails').addClass('d-none');
|
|
$('#checkNumber').val('');
|
|
$('#checkFile').val('');
|
|
}
|
|
});
|
|
});
|
|
|
|
// ---- Actions ----
|
|
function normalizeRefundStatus(status) {
|
|
return String(status || '').trim().toLowerCase().replace(/\s+/g, '_');
|
|
}
|
|
|
|
function handleRecordRefundClick(refundId, status, amount) {
|
|
const normalized = normalizeRefundStatus(status);
|
|
if (!['approved','partial','partially_paid'].includes(normalized)) {
|
|
alert('Refund must be approved or partial before recording a payout.');
|
|
return;
|
|
}
|
|
if (!amount || parseFloat(amount) <= 0) {
|
|
alert('Refund amount is not set.');
|
|
return;
|
|
}
|
|
showPaymentModal(refundId, amount);
|
|
}
|
|
|
|
function handleStatusClick(refundId, status, amount) {
|
|
const normalized = normalizeRefundStatus(status);
|
|
if (normalized !== 'rejected' && (!amount || parseFloat(amount) <= 0)) {
|
|
alert('Refund amount is not set.');
|
|
return;
|
|
}
|
|
showStatusModal(refundId, status);
|
|
}
|
|
|
|
function showStatusModal(refundId, status) {
|
|
$('#paymentForm').addClass('d-none');
|
|
$('#statusForm').removeClass('d-none');
|
|
|
|
$('#refundIdStatus').val(refundId);
|
|
$('#newStatus').val(status);
|
|
$('#reason').val('');
|
|
$('#statusText').text(status.toLowerCase());
|
|
$('#statusModal').modal('show');
|
|
}
|
|
|
|
function showPaymentModal(refundId, amount) {
|
|
$('#statusForm').addClass('d-none');
|
|
$('#paymentForm').removeClass('d-none');
|
|
|
|
$('#refundIdPayment').val(refundId);
|
|
$('#refundPayoutIdempotencyKey').val(
|
|
(window.crypto && crypto.randomUUID)
|
|
? crypto.randomUUID()
|
|
: ('refund-' + refundId + '-' + Date.now() + '-' + Math.random().toString(16).slice(2))
|
|
);
|
|
$('#paidAmount').val(Number(amount || 0).toFixed(2));
|
|
$('#paidAmount').attr('max', Number(amount || 0).toFixed(2));
|
|
$('#paymentMethod').val('').trigger('change');
|
|
$('#checkDetails').addClass('d-none');
|
|
$('#checkNumber').val('');
|
|
$('#checkFile').val('');
|
|
|
|
$('#statusModal').modal('show');
|
|
}
|
|
</script>
|
|
<?= $this->endSection() ?>
|