This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfirmOrder.php
223 lines (186 loc) · 5.28 KB
/
confirmOrder.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
<?php include "session.php"; ?>
<?php include_once "./models/DatabaseLink.php"; ?>
<?php
/*
*confirmOrder.php
*proccesses the order, and stores all the information in the correct table of the DB
*/
header("location: checkout.php");
//connect to the database
$db = new DatabaseLink();
$con = $db->connection;
$query = "";
$row = array();
//passed in variables
$curU = $_SESSION['accountId'];
$progress = $_SESSION['progress'];
//variables
$cart = array();
$quanities = array();
$size = 0;
$total =0;
$fail = 1;
$query = ("SELECT product_id FROM `cart_items` WHERE account_id = '$curU' " );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
//get size of the cart
while($row = mysql_fetch_array($result))
{
$cart[$size] = $row[0];
$size++;
}
/*
Check if all items are still in stock while proccessing.
*/
for($i = 0; $i < $size; $i++)
{
$query = ("SELECT amount FROM `cart_items` WHERE account_id = '$curU' AND product_id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$quanity = $row[0];
$query = ("SELECT name, price, inventory FROM `products` WHERE id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$name = $row[0];
$price = $row[1];
$quanityLeft = $row[2];
if($quanityLeft == 0 or $quanityLeft < $quanity)
{
$fail = 0;
}
else
{
$total = $total + $price*$quanity;
}
}
//set progress to none incase of failure.
$_SESSION['progress'] = -10;
$order = -1;
/*
insert a new entry ito the table, and retrieve the new order ID
*/
if($fail != 0 and
$_SESSION['shippingCity'] != "" and
$_SESSION['shippingZip'] != "" and
$_SESSION['shippingState'] != "" and
$_SESSION['creditnumber']!= "" and
$_SESSION['creditcode']!= "" and
$_SESSION['creditname']!= "" and
$_SESSION['shippingPhone']!= "" and
$_SESSION['billingAddress']!= "" and
$_SESSION['billingCity']!= "" and
$_SESSION['billingZip']!= "" and
$_SESSION['billingState']!= "" and
$_SESSION['shippingName']!= "" and
$_SESSION['billingName']!= "" and
$_SESSION['billingPhone']!= "" and
$_SESSION['shipping']!= "" and
$_SESSION['creditdate']!= "" )
{
$all = $total + $total*.06 + $_SESSION['shipping'];
$fields = array($_SESSION['shippingAddress'],
$_SESSION['shippingCity'],
$_SESSION['shippingZip'],
$_SESSION['shippingState'],
$_SESSION['creditnumber'],
$_SESSION['creditcode'],
$_SESSION['creditname'],
$_SESSION['shippingPhone'],
$_SESSION['billingAddress'],
$_SESSION['billingCity'],
$_SESSION['billingZip'],
$_SESSION['billingState'],
$_SESSION['shippingName'],
$_SESSION['billingName'],
$_SESSION['billingPhone'],
$_SESSION['shipping'],
$_SESSION['creditdate']
);
$query =
("INSERT INTO orders (
account_id,
shipping_address,
shipping_city,
shipping_zip,
shipping_state,
credit_num,
credit_sec,
credit_name,
phone,
billing_address,
billing_city,
billing_zip,
billing_state,
shipping_name,
billing_name,
billing_phone,
subtotal,
shipping_price,
total_amount,
credit_exp)
VALUES (
'$curU',
'$fields[0]',
'$fields[1]',
'$fields[2]',
'$fields[3]',
'$fields[4]',
'$fields[5]',
'$fields[6]',
'$fields[7]',
'$fields[8]',
'$fields[9]',
'$fields[10]',
'$fields[11]',
'$fields[12]',
'$fields[13]',
'$fields[14]',
'$total',
'$fields[15]',
'$all',
'$fields[16]')");
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$query = ("SELECT id FROM `orders` WHERE account_id='$curU'");
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
while($row = mysql_fetch_array($result))
{
$order = $row[0];
}
$_SESSION['progress'] = 100;
}
/*
Loop through everything in the order.
if everything is still in stock,
add it to the order_product table
remove amount from stock.
remove from cart
repeat per item in cart.
*/
for($i = 0; $i < $size; $i++)
{
if($fail != 0)
{
$query = ("SELECT amount FROM `cart_items` WHERE account_id = '$curU' AND product_id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$quanity = $row[0];
$query = ("SELECT name, price, inventory FROM `products` WHERE id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$name = $row[0];
$price = $row[1];
$quanityLeft = $row[2];
$query = ("INSERT INTO order_products (order_id, product_id, amount) VALUES ($order, $cart[$i], $quanity)");
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
//$row = mysql_fetch_array($result);
$query = ("SELECT inventory FROM `products` WHERE id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$a = $row[0] - $quanity;
$query = ("UPDATE `products` SET inventory = $a WHERE id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$query = ("DELETE FROM `cart_items` WHERE account_id= '$curU' AND product_id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
}
}
$db->disconnect();
?>