318 lines
16 KiB
PHP
318 lines
16 KiB
PHP
<?= $this->extend('layout/management_layout') ?>
|
|
<?= $this->section('content') ?>
|
|
|
|
<div class="container mt-5">
|
|
<h3>Event Charges</h3>
|
|
|
|
<?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
|
|
$classSectionNames = $classSectionNames ?? [];
|
|
$semesterOptions = $semesterOptions ?? [];
|
|
$schoolYearOptions = $schoolYearOptions ?? [];
|
|
?>
|
|
|
|
<?php
|
|
$filterParentId = $filterParentId ?? 0;
|
|
$parentBalances = $parentBalances ?? [];
|
|
$selectedEvent = null;
|
|
if (!empty($filterEventId)) {
|
|
foreach ($events as $event) {
|
|
if ((int)$event['id'] === (int)$filterEventId) {
|
|
$selectedEvent = $event;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<div class="card mb-4">
|
|
<div class="card-body">
|
|
<form method="get" action="<?= site_url('administrator/event-charges') ?>" class="row g-3 align-items-end">
|
|
<?php if ($filterEventId > 0): ?>
|
|
<input type="hidden" name="event_id" value="<?= esc($filterEventId) ?>">
|
|
<?php endif; ?>
|
|
<?php if ($filterParentId > 0): ?>
|
|
<input type="hidden" name="parent_id" value="<?= esc($filterParentId) ?>">
|
|
<?php endif; ?>
|
|
<div class="col-md-4">
|
|
<label for="semester_filter" class="form-label">Semester</label>
|
|
<select id="semester_filter" name="semester" class="form-select">
|
|
<option value="">-- All semesters --</option>
|
|
<?php foreach ($semesterOptions as $option): ?>
|
|
<option value="<?= esc($option) ?>" <?= $semester === $option ? 'selected' : '' ?>>
|
|
<?= esc($option) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<label for="school_year_filter" class="form-label">School Year</label>
|
|
<select id="school_year_filter" name="school_year" class="form-select">
|
|
<option value="">-- All school years --</option>
|
|
<?php foreach ($schoolYearOptions as $option): ?>
|
|
<option value="<?= esc($option) ?>" <?= $school_year === $option ? 'selected' : '' ?>>
|
|
<?= esc($option) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-auto">
|
|
<button type="submit" class="btn btn-outline-primary">Filter</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add New Charge Form -->
|
|
<div class="card mb-4">
|
|
<div class="card-body">
|
|
<form id="event-participant-form" action="<?= site_url('payment/event_charges') ?>" method="post">
|
|
<?= csrf_field() ?>
|
|
<div class="row g-3">
|
|
<div class="col-md-4">
|
|
<label for="event_id" class="form-label">Select Event</label>
|
|
<select name="event_id" id="event_id" class="form-select" required>
|
|
<option value="">-- All events --</option>
|
|
<?php foreach ($events as $event): ?>
|
|
<option value="<?= esc($event['id']) ?>" <?= isset($filterEventId) && $filterEventId == $event['id'] ? 'selected' : '' ?>>
|
|
<?= esc($event['event_name']) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<?php if ($selectedEvent): ?>
|
|
<div class="col-md-8">
|
|
<div class="border rounded bg-light p-3 h-100">
|
|
<div class="d-flex justify-content-between align-items-center mb-2">
|
|
<h6 class="m-0"><?= esc($selectedEvent['event_name']) ?></h6>
|
|
<span class="badge bg-info text-dark">
|
|
$<?= esc(number_format($selectedEvent['amount'] ?? 0, 2)) ?> fee
|
|
</span>
|
|
</div>
|
|
<p class="mb-1 text-muted small">
|
|
<?= esc($selectedEvent['description'] ?: 'No description provided for this event.') ?>
|
|
</p>
|
|
<?php if (!empty($selectedEvent['expiration_date'])): ?>
|
|
<small class="text-secondary">
|
|
Expires: <?= esc(local_date($selectedEvent['expiration_date'], 'm-d-Y')) ?>
|
|
</small>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="col-md-4">
|
|
<label for="parent_id" class="form-label">Parents List</label>
|
|
<select id="parent_id" name="parent_id" class="form-select" required>
|
|
<option value="">-- Select Parent --</option>
|
|
<?php foreach ($parents as $parent): ?>
|
|
<option value="<?= esc($parent['id']) ?>" <?= $filterParentId && $filterParentId == $parent['id'] ? 'selected' : '' ?>>
|
|
<?= esc($parent['firstname'] . ' ' . $parent['lastname']) ?> (<?= esc($parent['school_id']) ?>)
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mt-4" id="studentCheckboxContainer" style="display: none;">
|
|
<label class="form-label">Select Students:</label>
|
|
<div id="studentList" class="row g-3"></div>
|
|
</div>
|
|
|
|
<div class="d-flex flex-wrap gap-2 mt-3">
|
|
<button type="submit" class="btn btn-primary">Submit</button>
|
|
<a href="<?= site_url('administrator/events') ?>" class="btn btn-success">Event List</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<!-- Charge Tables grouped by event -->
|
|
<?php
|
|
$grouped = [];
|
|
foreach ($charges as $charge) {
|
|
$label = $charge['event_name'] ?? 'N/A';
|
|
$grouped[$label][] = $charge;
|
|
}
|
|
?>
|
|
|
|
<?php if (empty($grouped)): ?>
|
|
<div class="alert alert-info">No charges found.</div>
|
|
<?php else: ?>
|
|
<?php foreach ($grouped as $eventLabel => $rows): ?>
|
|
<div class="card mb-4">
|
|
<div class="card-header bg-light fw-semibold">
|
|
<?= esc($eventLabel) ?>
|
|
</div>
|
|
<div class="card-body p-0">
|
|
<?php
|
|
$totalParticipants = 0;
|
|
$totalCharged = 0.0;
|
|
?>
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered table-striped mb-0 no-mgmt-sticky">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Parent Name</th>
|
|
<th>Student Name</th>
|
|
<th>Class Section</th>
|
|
<th>Charged Amount</th>
|
|
<th>Created</th>
|
|
<th>Fees Paid</th>
|
|
<th>Payment</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($rows as $charge): ?>
|
|
<?php
|
|
$isParticipating = ($charge['participation'] === 'yes');
|
|
$isEventPaid = !empty($charge['event_paid']);
|
|
?>
|
|
<tr>
|
|
<td><?= esc($charge['id']) ?></td>
|
|
<td><?= esc($charge['parent_firstname'] . ' ' . $charge['parent_lastname']) ?></td>
|
|
<td>
|
|
<?= $charge['student_firstname']
|
|
? esc($charge['student_firstname'] . ' ' . $charge['student_lastname'])
|
|
: '-' ?>
|
|
</td>
|
|
<td>
|
|
<?= esc($classSectionNames[$charge['class_section_id'] ?? ''] ?? '—') ?>
|
|
</td>
|
|
<td>$<?= esc(number_format($charge['event_amount'] ?? 0, 2)) ?></td>
|
|
<td><?= esc(!empty($charge['created_at']) ? local_datetime($charge['created_at'], 'm-d-Y H:i') : '') ?></td>
|
|
<?php
|
|
$feeAmount = (float) ($charge['event_amount'] ?? 0);
|
|
$hasBalanceRecord = array_key_exists((int)$charge['parent_id'], $parentBalances);
|
|
$parentBalance = $hasBalanceRecord ? (float)$parentBalances[$charge['parent_id']] : null;
|
|
$feeIsPaid = $isParticipating && ($isEventPaid || ($hasBalanceRecord && $parentBalance <= 0));
|
|
?>
|
|
<td class="text-center">
|
|
<?php if ($feeIsPaid): ?>
|
|
<span class="badge bg-success">Paid</span>
|
|
<?php else: ?>
|
|
<span class="badge bg-danger">Unpaid</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="text-center">
|
|
<form method="post" action="<?= site_url('administrator/event-charges/payment/' . esc($charge['id'])) ?>" class="d-inline-flex align-items-center">
|
|
<?= csrf_field() ?>
|
|
<input type="hidden" name="paid" value="<?= $feeIsPaid ? '1' : '0' ?>">
|
|
<input type="checkbox" class="form-check-input" id="eventPayment_<?= esc($charge['id']) ?>"
|
|
<?= $isEventPaid ? 'checked' : '' ?>
|
|
onchange="this.form.paid.value = this.checked ? 1 : 0; this.form.submit();">
|
|
<label class="form-check-label ms-2" for="eventPayment_<?= esc($charge['id']) ?>" aria-hidden="true">Paid</label>
|
|
</form>
|
|
</td>
|
|
<td class="text-nowrap">
|
|
<div class="d-flex gap-1 align-items-center">
|
|
<a href="<?= site_url('administrator/event-charges') ?>?event_id=<?= esc($charge['event_id']) ?>&parent_id=<?= esc($charge['parent_id']) ?>#event-participant-form" class="btn btn-outline-primary btn-sm">
|
|
Edit
|
|
</a>
|
|
<form action="<?= site_url('administrator/event-charges/remove/' . esc($charge['id'])) ?>" method="post" onsubmit="return confirm('Remove this participation and update the charge?');">
|
|
<?= csrf_field() ?>
|
|
<button type="submit" class="btn btn-outline-danger btn-sm">Remove</button>
|
|
</form>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php
|
|
if ($isParticipating) {
|
|
$totalParticipants++;
|
|
$totalCharged += (float) ($charge['event_amount'] ?? 0);
|
|
}
|
|
?>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
<tfoot class="table-light">
|
|
<tr>
|
|
<th colspan="4">Totals</th>
|
|
<th>$<?= esc(number_format($totalCharged, 2)) ?></th>
|
|
<th colspan="5">
|
|
<span class="badge bg-secondary">
|
|
<?= esc($totalParticipants) ?> participating
|
|
</span>
|
|
</th>
|
|
</tr>
|
|
</tfoot>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?= $this->endSection() ?>
|
|
|
|
<?= $this->section('scripts') ?>
|
|
<script>
|
|
function loadStudentsWithCharges() {
|
|
let parentId = $('#parent_id').val();
|
|
let eventId = $('#event_id').val();
|
|
let semester = '<?= esc($semester) ?>';
|
|
let schoolYear = '<?= esc($school_year) ?>';
|
|
|
|
if (parentId && eventId) {
|
|
$.get('<?= base_url('administrator/get-students-with-charges') ?>', {
|
|
parent_id: parentId,
|
|
event_id: eventId,
|
|
semester: semester,
|
|
school_year: schoolYear
|
|
}, function(data) {
|
|
let container = $('#studentList');
|
|
container.empty();
|
|
|
|
if (data.length > 0) {
|
|
$('#studentCheckboxContainer').show();
|
|
data.forEach(student => {
|
|
container.append(`
|
|
<div class="col-md-4">
|
|
<div class="form-check">
|
|
<input type="hidden" name="participation[${student.id}]" value="no">
|
|
<input class="form-check-input" type="checkbox" name="participation[${student.id}]" value="yes" id="student_${student.id}" ${student.charged ? 'checked' : ''}>
|
|
<label class="form-check-label" for="student_${student.id}">
|
|
${student.name}
|
|
</label>
|
|
</div>
|
|
</div>
|
|
`);
|
|
});
|
|
} else {
|
|
$('#studentCheckboxContainer').hide();
|
|
}
|
|
}, 'json');
|
|
} else {
|
|
$('#studentCheckboxContainer').hide();
|
|
$('#studentList').empty();
|
|
}
|
|
}
|
|
|
|
$(function() {
|
|
$('#parent_id').on('change', loadStudentsWithCharges);
|
|
|
|
$('#event_id').on('change', function() {
|
|
let eventId = $(this).val();
|
|
let url = new URL(window.location.href);
|
|
if (eventId) {
|
|
url.searchParams.set('event_id', eventId);
|
|
} else {
|
|
url.searchParams.delete('event_id');
|
|
}
|
|
window.location.href = url.toString();
|
|
});
|
|
|
|
if ($('#event_id').val() && $('#parent_id').val()) {
|
|
loadStudentsWithCharges();
|
|
}
|
|
});
|
|
</script>
|
|
<?= $this->endSection() ?>
|