149 lines
6.5 KiB
PHP
149 lines
6.5 KiB
PHP
<?php
|
|
require_once 'session.php';
|
|
check_admin();
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$id = $_GET['id'] ?? null;
|
|
if (!$id) {
|
|
header("Location: index.php?page=payments");
|
|
exit;
|
|
}
|
|
|
|
$db = db();
|
|
$stmt = $db->prepare("SELECT * FROM payments WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$payment = $stmt->fetch();
|
|
|
|
if (!$payment) {
|
|
header("Location: index.php?page=payments");
|
|
exit;
|
|
}
|
|
|
|
$properties = $db->query("SELECT id, name FROM properties ORDER BY name")->fetchAll();
|
|
$tenants = $db->query("SELECT id, name FROM tenants ORDER BY name")->fetchAll();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$property_id = $_POST['property_id'];
|
|
$tenant_id = $_POST['tenant_id'];
|
|
$amount = $_POST['amount'];
|
|
$payment_date = $_POST['payment_date'];
|
|
$notes = $_POST['notes'];
|
|
|
|
$stmt = $db->prepare("UPDATE payments SET property_id = ?, tenant_id = ?, amount = ?, payment_date = ?, notes = ? WHERE id = ?");
|
|
try {
|
|
$db->beginTransaction();
|
|
$stmt->execute([$property_id, $tenant_id, $amount, $payment_date, $notes, $id]);
|
|
|
|
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, payment_id) VALUES (?, ?, ?)");
|
|
$stmt->execute([$_FILES['file']['name'], $target_file, $id]);
|
|
} else {
|
|
throw new Exception("Failed to upload file.");
|
|
}
|
|
}
|
|
$db->commit();
|
|
header("Location: index.php?page=payments&success=2");
|
|
exit;
|
|
} catch (Exception $e) {
|
|
$db->rollBack();
|
|
$error = "Error updating payment: " . $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 Payment - Property Management System</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body class="bg-dark text-light">
|
|
|
|
<div class="container mt-5">
|
|
<h1 class="text-primary mb-4">Edit Payment</h1>
|
|
|
|
<?php if (!empty($error)): ?>
|
|
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card bg-surface">
|
|
<div class="card-body">
|
|
<form action="edit_payment.php?id=<?= $id ?>" method="post" enctype="multipart/form-data">
|
|
<div class="mb-3">
|
|
<label for="property_id" class="form-label">Property</label>
|
|
<select class="form-select" id="property_id" name="property_id" required>
|
|
<?php foreach ($properties as $property): ?>
|
|
<option value="<?= $property['id'] ?>" <?= $payment['property_id'] == $property['id'] ? 'selected' : '' ?>><?= htmlspecialchars($property['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="tenant_id" class="form-label">Tenant</label>
|
|
<select class="form-select" id="tenant_id" name="tenant_id" required>
|
|
<?php foreach ($tenants as $tenant): ?>
|
|
<option value="<?= $tenant['id'] ?>" <?= $payment['tenant_id'] == $tenant['id'] ? 'selected' : '' ?>><?= htmlspecialchars($tenant['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="amount" class="form-label">Amount</label>
|
|
<div class="input-group">
|
|
<span class="input-group-text">$</span>
|
|
<input type="number" step="0.01" class="form-control" id="amount" name="amount" value="<?= htmlspecialchars($payment['amount']) ?>" required>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="payment_date" class="form-label">Payment Date</label>
|
|
<input type="date" class="form-control" id="payment_date" name="payment_date" value="<?= htmlspecialchars($payment['payment_date']) ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="notes" class="form-label">Notes</label>
|
|
<textarea class="form-control" id="notes" name="notes" rows="3"><?= htmlspecialchars($payment['notes']) ?></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="file" class="form-label">Upload New Document</label>
|
|
<input type="file" class="form-control" id="file" name="file">
|
|
</div>
|
|
<button type="submit" class="btn btn-primary"><i class="bi bi-check-circle"></i> Update Payment</button>
|
|
<a href="index.php?page=payments" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
|
|
<hr>
|
|
|
|
<h5 class="text-white">Uploaded Files</h5>
|
|
<?php
|
|
$stmt = $db->prepare("SELECT * FROM files WHERE payment_id = :payment_id");
|
|
$stmt->bindParam(':payment_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 payment.</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'] ?>&payment_id=<?= $id ?>" class="btn btn-danger btn-sm">Delete</a>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|