36557-vm/submit_booking.php
Flatlogic Bot 08fcb2dae0 0.2
2025-12-01 21:25:15 +00:00

111 lines
4.8 KiB
PHP

<?php
session_start();
require_once 'auth.php';
require_once 'db/config.php';
// 1. Role-Based Access Control
if (!is_logged_in() || !is_agent()) {
header('Location: login.php');
exit;
}
$message = '';
$error = '';
// 2. Form Processing
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$plotId = $_POST['plot_id'] ?? '';
$amount = $_POST['amount'] ?? '';
$bookingDate = $_POST['booking_date'] ?? '';
$proofDocument = $_FILES['proof_document'] ?? null;
// Basic validation
if (empty($plotId) || empty($amount) || empty($bookingDate) || $proofDocument['error'] !== UPLOAD_ERR_OK) {
$error = 'All fields and a valid proof document are required.';
} else {
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($proofDocument['name']);
$fileType = strtolower(pathinfo($uploadFile, PATHINFO_EXTENSION));
// Check if file is a valid type (e.g., pdf, jpg, png)
$allowedTypes = ['pdf', 'jpg', 'jpeg', 'png'];
if (!in_array($fileType, $allowedTypes)) {
$error = 'Invalid file type. Only PDF, JPG, and PNG are allowed.';
} elseif (move_uploaded_file($proofDocument['tmp_name'], $uploadFile)) {
// File uploaded successfully, insert into database
$db = db();
$stmt = $db->prepare(
"INSERT INTO bookings (user_id, plot_id, amount, booking_date, proof_document, status) VALUES (:user_id, :plot_id, :amount, :booking_date, :proof_document, 'pending')"
);
try {
$stmt->execute([
':user_id' => $_SESSION['user_id'],
':plot_id' => $plotId,
':amount' => $amount,
':booking_date' => $bookingDate,
':proof_document' => $uploadFile
]);
$message = 'Booking submitted successfully! It is now pending approval.';
} catch (PDOException $e) {
$error = 'Database error: ' . $e->getMessage();
}
} else {
$error = 'Failed to upload proof document.';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Submit Booking</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-8 offset-md-2">
<div class="card">
<div class="card-header">
<h2>Submit a New Booking</h2>
<p>Fill out the form below to submit a new booking for approval.</p>
</div>
<div class="card-body">
<?php if ($message): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form action="submit_booking.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="plot_id">Plot ID</label>
<input type="text" class="form-control" id="plot_id" name="plot_id" required>
</div>
<div class="form-group">
<label for="amount">Booking Amount (INR)</label>
<input type="number" class="form-control" id="amount" name="amount" step="0.01" required>
</div>
<div class="form-group">
<label for="booking_date">Booking Date</label>
<input type="date" class="form-control" id="booking_date" name="booking_date" required>
</div>
<div class="form-group">
<label for="proof_document">Proof of Booking (PDF, JPG, PNG)</label>
<input type="file" class="form-control-file" id="proof_document" name="proof_document" required>
</div>
<button type="submit" class="btn btn-primary">Submit for Approval</button>
<a href="dashboard.php" class="btn btn-secondary">Back to Dashboard</a>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>