-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeactivate-cug.php
177 lines (156 loc) · 6.45 KB
/
deactivate-cug.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
<?php
include 'authenticate.php';
checkUser("dealer");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Deactivation of CUG Number</title>
<link rel="icon" type="image/webp" href="logo.webp" />
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="deactivate-cug.css">
</head>
<body>
<header>
<div class="header-top">
<a href="./">
<h1>East Coast Railway</h1>
<h1>Closed User Group</h1>
</a>
</div>
</header>
<div class="heading-container">
<button class="back-btn" onclick="window.location.href = './dealer-page.php'"><img src="icon/back-button.webp"
alt="back button"></button>
<h2 class="heading">Deactivate CUG</h2>
</div>
<main class="main-content">
<form class="cug-form" method="POST" action="">
<div class="form-group">
<label for="cugNo" class="cug-no-label">CUG NO</label>
<input type="text" id="cugNo" name="cugNo" class="cug-no-input" placeholder="Enter CUG No." required>
<button type="submit" name="search" class="submit-button">GO</button>
</div>
</form>
</main>
<?php
// Include database connection script
include 'db_connect.php';
$cug_no = null;
$message = null;
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['search'])) {
$cug_no = $_POST['cugNo'];
// Select the record to display it
$select_sql = "SELECT * FROM cugdetails WHERE cug_number = ?";
$stmt = $conn->prepare($select_sql);
$stmt->bind_param("s", $cug_no);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<form method='POST' action=''>";
echo "<input type='hidden' name='cugNo' value='$cug_no'>";
echo "<button type='submit' name='deactivate' class='deactivate-button'>Deactivate</button>";
echo "</form>";
echo "<table>
<thead>
<tr>
<th>CUG Number</th>
<th>Employee Number</th>
<th>Employee Name</th>
<th>Designation</th>
<th>Unit</th>
<th>Department</th>
<th>Bill Unit No</th>
<th>Allocation</th>
<th>Operator</th>
<th>Plan</th>
<th>Status</th>
</tr>
</thead>
<tbody>";
echo "<tr>";
echo "<td>" . $row["cug_number"] . "</td>";
echo "<td>" . $row["emp_number"] . "</td>";
echo "<td>" . $row["empname"] . "</td>";
echo "<td>" . $row["designation"] . "</td>";
echo "<td>" . $row["unit"] . "</td>";
echo "<td>" . $row["department"] . "</td>";
echo "<td>" . $row["bill_unit_no"] . "</td>";
echo "<td>" . $row["allocation"] . "</td>";
echo "<td>" . $row["operator"] . "</td>";
echo "<td>" . $row["plan"] . "</td>";
echo "<td>" . $row["status"] . "</td>";
// echo "<td>" . $row["activate_from"] . "</td>";
// echo "<td>" . $row["inactive_at"] . "</td>";
echo "</tr>
</tbody>
</table>";
} else {
$message = "No records found";
}
$stmt->close();
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['deactivate'])) {
$cug_no = $_POST['cugNo'];
// Check if the status is already 'Inactive'
$select_status_sql = "SELECT status FROM cugdetails_transaction WHERE cug_number = ?";
$stmt_status = $conn->prepare($select_status_sql);
$stmt_status->bind_param("s", $cug_no);
$stmt_status->execute();
$result_status = $stmt_status->get_result();
if ($result_status->num_rows > 0) {
$row_status = $result_status->fetch_assoc();
if ($row_status['status'] == 'Inactive') {
$message = "CUG number $cug_no is already deactivated.";
} else {
// Begin transaction
$conn->begin_transaction();
try {
// Delete record from cugdetails table
$delete_sql = "DELETE FROM cugdetails WHERE cug_number = ?";
$stmt_delete = $conn->prepare($delete_sql);
$stmt_delete->bind_param("s", $cug_no);
// Update the status in cugdetails_transaction table
$update_sql = "UPDATE cugdetails_transaction SET status = 'Inactive', inactive_at = NOW() WHERE cug_number = ?";
$stmt_update = $conn->prepare($update_sql);
$stmt_update->bind_param("s", $cug_no);
// Execute both statements and commit if both succeed
if ($stmt_update->execute() && $stmt_delete->execute()) {
// Commit transaction
$conn->commit();
$message = "CUG number $cug_no deactivated successfully.";
} else {
// Rollback transaction if either statement fails
$conn->rollback();
$message = "Error deactivating CUG number.";
}
} catch (Exception $e) {
// Rollback transaction in case of error
$conn->rollback();
$message = "Error deactivating and deleting CUG number: " . $e->getMessage();
}
}
} else {
$message = "CUG number not found.";
}
$stmt_status->close();
}
// Display the message if available
if ($message) {
echo "<p class='session-message'>" . htmlspecialchars($message) . "</p>";
}
// Close the database connection
$conn->close();
?>
<footer>
<p>© 2024 East Coast Railway. All rights reserved.</p>
<div class="footer-links">
<a href="#">Privacy Policy</a>
<a href="#">Terms of Service</a>
</div>
</footer>
</body>
</html>