home/abhiramc/public_html/acts.service/actions/delete_employee.php 0000644 00000010206 15021217742 0021447 0 ustar 00 false, 'message' => ''];
try {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the raw POST data
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['empid'])) {
$empId = $data['empid'];
// Include the Database class
include('../config/database.php');
// Create a new instance of the Database class and get the connection
$database = new Database();
$db = $database->getConnection();
// Start a transaction
$db->beginTransaction();
// Get the file details from the database first
$fileQuery = "SELECT doc_adhar, doc_polveri, doc_other, emppic FROM acts_emp WHERE empid = :empid";
$fileStmt = $db->prepare($fileQuery);
$fileStmt->bindParam(':empid', $empId);
$fileStmt->execute();
$fileResult = $fileStmt->fetch(PDO::FETCH_ASSOC);
// Delete doc_adhar file if it exists
if ($fileResult && isset($fileResult['doc_adhar']) && $fileResult['doc_adhar'] !== '') {
$filePath = '../uploads/adhar/' . $fileResult['doc_adhar'];
if (file_exists($filePath)) {
unlink($filePath); // Delete the file
}
}
// Delete doc_polveri file if it exists
if (isset($fileResult['doc_polveri']) && $fileResult['doc_polveri'] !== '') {
$filePath = '../uploads/police_verification/' . $fileResult['doc_polveri'];
if (file_exists($filePath)) {
unlink($filePath); // Delete the file
}
}
// Delete doc_other file if it exists
if (isset($fileResult['doc_other']) && $fileResult['doc_other'] !== '') {
$filePath = '../uploads/other_documents/' . $fileResult['doc_other'];
if (file_exists($filePath)) {
unlink($filePath); // Delete the file
}
}
// Delete emppic (employee image) file if it exists
if (isset($fileResult['emppic']) && $fileResult['emppic'] !== '') {
$filePath = '../uploads/images/' . $fileResult['emppic'];
if (file_exists($filePath)) {
unlink($filePath); // Delete the file
}
}
// After deleting the files, delete the bank details
$bankQuery = "DELETE FROM acts_emp_bank WHERE empid = :empid";
$bankStmt = $db->prepare($bankQuery);
$bankStmt->bindParam(':empid', $empId);
if ($bankStmt->execute()) {
// Now, delete the employee record
$query = "DELETE FROM acts_emp WHERE empid = :empid";
$stmt = $db->prepare($query);
$stmt->bindParam(':empid', $empId);
if ($stmt->execute()) {
// Commit the transaction if everything is successful
$db->commit();
$response['success'] = true;
$response['message'] = 'Record and associated data successfully deleted.';
} else {
// If failed to delete the employee record, rollback
$db->rollBack();
$response['message'] = 'Failed to delete the employee record.';
}
} else {
// If failed to delete bank details, rollback
$db->rollBack();
$response['message'] = 'Failed to delete the bank details.';
}
} else {
$response['message'] = 'Invalid request.';
}
} else {
$response['message'] = 'Invalid request method.';
}
} catch (Exception $e) {
// In case of any error, rollback transaction and catch the exception
$db->rollBack();
$response['message'] = 'Error: ' . $e->getMessage();
}
// Return JSON response
echo json_encode($response);
?>