Skip to content

Commit 011367d

Browse files
Version 2.1 RentalTransaction & FullCalendar Added
1 parent ded767c commit 011367d

File tree

14 files changed

+564
-125
lines changed

14 files changed

+564
-125
lines changed

assets/js/fullcalendar.js

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
11
document.addEventListener('DOMContentLoaded', function() {
2-
var calendarEl = document.getElementById('calendar');
3-
4-
var calendar = new FullCalendar.Calendar(calendarEl, {
2+
var calendarEl = document.getElementById('calendar');
3+
4+
var calendar = new FullCalendar.Calendar(calendarEl, {
55
themeSystem: 'bootstrap5',
66
initialView: 'dayGridMonth',
7-
initialDate: '2023-09-07',
7+
initialDate: new Date(),
88
headerToolbar: {
9-
left: 'prev,next today',
10-
center: 'title',
11-
right: 'dayGridMonth,timeGridWeek,timeGridDay,listMonth'
9+
left: 'prev,next today',
10+
center: 'title',
11+
right: 'dayGridMonth,timeGridWeek,timeGridDay,listMonth'
1212
},
13-
events: [
14-
{
15-
title: '<cottage> | <customer>',
16-
start: '2023-09-08T09:30:00',
17-
end: '2023-09-08T11:00:00'
18-
},{
19-
title: '<cottage> | <customer>',
20-
start: '2023-09-09T09:30:00',
21-
end: '2023-09-09T11:00:00'
22-
},{
23-
title: '<cottage> | <customer>',
24-
start: '2023-09-10T09:30:00',
25-
end: '2023-09-010T11:00:00'
26-
},{
27-
title: '<cottage> | <customer>',
28-
start: '2023-09-11T09:30:00',
29-
end: '2023-09-11T11:00:00'
30-
},{
31-
title: '<cottage> | <customer>',
32-
start: '2023-09-15T09:30:00',
33-
end: '2023-09-15T11:00:00'
34-
},
35-
]
36-
});
37-
38-
calendar.render();
39-
});
13+
events: {
14+
url: 'functions/data/get-calendar.php',
15+
method: 'GET',
16+
extraParams: {
17+
action: 'get_calendar'
18+
},
19+
failure: function() {
20+
alert('There was an error while fetching calendar data.');
21+
}
22+
}
23+
});
24+
25+
calendar.render();
26+
});

