-
Notifications
You must be signed in to change notification settings - Fork 1
/
ManualOrder.php
420 lines (333 loc) · 16.9 KB
/
ManualOrder.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
require 'database/model.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Manual Order</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/style.css" charset="utf-8">
<link rel="icon" href="images/favicon.ico" type="image/gif" sizes="32x32">
<script src="js/jquery-2.2.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<?php
session_start();
if(!isset($_SESSION['user']) && !isset($_COOKIE['user'])){
header("Location: Login.php");
}
if(isset($_SESSION['user'])){
if($_SESSION['user']!="admin") {
echo "You have no access to this page!";
exit;
}
}
if(isset($_COOKIE['user'])){
if($_COOKIE['user']!="admin"){
echo "You have no access to this page!";
exit;
}
}
?>
<nav class="navbar navbar-inverse navbar-static-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="AdminHome.php">MyCafe</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="Products.php">Products</a></li>
<li><a href="Users.php">Users</a></li>
<li class="active"><a href="ManualOrder.php">Manual Order<span class="sr-only">(current)</span></a></li>
<li><a href="Checks.php">Checks</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><img src="images/admin.png" alt="admin" width="50px" height="50px" /></li>
<li><a>Admin</a></li>
<li>
<form action="Logout.php" method="post" >
<input type="hidden" name="ss" value="any">
<button type="submit" class="add-user btn btn-default logout">Logout</button>
</form>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="col-md-3 panel panel-default" id="create_order">
<div class="panel panel-heading panel panel-warning">
<h1> Order </h1>
</div>
<form method="post" >
<div class="row panel-body" id="create_order_products">
</div>
<label class="col-sm-2 control-label">Notes:</label>
<textarea class="form-control" rows="3" id="notes"></textarea>
<label class="col-sm-2 control-label">Rooms:</label>
<select name="room" id="room" class="form-control" id="user">
<?php
/**
* get all rooms numbers
*/
$obj_rooms = ORM::getInstance();
$obj_rooms->setTable('rooms');
$all_rooms = $obj_rooms->select_all();
if ($all_rooms->num_rows > 0) {
while ($room = $all_rooms->fetch_assoc()) {
foreach ($room as $key => $value) {
?>
<option value="<?php $room['number'] ?>"><?php echo $room['number'] ?>
<?php
}
}
} else {
?>
<option>NO Rooms
<?php
}
?>
</select>
<input type='submit' name='confirm' value='Confirm' class="btn btn-primary" onclick="get_order()"><br/>
<label id="total_price" class=" control-label">Total Price: 0</label>
</form>
</div>
<div class="col-md-9 panel panel-default">
<div class="row panel-body">
<div class="panel panel-heading panel panel-warning">
<h1> Add to user:</h1>
</div>
<select class="form-control " id="user">
<?php
/**
* get all users name
*/
$obj_users = ORM::getInstance();
$obj_users->setTable('users');
$all_users = $obj_users->select(array('is_admin' => 0));
if ($all_users->num_rows > 0) {
while ($user = $all_users->fetch_assoc()) {
?>
<option value="<?php echo $user['name']; ?>"> <?php echo $user['name']; ?>
<?php
}
} else {
?>
<option>NO Users
<?php
}
?>
</select>
</div>
<div class="row">
<div class="row panel-body">
<div class=" panel panel-heading panel panel-warning">
<h1>Menu</h1>
</div>
<?php
/**
* Select all products
*/
$obj_product = ORM::getInstance();
$obj_product->setTable('products');
$products = $obj_product->select(array("is_available" => 1));
//if there is more than one product while loop every time fetch row
if ($products->num_rows > 0) {
$j = 1;
while ($row = $products->fetch_assoc()) {
?>
<div class="col-md-3 product_div">
<img src="<?php echo "images/products/" . $row['pic']; ?>" width="120px" height="120px" class="img-responsive img-circle"
onclick="add_product('<?php echo $row['name']; ?>',<?php echo $row['id']; ?>,<?php echo $row['price']; ?>)">
<div class="row col-lg-offset-2 badge "> <?php echo $row['price']; ?> .LE</div>
</div>
<?php
$j = $j + 1;
}
} else {
echo "NO Products!!";
}
?>
</div>
</div>
</div>
</div>
<script>
//global array of products id
var products_id = [];
/**
* this function to append the product in order form
* @param {type} name
* @param {type} id
* @param {type} price
* @returns {undefined}
*/
function add_product(name, id, price) {
//if condition to check id the product is ordered before in this
//order to increase the amount or not
//check if the id of the product in the global array or not
if (products_id.indexOf(id) === -1) {
//if product isn`t orderd before push it`s id in the array of products
products_id.push(id);
//select the div of order to append on the products
var elem_order = document.getElementById("create_order_products");
//create div it`s id equal to the product id
var elem_product = document.createElement("div");
elem_product.setAttribute("id", id);
elem_product.setAttribute("class", "product");
//create label for product with it`s name
var elem_product_name = document.createElement("label");
elem_product_name.setAttribute("class", " control-label");
elem_product_name.innerHTML = " Name: " + name;
//create input field for amount of product
var elem_product_amount = document.createElement("input");
elem_product_amount.setAttribute("class", "form-control");
elem_product_amount.setAttribute("type", "number");
elem_product_amount.setAttribute("name", "amount");
elem_product_amount.setAttribute("value", "1");
elem_product_amount.setAttribute("min", "1");
elem_product_amount.setAttribute("onclick", "add_amount(" + id + "," + price + ")");
//create label for product with it`s price
var elem_product_price = document.createElement("label");
elem_product_price.setAttribute("class", " control-label");
elem_product_price.innerHTML = " Price: " + price;
//create button to cancel product
var cancel_btn = document.createElement("button");
cancel_btn.innerHTML = "x";
cancel_btn.setAttribute("class", " btn btn-danger pull-right ");
cancel_btn.setAttribute("onclick", "cancel(" + id + ")");
//appeand parameter
elem_product.appendChild(elem_product_name);
elem_product.appendChild(cancel_btn);
elem_product.appendChild(elem_product_amount);
elem_product.appendChild(elem_product_price);
elem_order.appendChild(elem_product);
} else {
//get the div of product by it`s id number
var elem_exists_product = document.getElementById(id);
//get value of the product and increase it by one
var value = elem_exists_product.childNodes[2].value;
value = parseInt(value) + 1;
elem_exists_product.childNodes[2].setAttribute("value", value);
//increase the price by increase it`s amount
var new_price = price * value;
elem_exists_product.childNodes[3].innerHTML = " Price: " + new_price;
}
//set the total price of the order in the label of total price
//by select all products dev and get it`s price
var total_price = 0;
var products = document.getElementsByClassName("product");
for (var i = 0; i < products.length; i++) {
total_price += parseInt(products[i].childNodes[3].innerHTML.split(" ")[3]);
}
var elem_order_price = document.getElementById("total_price");
elem_order_price.innerHTML = "Total Price: " + total_price;
}
/**
* this function that get the order informations to insert in database
* @returns {undefined}
*/
function get_order() {
//check if the form order has childs of products or not
var elem_order = document.getElementById("create_order_products");
if (elem_order.childElementCount > 0) {
//get value of all price of order
var elem_order_price = document.getElementById("total_price");
var orderPrice = elem_order_price.innerHTML.split(" ");
//get all order notes
var elem_order_notes = document.getElementById("notes").value;
//get room name from the select
var elem_order_room = document.getElementById("room");
var order_room = elem_order_room.options[elem_order_room.selectedIndex].text;
//get user name from the select
var elem_order_user = document.getElementById("user");
var order_user = elem_order_user.options[elem_order_user.selectedIndex].text;
var product_info = "";
//forloop to get all product and send it in array to request
for (var i = 1; i <= elem_order.childElementCount; i++) {
var all_products = elem_order.childNodes;
//console.log(all_products[1]);
var product = all_products[i];
//alert(product.nodeType ? "true" : "false" );
var product_id = product.getAttribute("id");
var product_amount = product.childNodes[2].value;
var product_price = product.childNodes[3].innerHTML.split(" ")[3];
product_info += product_id + ":" + product_amount + ":" + product_price + ",";
}
//open xmlhttp request that render to user_order and send total order & products
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "admin_order.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("order_price=" + orderPrice[2] + "&room=" + order_room + "&user=" + order_user + "¬es=" + elem_order_notes + "&array=" + product_info);
//on change check even the request send or not
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
console.log(xmlhttp.responseText);
}
};
}
}
/**
* this function used to increase the amount of product when changing it by hand
* @param {type} id
* @param {type} price
* @returns {undefined}
*/
function add_amount(id, price) {
//get the div of product by it`s id number
var elem_exists_product = document.getElementById(id);
//get value of the product
var value = elem_exists_product.childNodes[2].value;
value = parseInt(value);
//if condition to make the value of product not decrease about 1
if (value < 1) {
value = 1;
elem_exists_product.childNodes[2].setAttribute("value", value);
}
//increase the price by increase it`s amount
var new_price = price * value;
elem_exists_product.childNodes[3].innerHTML = " Price: " + new_price;
//set the total price of the order in the label of total price
//by select all products dev and get it`s price
var total_price = 0;
var products = document.getElementsByClassName("product");
for (var i = 0; i < products.length; i++) {
total_price += parseInt(products[i].childNodes[3].innerHTML.split(" ")[3]);
}
var elem_order_price = document.getElementById("total_price");
elem_order_price.innerHTML = "Total Price: " + total_price;
}
/**
* cancel function to cancel request of certain product
*/
function cancel(id) {
//get the div of product by it`s id number
var elem_exists_product = document.getElementById(id);
//remove the product request
elem_exists_product.remove();
//remove the id of element from the array
var index = products_id.indexOf(id)
if (index > -1) {
products_id.splice(index, 1);
}
//set the total price of the order in the label of total price
//by select all products dev and get it`s price
var total_price = 0;
var products = document.getElementsByClassName("product");
for (var i = 0; i < products.length; i++) {
total_price += parseInt(products[i].childNodes[3].innerHTML.split(" ")[3]);
}
var elem_order_price = document.getElementById("total_price");
elem_order_price.innerHTML = "Total Price: " + total_price;
}
</script>
</body>
</html>