<?php
session_start(); // <-- session start karna bahut important
include 'db.php';

// Check if invoice ID is provided
if(isset($_GET['id']) && is_numeric($_GET['id'])){
    $invoice_id = $_GET['id'];

    // Delete related invoice items
    mysqli_query($con, "DELETE FROM invoice_items WHERE invoice_id='$invoice_id'");

    // Delete related payments
    mysqli_query($con, "DELETE FROM invoice_payments WHERE invoice_id='$invoice_id'");

    // Delete other charges
    mysqli_query($con, "DELETE FROM invoice_other_charges WHERE invoice_id='$invoice_id'");

    // Delete invoice
    mysqli_query($con, "DELETE FROM invoices WHERE id='$invoice_id'");

    // Set session message
    $_SESSION['message'] = "Invoice deleted successfully";

    // Redirect back
    header("Location: invoice_data.php");
    exit;
} else {
    // Redirect back if no ID provided
    $_SESSION['message'] = "Invalid invoice ID";
    header("Location: invoice_data.php");
    exit;
}
?>
