-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoice.php
More file actions
243 lines (240 loc) · 8.14 KB
/
invoice.php
File metadata and controls
243 lines (240 loc) · 8.14 KB
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
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>LuLa Shop</title>
<?php include 'menu.php'; ?>
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<?php
//TODO: create account for this app
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lulashop";
// Connect to the database
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
die();
}
?>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$success = false;
$userID = $_SESSION["userID"];
if(isset($_POST["invoice"])){
$billAddressID = $_POST["billAddressID"];
if ($_POST["shipAddress"] == "same"){
$shipAddressID = $billAddressID;
} else {
$shipAddressID = $_POST["shipAddressID"];
}
if(empty($billAddressID) || empty($shipAddressID)){
echo "ERROR: Please review your address selections.";
} else {
$sql = "SELECT DISTINCT `memberID`
FROM `cart`,`inventory`
WHERE `cart`.`sku`=`inventory`.`sku`";
$pdo = $conn->query($sql);
while($seller = $pdo->fetchColumn()){
$sql2 = "SELECT *
FROM `address`
WHERE `addressID`=$billAddressID";
$pdo2 = $conn->query($sql2);
$pdo2->setFetchMode(PDO::FETCH_ASSOC);
$billAddressArr = $pdo2->fetch();
$sql2 = "SELECT *
FROM `address`
WHERE `addressID`=$shipAddressID";
$pdo2 = $conn->query($sql2);
$pdo2->setFetchMode(PDO::FETCH_ASSOC);
$shipAddressArr = $pdo2->fetch();
$sql2 = "INSERT INTO `invoice`
(
paid,
bill_street1,
bill_street2,
bill_city,
bill_state,
bill_zip,
ship_street1,
ship_street2,
ship_city,
ship_state,
ship_zip,
shipped,
userID,
memberID
)
VALUES
(
0,'".
$billAddressArr['street1']."','".
$billAddressArr['street2']."','".
$billAddressArr['city']."','".
$billAddressArr['state']."','".
$billAddressArr['zip']."','".
$shipAddressArr['street1']."','".
$shipAddressArr['street2']."','".
$shipAddressArr['city']."','".
$shipAddressArr['state']."','".
$shipAddressArr['zip']."',
0,
$userID,
$seller
)";
$conn->exec($sql2);
$invoiceNumber = $conn->lastInsertId();
$sql2 = "SELECT `cart`.`sku`
FROM `cart`,`inventory`
WHERE `cart`.`sku`=`inventory`.`sku`
AND `inventory`.`memberID`=$seller";
$pdo2 = $conn->query($sql2);
while($skuBySeller = $pdo2->fetchColumn()){
$sql3 = "INSERT INTO `invoiceItem`
(
invoiceNumber,
sku,
quantity,
free
)
VALUES
(
$invoiceNumber,
$skuBySeller,
1,
0
)
";
$conn->exec($sql3);
}
}
$sql = "SELECT `email` FROM `user` WHERE `userID`=$userID";
$pdo = $conn->query($sql);
$to = $pdo->fetchColumn();
$txt = "We have recieved the following orders:\n";
$sql = "SELECT `price`,`category`,`size`,`firstName`,`lastName`
FROM `cart`,`inventory`,`member`
WHERE `cart`.`userID`=$userID
AND `cart`.`sku`=`inventory`.`sku`
AND `inventory`.`memberID`=`member`.`memberID`";
$pdo = $conn->query($sql);
$pdo->setFetchMode(PDO::FETCH_NUM);
$txt .= "Price \t Category \t Size \t Consultant\n";
while ($item = $pdo->fetch()){
foreach ($item as $col){
$txt .=$col." \t ";
}
$txt .="\n";
}
$txt .= "Billing Address:\n";
foreach($billAddressArr as $b){
$txt .= $b."\n";
}
$txt .= "Shipping Address:\n";
foreach($shipAddressArr as $s){
$txt .= $s."\n";
}
$headers = "From: donotreply@localhost.com"; //TODO: change this to actual domain for deployment
$subject = "lulashop event notification";
if (!mail($to,$subject,$txt,$headers)){
echo "Email notification failed.<br>";
}
$sql = "DELETE FROM `cart` WHERE `userID`=$userID";
$conn->exec($sql);
echo "Invoices Created. Congrats! You will recieve a confirmation email.";
$success = true;
}
}
} else {
echo "access violation";
die();
}
?>
<div class="w3-container" <?php if($success)echo"style=\"display:none\"";?>>
<form action="invoice.php" method="post">
<div class="w3-row-padding">
<div class="w3-half">
<select class="w3-select" name="billAddressID">
<option value="NULL" disabled>Select billing address</option>
<?php
$sql = "SELECT * FROM `address` WHERE `ownerID`='u_".$userID."';";
echo "<br>".$sql;
$pdo = $conn->query($sql);
while ($result = $pdo->fetch()) {
echo "<option value=\"".$result['addressID']."\"";
if (isset($_POST["billAddressID"])){
if ($result['addressID'] == $_POST["billAddressID"]){
echo "selected";
}
}
echo ">";
echo $result['street1']." ".$result['street2']." ".$result['appt']." ".$result['city']." ".$result['state']." ".$result['zip'];
echo "</option>";
}
?>
</select>
<label class="w3-label w3-validate">billing address</label>
</div>
</div>
<div class="w3-row-padding">
<div class="w3-half">
<input class="w3-radio" type="radio" name="shipAddress" value="same"
<?php
if (isset($_POST["shipAddress"])) {
if ($_POST['shipAddress']=="same") {
echo "checked";
}
} else {
echo "checked";
}
?>>
<label class="w3-label">Shipping address is same as billing</label>
<input class="w3-radio" type="radio" name="shipAddress" value="diff"
<?php
if (isset($_POST["shipAddress"])) {
if ($_POST['shipAddress']=="diff") {
echo "checked";
}
}
?>>
<label class="w3-label">Use this address for shipping</label>
</div>
</div>
<div class="w3-row-padding">
<div class="w3-half">
<select class="w3-select" name="shipAddressID">
<option value="NULL" disabled>Select shipping address</option>
<?php
$pdo = $conn->query($sql);
while ($result = $pdo->fetch()) {
echo "<option value=\"".$result['addressID']."\"";
if (isset($_POST["shipAddressID"])){
if ($result['addressID'] == $_POST["shipAddressID"]){
echo "selected";
}
}
echo ">";
echo $result['street1']." ".$result['street2']." ".$result['appt']." ".$result['city']." ".$result['state']." ".$result['zip'];
echo "</option>";
}
?>
</select>
<label class="w3-label w3-validate">shipping address</label>
</div>
</div>
<div class="w3-row-padding">
<button class='w3-button' style='font-size:24px' type='submit' name='invoice' value='checkout'>Checkout<i class='material-icons'>shopping_cart</i></button>
</div>
</form>
</body>