269 lines
13 KiB
PHP
269 lines
13 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
require_once 'stripe/init.php';
|
|
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'coach') {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$coach_id = $_SESSION['user_id'];
|
|
|
|
// Handle form submission for creating a new package
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_package'])) {
|
|
$name = $_POST['name'];
|
|
$description = $_POST['description'];
|
|
$price = $_POST['price'];
|
|
$package_type = $_POST['package_type'];
|
|
$max_clients = !empty($_POST['max_clients']) ? $_POST['max_clients'] : null;
|
|
$start_date = !empty($_POST['start_date']) ? $_POST['start_date'] : null;
|
|
$end_date = !empty($_POST['end_date']) ? $_POST['end_date'] : null;
|
|
$payment_plan = !empty($_POST['payment_plan']) ? $_POST['payment_plan'] : null;
|
|
$service_items = $_POST['service_items'];
|
|
|
|
try {
|
|
// Step 1: Create a Product in Stripe
|
|
$product = \Stripe\Product::create([
|
|
'name' => $name,
|
|
'description' => $description,
|
|
]);
|
|
|
|
// Step 2: Create a Price in Stripe
|
|
$price_obj = \Stripe\Price::create([
|
|
'product' => $product->id,
|
|
'unit_amount' => $price * 100, // Price in cents
|
|
'currency' => 'usd',
|
|
]);
|
|
|
|
// Step 3: Save the package to the database
|
|
$stmt = db()->prepare(
|
|
'INSERT INTO service_packages (coach_id, name, description, price, stripe_product_id, stripe_price_id, package_type, max_clients, start_date, end_date, payment_plan, num_sessions)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)'
|
|
);
|
|
$stmt->execute([$coach_id, $name, $description, $price, $product->id, $price_obj->id, $package_type, $max_clients, $start_date, $end_date, $payment_plan]);
|
|
$package_id = db()->lastInsertId();
|
|
|
|
// Step 4: Save service items
|
|
$total_sessions = 0;
|
|
foreach ($service_items as $item) {
|
|
$item_type = $item['type'];
|
|
$item_quantity = $item['quantity'];
|
|
if (!empty($item_type) && !empty($item_quantity)) {
|
|
$stmt = db()->prepare('INSERT INTO package_service_items (package_id, service_type, quantity) VALUES (?, ?, ?)');
|
|
$stmt->execute([$package_id, $item_type, $item_quantity]);
|
|
$total_sessions += $item_quantity;
|
|
}
|
|
}
|
|
|
|
// Step 5: Update num_sessions in service_packages
|
|
$stmt = db()->prepare('UPDATE service_packages SET num_sessions = ? WHERE id = ?');
|
|
$stmt->execute([$total_sessions, $package_id]);
|
|
|
|
|
|
$success_message = 'Service package created successfully!';
|
|
} catch (\Stripe\Exception\ApiErrorException $e) {
|
|
$error_message = 'Stripe Error: ' . $e->getMessage();
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Database Error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
// Fetch existing packages for the coach
|
|
$stmt = db()->prepare('SELECT * FROM service_packages WHERE coach_id = ?');
|
|
$stmt->execute([$coach_id]);
|
|
$packages = $stmt->fetchAll();
|
|
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Manage Service Packages</title>
|
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<h2>Manage Service Packages</h2>
|
|
|
|
<?php if (isset($success_message)): ?>
|
|
<div class="alert alert-success"><?= htmlspecialchars($success_message) ?></div>
|
|
<?php endif; ?>
|
|
<?php if (isset($error_message)): ?>
|
|
<div class="alert alert-danger"><?= htmlspecialchars($error_message) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">Create New Package</div>
|
|
<div class="card-body">
|
|
<form method="POST">
|
|
<div class="form-group">
|
|
<label for="name">Package Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="description">Description</label>
|
|
<textarea class="form-control" id="description" name="description"></textarea>
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-6">
|
|
<label for="price">Price (USD)</label>
|
|
<input type="number" class="form-control" id="price" name="price" min="1" step="0.01" required>
|
|
</div>
|
|
<div class="form-group col-md-6">
|
|
<label for="package_type">Package Type</label>
|
|
<select class="form-control" id="package_type" name="package_type">
|
|
<option value="individual">Individual</option>
|
|
<option value="group">Group</option>
|
|
<option value="workshop">Workshop</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="form-row" id="group_workshop_fields" style="display: none;">
|
|
<div class="form-group col-md-6">
|
|
<label for="max_clients">Max Clients</label>
|
|
<input type="number" class="form-control" id="max_clients" name="max_clients" min="1">
|
|
</div>
|
|
</div>
|
|
<div class="form-row" id="workshop_fields" style="display: none;">
|
|
<div class="form-group col-md-6">
|
|
<label for="start_date">Start Date</label>
|
|
<input type="datetime-local" class="form-control" id="start_date" name="start_date">
|
|
</div>
|
|
<div class="form-group col-md-6">
|
|
<label for="end_date">End Date</label>
|
|
<input type="datetime-local" class="form-control" id="end_date" name="end_date">
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="payment_plan">Payment Plan Details</label>
|
|
<textarea class="form-control" id="payment_plan" name="payment_plan" placeholder="e.g., 3 monthly payments of $100"></textarea>
|
|
</div>
|
|
|
|
<hr>
|
|
<h5>Service Items</h5>
|
|
<div id="service_items_container">
|
|
<div class="row service-item">
|
|
<div class="col-md-5">
|
|
<div class="form-group">
|
|
<label>Service Type</label>
|
|
<select class="form-control" name="service_items[0][type]">
|
|
<option value="">-- Select --</option>
|
|
<option value="individual_session">Individual Session</option>
|
|
<option value="group_session">Group Session</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-5">
|
|
<div class="form-group">
|
|
<label>Quantity</label>
|
|
<input type="number" class="form-control" name="service_items[0][quantity]" min="1">
|
|
</div>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<button type="button" class="btn btn-danger remove-item" style="margin-top: 32px;">Remove</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<button type="button" id="add_service_item" class="btn btn-secondary">Add Service Item</button>
|
|
<hr>
|
|
|
|
<button type="submit" name="create_package" class="btn btn-primary">Create Package</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">Your Packages</div>
|
|
<div class="card-body">
|
|
<?php if (empty($packages)): ?>
|
|
<p>You have not created any packages yet.</p>
|
|
<?php else: ?>
|
|
<ul class="list-group">
|
|
<?php foreach ($packages as $package): ?>
|
|
<li class="list-group-item">
|
|
<h5><?= htmlspecialchars($package['name']) ?></h5>
|
|
<p><?= htmlspecialchars($package['description']) ?></p>
|
|
<p>
|
|
Price: $<?= htmlspecialchars($package['price']) ?> |
|
|
Type: <?= htmlspecialchars(ucfirst($package['package_type'])) ?> |
|
|
Total Sessions: <?= htmlspecialchars($package['num_sessions']) ?>
|
|
</p>
|
|
<?php if($package['max_clients']): ?>
|
|
<p>Max Clients: <?= htmlspecialchars($package['max_clients']) ?></p>
|
|
<?php endif; ?>
|
|
<?php if($package['start_date']): ?>
|
|
<p>Date: <?= date('M j, Y H:i', strtotime($package['start_date'])) ?> - <?= date('M j, Y H:i', strtotime($package['end_date'])) ?></p>
|
|
<?php endif; ?>
|
|
<?php if($package['payment_plan']): ?>
|
|
<p>Payment Plan: <?= htmlspecialchars($package['payment_plan']) ?></p>
|
|
<?php endif; ?>
|
|
|
|
<!-- TODO: Add Edit/Delete buttons -->
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<a href="dashboard.php" class="btn btn-secondary mt-4">Back to Dashboard</a>
|
|
</div>
|
|
|
|
<script>
|
|
document.getElementById('package_type').addEventListener('change', function() {
|
|
var groupWorkshopFields = document.getElementById('group_workshop_fields');
|
|
var workshopFields = document.getElementById('workshop_fields');
|
|
if (this.value === 'group' || this.value === 'workshop') {
|
|
groupWorkshopFields.style.display = 'flex';
|
|
} else {
|
|
groupWorkshopFields.style.display = 'none';
|
|
}
|
|
if (this.value === 'workshop') {
|
|
workshopFields.style.display = 'flex';
|
|
} else {
|
|
workshopFields.style.display = 'none';
|
|
}
|
|
});
|
|
|
|
var itemIndex = 1;
|
|
document.getElementById('add_service_item').addEventListener('click', function() {
|
|
var container = document.getElementById('service_items_container');
|
|
var newItem = document.createElement('div');
|
|
newItem.classList.add('row', 'service-item');
|
|
newItem.innerHTML = `
|
|
<div class="col-md-5">
|
|
<div class="form-group">
|
|
<label>Service Type</label>
|
|
<select class="form-control" name="service_items[${itemIndex}][type]">
|
|
<option value="">-- Select --</option>
|
|
<option value="individual_session">Individual Session</option>
|
|
<option value="group_session">Group Session</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-5">
|
|
<div class="form-group">
|
|
<label>Quantity</label>
|
|
<input type="number" class="form-control" name="service_items[${itemIndex}][quantity]" min="1">
|
|
</div>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<button type="button" class="btn btn-danger remove-item" style="margin-top: 32px;">Remove</button>
|
|
</div>
|
|
`;
|
|
container.appendChild(newItem);
|
|
itemIndex++;
|
|
});
|
|
|
|
document.getElementById('service_items_container').addEventListener('click', function(e) {
|
|
if (e.target && e.target.classList.contains('remove-item')) {
|
|
e.target.closest('.service-item').remove();
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|