-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvieworders.php
265 lines (224 loc) · 10.5 KB
/
vieworders.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
<?php
session_start();
require_once("Scripts/DBConnect.php");
require_once('Scripts/GeneralScripts.php');
// If logged in as staff, must have permissions
// If logged in as customer fine
if(isset($_SESSION['UserID'])){
if($_SESSION['UserType'] == "Staff"){
// If Staff, Check Perms
checkLoginPermissions(5);
}
}
else{
// If not logged in, redirect to index.php
header("location: index.php");
}
// Search Variables
$SearchText = $_GET['SearchText'] ?? '';
$ResultLimit = $_GET['ResultLimit'] ?? 10;
$sqlResultLimit = ($ResultLimit == -1) ? "" : "LIMIT $ResultLimit";
if($_SERVER['REQUEST_METHOD'] == "GET"){
if($_SESSION['UserType'] == "Staff"){
// Delete Order if DeleteID Given
if(isset($_GET['DeleteID'])){
$DeleteID = $_GET['DeleteID'];
$sqlDelete = " DELETE FROM tblOrderProducts
WHERE OrderID = $DeleteID";
mysqli_query($db, $sqlDelete);
$sqlDelete = " DELETE FROM tblOrder
WHERE OrderID = $DeleteID
Limit 1";
mysqli_query($db, $sqlDelete);
}
}
}
// If Logged in as customer, only show orders for that customer
$sqlCustomerLimit = "";
if($_SESSION['UserType'] == 'Customer'){
$UserID = $_SESSION['UserID'];
$sql = "SELECT
cl.CustomerID
FROM tblCustomerLogin cl
WHERE cl.CustomerLoginID = $UserID
LIMIT 1";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_assoc($result);
$CustomerID = $row['CustomerID'];
$sqlCustomerLimit = "AND o.CustomerID = $CustomerID";
}
// Get List of Orders
$sqlOrders = " SELECT
o.OrderID,
o.CreationDate,
o.CustomerID,
c.CustomerName,
CONCAT(s.Forename, ' ', s.Surname) as StaffName,
o.TotalCost,
o.DeliveryDate
FROM tblOrder o
JOIN tblCustomer c
ON o.CustomerID = c.CustomerID
JOIN tblStaff s
ON o.StaffID = s.StaffID
WHERE o.OrderID LIKE '%$SearchText%'
$sqlCustomerLimit
ORDER BY o.CreationDate DESC
$sqlResultLimit";
$Orders = mysqli_query($db, $sqlOrders);
// Get List of Customers
$sqlCustomers = " SELECT
c.CustomerID,
c.CustomerName
FROM tblCustomer c";
$Customers = mysqli_query($db, $sqlCustomers);
?>
<script>
function showSubTable(OrderID){
SubTable = document.getElementById(`SubTable-${OrderID}`);
if(SubTable.hidden){
SubTable.hidden = false;
}
else{
SubTable.hidden = true;
}
}
function deleteOrder(OrderID){
window.location.replace(`vieworders.php?DeleteID=${OrderID}`);
}
function addOrder(){
window.location.replace("addorder.php?NewBasket=1");
}
function editOrder(CustomerID, OrderID){
window.location.replace(`addorder.php?Edit=1&CustomerID=${CustomerID}&OrderID=${OrderID}`);
}
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<title>CarCo - Orders</title>
</head>
<body class="CentrePage">
<?php include("Widgets/navigation.php") ?>
<main class="content">
<!-- Orders Section -->
<section id="Orders">
<?php include("Widgets/SearchBar.php"); ?>
<!-- Error / Success Message -->
<p <?php echo(str_contains($_GET['UploadStatus'] ?? "", "Error") ? "class='error'" : "class='message'"); ?>><?php echo($_GET['UploadStatus'] ?? '') ?></p>
<table>
<caption><h2>Orders</h2></caption>
<thead>
<tr>
<th>OrderID</th>
<th>CreationDate</th>
<th>Customer</th>
<th>Created By</th>
<th>Delivery Date</th>
<th>Total Cost</th>
<th>Show More</th>
<th <?php echo(hideContent(5)); ?>>Edit</th>
<th <?php echo(hideContent(5)); ?>>Delete</th>
</tr>
</thead>
<tbody>
<?php
if($Orders->num_rows>0){
while($row = mysqli_fetch_assoc($Orders)) {
$OrderID = $row['OrderID'];
$CreationDate = $row['CreationDate'];
$CustomerID = $row['CustomerID'];
$CustomerName = $row['CustomerName'];
$StaffName = $row['StaffName'];
$DeliveryDate = $row['DeliveryDate'];
$TotalCost = $row['TotalCost'];
$PermissionCheck = hideContent(5);
echo("
<tr>
<td>$OrderID</td>
<td>$CreationDate</td>
<td>$CustomerName</td>
<td>$StaffName</td>
<td>$DeliveryDate</td>
<td>£$TotalCost</td>
<td class='info'><button type='button' onclick='showSubTable($OrderID)'>ⓘ</button></td>
<td class='edit' $PermissionCheck><button type=button onclick='editOrder($CustomerID, $OrderID)'>🖉</button></td>
<td class='delete' $PermissionCheck><button type=button onclick='deleteOrder($OrderID)'>🗑</button></td>
</tr>
");
// Get Order Products
$sqlOrderProducts = " SELECT
sp.ProductName,
op.Quantity,
sp.CostPerUnit,
op.Subtotal
FROM tblOrderProducts op
JOIN tblSystemProduct sp
ON op.SystemProductID = sp.SystemProductID
WHERE op.OrderID = $OrderID";
$OrderProducts = mysqli_query($db, $sqlOrderProducts);
echo("
<tr id='SubTable-$OrderID' hidden>
<td colspan='9'>
<table class='SubTable'>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Cost Per Unit</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
");
while($row2 = mysqli_fetch_assoc($OrderProducts)){
$ProductName = $row2['ProductName'];
$Quantity = $row2['Quantity'];
$CostPerUnit = $row2['CostPerUnit'];
$Subtotal = $row2['Subtotal'];
echo("
<tr>
<td>$ProductName</td>
<td>$Quantity</td>
<td>£$CostPerUnit</td>
<td>£$Subtotal</td>
</tr>
");
}
echo("
</tbody>
<tfoot>
<td colspan=3></td>
<td><b>£$TotalCost</b></td>
<tfood>
</table>
</td>
</tr>
");
}
}
else {
echo("
<tr>
<td colspan='9'>No Results Found.</td>
</tr>
");
}
?>
</tbody>
<tfoot>
<tr>
<td colspan="9"><i><?php echo("$Orders->num_rows Results.") ?></i></td>
</tr>
</tfoot>
</table>
<br>
<button type="button" id="NewOrderButton" onclick="addOrder()" <?php echo(hideContent(5)); ?>>Add New Order</button>
</section>
</main>
<?php include("Widgets/footer.php"); ?>
</body>
</html>