-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsellDRAFT.php
242 lines (201 loc) · 8.67 KB
/
sellDRAFT.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
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
$dbname = 'users';
$conn = mysqli_connect($hostname, $username, $password, $dbname);
?>
<?php
session_start();
$userID = $_SESSION['$userID'];
if(!empty($userID)){
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<title>Sell Product</title>
<link rel="stylesheet" href="sell.css">
<link rel="icon" href="icons/sell.png" type="image/icon type">
</head>
<body>
<div class="navbar">
<div class="navbar-shop-logo">
<a href="dashboard.php"><img src="icons/shop.png" style="width: 90px; height: 90px;">
</div>
<div class="navbar-dashboard-button">
<a href='dashboard.php'><button>Dashboard</button></a>
</div>
<div class="navbar-product-button">
<a href='product.php'><button>Products</button></a>
</div>
<div class="navbar-add-product-button">
<a href='add.php'><button>Add</button></a>
</div>
<!-- <div class="navbar-update-product-button">
<a href='update.html'><button>Update</button></a>
</div> -->
<div class="navbar-return-product-button">
<a href='return.php'><button>Return</button></a>
</div>
<div class="navbar-logout-button">
<a href='logout.php'><button>Logout</button></a>
</div>
</div>
<?php
if(isset($_POST['customer_contact']) && isset($_POST['product_id'])){
$customer_name = $_POST['customer_name'];
$customer_contact = $_POST['customer_contact'];
$product_id = $_POST['product_id'];
$product_quantity = $_POST['product_quantity'];
$queryCheck = "SELECT customer_contact FROM customers WHERE customer_contact='$customer_contact'";
$data = $conn->query($queryCheck);
if(mysqli_num_rows($data) == 0){
$sql = "INSERT INTO customers (customer_name, customer_contact)
VALUES ('$customer_name', '$customer_contact')";
if($conn->query($sql) == "TRUE"){
$x = 0;
}
$sellQuery = "SELECT product_quantity FROM products WHERE product_id = '$product_id'";
$data = $conn->query($sellQuery);
$Quantity = mysqli_fetch_array($data);
//$quantityINT = intval($Quantity);
//$productQuantityINT = $product_quantity;
if($Quantity['product_quantity'] < $product_quantity){
echo '<script>alert("Not enough Quantity")</script>';
}
else{
$productQuantity = $Quantity['product_quantity'] - intval($product_quantity);
$update = "UPDATE products
SET product_quantity = '$productQuantity'
WHERE product_id = '$product_id'";
if($conn->query($update) == TRUE){
echo '<script>alert("Product Sold Successfully!")</script>';
}
}
//implementin order ID generator
$orderQuery = "SELECT * FROM orders ORDER BY order_id DESC LIMIT 1";
$result = $conn->query($orderQuery);
$row = mysqli_fetch_array($result);
$lastID = $row['order_id'];
if($lastID == " "){
$orderID = "HK1";
}
else{
$orderID = substr($lastID, 3);
$orderID = intval($orderID);
$orderID = "HK" . ($orderID+1);
}
$priceQuery = "SELECT product_price FROM products WHERE product_id = '$product_id'";
$check = $conn->query($priceQuery);
$priceOutput = mysqli_fetch_array($check);
$totalPrice = $product_quantity * $priceOutput['product_price'];
$insertOrderIDquery = "INSERT INTO orders (order_id, customer_contact, product_id, product_quantity, total_price) VALUES ('$orderID', '$customer_contact', '$product_id', '$product_quantity', '$totalPrice')";
$insertion = $conn->query($insertOrderIDquery);
}
else{
$sellQuery = "SELECT product_quantity FROM products WHERE product_id = '$product_id'";
$data = $conn->query($sellQuery);
$Quantity = mysqli_fetch_array($data);
//$quantityINT = intval($Quantity);
//$productQuantityINT = $product_quantity;
if($Quantity['product_quantity'] < $product_quantity){
echo '<script>alert("Not enough Quantity")</script>';
}
else{
$productQuantity = $Quantity['product_quantity'] - intval($product_quantity);
$update = "UPDATE products
SET product_quantity = '$productQuantity'
WHERE product_id = '$product_id'";
//implementin order ID generator
$orderQuery = "SELECT * FROM orders ORDER BY order_id DESC LIMIT 1";
$result = $conn->query($orderQuery);
$row = mysqli_fetch_array($result);
$lastID = $row['order_id'];
if($lastID == " "){
$orderID = "HK1";
}
else{
$orderID = substr($lastID, 2);
$orderID = intval($orderID);
$orderID = "HK" . ($orderID+1);
}
//implementing total price calculator
$priceQuery = "SELECT product_price FROM products WHERE product_id = '$product_id'";
$check = $conn->query($priceQuery);
$priceOutput = mysqli_fetch_array($check);
$totalPrice = $product_quantity * $priceOutput['product_price'];
$insertOrderIDquery = "INSERT INTO orders (order_id, customer_contact, product_id, product_quantity, total_price) VALUES ('$orderID', '$customer_contact', '$product_id', '$product_quantity', '$totalPrice')";
$insertion = $conn->query($insertOrderIDquery);
if($conn->query($update) == TRUE){
echo '<script>alert("Product Sold Successfully!")</script>';
}
}
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" class="wrapper">
<div class="wrapper-sell-product-label">
<img src="icons/sell.png" style="width: 22px;height: 22px;">
<label>Sell Product</label>
</div>
<div>
<input type="text" class="wrapper-customer-name-input" name="customer_name" placeholder="Enter Customer Name">
</div>
<div>
<input type="text" pattern="[88]{2}[01]{2}[3-9]{1}[0-9]{8}" class="wrapper-contact-number-input" placeholder="Enter Customer Contact Number">
</div>
<div>
<input type="number" onkeyup="return event.charCode >= 48" min="1" name="product_id" class="wrapper-product-id-input" placeholder="Enter Product ID">
</div>
<div>
<input type="number" onkeyup="return event.charCode >= 48" min="1" name="product_quantity" class="wrapper-quantity-input" placeholder="Enter Product Quantity">
</div>
<div>
<input type="text" class="wrapper-promo-code-input" name="promo_code" placeholder="Enter Promo Code">
</div>
<div class="wrapper-bill-label">
<label>Total Bill</label>
</div>
<div>
<output type="text" class="wrapper-bill-output">
</div>
<div>
<button type="submit" class="wrapper-add-to-cart-button" required>Add to Cart</button>
</div>
<div>
<button type="submit" class="wrapper-sell-button" required>Sell Product</button>
</div>
</form>
<!-- code added here, you've to work here. -->
<div class="recent-sell">
<table class="table">
<thead>
<tr>
<th scope="col">Product ID</th>
<th scope="col">Product Name</th>
<th scope="col">Price</th>
<th scope="col">Availability</th>
</tr>
</thead>
<tbody>
<!-- Backend Code -->
</tbody>
</table>
</div>
<div class="footer">
<label>©</label>
<a href = https://github.com/rr-uchchash360>Md. Rafiur Rahman</a>
<a href = https://github.com/zubayertahmid>Zubayer Tahmid</a>
</div>
</body>
</html>
<?php
}
else{
header('location:login.php');
}
?>