<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Include database configuration
require_once '../database/db_config.php';
// Authentication check
session_start();
if (!isset($_SESSION['admin_id']) && !isset($_SESSION['user_id'])) {
echo "<div style='background-color: #f8d7da; color: #721c24; padding: 10px; border: 1px solid #f5c6cb; border-radius: 4px; margin-bottom: 10px;'>
<strong>Error:</strong> You must be logged in to access this page.
</div>";
exit;
}
// Check if the user has admin rights
$is_admin = isset($_SESSION['role']) && $_SESSION['role'] === 'admin';
if (!$is_admin) {
echo "<div style='background-color: #f8d7da; color: #721c24; padding: 10px; border: 1px solid #f5c6cb; border-radius: 4px; margin-bottom: 10px;'>
<strong>Error:</strong> You must have administrator privileges to access this page.
</div>";
exit;
}
// Initialize variables
$price = isset($_POST['price']) ? floatval($_POST['price']) : 10000;
$duration = isset($_POST['duration']) ? $_POST['duration'] : '6 months';
$course_id = isset($_POST['course_id']) ? intval($_POST['course_id']) : 0;
// Get course data if a course ID is provided
$course_data = null;
if ($course_id > 0) {
$course_query = "SELECT id, title, price, discount_price, duration FROM courses WHERE id = ?";
$stmt = $conn->prepare($course_query);
$stmt->bind_param("i", $course_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$course_data = $result->fetch_assoc();
$price = $course_data['discount_price'] > 0 ? $course_data['discount_price'] : $course_data['price'];
$duration = $course_data['duration'];
}
}
// Get all courses for dropdown
$courses_query = "SELECT id, title, price, discount_price, duration FROM courses ORDER BY title";
$courses_result = $conn->query($courses_query);
$courses = [];
if ($courses_result) {
while ($row = $courses_result->fetch_assoc()) {
$courses[] = $row;
}
}
// Calculate payment plans based on provided duration and price
function calculatePaymentPlans($duration, $price) {
// Parse duration
$duration_parts = explode(' ', $duration);
$duration_value = isset($duration_parts[0]) ? intval($duration_parts[0]) : 3;
$duration_unit = isset($duration_parts[1]) ? strtolower($duration_parts[1]) : 'months';
// Normalize to months
$duration_in_months = $duration_value;
if ($duration_unit === 'days') {
$duration_in_months = max(1, ceil($duration_value / 30));
} else if ($duration_unit === 'weeks') {
$duration_in_months = max(1, ceil($duration_value / 4));
} else if ($duration_unit === 'years') {
$duration_in_months = $duration_value * 12;
}
// Calculate payment plans
$monthly_price = round($price / $duration_in_months);
$half_duration_months = max(1, ceil($duration_in_months / 2));
$half_duration_price = round($price / $half_duration_months);
// Create payment plans array
$plans = [
'full' => [
'name' => 'Full Payment',
'installments' => 1,
'amount_per_installment' => $price,
'total' => $price,
'frequency' => 'One-time payment',
'description' => 'Pay the full amount at once'
],
'half_duration' => [
'name' => 'Half Duration Installments',
'installments' => $half_duration_months,
'amount_per_installment' => $half_duration_price,
'total' => $half_duration_price * $half_duration_months,
'frequency' => 'Every ' . ceil($duration_in_months / $half_duration_months) . ' month(s)',
'description' => 'Pay in ' . $half_duration_months . ' installments over the course duration'
],
'monthly' => [
'name' => 'Monthly Installments',
'installments' => $duration_in_months,
'amount_per_installment' => $monthly_price,
'total' => $monthly_price * $duration_in_months,
'frequency' => 'Monthly',
'description' => 'Pay monthly over ' . $duration_in_months . ' months'
]
];
// Add six-month plan if course is longer than 6 months
if ($duration_in_months > 6) {
$six_month_installments = max(1, ceil($duration_in_months / 6));
$six_month_price = round($price / $six_month_installments);
$plans['six_month'] = [
'name' => 'Six-Month Installments',
'installments' => $six_month_installments,
'amount_per_installment' => $six_month_price,
'total' => $six_month_price * $six_month_installments,
'frequency' => 'Every 6 months',
'description' => 'Pay every 6 months over the course duration'
];
}
return [
'duration_details' => [
'original' => $duration,
'value' => $duration_value,
'unit' => $duration_unit,
'months' => $duration_in_months
],
'plans' => $plans
];
}
// Calculate plans if form submitted
$payment_plans = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$payment_plans = calculatePaymentPlans($duration, $price);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Payment Plan Calculator</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<style>
.card {
transition: all 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
.plan-card {
border-radius: 10px;
overflow: hidden;
}
.plan-header {
padding: 15px;
color: white;
}
.plan-monthly .plan-header {
background: linear-gradient(45deg, #4e73df, #2e59d9);
}
.plan-half_duration .plan-header {
background: linear-gradient(45deg, #1cc88a, #13855c);
}
.plan-full .plan-header {
background: linear-gradient(45deg, #36b9cc, #258391);
}
.plan-six_month .plan-header {
background: linear-gradient(45deg, #f6c23e, #dda20a);
}
.installment-badge {
position: absolute;
top: 10px;
right: 10px;
padding: 5px 10px;
border-radius: 20px;
font-size: 12px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container py-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1>Payment Plan Calculator</h1>
<a href="../index.php" class="btn btn-secondary">Back to Admin</a>
</div>
<div class="row">
<div class="col-md-4">
<div class="card mb-4">
<div class="card-header bg-primary text-white">
<h5 class="mb-0">Calculate Payment Plans</h5>
</div>
<div class="card-body">
<form method="post" action="">
<div class="mb-3">
<label for="course_id" class="form-label">Select Course</label>
<select name="course_id" id="course_id" class="form-select">
<option value="0">-- Custom Values --</option>
<?php foreach ($courses as $course):
$course_price = $course['discount_price'] > 0 ? $course['discount_price'] : $course['price'];
?>
<option value="<?php echo $course['id']; ?>" <?php echo $course_id === $course['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($course['title'] . ' - ' . $course['duration'] . ' - ₹' . number_format($course_price, 2)); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="price" class="form-label">Course Price</label>
<div class="input-group">
<span class="input-group-text">₹</span>
<input type="number" class="form-control" id="price" name="price" value="<?php echo $price; ?>" min="0" step="1">
</div>
</div>
<div class="mb-3">
<label for="duration" class="form-label">Course Duration</label>
<input type="text" class="form-control" id="duration" name="duration" value="<?php echo htmlspecialchars($duration); ?>" placeholder="e.g. 6 months, 1 year, 12 weeks">
<div class="form-text">Format: number + unit (days, weeks, months, years)</div>
</div>
<button type="submit" class="btn btn-primary w-100">Calculate Plans</button>
</form>
</div>
</div>
<?php if ($payment_plans): ?>
<div class="card">
<div class="card-header bg-info text-white">
<h5 class="mb-0">Duration Details</h5>
</div>
<div class="card-body">
<table class="table table-striped">
<tr>
<td>Original Format:</td>
<td><?php echo $payment_plans['duration_details']['original']; ?></td>
</tr>
<tr>
<td>Value:</td>
<td><?php echo $payment_plans['duration_details']['value']; ?></td>
</tr>
<tr>
<td>Unit:</td>
<td><?php echo $payment_plans['duration_details']['unit']; ?></td>
</tr>
<tr>
<td>Duration in Months:</td>
<td><?php echo $payment_plans['duration_details']['months']; ?></td>
</tr>
</table>
</div>
</div>
<?php endif; ?>
</div>
<div class="col-md-8">
<?php if ($payment_plans): ?>
<div class="row">
<div class="col-12 mb-4">
<div class="card">
<div class="card-header bg-success text-white">
<h5 class="mb-0">Payment Plans for <?php echo $course_data ? htmlspecialchars($course_data['title']) : 'Custom Course'; ?></h5>
</div>
<div class="card-body">
<div class="alert alert-info">
<i class="fas fa-info-circle me-2"></i>
Based on a course duration of <strong><?php echo $payment_plans['duration_details']['original']; ?></strong>
(calculated as <strong><?php echo $payment_plans['duration_details']['months']; ?> months</strong>)
and a price of <strong>₹<?php echo number_format($price, 2); ?></strong>.
</div>
<div class="row">
<?php foreach ($payment_plans['plans'] as $plan_key => $plan): ?>
<div class="col-lg-6 mb-4">
<div class="card h-100 plan-card plan-<?php echo $plan_key; ?>">
<div class="plan-header">
<h5><?php echo $plan['name']; ?></h5>
<h3 class="mb-0">₹<?php echo number_format($plan['amount_per_installment'], 2); ?></h3>
<span class="opacity-75">per installment</span>
<span class="badge bg-light text-dark installment-badge">
<?php echo $plan['installments']; ?> installment<?php echo $plan['installments'] > 1 ? 's' : ''; ?>
</span>
</div>
<div class="card-body">
<ul class="list-group list-group-flush mb-3">
<li class="list-group-item d-flex justify-content-between">
<span>Frequency:</span>
<strong><?php echo $plan['frequency']; ?></strong>
</li>
<li class="list-group-item d-flex justify-content-between">
<span>Installment amount:</span>
<strong>₹<?php echo number_format($plan['amount_per_installment'], 2); ?></strong>
</li>
<li class="list-group-item d-flex justify-content-between">
<span>Total amount:</span>
<strong>₹<?php echo number_format($plan['total'], 2); ?></strong>
</li>
<li class="list-group-item">
<span>Description:</span><br>
<small><?php echo $plan['description']; ?></small>
</li>
</ul>
<div class="alert alert-secondary mb-0">
<strong>Database Value:</strong> <code><?php echo $plan_key; ?></code>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<div class="col-12">
<div class="card">
<div class="card-header bg-warning text-dark">
<h5 class="mb-0">Implementation Guide</h5>
</div>
<div class="card-body">
<h6>Database Configuration</h6>
<p>These payment plans should be saved in the <code>payment_plan</code> column of the <code>enrollments</code> table with one of these values:</p>
<ul>
<?php foreach ($payment_plans['plans'] as $plan_key => $plan): ?>
<li><code><?php echo $plan_key; ?></code> - <?php echo $plan['name']; ?></li>
<?php endforeach; ?>
</ul>
<h6 class="mt-4">PHP Calculation Code Example</h6>
<pre class="bg-light p-3 rounded"><code>// Parse course duration to get value and unit
$duration_parts = explode(' ', $course['duration']);
$duration_value = isset($duration_parts[0]) ? intval($duration_parts[0]) : 3;
$duration_unit = isset($duration_parts[1]) ? strtolower($duration_parts[1]) : 'months';
// Normalize to months for calculation
$duration_in_months = $duration_value;
if ($duration_unit === 'days') {
$duration_in_months = max(1, ceil($duration_value / 30));
} else if ($duration_unit === 'weeks') {
$duration_in_months = max(1, ceil($duration_value / 4));
} else if ($duration_unit === 'years') {
$duration_in_months = $duration_value * 12;
}
// Calculate payment plans
$monthly_price = round($price / $duration_in_months);
$half_duration_months = max(1, ceil($duration_in_months / 2));
$half_duration_price = round($price / $half_duration_months);</code></pre>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="card">
<div class="card-body text-center py-5">
<h3>Select a course or enter custom values</h3>
<p class="text-muted">Submit the form to view payment plan calculations</p>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Auto-update form when course is selected
document.getElementById('course_id').addEventListener('change', function() {
if (this.value != '0') {
// Auto-submit the form
this.form.submit();
}
});
</script>
</body>
</html>