114 lines
5.0 KiB
PHP
114 lines
5.0 KiB
PHP
<?php
|
|
require_once 'session.php';
|
|
check_admin();
|
|
|
|
require_once 'db/config.php';
|
|
|
|
$db = db();
|
|
$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("INSERT INTO payments (property_id, tenant_id, amount, payment_date, notes) VALUES (?, ?, ?, ?, ?)");
|
|
try {
|
|
$db->beginTransaction();
|
|
$stmt->execute([$property_id, $tenant_id, $amount, $payment_date, $notes]);
|
|
$payment_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, payment_id) VALUES (?, ?, ?)");
|
|
$stmt->execute([$_FILES['file']['name'], $target_file, $payment_id]);
|
|
} else {
|
|
throw new Exception("Failed to upload file.");
|
|
}
|
|
}
|
|
$db->commit();
|
|
header("Location: index.php?page=payments&success=1");
|
|
exit;
|
|
} catch (Exception $e) {
|
|
$db->rollBack();
|
|
$error = "Error adding 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>Add 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">Record New 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="add_payment.php" 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>
|
|
<option value="">Select Property</option>
|
|
<?php foreach ($properties as $property): ?>
|
|
<option value="<?= $property['id'] ?>"><?= 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>
|
|
<option value="">Select Tenant</option>
|
|
<?php foreach ($tenants as $tenant): ?>
|
|
<option value="<?= $tenant['id'] ?>"><?= 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" 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="<?= date('Y-m-d') ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="notes" class="form-label">Notes</label>
|
|
<textarea class="form-control" id="notes" name="notes" rows="3"></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="file" class="form-label">Upload 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> Save Payment</button>
|
|
<a href="index.php?page=payments" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|