-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
184 lines (166 loc) · 7.57 KB
/
index.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
<?php
$title = "Calculate Balances";
include('header.php');
// Establish database connection
$db_server = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "balancecalc";
$conn = "";
try {
$conn = mysqli_connect($db_server, $db_user, $db_pass, $db_name);
} catch (mysqli_sql_exception) {
echo "Could not Connect to the database";
}
?>
<div class="container">
<div class="sub-container">
<h1><?php echo $title; ?></h1>
<?php include('nav.php'); ?>
</div>
<form action="index.php" method="post">
<div class='grid2'>
<div class='grid2'>
<?php
$sql = "SELECT * FROM balances";
$result = mysqli_query($conn, $sql);
$counter = 0;
if (mysqli_num_rows($result) > 0) {
?>
<table class="client-table">
<thead>
<tr>
<th>#</th>
<th>Type</th>
<th>Category</th>
<th>Price</th>
<th>Client Quantity</th>
<th>Edit Price</th> <!-- New column for Edit Price -->
<th>Delete Item</th> <!-- New column for Delete Item -->
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
$counter++;
?>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row['type']; ?></td>
<td><?php echo $row['category']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><input type="number" name="clientQuantity[<?php echo $row['id']; ?>]" placeholder="1, 3, 7, 25, 50..." value=""></td>
<td><a href="edit_price.php?id=<?php echo $row['id']; ?>">Edit</a></td> <!-- Link to Edit Price page -->
<!-- Delete button -->
<td>
<form action="delete_entry.php" method="post">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<a href="">
<button type="submit" id="deleteBtn" name="delete">Delete</button>
</a>
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<input type="reset" value="Reset Quantities" class="dark-lightred">
</div>
<!-- <input type="submit" name="calculatePrices" value="Calculate Prices" class="dark-lightgreen"> -->
<input type="submit" name="calculatePrices" value="Calculate Prices" class="dark-lightgreen">
<?php
} else {
echo "<div class='sub-container fail'>No cards recorded yet.</div>";
}
?>
</div>
</form>
<!-- Demo -->
<!-- Demo -->
<!-- Demo -->
<?php
if (isset($_POST['calculatePrices'])) {
if (isset($_POST['clientQuantity'])) {
// Your existing code to display entered client quantities
?>
<div class="result-table">
<h2>Entered Client Quantities</h2>
<table>
<thead>
<tr>
<th>#</th>
<th>Type</th>
<th>Category</th>
<th>Client Quantity</th>
<th>Card Total Price</th>
</tr>
</thead>
<tbody>
<?php
$summary = 0;
foreach ($_POST['clientQuantity'] as $id => $quantity) {
// Fetch the corresponding type and category using $id from the POST data
$sql = "SELECT type, category, price FROM balances WHERE id = $id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
// $summary += $quantity * $row['price'];
// Convert $quantity and $row['price'] to numeric values
$quantity = (float)$quantity;
$price = (float)$row['price'];
$totalPrice = $quantity * $price;
$summary += $totalPrice;
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['type']; ?></td>
<td><?php echo $row['category']; ?></td>
<td><?php echo $quantity; ?></td>
<td><?php echo $quantity * $row['price']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<!-- Summary Table -->
<table>
<thead>
<tr>
<!-- <th>Summary</th> -->
</tr>
</thead>
<tbody>
<tr>
<td><?php echo "<h3>Summary</h3>"; ?></td>
<td><?php /*echo "<h3>" . number_format($summary, 2) . " IQD</h3>";*/ ?></td>
<td><?php echo "<h3>" . number_format($summary, 3) . " IQD</h3>";
?></td>
<td colspan='5'><?php #echo "<h3>" . $summary . " IQD</h3>";
?></td>
</tr>
<tbody>
</table>
</div>
<?php
// Inserting transaction details into a new table
foreach ($_POST['clientQuantity'] as $id => $quantity) {
$sql = "SELECT type, category, price FROM balances WHERE id = $id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
// Calculate total price for each item
$quantity = (float)$quantity;
$price = (float)$row['price'];
$totalPrice = $quantity * $price;
// Insert into a new transactions table
$insertSql = "INSERT INTO transactions (type, category, quantity, total_price, transaction_date, transaction_datetime)
VALUES ('{$row['type']}', '{$row['category']}', $quantity, $totalPrice, NOW(), NOW())";
mysqli_query($conn, $insertSql);
}
}
}
mysqli_close($conn);
?>
</div>
<?php include('footer.php'); ?>