This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
order.php
125 lines (117 loc) · 4.59 KB
/
order.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
<?php
/************************************************
File: order.php
Author: Oliver Chi
Purpose: display order; generate new order;
**************************************************/
require("libcommon.php");//add common interfaces
$db = loadDB(); //load database
// Set out order page
require("layout.php");//get layout of HTML
pageCookie("order");//set order page cookie
/* Actions */
if ($_SERVER["REQUEST_METHOD"] == "POST") {//pay order and store order
echo var_dump($_POST);
// $info = json_decode($_POST);
// for ($i=0; $i < count($info);$i++){
// $product = json_decode( info[$i] );
// }
//
//
// echo "Order ID: ".$order->id." Date: ".$order->date." Paid: ".$order->ifpaid;
} else if ($_SERVER["REQUEST_METHOD"] == "GET") {//display current order waiting for payment
if (isset($_GET["order"])){
$info = json_decode("[".$_GET["order"]."]");//json missing infomation
//create new order in Database
if (getNextID("orders")!=""){
$orderid = getNextID("orders");
} else {
$orderid = 10000001;
}
$userid = getUserID();
$date = date("Y-m-d");
if (insertOrder($orderid, $userid, $date, 0, 1, getNextID("delivery"))){//insert order
$j = 0; // calculate insert times
$totalprice = 0; //for calculate totalprice
for ($i=0; $i < count($info);$i++){
if (getNextID("orderproducts")!=""){//get next id in orderproducts table
$id = getNextID("orderproducts");
} else {
$id = 10000001;
}
$productid = $info[$i]->id;
$amount = intval($info[$i]->quantity);
$result = $db->query("SELECT product.price FROM product WHERE product.id = $productid ;");//sql
while($product = $result->fetchArray()){//get product price
$price = floatval($product[0]);
}
$totalprice = $totalprice + $price * $amount;//calculate totalprice
if (insertOrderProducts($id, $orderid, $productid, $price, 1.0, $amount)){//insert product in orderproducts table
$j = $j + 1;
} else {
echo "FAIL: insert product $productid into orderproducts table";
}
}
if ($j == $i && updateTotalprice($orderid, $totalprice)) {//successfully generate order
echo "[Successfully Generate Order In Database]: ";
echo " Order ID: $orderid --";
echo " Date: $date --";
echo " Paid: YES";
}
} else {
echo "Database Error. Please Try It Later.";
}
} else {
echo $header;//display header
/* redesign content of page */
/* display current order */
echo "<script language = 'javascript' type = 'text/javascript'>";
echo "// Pay Current Order (protected method)
// Store Order Into Database
function payOrder(info){
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200){
$('#order_info').empty();
$('#order_info').text(this.responseText);
$('#paythisorder').css('display','none');
}
}
ajaxRequest.open('GET', 'order.php?order='+info, true);
ajaxRequest.send();
}
";
echo "</script>";
echo "<div id='content'><!-- Order Content--><div id='order'><!-- Display Order --><h2> Current Order:</h2>";
echo "<center><h3 id='order_info'>Order ID: newOrder Date: ".date("d/m/Y")." Paid: NO</h3></center>";
echo "<table>
<tr>
<th>Product ID</th><th>Product Name</th><th>Quantity</th><th>Price per Unit</th>
</tr>
";
$totalprice=0;
//echo var_dump($_GET);//only for test
for ($i=0; $i < count($_GET['product']);$i++){
$product = json_decode( $_GET['product'][$i] );
echo "<tr>";
echo "<td>".$product->id."</td>";
$result = $db->query("SELECT product.title, product.price FROM product WHERE product.id == ".$product->id);//sql
while($book = $result->fetchArray()){
echo "<td>".$book['title']."</td>";
echo "<td>".$product->quantity."</td>";
echo "<td>$".$book['price']."</td></tr>";
$totalprice = $totalprice + (float)$book['price']*((float)($product->quantity));
}
}
echo "<tr><td> </td><td> </td><td>Total:</td><td>$".(string)$totalprice."</td></tr></table>";
echo "</table>";
$info = json_encode($_GET['product']);
//echo var_dump($info);//for test
echo "<input id='paythisorder' onclick='payOrder($info)' type='button' value='Pay This Order'>";
echo "</div></div><!-- End of Content-->";
echo $footer;//display footer
}
}
$db->close();
exit;
?>