144 lines
6.4 KiB
PHP
144 lines
6.4 KiB
PHP
<?php
|
|
require_once 'session.php';
|
|
check_admin();
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$properties = [];
|
|
try {
|
|
$db = db();
|
|
$stmt = $db->query('SELECT id, name FROM properties ORDER BY name');
|
|
$properties = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
// Don't block the page, just log error or show a message
|
|
error_log("Failed to fetch properties: " . $e->getMessage());
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = $_POST['name'] ?? '';
|
|
$email = $_POST['email'] ?? '';
|
|
$phone = $_POST['phone'] ?? '';
|
|
$property_id = !empty($_POST['property_id']) ? (int)$_POST['property_id'] : null;
|
|
$lease_start = !empty($_POST['lease_start']) ? $_POST['lease_start'] : null;
|
|
$lease_end = !empty($_POST['lease_end']) ? $_POST['lease_end'] : null;
|
|
$rent_due = !empty($_POST['rent_due']) ? (float)$_POST['rent_due'] : null;
|
|
$security_deposit = !empty($_POST['security_deposit']) ? (float)$_POST['security_deposit'] : null;
|
|
$status = $_POST['status'] ?? 'active';
|
|
|
|
try {
|
|
$db = db();
|
|
$db->beginTransaction();
|
|
$sql = "INSERT INTO tenants (name, email, phone, property_id, lease_start, lease_end, rent_due, security_deposit, status) VALUES (:name, :email, :phone, :property_id, :lease_start, :lease_end, :rent_due, :security_deposit, :status)";
|
|
$stmt = $db->prepare($sql);
|
|
$stmt->bindParam(':name', $name);
|
|
$stmt->bindParam(':email', $email);
|
|
$stmt->bindParam(':phone', $phone);
|
|
$stmt->bindParam(':property_id', $property_id);
|
|
$stmt->bindParam(':lease_start', $lease_start);
|
|
$stmt->bindParam(':lease_end', $lease_end);
|
|
$stmt->bindParam(':rent_due', $rent_due);
|
|
$stmt->bindParam(':security_deposit', $security_deposit);
|
|
$stmt->bindParam(':status', $status);
|
|
$stmt->execute();
|
|
$tenant_id = $db->lastInsertId();
|
|
|
|
if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
|
|
$upload_dir = 'uploads/';
|
|
$file_name = uniqid() . '_' . basename($_FILES['file']['name']);
|
|
$target_file = $upload_dir . $file_name;
|
|
|
|
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
|
|
$stmt = $db->prepare("INSERT INTO files (file_name, file_path, tenant_id) VALUES (?, ?, ?)");
|
|
$stmt->execute([$_FILES['file']['name'], $target_file, $tenant_id]);
|
|
} else {
|
|
throw new Exception("Failed to upload file.");
|
|
}
|
|
}
|
|
$db->commit();
|
|
header('Location: index.php?tab=tenants&message=Tenant added successfully.');
|
|
exit;
|
|
} catch (Exception $e) {
|
|
$db->rollBack();
|
|
$error = "Error adding tenant: " . $e->getMessage();
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Add Tenant</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">
|
|
<h1 class="text-white">Add New Tenant</h1>
|
|
<?php if (!empty($error)): ?>
|
|
<div class="alert alert-danger"><?php echo $error; ?></div>
|
|
<?php endif; ?>
|
|
<form action="add_tenant.php" method="post" class="card p-4 card-dark" enctype="multipart/form-data">
|
|
<div class="form-group">
|
|
<label for="name">Full Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" required>
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-6">
|
|
<label for="email">Email</label>
|
|
<input type="email" class="form-control" id="email" name="email" required>
|
|
</div>
|
|
<div class="form-group col-md-6">
|
|
<label for="phone">Phone</label>
|
|
<input type="tel" class="form-control" id="phone" name="phone">
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="property_id">Assign to Property</label>
|
|
<select class="form-control" id="property_id" name="property_id">
|
|
<option value="">None</option>
|
|
<?php foreach ($properties as $prop): ?>
|
|
<option value="<?php echo htmlspecialchars($prop['id']); ?>">
|
|
<?php echo htmlspecialchars($prop['name']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-6">
|
|
<label for="lease_start">Lease Start</label>
|
|
<input type="date" class="form-control" id="lease_start" name="lease_start">
|
|
</div>
|
|
<div class="form-group col-md-6">
|
|
<label for="lease_end">Lease End</label>
|
|
<input type="date" class="form-control" id="lease_end" name="lease_end">
|
|
</div>
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-6">
|
|
<label for="rent_due">Rent Amount</label>
|
|
<input type="number" step="0.01" class="form-control" id="rent_due" name="rent_due">
|
|
</div>
|
|
<div class="form-group col-md-6">
|
|
<label for="security_deposit">Security Deposit</label>
|
|
<input type="number" step="0.01" class="form-control" id="security_deposit" name="security_deposit">
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="status">Status</label>
|
|
<select class="form-control" id="status" name="status">
|
|
<option value="active" selected>Active</option>
|
|
<option value="inactive">Inactive</option>
|
|
<option value="moved_out">Moved Out</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="file">Upload Document</label>
|
|
<input type="file" class="form-control" id="file" name="file">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Add Tenant</button>
|
|
<a href="index.php?tab=tenants" class="btn btn-secondary mt-2">Cancel</a>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|