-
Notifications
You must be signed in to change notification settings - Fork 0
/
createOrder.php
194 lines (157 loc) · 7.33 KB
/
createOrder.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
<?php
include("includes/header.php");
require_once ("../../mysqli_Coffee_config.php"); // Connect to the database
include("includes/generateRandomIDs.php");
?>
<?php
$query = "SELECT storeName FROM Store";
$result = mysqli_query($dbc, $query);
if ($result){
$all_rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
}
else{
exit;
}
mysqli_free_result($result);
$query2 = "SELECT supplierName FROM Supplier";
$result2 = mysqli_query($dbc, $query2);
if ($result2){
$all_rows2 = mysqli_fetch_all($result2, MYSQLI_ASSOC);
}
else{
exit;
}
mysqli_free_result($result2);
if (isset($_POST['submitOrder'])){
$errors = array();
$selectedStore = $_POST['store'];
if ($selectedStore == ""){
$errors['store'] = "Please select the store you wish to make an order for";
}
$selectedSupplier = $_POST['supplier'];
if ($selectedSupplier == ""){
$errors['store'] = "Please select the supplier you wish to place an order from";
}
if(isset($_POST['items'])){
$selectedItem = $_POST['items'];
if (in_array("", $selectedItem)){
$errors['item'] = "Please select the item you wish to place an order for";
}
}
else{
$errors['item'] = "Please select the item you wish to place an order for";
}
if (!$errors){
// ini_set('display_errors', 1);
$supplierQuery = "SELECT supplierID FROM Supplier WHERE supplierName = ?";
$stmt6 = mysqli_prepare($dbc, $supplierQuery);
mysqli_stmt_bind_param($stmt6, 's', $selectedSupplier);
mysqli_stmt_execute($stmt6);
$result3 = mysqli_stmt_get_result($stmt6);
$row2 = mysqli_fetch_assoc($result3);
$supplierID = $row2['supplierID'];
$query3 = "SELECT storeID FROM Store WHERE storeName = ?";
$stmt = mysqli_prepare($dbc, $query3);
mysqli_stmt_bind_param($stmt, 's', $selectedStore);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
$storeID = $row['storeID'];
$query4 = "SELECT productID FROM Item WHERE itemName IN ('" . implode("', '", $selectedItem) . "')";
$result2 = mysqli_query($dbc, $query4);
/*
code block to check whether the item already exists or not
*/
$productIDs = array();
if (mysqli_num_rows($result) >= 1 && mysqli_num_rows($result2) >= 1 && mysqli_num_rows($result3) >= 1){
while ($row = mysqli_fetch_assoc($result2)){
$productIDs[] = $row['productID'];
}
$existingProducts = array();
foreach($productIDs as $productID){
$sql = "SELECT productID FROM StoreHasItem WHERE storeID = '$storeID' AND productID = '$productID'";
$itemExists = mysqli_query($dbc, $sql);
if(mysqli_num_rows($itemExists) >= 1){
$existingProducts[] = $productID;
}
}
if(empty($existingProducts)){
$trackingNumber = generateRandomTrackingNumber();
$dateOrdered = date("Y-m-d");
$query5 = "INSERT INTO PurchaseOrder(trackingNum, storeID, dateOrdered) VALUES (?,?,?)";
$stmt3 = mysqli_prepare($dbc, $query5);
mysqli_stmt_bind_param($stmt3, 'iss', $trackingNumber, $storeID, $dateOrdered);
mysqli_stmt_execute($stmt3);
foreach($productIDs as $productID){
$quantity = generateRandomPurchaseQuantity($storeID);
$query6 = "INSERT INTO OrderHasItem (productID, trackingNum, quantity) VALUES (?, ?, ?)";
$stmt4= mysqli_prepare($dbc, $query6);
mysqli_stmt_bind_param($stmt4, 'sii', $productID, $trackingNumber, $quantity);
mysqli_stmt_execute($stmt4);
$query7 = 'INSERT INTO StoreHasItem (storeID, productID, quantity) VALUES (?,?,?)';
$stmt5= mysqli_prepare($dbc, $query7);
mysqli_stmt_bind_param($stmt5, 'ssi', $storeID, $productID, $quantity);
mysqli_stmt_execute($stmt5);
}
generateShipmentEntryInfo($dbc, $trackingNumber, $supplierID , $dateOrdered);
if (mysqli_stmt_affected_rows($stmt3) && mysqli_stmt_affected_rows($stmt4) && mysqli_stmt_affected_rows($stmt5)){
echo("<div class=\"success\"><h2>Order confirmation for order number $trackingNumber,
shipment entries successful.</h2></div>");
}
}
else{
echo("<div class=\"success\"><h2>We're sorry, the item(s) with product code(s) <span class=\"textEmphasis\">" . implode("', '", $existingProducts). "</span> already exists for this store.</h2></div>");
}
}
}
exit;
}
?>
<h2 class="form_header">PURCHASE ORDER FORM</h2>
<div class="formDescription">
<p>Please fill out the form with your order details:
</p>
</div>
<form class="createOrderForm" class="topBefore" method="POST" action="createOrder.php">
<?php if ($errors){
echo ("<h3 class=\"warning\">Please fix the item(s) indicated: </h3>");
};
?>
<?php if ($errors['store']) echo("<h2 class=\"warning\">{$errors['store']}</h2>");?>
<select name="store" id="store">
<option value="">SELECT STORE</option>
<?php
foreach($all_rows as $store){
echo("<option");
if ($selectedStore == $store['storeName']){
echo(" selected");
}
echo(">".$store['storeName']."</option>");
}
?>
</select>
<?php if ($errors['supplier']) echo("<h2 class=\"warning\">{$errors['supplier']}</h2>");?>
<select name="supplier" id="supplier" onchange="getSupplierInventory(this);" onfocus="this.selectedIndex = -1;">
<option value="">SELECT SUPPLIER</option>
<?php
foreach($all_rows2 as $supplier){
echo("<option id=\"supplier\" ");
if ($selectedSupplier == $supplier['supplierName']){
echo(" selected");
}
echo(">".$supplier['supplierName']."</option>");
}
?>
</select>
<?php if ($errors['item']) echo("<h2 class=\"warning\">{$errors['item']}</h2>");?>
<select name="items[]" id="items" class="items" size="8" multiple <?php if (isset($selectedItem)){
echo("style=\"display:block;\"");
}
else{
echo("style=\"display:none;\"");
}?>></select>
<input id="submit" type="submit" value="ADD PRODUCT" name="submitOrder">
</form>
</main>
</body>
</html>