????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................<?php
include('session.php');
include 'connection.php';

$user = 'xceljsr_db';
$password = 'xcel@123#$';
$database = 'xceljsr_db';
$servername = 'localhost';
$mysqli = new mysqli($servername, $user, $password, $database);

// Checking for connections
if ($mysqli->connect_error) {
    echo 'Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
    exit;
}

header('Content-Type: text/plain');  // Plain text for AJAX response

if (isset($_POST['id']) && is_numeric($_POST['id'])) {
    $id = intval($_POST['id']);
    $addedby = $_SESSION['uid'];  // Ensure only own records can be deleted

    // Begin transaction
    $mysqli->autocommit(FALSE);

    try {
        // Fetch the row to get admission_no and received_amount
        $fetch_sql = "SELECT admission_no, received_amount FROM extra_fee WHERE id = $id AND addedby = '$addedby'";
        $fetch_result = $mysqli->query($fetch_sql);
        
        if ($fetch_result && $fetch_result->num_rows > 0) {
            $row = $fetch_result->fetch_assoc();
            $admission_no = mysqli_real_escape_string($mysqli, $row['admission_no']);
            $received_amount = floatval($row['received_amount']);

            if ($received_amount <= 0) {
                throw new Exception('Invalid received amount.');
            }

            // Update accounts: subtract received_amount from extra_fee (ensure >=0)
            $update_sql = "UPDATE accounts SET extra_fee = GREATEST(extra_fee - $received_amount, 0) WHERE admission_no = '$admission_no'";
            if (!$mysqli->query($update_sql)) {
                throw new Exception('Error updating accounts: ' . $mysqli->error);
            }

            // Delete from extra_fee
            $delete_sql = "DELETE FROM extra_fee WHERE id = $id";
            if (!$mysqli->query($delete_sql)) {
                throw new Exception('Error deleting extra fee: ' . $mysqli->error);
            }

            $mysqli->commit();
            echo 'Extra Fee record deleted successfully! Amount subtracted from accounts.';
        } else {
            throw new Exception('Record not found or access denied.');
        }

    } catch (Exception $e) {
        $mysqli->rollback();
        echo 'Error: ' . $e->getMessage();
    }

} else {
    echo 'Error: Invalid ID provided.';
}

$mysqli->close();
exit;
?>