-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRentAStore.php
More file actions
349 lines (288 loc) · 15.7 KB
/
Copy pathRentAStore.php
File metadata and controls
349 lines (288 loc) · 15.7 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
include("session.php");
include("config.php");
error_reporting(E_ALL);
ini_set('display_errors','On');
include("redirect.php");
//get userID
if(isset($_SESSION['userID'])){
$userID= $_SESSION['userID'];
}else redirect("index.php");
//get AdID
$AdID=null;
if(isset($_POST['rentStore'])){
$AdID = $_POST['rentStore'];
$_SESSION['AdID'] = $AdID;
}elseif (isset($_SESSION['AdID'])){
$AdID = $_SESSION['AdID'];
}
/**
*
* Get times as option-list.
*
* @return string List of times
*/
function get_times( $default = '19:00', $interval = '+60 minutes' ) {
$output = '';
$current = strtotime( '08:00' );
$end = strtotime( '20:00' );
while( $current <= $end ) {
$time = date( 'H:i', $current );
$sel = ( $time == $default ) ? ' selected' : '';
$output .= "<option value=\"{$time}\"{$sel}>" . date( 'h.i A', $current ) .'</option>';
$current = strtotime( $interval, $current );
}
return $output;
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/css/bootstrap-datetimepicker.min.css">
<script>
$(function() {
$('#datetimepicker').datetimepicker({
format: 'YYYY-MM-DD'
});
});
</script>
<title>Rent a store</title>
</head>
<body bgcolor = "#FFFFFF">
<?php
//the menu if the user is not logged in
if (!isset($_SESSION['login_user']) || ($_SESSION['login_user'])==null ){
echo " <nav class='navbar navbar-inverse'>
<div class='container-fluid'>
<div class='navbar-header'>
<a class='navbar-brand' href='#'>OCN</a>
</div>
<ul class='nav navbar-nav'>
<li class='active'><a href='register.php'>Register</a></li>
<li class='active'><a href='index.php'>Log In</a></li>
</ul>
</div>
</nav>";
}else {
//the menu if the user is logged in
$username = $_SESSION['login_user']; // username
$userType = $_SESSION['usetype']; // user type regular or admin, this should be extracted from the database
echo " <nav class='navbar navbar-inverse'>
<div class='container-fluid'>
<div class='navbar-header'>
<a class='navbar-brand' href='#'>Welcome ". $username ."</a>
</div>
<ul class='nav navbar-nav'>
<li class='active'><a href='viewAds.php' >Browse Ads</a></li>
<li class='active'><a href='account.php'>My Account</a></li>
<li class='active'><a href='logout.php'>Logout</a></li>
</ul>
</div>
</nav>";
if($userType=='Admin'){
// the menu part only for admin users
echo " <nav class='navbar navbar-inverse'>
<div class='container-fluid'>
<ul class='nav navbar-nav'>
<li class='active'><a href='viewPayments.php'>View payments</a></li>
<li class='active'><a href='manage_users_ads.php'>Manage users ads</a></li
</ul>
</div>
</nav>";
}
}
?>
<div class = "container">
<div class="row">
<div class="col-sm-2" style="background-color:lavender;"></div>
<div class="col-sm-8">
<div class="well well-sm">
<form action = "" method = "post" class="form-horizontal">
<label>Pick a store:</label>
<select name = "SLnum" class="custom-select">
<option value="0" selected="">Pick a store</option>
<option value="1">Store 1</option>
<option value="2">Store 2</option>
<option value="3">Store 3</option>
<option value="4">Store 4</option>
</select><br><br>
<div class="form-group">
</div>
<!--day selection-->
<div class="form-group">
<div class='input-group date' id='datetimepicker'>
<label class="control-label" for="date">Pick a day:</label>
<input name="day" type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<br><br>
<!--hour selection-->
<label>Pick a start time</label>
<select name = "hour" class="custom-select">
<?php echo get_times(); ?>
</select>
<br><br>
<!--number of hours to book (maximum 12)-->
<label>choose the number of hours to book</label>
<select name = "numOfHours" class="custom-select">
<option value="1" selected="">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<br><br>
<!--delivery option -->
<label>Do you want to allow delivery?</label>
<label class="radio-inline">
<input type="radio" name="delivery" value ="n" checked="checked">no
</label>
<label class="radio-inline">
<input type="radio" name="delivery" value="y">yes
</label><br>
<!--method of payment-->
<label>Do you want to use your credit card?</label>
<label class="radio-inline">
<input type="radio" name="payment" value ="n" checked="checked">no
</label>
<label class="radio-inline">
<input type="radio" name="payment" value="y">yes
</label>
<div class="well well-sm">
<input type="submit" name="submit" class="btn btn-info btn-block" value="proceed to rent a store" />
</div>
</form>
</div>
<?php
if (($_SERVER['REQUEST_METHOD'] === 'POST')&& (isset($_POST['day']))){
if(isset($_POST['SLnum'],$_POST['day'],$_POST['hour'])){
$SLnum =mysqli_real_escape_string($conn,$_POST['SLnum']);
$day = mysqli_real_escape_string($conn,$_POST['day']);
$hour = (mysqli_real_escape_string($conn,$_POST['hour'])).":00";
$delivery = mysqli_real_escape_string($conn,$_POST['delivery']);
$numOfHours = mysqli_real_escape_string($conn,$_POST['numOfHours']);
$payment = mysqli_real_escape_string($conn,$_POST['payment']);
if($SLnum==0){
echo "Please choose a store.";
}else{
// get all the bookings in the selected date and check if the selected hour is already booked
$sql = "SELECT * FROM STOREBOOKINGS WHERE SLnum= $SLnum AND date = '$day'";
$result = $conn->query($sql);
$count = mysqli_num_rows($result);
$isBooked = false;
if($count!=0){ //there exists some bookings in the selected day
echo "Here is a list of all booked hours on $day in the store #$SLnum: <br> ";
$bookedHours = "";
while($row = $result->fetch_assoc()){//check if this hour is already booked
$bookedHours .= $row['time'];
if($row['time']===$hour){
$bookedHours .= "  <span style='color: red;'> this hour is already booked</span>";
$isBooked = true;
}
$bookedHours .= "<br>";
};
}
if($isBooked == false){ // no bookings on this datetime
//finish the booking (add to storebookings table)
$counter =$numOfHours;
$hourBooking = $hour;
while($counter!=0){ // make a booking for each hour
$sql = "INSERT INTO STOREBOOKINGS (userID,AdID,date,time,SLnum)
VALUES ($userID,$AdID,'$day','$hourBooking',$SLnum);";
if(mysqli_query($conn, $sql)){
$counter--;
$hourBooking=strtotime( "+60 minutes", strtotime($hourBooking) );
$hourBooking=date( 'H:i', $hourBooking );
$hourBooking.=":00";
}
}
//add ad to store ads
$sql = "INSERT INTO storeAds (deliveryAvailable, AdID, SLnum)
VALUES ('$delivery',$AdID,$SLnum);";
if(mysqli_query($conn, $sql)){
echo "You now have a store ad.<br>";
}
echo "you have successfuly booked store $SLnum in $day at $hour <br>";
$isWeekend = false;
$extraCharge =1;
//if the day of booking is a weekend, get extra charge
$weekday = date('w',strtotime($day));
if($weekday == 0 || $weekday ==6){
$sql = "SELECT * FROM PhysicalStore WHERE SLnum = $SLnum";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$extraCharge = $row['extraCharge'];
$isWeekend = true;
}
//
// rent calculations
//
$hourCharge = 10;
$deliveryCharge = 5;
$creditPercentage = 1.03;
//get user's card if (card will be used)
if($payment==="y"){
$sql="SELECT * FROM users WHERE userID = $userID";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$card = $row['card'];
}else{
$creditPercentage = 1; // if the user is paying in the store
$card = 0;
}
if($isWeekend){ // change fees if it's a weekday
$hourCharge =15;
$deliveryCharge = 10;
}
if($delivery==='n'){ //if the delivery is not selected
$deliveryCharge =0;
}
// calculate rent
$rentTotal = ( (($hourCharge*$numOfHours)*$extraCharge)
+ ($deliveryCharge*$numOfHours) ) * $creditPercentage;
// add to transactions table
$sql = "INSERT INTO Transactions (purchaseType,date,bill,is_item,buyerID,sellerID,card)
VALUES ('StoreRent',CURDATE(),$rentTotal,1, $userID,75, $card);";
if(mysqli_query($conn, $sql)){
echo "Transaction completed<br>";
}
// print transaction
$hoursByPrice = $hourCharge*$numOfHours;
echo"Here is your transaction details:<br>
Total booked hours price  ".($hoursByPrice)."<br>
+ extra weekend charge: ".(($hoursByPrice*$extraCharge)-$hoursByPrice)."<br>
+ Delivery charge     ".($deliveryCharge*$numOfHours)."<br>
* credit card use percentage ".(($creditPercentage-1)*100)."<br>
Total Rent fees    ".($rentTotal)."<br>";
echo"your can now go back to myaccount";
}else{//print the availabilty of this day
echo $bookedHours;
}
}
}
}else{
echo "Check all the fields to successfuly rent a store!";
}
?>
</div>
<div class="col-sm-2" style="background-color:lavender;"></div>
</div>
</div>
</body>
</html>