-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotes.php
233 lines (212 loc) · 11 KB
/
quotes.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
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'db.php';
if (!isset($_SESSION['username']) || $_SESSION['role'] !== 'admin') {
header("Location: index.php");
exit;
}
// Pagination settings
$limit = 10; // Number of quotes per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;
// Handle quote status update
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_quote'])) {
$quote_id = sanitize($_POST['quote_id'], $conn);
$quote_status = sanitize($_POST['quote_status'], $conn);
$update_query = "UPDATE quotes SET status='$quote_status' WHERE id='$quote_id'";
if ($conn->query($update_query) === TRUE) {
$success_message = "Updated quote #$quote_id for " . sanitize($_POST['company_name'], $conn) . " - " . sanitize($_POST['contact_name'], $conn);
} else {
$error_message = "Error updating quote status: " . $conn->error;
}
}
// Handle quote deletion
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['delete_quote'])) {
$quote_id = sanitize($_POST['quote_id'], $conn);
$delete_query = "DELETE FROM quotes WHERE id='$quote_id'";
if ($conn->query($delete_query) === TRUE) {
$success_message = "Deleted quote #$quote_id for " . sanitize($_POST['company_name'], $conn) . " - " . sanitize($_POST['contact_name'], $conn);
} else {
$error_message = "Error deleting quote: " . $conn->error;
}
}
// Fetch quotes with optional search
$search = '';
if (isset($_POST['search'])) {
$search = sanitize($_POST['search'], $conn);
}
$query = "SELECT q.*, SUM(qi.total_price) AS total_price FROM quotes q
LEFT JOIN quote_items qi ON q.id = qi.quote_id
WHERE q.id LIKE '%$search%' OR q.company_name LIKE '%$search%' OR q.contact_name LIKE '%$search%'
GROUP BY q.id
LIMIT $limit OFFSET $offset";
$result = $conn->query($query);
// Get total quotes count for pagination
$total_query = "SELECT COUNT(*) AS total FROM quotes WHERE id LIKE '%$search%' OR company_name LIKE '%$search%' OR contact_name LIKE '%$search%'";
$total_result = $conn->query($total_query);
$total_row = $total_result->fetch_assoc();
$total_quotes = $total_row['total'];
$total_pages = ceil($total_quotes / $limit);
// Helper function for formatting date
function formatDate($datetime) {
return date('j M Y - H:i', strtotime($datetime));
}
// Helper function for rendering status
function renderStatus($status) {
$class = '';
$icon = '';
switch ($status) {
case 'Approved':
$class = 'text-success';
$icon = '✔️';
break;
case 'Rejected':
$class = 'text-danger';
$icon = '❌';
break;
case 'Completed':
$class = 'text-info';
$icon = '✅';
break;
default: // Pending
$class = 'text-warning';
$icon = '⏳';
break;
}
return "<span class='$class'>$icon $status</span>";
}
include 'header.php';
include 'nav.php';
?>
<link rel="stylesheet" href="assets/css/bootstrap4.5.2.min.css">
<body>
<div class="container mt-5">
<h5>Quotes Management</h5><hr>
<?php if (isset($success_message)): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo $success_message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php elseif (isset($error_message)): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php echo $error_message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php endif; ?>
<form method="POST" class="mb-4">
<input type="text" name="search" class="form-control" placeholder="Search by Quote Number, Company Name, or Contact Name" value="<?php echo $search; ?>">
<button type="submit" class="btn btn-primary mt-2">Search</button>
</form>
<table class="table table-bordered">
<thead>
<tr>
<th style="width: 10%; text-align: center;">QTE #</th>
<th style="width: 30%;">Company Name</th>
<th style="width: 10%; text-align: center;">Total</th>
<th style="width: 20%;">Created At</th>
<th style="width: 15%;">Status</th>
<th style="width: 10%; text-align: center;">Actions</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td style="text-align: center;"><?php echo $row['id']; ?></td>
<td>
<?php echo $row['company_name']; ?><br>
<?php echo $row['contact_name']; ?><br>
<small><?php echo $row['contact_position']; ?></small>
</td>
<td style="text-align: center;">$ <?php echo number_format($row['total_price'], 2); ?></td>
<td><?php echo formatDate($row['created_at']); ?></td>
<td><?php echo renderStatus($row['status']); ?></td>
<td>
<button class="btn btn-warning" data-toggle="modal" data-target="#editModal<?php echo $row['id']; ?>">Edit</button>
<button class="btn btn-danger" data-toggle="modal" data-target="#deleteModal<?php echo $row['id']; ?>">X</button>
</td>
</tr>
<!-- Edit Modal -->
<div class="modal fade" id="editModal<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editModalLabel">Edit Quote Status for Quote #<?php echo $row['id']; ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="POST">
<div class="modal-body">
<input type="hidden" name="quote_id" value="<?php echo $row['id']; ?>">
<input type="hidden" name="company_name" value="<?php echo $row['company_name']; ?>">
<input type="hidden" name="contact_name" value="<?php echo $row['contact_name']; ?>">
<div class="form-group">
<label for="quote_status">Quote Status</label>
<select name="quote_status" class="form-control" required>
<option value="Pending" <?php if ($row['status'] == 'Pending') echo 'selected'; ?>>Pending</option>
<option value="Approved" <?php if ($row['status'] == 'Approved') echo 'selected'; ?>>Approved</option>
<option value="Rejected" <?php if ($row['status'] == 'Rejected') echo 'selected'; ?>>Rejected</option>
<option value="Completed" <?php if ($row['status'] == 'Completed') echo 'selected'; ?>>Completed</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" name="update_quote" class="btn btn-primary">Update</button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Modal -->
<div class="modal fade" id="deleteModal<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Confirm Deletion of Quote #<?php echo $row['id']; ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="POST">
<div class="modal-body">
<input type="hidden" name="quote_id" value="<?php echo $row['id']; ?>">
<input type="hidden" name="company_name" value="<?php echo $row['company_name']; ?>">
<input type="hidden" name="contact_name" value="<?php echo $row['contact_name']; ?>">
<p>Are you sure you want to delete the quote with ID <strong><?php echo $row['id']; ?></strong> for <strong><?php echo $row['company_name']; ?></strong>?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" name="delete_quote" class="btn btn-danger">X</button>
</div>
</form>
</div>
</div>
</div>
<?php endwhile; ?>
</tbody>
</table>
<!-- Pagination -->
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<li class="page-item <?php if ($page <= 1) echo 'disabled'; ?>">
<a class="page-link" href="?page=<?php echo $page - 1; ?>">Previous</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php if ($i == $page) echo 'active'; ?>">
<a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<li class="page-item <?php if ($page >= $total_pages) echo 'disabled'; ?>">
<a class="page-link" href="?page=<?php echo $page + 1; ?>">Next</a>
</li>
</ul>
</nav>
</div>
<?php include 'footer.php';?>