assets/js/main.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,23 @@ new DataTable('table.table-display',{
109109
$('input[name="priceNight"]').val(night);
110110
console.log(id);
111111
});
112-
}else if (currentPath.includes("/CUSTMBRS/rents.php")) {
113-
$('a[data-bs-target="#return"]').on('click', function() {
114-
var id = $(this).data('id');
115-
$('input[name="data_id"]').val(id);
116-
console.log(id);
117-
});
118-
119-
$('a[data-bs-target="#stock-in"]').on('click', function() {
120-
var id = $(this).data('id');
121-
console.log(id);
122-
$('input[name="data_id"]').val(id);
123-
});
124-
125-
$('a[data-bs-target="#stock-out"]').on('click', function() {
112+
}else if (currentPath.includes("/CUSTMBRS/rent.php")) {
113+
$('a[data-bs-target="#update"]').on('click', function() {
126114
var id = $(this).data('id');
127-
console.log(id);
128-
$('input[name="data_id"]').val(id);
115+
var start = $(this).data('start');
116+
var end = $(this).data('end');
117+
var type = $(this).data('type');
118+
$('input[name="id"]').val(id);
119+
$('select[name="type"]').val(type);
120+
$('input[name="start"]').val(start);
121+
$('input[name="end"]').val(end);
122+
console.log(id, name, start, end, type);
129123
});
124+
$('button[data-bs-target="#proceed"]').on('click', function() {
125+
var id = $(this).data('id');
126+
console.log(id);
127+
$('input[name="id"]').val(id);
128+
});
130129
} else{
131130
console.log("The URL is neither /customer nor /list");
132131
}

functions/data/get-calendar.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
include_once '../connection.php';
3+
$calendarData = array();
4+
5+
$sql = 'SELECT r.*, co.fullname, c.name AS cottage_name
6+
FROM rentals AS r
7+
JOIN cottages AS c ON r.cottage_id = c.id
8+
JOIN transactions AS t ON r.transact_id = t.id
9+
JOIN customers AS co ON t.customer_id = co.id';
10+
$stmt = $db->prepare($sql);
11+
$stmt->execute();
12+
$results = $stmt->fetchAll();
13+
14+
foreach ($results as $row) {
15+
$event = array(
16+
'title' => $row['cottage_name'].' | '.$row['fullname'],
17+
'start' => $row['start_datetime'],
18+
'end' => $row['end_datetime']
19+
);
20+
array_push($calendarData, $event);
21+
}
22+
23+
// Return the calendar data as JSON
24+
header('Content-Type: application/json');
25+
echo json_encode($calendarData);
26+
?>

functions/data/get-data.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
function customers(){
4+
global $db;
5+
$sql = 'SELECT * FROM customers ORDER BY fullname ASC';
6+
$stmt = $db->prepare($sql);
7+
$stmt->execute();
8+
$results = $stmt->fetchAll();
9+
$select = false;
10+
foreach ($results as $row) {
11+
?>
12+
<option value="<?php echo $row['id'] ?>" <?php if (!$select) { echo 'selected'; $select = true; } ?>><?php echo $row['fullname'] ?></option>
13+
<?php
14+
}
15+
}
16+
17+
function cotteges(){
18+
global $db;
19+
$sql = 'SELECT * FROM `cottages` ORDER BY `name` ASC';
20+
$stmt = $db->prepare($sql);
21+
$stmt->execute();
22+
$results = $stmt->fetchAll();
23+
$select = false;
24+
foreach ($results as $row) {
25+
?>
26+
<option value="<?php echo $row['id'] ?>" <?php if (!$select) { echo 'selected'; $select = true; } ?>><?php echo $row['name'] ?></option>
27+
<?php
28+
}
29+
}
30+
31+
function customer_info($id){
32+
global $db;
33+
$sql = 'SELECT * FROM `transactions` WHERE `user_id` = :id AND `status` = "Pending"';
34+
$stmt = $db->prepare($sql);
35+
$stmt->bindParam(':id', $id);
36+
$stmt->execute();
37+
$results = $stmt->fetchAll();
38+
$sql = 'SELECT * FROM `customers` WHERE `id` = :id';
39+
$stmt = $db->prepare($sql);
40+
$stmt->bindParam(':id', $results[0]['customer_id']);
41+
$stmt->execute();
42+
$results = $stmt->fetchAll();
43+
return $results[0] ?? '';
44+
}
45+

functions/login.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
include_once 'connection.php';
3-
3+
if (session_start() === PHP_SESSION_NONE) {
4+
session_start();
5+
}
46
$username = $_POST['username'];
57
$password = $_POST['password'];
68

@@ -10,8 +12,6 @@
1012
$user = $stmt->fetch(PDO::FETCH_ASSOC);
1113

1214
if ($user && password_verify($password, $user['password'])) {
13-
14-
session_start();
1515
$_SESSION['username'] = $username;
1616
$_SESSION['type'] = $user['type'];
1717
$_SESSION['id'] = $user['id'];

functions/setup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
id INT PRIMARY KEY AUTO_INCREMENT,
5959
cottage_id INT,
6060
transact_id INT,
61-
price DECIMAL(10,2),
61+
type VARCHAR(255),
6262
start_datetime DATETIME,
6363
end_datetime DATETIME,
6464
created_at DATE DEFAULT CURRENT_TIMESTAMP,

functions/tables/datatables.php

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
include_once 'functions/connection.php';
3+
include_once 'functions/data/get-data.php';
34

45
function user_logs(){
56
global $db;
@@ -45,19 +46,6 @@ function customer_list(){
4546
}
4647
}
4748

48-
function customers(){
49-
global $db;
50-
$sql = 'SELECT * FROM customers ORDER BY fullname ASC';
51-
$stmt = $db->prepare($sql);
52-
$stmt->execute();
53-
$results = $stmt->fetchAll();
54-
$select = false;
55-
foreach ($results as $row) {
56-
?>
57-
<option value="<?php echo $row['id'] ?>" <?php if (!$select) { echo 'selected'; $select = true; } ?>><?php echo $row['fullname'] ?></option>
58-
<?php
59-
}
60-
}
6149

6250
function staff_list(){
6351
global $db;
@@ -111,4 +99,44 @@ function cottage_list(){
11199

112100
<?php
113101
}
114-
}
102+
}
103+
104+
function transaction_list(){
105+
global $db;
106+
$user_id = $_SESSION['id'];
107+
108+
$sql = "SELECT * FROM transactions WHERE `user_id` = :user_id AND `status` = 'Pending'";
109+
$stmt = $db->prepare($sql);
110+
$stmt->bindParam(':user_id', $user_id);
111+
$stmt->execute();
112+
$transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);
113+
114+
foreach ($transactions as $transaction) {
115+
$transaction_id = $transaction['id'];
116+
117+
$sql = "SELECT r.*, c.name AS cottage_name
118+
FROM rentals AS r
119+
JOIN cottages AS c ON r.cottage_id = c.id
120+
WHERE r.transact_id = :transact_id";
121+
$stmt = $db->prepare($sql);
122+
$stmt->bindParam(':transact_id', $transaction_id);
123+
$stmt->execute();
124+
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
125+
126+
foreach ($results as $row) {
127+
?>
128+
<tr>
129+
<td><img class="rounded-circle me-2" width="30" height="30" src="assets/img/icon.png">&nbsp;<?php echo $row['cottage_name']; ?></td>
130+
<td><?php echo $row['start_datetime']; ?></td>
131+
<td><?php echo $row['end_datetime']; ?></td>
132+
<td><?php echo $row['type']; ?></td>
133+
<td><?php echo $row['created_at']; ?></td>
134+
<td class="text-center">
135+
<a class="mx-1" href="#" data-bs-target="#update" data-id="<?php echo $row['id']?>" data-type="<?php echo $row['type']?>" data-start="<?php echo $row['start_datetime']?>" data-end="<?php echo $row['end_datetime']?>" data-bs-toggle="modal"><i class="fas fa-user-edit fs-4 text-warning"></i></a>
136+
<a class="mx-1" href="#" data-bs-target="#remove" data-id="<?php echo $row['id']?>" data-bs-toggle="modal"><i class="fas fa-trash-alt fs-4 text-danger"></i></a>
137+
</td>
138+
</tr>
139+
<?php
140+
}
141+
}
142+
}

functions/transaction-add.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
include_once 'connection.php';
3+
if (session_start() === PHP_SESSION_NONE) {
4+
session_start();
5+
}
6+
try {
7+
$user_id = $_SESSION['id'];
8+
$cottage_id = $_POST['id'];
9+
$start = $_POST['start'];
10+
$end = $_POST['end'];
11+
$type = $_POST['type'];
12+
13+
$current_timestamp = time();
14+
$start_timestamp = strtotime($start);
15+
$end_timestamp = strtotime($end);
16+
17+
$sql = "SELECT * FROM transactions WHERE `user_id` = :user_id AND `status` = 'Pending'";
18+
$stmt = $db->prepare($sql);
19+
$stmt->bindParam(':user_id', $user_id);
20+
$stmt->execute();
21+
$transaction = $stmt->fetch(PDO::FETCH_ASSOC);
22+
23+
$transaction_id = $transaction['id'];
24+
$customer_id = $transaction['customer_id'];
25+
26+
$sql = "SELECT * FROM rentals
27+
WHERE `cottage_id` = :cottage_id
28+
AND (:start_datetime BETWEEN `start_datetime` AND `end_datetime`
29+
OR :end_datetime BETWEEN `start_datetime` AND `end_datetime`
30+
OR `start_datetime` BETWEEN :start_datetime AND :end_datetime)";
31+
$stmt = $db->prepare($sql);
32+
$stmt->bindParam(':cottage_id', $cottage_id);
33+
$stmt->bindParam(':start_datetime', $start);
34+
$stmt->bindParam(':end_datetime', $end);
35+
$stmt->execute();
36+
$rental = $stmt->fetch(PDO::FETCH_ASSOC);
37+
38+
if ($rental) {
39+
header('Location: ../rent.php?type=error&message=Cottage is already rented on the selected date');
40+
exit;
41+
}
42+
43+
if ($start_timestamp < $current_timestamp) {
44+
header('Location: ../rent.php?type=error&message=Start date must be greater than current date');
45+
exit;
46+
}
47+
if ($end_timestamp < $current_timestamp) {
48+
header('Location: ../rent.php?type=error&message=End date must be greater than current date');
49+
exit;
50+
}
51+
52+
if (!$transaction) {
53+
header('Location: ../rent.php?type=error&message=No pending transaction found');
54+
}
55+
56+
$sql = "INSERT INTO rentals (`cottage_id`, `transact_id`, `type`, `start_datetime`, `end_datetime`) VALUES (:cottage_id, :transact_id, :types, :start_datetime, :end_datetime)";
57+
$stmt = $db->prepare($sql);
58+
$stmt->bindParam(':cottage_id', $cottage_id);
59+
$stmt->bindParam(':transact_id', $transaction_id);
60+
$stmt->bindParam(':types', $type);
61+
$stmt->bindParam(':start_datetime', $start);
62+
$stmt->bindParam(':end_datetime', $end);
63+
$stmt->execute();
64+
65+
generate_logs('Add Cottage', $user_id, 'Added new rental');
66+
header('Location: ../rent.php?type=success&message=New transaction was added successfully');
67+
} catch (\Throwable $th) {
68+
// generate_logs('Add Cottage', 'Error on adding new rental: ' . $th->getMessage());
69+
echo $th->getMessage();
70+
}
71+
?>

functions/transaction-create.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
include_once 'connection.php';
3+
try {
4+
if (session_start() === PHP_SESSION_NONE) {
5+
session_start();
6+
};
7+
$customer_id = $_POST['id'];
8+
$user_id = $_SESSION['id'];
9+
10+
$sql = "SELECT * FROM transactions WHERE user_id = :user_id AND status = 'Pending'";
11+
$stmt = $db->prepare($sql);
12+
$stmt->bindParam(':user_id', $user_id);
13+
$stmt->execute();
14+
$transaction = $stmt->fetch(PDO::FETCH_ASSOC);
15+
16+
if ($transaction) {
17+
header('Location: ../rent.php?type=error&message=You have a pending transaction');
18+
exit();
19+
}
20+
21+
$sql = "SELECT * FROM customers WHERE id = :id";
22+
$stmt = $db->prepare($sql);
23+
$stmt->bindParam(':id', $customer_id);
24+
$stmt->execute();
25+
$customer = $stmt->fetch(PDO::FETCH_ASSOC);
26+
27+
$sql = "INSERT INTO transactions (customer_id, user_id, status) VALUES (:customer_id, :user_id, 'Pending')";
28+
$stmt = $db->prepare($sql);
29+
$stmt->bindParam(':customer_id', $customer_id);
30+
$stmt->bindParam(':user_id', $user_id);
31+
$stmt->execute();
32+
33+
generate_logs('Adding Transaction', $customer['fullname'].' | New transaction was added');
34+
header('Location: ../rent.php?type=success&message=New transaction was added successfully');
35+
} catch (\Throwable $th) {
36+
generate_logs('Adding Transaction', $th);
37+
}
38+
?>

0 commit comments

Comments
 (0)