-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecurring_monthly_expenses.php
359 lines (321 loc) · 14.5 KB
/
recurring_monthly_expenses.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
session_start();
include("dbconnect.php");
if (!isset($_SESSION["user_id"])) {
header("Location: login.php");
exit;
}
$user_id = $_SESSION["user_id"];
// Store wallet details as an associative array-----------------------------------------------------
$sql_wallets = "SELECT wallet_id, organization_name, category FROM wallets WHERE userID = ?";
if ($stmt_wallets = mysqli_prepare($conn, $sql_wallets)) {
mysqli_stmt_bind_param($stmt_wallets, "i", $user_id);
mysqli_stmt_execute($stmt_wallets);
mysqli_stmt_store_result($stmt_wallets);
$num_rows_wallets = mysqli_stmt_num_rows($stmt_wallets);
if ($num_rows_wallets > 0) {
$wallet_list = array();
mysqli_stmt_bind_result($stmt_wallets, $wallet_id, $organization_name, $category);
while (mysqli_stmt_fetch($stmt_wallets)) {
$wallet_list[] = array(
"wallet_id" => $wallet_id,
"organization_name" => $organization_name,
"category" => $category
);
}
}
mysqli_stmt_close($stmt_wallets);
} else {
echo "Error fetching wallet IDs: " . mysqli_error($conn);
}
// Check user wallet registered-----------------------------------------------------
$sql_check_wallet = "SELECT wallet_id FROM wallets WHERE userID = ?";
if ($stmt_check_wallet = mysqli_prepare($conn, $sql_check_wallet)) {
mysqli_stmt_bind_param($stmt_check_wallet, "i", $user_id);
mysqli_stmt_execute($stmt_check_wallet);
mysqli_stmt_store_result($stmt_check_wallet);
$num_rows = mysqli_stmt_num_rows($stmt_check_wallet);
if ($num_rows == 0) {
echo "No wallet added yet.";
exit();
}
mysqli_stmt_close($stmt_check_wallet);
} else {
echo "Error checking wallet: " . mysqli_error($conn);
exit();
}
// Check wallet is empty------------------------------------------------------------------
$sql_check_wallet_empty = "SELECT amount FROM wallets WHERE userID = ? LIMIT 1";
if ($stmt_check_wallet_empty = mysqli_prepare($conn, $sql_check_wallet_empty)) {
mysqli_stmt_bind_param($stmt_check_wallet_empty, "i", $user_id);
mysqli_stmt_execute($stmt_check_wallet_empty);
mysqli_stmt_store_result($stmt_check_wallet_empty);
mysqli_stmt_bind_result($stmt_check_wallet_empty, $wallet_amount);
mysqli_stmt_fetch($stmt_check_wallet_empty);
if ($wallet_amount == 0) {
echo "Wallet is empty.";
exit();
}
mysqli_stmt_close($stmt_check_wallet_empty);
} else {
echo "Error checking wallet balance: " . mysqli_error($conn);
exit();
}
$wallet_id = $payment_status = $monthly_payment = $descriptions = $shared_percentage = $media_company = $expense_type = "";
$monthly_payment_err = "";
// Excute forms--------------------------------------------------------------------------------
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input_wallet_id = trim($_POST["wallet_id"]);
if (empty($input_wallet_id)) {
$monthly_payment_err = "Please, enter wallet ID.";
} else {
$wallet_id = $input_wallet_id;
}
$payment_status = trim($_POST["payment_status"]);
$input_monthly_payment = trim($_POST["monthly_payment"]);
if (empty($input_monthly_payment)) {
$monthly_payment_err = "Please, enter the monthly payment amount.";
} else {
$monthly_payment = $input_monthly_payment;
}
$expense_type = $_POST["expense_type"];
if ($payment_status == "Paid") {
$total_payment_amount = $monthly_payment;
if ($expense_type == "1") {
if (!empty($_POST["shared_percentage"])) {
$shared_percentage = trim($_POST["shared_percentage"]);
if (!is_numeric($shared_percentage) || $shared_percentage < 0 || $shared_percentage > 100) {
echo "Shared percentage must be a valid number between 0 and 100.";
exit();
}
$total_payment_amount = ($shared_percentage / 100) * $total_payment_amount;
}
//---- wallet deduction----------------------------------------------
$sql_update_wallet = "UPDATE wallets SET amount=amount - ? WHERE wallet_id=?";
if ($stmt_update_wallet = mysqli_prepare($conn, $sql_update_wallet)) {
mysqli_stmt_bind_param($stmt_update_wallet, "di", $total_payment_amount, $wallet_id);
if (!mysqli_stmt_execute($stmt_update_wallet)) {
echo "Error updating wallet amount.";
}
mysqli_stmt_close($stmt_update_wallet);
} else {
echo "Error preparing update statement for wallet amount.";
}
} elseif ($expense_type == "0") {
$sql_update_wallet = "UPDATE wallets SET amount = amount - ? WHERE wallet_id = ?";
if ($stmt_update_wallet = mysqli_prepare($conn, $sql_update_wallet)) {
mysqli_stmt_bind_param($stmt_update_wallet, "di", $total_payment_amount, $wallet_id);
if (!mysqli_stmt_execute($stmt_update_wallet)) {
echo "Error updating wallet amount.";
}
mysqli_stmt_close($stmt_update_wallet);
} else {
echo "Error preparing update statement for wallet amount.";
}
}
}
// Assign next payment date(first entry)-----------------------------------------
$next_payment_date = date('Y-m-d', strtotime('+1 month'));
$sql = "INSERT INTO recurring_monthly_expenses (wallet_id, payment_status, monthly_payment, descriptions, shared_percentage, media_company, expense_type, next_payment_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
if ($stmt = mysqli_prepare($conn, $sql)) {
mysqli_stmt_bind_param($stmt, "isdsdsis", $param_wallet_id, $param_payment_status, $param_monthly_payment, $param_descriptions, $param_shared_percentage, $param_media_company, $param_expense_type, $next_payment_date);
$param_wallet_id = $wallet_id;
$param_payment_status = $payment_status;
$param_monthly_payment = $monthly_payment;
if ($expense_type == "1") {
$descriptions = trim($_POST["descriptions"]);
$shared_percentage = trim($_POST["shared_percentage"]);
$param_descriptions = $descriptions;
$param_shared_percentage = $shared_percentage;
$param_media_company = NULL;
} elseif ($expense_type == "0") {
$media_company = trim($_POST["media_company"]);
$param_descriptions = NULL;
$param_shared_percentage = NULL;
$param_media_company = $media_company;
}
$param_expense_type = $expense_type;
if (mysqli_stmt_execute($stmt)) {
header("location: recurring_monthly_expenses.php");
exit();
} else {
echo "Error executing main statement: " . mysqli_error($conn);
}
}
mysqli_stmt_close($stmt);
}
$sql = "SELECT CASE WHEN expense_type=1 THEN 'Utilities' ELSE 'Digital Subscription' END AS expense_type_name, 'Paid' AS payment_status, SUM(monthly_payment) AS total_spend
FROM recurring_monthly_expenses
INNER JOIN wallets ON recurring_monthly_expenses.wallet_id=wallets.wallet_id
WHERE payment_status='Paid' AND userID = ?
GROUP BY expense_type";
if ($stmt = mysqli_prepare($conn, $sql)) {
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
} else {
echo "Error in preparing SQL statement: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add New Recurring Expense</title>
<style>
.hidden {
display: none;
}
table {
width: 50%;
border-collapse: collapse;
border: 1px solid black;
}
th,
td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<section id="header">
<div class="row">
<br>
<div class="col-md-2" style="text-align: center; font-size: 30px; color:#000000;">
<a href="recurring_monthly_expenses.php" style="text-decoration: none; color: inherit;">Recurring Expenses</a>
</div>
<br><br>
<div class="col-md-10" style="text-align: right">
</br>
<a href="dashboard.php"> Dashboard </a>
<a href="show_wallet.php" style="margin-left: 20px;"> Wallets </a>
<a href="payments.php" style="margin-left: 20px;"> Payments </a>
<a href="monthly_budget_plan.php" style="margin-left: 20px;"> Monthly Budget Plan </a>
<a href="rme_expense_history.php" style="margin-left: 20px;">Recurring Expense History </a>
<a href="index.php" style="margin-left: 20px;"> Log out </a>
</div>
</div>
</section>
<br><br>
<h2>Add New Recurring Expense</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label for="wallet_id">Wallet:</label>
<select name="wallet_id" id="wallet_id" required>
<option value="">Choose Wallet</option>
<?php
if (!empty($wallet_list)) {
foreach ($wallet_list as $wallet) {
$wallet_id = $wallet['wallet_id'];
$organization_name = $wallet['organization_name'];
$category = $wallet['category'];
echo "<option value='$wallet_id'>$wallet_id - $organization_name ($category)</option>";
}
} else {
echo "<option value='' disabled>No wallets found!</option>";
}
?>
</select><br><br>
<label for="monthly_payment">Monthly Payment:</label>
<input type="number" id="monthly_payment" name="monthly_payment" required>
<span class="error"><?php echo $monthly_payment_err; ?></span><br><br>
<label for="payment_status">Payment Status:</label>
<select id="payment_status" name="payment_status" required>
<option value="">Select Status</option>
<option value="Pending">Pending</option>
<option value="Paid">Paid</option>
</select><br><br>
<label for="expense_type">Expense Type:</label>
<select id="expense_type" name="expense_type" required>
<option value="">Select Expense Type</option>
<option value="1">Utilities</option>
<option value="0">Digital_Subscriptions</option>
</select><br><br>
<div id="descriptionsDiv" class="hidden">
<label for="descriptions">Description:</label>
<input type="text" id="descriptions" name="descriptions"><br><br>
</div>
<div id="sharedPercentageDiv" class="hidden">
<label for="shared_percentage">Preferred Percentage(%):</label>
<input type="number" id="shared_percentage" name="shared_percentage"><br><br>
</div>
<div id="mediaCompanyDiv" class="hidden">
<label for="media_company_select">Media Company:</label>
<select id="media_company_select" name="media_company">
<option value="None">None</option>
<option value="Netflix">Netflix</option>
<option value="Amazon Prime Video">Amazon Prime Video</option>
<option value="Spotify">Spotify</option>
<option value="Apple TV">Apple TV</option>
<option value="Other">Other</option>
</select>
<input type="text" id="other_media_company" name="other_media_company" placeholder="Enter other media company" style="display: none;">
<br><br>
</div>
<input type="submit" name="add_expense" value="Add Expense" id="addExpenseButton">
<span id="addExpenseDone" style="display: none;">Done</span>
<span id="nextPaymentDate" style="display: none;"></span>
</form>
<br>
<br>
<h2>Total Spend of Monthly Recurring Expenses</h2>
<table>
<tr>
<th>Expense Type</th>
<th>Payment Status</th>
<th>Total Spend</th>
</tr>
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['expense_type_name'] . "</td>";
echo "<td>" . $row['payment_status'] . "</td>";
echo "<td>৳" . number_format($row['total_spend'], 2) . "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='3'>No recurring expenses available for the logged-in user.</td></tr>";
}
?>
</table>
<br>
<script>
document.getElementById('expense_type').addEventListener('change', function() {
var selectedOption = this.value;
var descriptionsDiv = document.getElementById('descriptionsDiv');
var sharedPercentageDiv = document.getElementById('sharedPercentageDiv');
var mediaCompanyDiv = document.getElementById('mediaCompanyDiv');
if (selectedOption === '1') {
descriptionsDiv.classList.remove('hidden');
mediaCompanyDiv.classList.add('hidden');
sharedPercentageDiv.classList.remove('hidden');
} else if (selectedOption === '0') {
descriptionsDiv.classList.add('hidden');
mediaCompanyDiv.classList.remove('hidden');
sharedPercentageDiv.classList.add('hidden');
} else {
descriptionsDiv.classList.add('hidden');
mediaCompanyDiv.classList.add('hidden');
sharedPercentageDiv.classList.add('hidden');
}
})
document.getElementById('media_company_select').addEventListener('change', function() {
var selectedOption = this.value;
var otherMediaCompanyInput = document.getElementById('other_media_company');
if (selectedOption === 'Other') {
otherMediaCompanyInput.style.display = 'inline';
} else {
otherMediaCompanyInput.style.display = 'none';
otherMediaCompanyInput.value = '';
}
});
document.getElementById('addExpenseButton').addEventListener('click', function() {
document.getElementById('addExpenseDone').style.display = 'inline';
});
</script>
</body>
</html>