36952-vm/edit_tenant.php
Flatlogic Bot 76d7d99142 PMS 1
2025-12-15 01:31:18 +00:00

189 lines
8.6 KiB
PHP

<?php
require_once 'session.php';
check_admin();
require_once 'db/config.php';
$id = $_GET['id'] ?? 0;
if (!$id) {
header('Location: index.php?tab=tenants');
exit;
}
$db = db();
$tenant = null;
$properties = [];
try {
// Fetch tenant
$stmt = $db->prepare("SELECT * FROM tenants WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$tenant = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$tenant) {
header('Location: index.php?tab=tenants');
exit;
}
// Fetch properties for dropdown
$stmt = $db->query('SELECT id, name FROM properties ORDER BY name');
$properties = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$error = "DB Error: " . $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->beginTransaction();
$sql = "UPDATE tenants SET name = :name, email = :email, phone = :phone, property_id = :property_id, lease_start = :lease_start, lease_end = :lease_end, rent_due = :rent_due, security_deposit = :security_deposit, status = :status WHERE id = :id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$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();
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, $id]);
} else {
throw new Exception("Failed to upload file.");
}
}
$db->commit();
header('Location: index.php?tab=tenants&message=Tenant updated successfully.');
exit;
} catch (Exception $e) {
$db->rollBack();
$error = "Error updating 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>Edit 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">Edit Tenant</h1>
<?php if (!empty($error)): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if ($tenant): ?>
<form action="edit_tenant.php?id=<?php echo $id; ?>" 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" value="<?php echo htmlspecialchars($tenant['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" value="<?php echo htmlspecialchars($tenant['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" value="<?php echo htmlspecialchars($tenant['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 ($tenant['property_id'] == $prop['id']) ? 'selected' : ''; ?>>
<?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" value="<?php echo htmlspecialchars($tenant['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" value="<?php echo htmlspecialchars($tenant['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" value="<?php echo htmlspecialchars($tenant['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" value="<?php echo htmlspecialchars($tenant['security_deposit']); ?>">
</div>
</div>
<div class="form-group">
<label for="status">Status</label>
<select class="form-control" id="status" name="status">
<option value="active" <?php echo ($tenant['status'] === 'active') ? 'selected' : ''; ?>>Active</option>
<option value="inactive" <?php echo ($tenant['status'] === 'inactive') ? 'selected' : ''; ?>>Inactive</option>
<option value="moved_out" <?php echo ($tenant['status'] === 'moved_out') ? 'selected' : ''; ?>>Moved Out</option>
</select>
</div>
<div class="form-group">
<label for="file">Upload New Document</label>
<input type="file" class="form-control" id="file" name="file">
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
<a href="index.php?tab=tenants" class="btn btn-secondary mt-2">Cancel</a>
</form>
<hr>
<h5 class="text-white">Uploaded Files</h5>
<?php
$stmt = $db->prepare("SELECT * FROM files WHERE tenant_id = :tenant_id");
$stmt->bindParam(':tenant_id', $id, PDO::PARAM_INT);
$stmt->execute();
$files = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<?php if (empty($files)): ?>
<p class="text-white">No files uploaded for this tenant.</p>
<?php else: ?>
<ul class="list-group">
<?php foreach ($files as $file): ?>
<li class="list-group-item d-flex justify-content-between align-items-center">
<a href="<?= htmlspecialchars($file['file_path']) ?>" target="_blank"><?= htmlspecialchars($file['file_name']) ?></a>
<a href="delete_file.php?id=<?= $file['id'] ?>&tenant_id=<?= $id ?>" class="btn btn-danger btn-sm">Delete</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php else: ?>
<div class="alert alert-warning">Tenant not found.</div>
<?php endif; ?>
</div>
</body>
</html>