-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_contribution.php
More file actions
47 lines (41 loc) · 1.71 KB
/
delete_contribution.php
File metadata and controls
47 lines (41 loc) · 1.71 KB
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
<?php
include 'db_connect.php';
if (isset($_POST['id'], $_POST['amount'], $_POST['salary_account_number'])) {
$contribution_id = $_POST['id'];
$amount = $_POST['amount'];
$salary_account_number = $_POST['salary_account_number'];
// Start transaction
$conn->begin_transaction();
try {
// Delete the contribution
$delete_sql = "DELETE FROM contributions WHERE id = ?";
$stmt = $conn->prepare($delete_sql);
$stmt->bind_param("i", $contribution_id);
$stmt->execute();
if ($stmt->affected_rows == 0) {
throw new Exception("Failed to delete contribution.");
}
// Check if the balance exists in member_balances
$balance_check_sql = "SELECT balance FROM member_balances WHERE salary_account_number = ?";
$stmt = $conn->prepare($balance_check_sql);
$stmt->bind_param("s", $salary_account_number);
$stmt->execute();
$balance_result = $stmt->get_result();
if ($balance_result->num_rows > 0) {
// Deduct the contribution amount from balance
$row = $balance_result->fetch_assoc();
$new_balance = max(0, $row['balance'] - $amount); // Ensure balance doesn't go negative
$update_balance_sql = "UPDATE member_balances SET balance = ?, date_updated = NOW() WHERE salary_account_number = ?";
$stmt = $conn->prepare($update_balance_sql);
$stmt->bind_param("ds", $new_balance, $salary_account_number);
$stmt->execute();
}
// Commit transaction
$conn->commit();
echo 1;
} catch (Exception $e) {
$conn->rollback();
echo 0;
}
}
?>