-
Notifications
You must be signed in to change notification settings - Fork 0
/
payRent.php
335 lines (287 loc) · 13.5 KB
/
payRent.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
require("session.php");
require "connect.php";
$sql = "SELECT apartment.apartment_id, name
FROM apartment
JOIN house ON house.apartment_id = apartment.apartment_id
WHERE house.house_id IN( SELECT house_id FROM assigned )
GROUP BY apartment.apartment_id;";
$result = mysqli_query($conn, $sql);
$apartments = mysqli_fetch_all($result, MYSQLI_ASSOC);
$payment_types = [
["name" => "Rent", "id" => "RENT"]
];
$rent_types = [
["name" => "1 Month Rent", "id" => "1"],
["name" => "2 Month Rent", "id" => "2"],
["name" => "3 Month Rent", "id" => "3"],
["name" => "6 Month Rent", "id" => "6"],
["name" => "12 Month Rent", "id" => "12"],
];
if (isset($_GET['confirm_payment'])) {
$errors = [];
if (empty($_GET["tenant_id"])) {
$errors["tenant_id"] = "The tenant_id is required. <br/>";
}
if (empty($_GET["house_id"])) {
$errors["house_id"] = "The house_id is required. <br/>";
}
if (empty($_GET["amount"])) {
$errors["amount"] = "The amount is required. <br/>";
}
if (empty($_GET["rent_type"])) {
$errors["rent_type"] = "The rent_type is required. <br/>";
}
if (!array_filter($errors)) {
$house_id = mysqli_real_escape_string($conn, $_GET["house_id"]);
$tenant_id = mysqli_real_escape_string($conn, $_GET["tenant_id"]);
$amount = mysqli_real_escape_string($conn, $_GET["amount"]);
$description = "House Rent for " . mysqli_real_escape_string($conn, $_GET["rent_type"]) . " months";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
mysqli_begin_transaction($conn, MYSQLI_TRANS_START_READ_WRITE);
try {
$sql = "SELECT owner_id FROM apartment
WHERE apartment_id =
(SELECT apartment_id FROM house WHERE house_id ='$house_id' );";
$result = mysqli_query($conn, $sql);
$owner = mysqli_fetch_assoc($result);
$owner_id = $owner["owner_id"];
$sql = "INSERT INTO payment(house_id,tenant_id,owner_id, amount, payment_type, description) VALUES('$house_id','$tenant_id','$owner_id','$amount','RENT','$description') ;";
mysqli_query($conn, $sql);
/* If code reaches this point without errors then commit the data in the database */
mysqli_commit($conn);
header("Location: assignmentDetails.php?id=" . $house_id);
} catch (mysqli_sql_exception $exception) {
mysqli_rollback($conn);
echo "query error: " . mysqli_error($conn);
throw $exception;
}
} else {
header("Location: payRent.php");
}
} else {
if (isset($_GET['apartment_id']) && !empty($_GET['apartment_id']) && isset($_GET['house_id']) && !empty($_GET['house_id'])) {
$house_id = mysqli_real_escape_string($conn, $_GET["house_id"]);
$sql = "SELECT
tenant.tenant_id,
tenant.name,
tenant.email,
tenant.phone,
tenant.address,
tenant.occupation,
assigned.house_id,
house.house_no,
house.type,
house.rent,
house.description
FROM tenant
JOIN assigned ON assigned.tenant_id = tenant.tenant_id
JOIN house ON house.house_id = assigned.house_id
WHERE assigned.house_id = '$house_id';";
$result = mysqli_query($conn, $sql);
$details = mysqli_fetch_assoc($result);
}
if (isset($_GET['apartment_id']) && !empty($_GET['apartment_id'])) {
$apartment_id = mysqli_real_escape_string($conn, $_GET["apartment_id"]);
$sql = "SELECT house_id, house_no
FROM house
WHERE apartment_id = '$apartment_id' AND
house_id IN( SELECT house_id FROM assigned );";
$result = mysqli_query($conn, $sql);
$houses = mysqli_fetch_all($result, MYSQLI_ASSOC);
}
if (isset($_GET['rent_type']) && !empty($_GET['rent_type'])) {
$rent_type = mysqli_real_escape_string($conn, $_GET["rent_type"]);
}
}
?>
<?php include "header.php"; ?>
<div id="tobePrint" class="flex justify-center items-center">
<div class="w-full m-4 ">
<form action="payRent.php" method="get" class="max-w-5xl mx-auto w-full bg-gray-50 rounded-xl px-6 py-6 shadow overflow-hidden">
<div class=" text-center">
<h1 class="text-3xl text-gray-900 my-2">
<b>Pay House Rent</b>
</h1>
</div>
<div class="flex gap-4">
<div class="flex-1 flex flex-col justify-start items-center gap-4 p-4">
<div class="w-full ">
<label for="apartment_id" class=" label required">
Select Apartment:
</label>
<select onchange="this.form.submit()" name="apartment_id" class="input" required>
<option <?php if (!isset($apartment_id)) {
echo "selected";
} ?> disabled>------ Select Apartment ------</option>
<?php foreach ($apartments as $apartment) { ?>
<option <?php if (isset($apartment_id) && $apartment["apartment_id"] == $apartment_id) {
echo "selected";
} ?> value="<?php echo $apartment['apartment_id']; ?>"><?php echo $apartment["name"] ?></option>
<?php } ?>
</select>
</div>
<?php if (isset($apartment_id)) { ?>
<div class="w-full ">
<label for="house_id" class=" label required">
Select House:
</label>
<select onchange="this.form.submit()" name="house_id" class="input" required>
<option <?php if (!isset($house_id)) {
echo "selected";
} ?> value="">------ Select House ------</option>
<?php foreach ($houses as $house) { ?>
<option <?php if (isset($house_id) && $house["house_id"] == $house_id) {
echo "selected";
} ?> value="<?php echo $house['house_id']; ?>"><?php echo $house["house_no"] ?></option>
<?php } ?>
</select>
</div>
<?php } ?>
<?php if (isset($house_id) && $details) { ?>
<input type="hidden" name="tenant_id" value="<?php echo $details['tenant_id']; ?>">
<div class="w-full ">
<label for="rent_type" class=" label required">
Select Rent Type:
</label>
<select onchange="this.form.submit()" name="rent_type" class="input" required>
<option <?php if (!isset($rent_type)) {
echo "selected";
} ?> disabled>------ Select Type ------</option>
<?php foreach ($rent_types as $rent_t) { ?>
<option <?php if (isset($rent_type) && $rent_t["id"] == $rent_type) {
echo "selected";
} ?> value="<?php echo $rent_t['id']; ?>"><?php echo $rent_t["name"] ?></option>
<?php } ?>
</select>
</div>
<?php if (isset($rent_type)) { ?>
<div class="w-full py-4 bg-indigo-100 rounded-2xl">
<div class="px-4 w-full py-1.5 sm:grid sm:grid-cols-2 items-center sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500 ">
Rent Per Month(in Rs.)
</dt>
<dd class="mt-1 text-base font-medium text-gray-900 sm:mt-0 sm:col-span-1 max-w-xs break-words">
<?php echo number_format(htmlspecialchars($details["rent"])); ?> /month
</dd>
</div>
<div class="px-4 w-full py-1.5 sm:grid sm:grid-cols-2 items-center sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500">Total Months:
</dt>
<dd class="mt-1 text-base font-medium text-gray-900 sm:mt-0 sm:col-span-1 max-w-xs break-words">
<?php echo number_format(htmlspecialchars($rent_type)); ?> month
</dd>
</div>
<input type="hidden" name="amount" value="<?php echo htmlspecialchars($details["rent"] * $rent_type); ?>">
<div class="px-4 w-full py-1.5 sm:grid sm:grid-cols-2 items-center sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500 ">
Total Rent to Pay (in Rs.)
</dt>
<dd class="mt-1 text-xl font-medium text-gray-900 sm:mt-0 sm:col-span-1 max-w-xs break-words">
<p>
<span class="text-sm ">
<?php echo number_format(htmlspecialchars($details["rent"])); ?>
x
<?php echo number_format(htmlspecialchars($rent_type)); ?><br>
</span>
=
<?php echo number_format(htmlspecialchars($details["rent"] * $rent_type)); ?>
</p>
</dd>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
<div class="flex-1 flex flex-col justify-start items-center gap-4 p-4">
<?php if (isset($house_id) && $details) { ?>
<div class="border-t border-gray-200 py-4 w-full bg-indigo-100 rounded-2xl">
<h4 class=" font-medium text-base text-gray-900 uppercase mb-2 ml-6 ">House Detail</h4>
<div class="px-4 py-1.5 sm:grid sm:grid-cols-3 sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500 ">
House no.
</dt>
<dd class="mt-1 text-base font-medium text-gray-900 sm:mt-0 sm:col-span-2 max-w-xs break-words">
<?php echo htmlspecialchars($details["house_no"]); ?>
</dd>
</div>
<div class="px-4 py-1.5 sm:grid sm:grid-cols-3 sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
House Type
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
<?php echo htmlspecialchars($details["type"]); ?>
</dd>
</div>
<div class="px-4 py-1.5 sm:grid sm:grid-cols-3 sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
Description
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2 max-w-xs break-words">
<?php echo htmlspecialchars($details["description"]); ?>
</dd>
</div>
<div class="px-4 py-1.5 sm:grid sm:grid-cols-3 sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
Rent
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
Rs. <?php echo number_format(htmlspecialchars($details["rent"])); ?> / month
</dd>
</div>
</div>
<div class="border-t border-gray-200 py-4 bg-indigo-100 rounded-2xl">
<h4 class=" font-medium text-base text-gray-900 uppercase mb-2 ml-6 ">Tenant Detail</h4>
<div class="px-4 py-1.5 sm:grid sm:grid-cols-3 sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
Full name
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2 max-w-xs break-words ">
<?php echo htmlspecialchars($details["name"]); ?>
</dd>
</div>
<div class="px-4 py-1.5 sm:grid sm:grid-cols-3 sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
Email address
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2 max-w-xs break-words ">
<?php echo htmlspecialchars($details["email"]); ?>
</dd>
</div>
<div class="px-4 py-1.5 sm:grid sm:grid-cols-3 sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
Phone no.
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2 max-w-xs break-words ">
<?php echo htmlspecialchars($details["phone"]); ?>
</dd>
</div>
<div class="px-4 py-1.5 sm:grid sm:grid-cols-3 sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
Occupation
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2 max-w-xs break-words ">
<?php echo htmlspecialchars($details["occupation"]); ?>
</dd>
</div>
<div class="px-4 py-1.5 sm:grid sm:grid-cols-3 sm:gap-2 sm:px-6">
<dt class="text-sm font-medium text-gray-500">
Address
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2 max-w-xs break-words ">
<?php echo htmlspecialchars($details["address"]); ?>
</dd>
</div>
</div>
<?php } ?>
</div>
</div>
<div class="w-full flex gap-4 ">
<a href="/rms/index.php" class="btn secondary w-auto ">
Cancel </a>
<input name="confirm_payment" onclick="return confirm('Please, confirm the payment.\nPress OK to confirm.\npress CANCEL to abort the asignment')" type="submit" class="btn primary" value="Confirm Payment">
</div>
</form>
</div>
</div>
<?php include "footer.php"; ?>