forked from keterFree/posperity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_sale.php
181 lines (153 loc) · 7.15 KB
/
process_sale.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
<?php
include "redisconnect.php";
$me = $_COOKIE['PHPSESSID'];
$logged = $redis->hgetall("user:$me");
// Start session
session_start();
// Close Redis connection (Predis automatically handles connections, so no explicit close is needed)
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sales</title>
<link rel="stylesheet" href="home.css">
<script src="https://kit.fontawesome.com/f7e75704ad.js" crossorigin="anonymous"></script>
<script src="title.js"></script>
<title>Sales success</title>
</head>
<body>
<header>
<h1>
<?php
if (isset($logged['merchantname'])) {
echo "{$logged['merchantname']} Invoice";
}
?>
</h1>
</header>
<style>
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
color: black;
}
th {
background-color: #f2f2f2;
color: black;
}
</style>
</style>
<div id="maindiv">
<div class="actionsBar">
</div>
<div class="main-content" id="div2">
<h1>Sale Details</h1>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['sell'])) {
// Retrieve the submitted quantities, product IDs, prices, and discounts
$quantities = $_POST['quantity'];
$productIDs = $_POST['product_id'];
$prices = $_POST['prod_price']; // Prices from the form
$discounts = $_POST['discount'];
// Check if all arrays have the same length (for safety)
if (count($quantities) === count($productIDs) && count($productIDs) === count($prices) && count($prices) === count($discounts)) {
// Database connection
include 'dbconfig.php';
// Prepare and bind parameters for the update query
$updateSql = "UPDATE `product` SET `quantity` = `quantity` - ? WHERE `product_id` = ? AND `quantity` >= ?";
$updateStmt = $conn->prepare($updateSql);
$updateStmt->bind_param("iii", $quantity, $productId, $quantity);
// Prepare and bind parameters for the insert query
$insertSql = "INSERT INTO `sale` (`product_id`, `merchant_id`, `Timestamp`, `quantity`, `price`, `discount`, `selling_price`, `payment_method`, `user_id`) VALUES (?, ?, NOW(), ?, ?, ?, ?, 'money', ?)";
$insertStmt = $conn->prepare($insertSql);
$insertStmt->bind_param("iisddii", $productId, $merchantId, $quantity, $price, $discount, $sellingPrice, $userId);
// Get session variables
$userId = $logged['userid'];
$merchantId = $logged['merchantid'];
// Initialize arrays to store sale details
$productsSold = [];
$quantitiesSold = [];
$discountsApplied = [];
$totalQuantity = 0;
$totalDiscount = 0;
$totalRevenue = 0;
// Process each selected product
foreach ($quantities as $index => $quantity) {
$productId = $productIDs[$index];
$price = $prices[$index];
$discount = $discounts[$index];
// Calculate the selling price
$sellingPrice = ($price * $quantity) - $discount;
// Execute the update query
if ($updateStmt->execute()) {
// Execute the insert query for transaction details
$insertStmt->execute();
// Store sale details for display
$productsSold[] = $productId;
$quantitiesSold[] = $quantity;
$discountsApplied[] = $discount;
$prodPrice[] = $price;
$prodSellingPrice[] = $sellingPrice;
$totalQuantity += $quantity;
$totalDiscount += $discount;
$totalRevenue += $sellingPrice;
}
}
// Close prepared statements and database connection
$updateStmt->close();
$insertStmt->close();
// Display the products, quantities sold, discounts applied, and totals
echo '<h2>Confirmed</h2>';
echo '<table>';
echo '<tr><th>Product Name</th><th>Quantity Sold</th><th>Discount Applied</th></tr>';
foreach ($productsSold as $index => $productId) {
// Fetch product name from database
$productNameSql = "SELECT `name` FROM `product` WHERE `product_id` = ?";
$productNameStmt = $conn->prepare($productNameSql);
$productNameStmt->bind_param("i", $productId);
$productNameStmt->execute();
$productNameResult = $productNameStmt->get_result();
if ($productNameResult->num_rows > 0) {
$productNameRow = $productNameResult->fetch_assoc();
$productName = htmlspecialchars($productNameRow['name']);
} else {
$productName = 'Unknown Product';
}
$productNameStmt->close();
echo '<tr>';
echo '<td>' . $productName . '</td>';
echo '<td>' . $quantitiesSold[$index] . 'x' . $prodPrice[$index] . '</td>';
echo '<td>' . $discountsApplied[$index] . '(' . $prodSellingPrice[$index] . ')' . '</td>';
echo '</tr>';
}
echo '<tr><th>Total</th><th>' . $totalQuantity . '</th><th>' . $totalDiscount . '</th></tr>';
echo '</table>';
echo '<p>Total Revenue: Ksh' . $totalRevenue . '</p>';
echo '<div><button class="button" onclick="toRoot()"><i class="fa-solid fa-house-user"></i></button></div>';
$conn->close();
} else {
echo "Error: Quantity, product ID, price, and discount arrays do not match.";
}
} else {
// Redirect to the homepage or error page if the form was not submitted properly
echo '<script>window.location.href = "index.php"</script>';
exit();
}
?>
</div>
</div>
<footer>
<p style="font-size: 10px;">
© 2024 posperity,all rights reserved</p>
</footer>
</body>
</html>