-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_invoice.php
More file actions
176 lines (153 loc) · 6.62 KB
/
generate_invoice.php
File metadata and controls
176 lines (153 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
session_start();
include 'includes/db_connect.php';
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'owner') {
header("Location: dashboard.php");
exit();
}
$owner_id = $_SESSION['user_id'];
$message = "";
if (isset($_POST['invoice_btn'])) {
$booking_id = $_POST['booking_id'];
$amount = $_POST['amount'];
$month_year = $_POST['month_year'];
$sql = "INSERT INTO invoices (booking_id, amount, month_year)
VALUES ('$booking_id', '$amount', '$month_year')";
if ($conn->query($sql) === TRUE) {
$message = "Invoice generated successfully!";
} else {
$message = "Error: " . $conn->error;
}
}
$sql = "SELECT b.id as booking_id, u.full_name, p.title, p.price
FROM bookings b
JOIN properties p ON b.property_id = p.id
JOIN users u ON b.tenant_id = u.id
WHERE p.owner_id = '$owner_id' AND b.status = 'approved'";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Generate Invoice - SuperTenant</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="css/style.css">
<style>
.error-msg {
color: #e74c3c;
font-size: 13px;
min-height: 20px;
display: block;
margin-top: 2px;
margin-bottom: 5px;
font-weight: bold;
}
input.invalid, select.invalid {
border-color: #e74c3c;
background-color: #fdecea;
}
input.valid, select.valid {
border-color: #27ae60;
background-color: #e8f5e9;
}
</style>
</head>
<body>
<?php include 'includes/navbar.php'; ?>
<main class="container">
<h2 class="section-title">Generate Monthly Rent Invoice</h2>
<?php if ($message != "") : ?>
<p class="alert alert-success mt-20"><?php echo $message; ?></p>
<?php endif; ?>
<?php if ($result->num_rows > 0) : ?>
<form action="generate_invoice.php" method="POST" class="mt-20" id="invoiceForm" novalidate>
<label>Select Tenant / Property:</label>
<select name="booking_id" id="booking_id">
<option value="">-- Select --</option>
<?php while ($row = $result->fetch_assoc()) : ?>
<option value="<?php echo $row['booking_id']; ?>">
<?php echo $row['full_name'] . " - " . $row['title'] . " (₹" . $row['price'] . ")"; ?>
</option>
<?php endwhile; ?>
</select>
<span id="bookingError" class="error-msg"></span>
<label>Billing Month:</label>
<input type="month" name="month_year" id="month_year">
<span id="monthError" class="error-msg"></span>
<label>Amount Due (₹):</label>
<input type="text" name="amount" id="amount" placeholder="Enter rent amount">
<span id="amountError" class="error-msg"></span>
<button type="submit" name="invoice_btn" class="mt-20">Generate Invoice</button>
</form>
<?php else : ?>
<p class="text-info font-bold mt-20">You have no active tenants. Approve a booking request first.</p>
<?php endif; ?>
</main>
<?php include 'includes/footer.php'; ?>
<script>
document.addEventListener("DOMContentLoaded", function() {
const form = document.getElementById("invoiceForm");
if (form) {
const bookingInput = document.getElementById("booking_id");
const monthInput = document.getElementById("month_year");
const amountInput = document.getElementById("amount");
const bookingError = document.getElementById("bookingError");
const monthError = document.getElementById("monthError");
const amountError = document.getElementById("amountError");
function validateBooking() {
const val = bookingInput.value;
if (val === "") {
bookingError.textContent = "Please select a tenant and property.";
bookingInput.classList.add("invalid");
bookingInput.classList.remove("valid");
return false;
}
bookingError.textContent = "";
bookingInput.classList.remove("invalid");
bookingInput.classList.add("valid");
return true;
}
function validateMonth() {
const val = monthInput.value;
if (val === "") {
monthError.textContent = "Please select a billing month.";
monthInput.classList.add("invalid");
monthInput.classList.remove("valid");
return false;
}
monthError.textContent = "";
monthInput.classList.remove("invalid");
monthInput.classList.add("valid");
return true;
}
function validateAmount() {
const val = amountInput.value.trim();
if (val === "" || isNaN(val) || Number(val) <= 0) {
amountError.textContent = "Please enter a valid positive amount.";
amountInput.classList.add("invalid");
amountInput.classList.remove("valid");
return false;
}
amountError.textContent = "";
amountInput.classList.remove("invalid");
amountInput.classList.add("valid");
return true;
}
bookingInput.addEventListener("change", validateBooking);
monthInput.addEventListener("input", validateMonth);
monthInput.addEventListener("change", validateMonth);
amountInput.addEventListener("input", validateAmount);
form.addEventListener("submit", function(e) {
const isBookingValid = validateBooking();
const isMonthValid = validateMonth();
const isAmountValid = validateAmount();
if (!isBookingValid || !isMonthValid || !isAmountValid) {
e.preventDefault();
}
});
}
});
</script>
</body>
</html>