<?php
// books_expense_del.php
require_once 'functions.php';
checkLogin(); // Security check
$conn = connect();

if(isset($_GET['exp_id'])){
    // Sanitize
    $exp_id = (int)$_GET['exp_id'];

    // Check permissions
    if(isset($_SESSION['log_delete']) && $_SESSION['log_delete'] == '1'){
        
        $sql = "DELETE FROM expenses WHERE exp_id = $exp_id";
        
        if(mysqli_query($conn, $sql)){
            // Success: Return to Finance page (CR tab)
            header("Location: finance.php?tab=CR&msg=deleted");
        } else {
            die("Error deleting record: " . mysqli_error($conn));
        }
    } else {
        // Permission denied
        echo "<script>alert('You do not have permission to delete records.'); window.location='finance.php?tab=CR';</script>";
    }
} else {
    header("Location: finance.php?tab=CR");
}
?>