<?php
require_once('includes/load.php');
page_require_level(3); // Require minimum user level 3 (admin)

if (isset($_GET['id'])) {
    $sale_id = (int)$_GET['id'];

    $sale = find_sale_by_id($sale_id);

    if (!$sale) {
        $session->msg("d", "Sale not found.");
        redirect('sales.php');
    }

    $query = "DELETE FROM sales WHERE id = {$sale_id}";
    if ($db->query($query)) {
        $session->msg("s", "Sale deleted successfully.");
    } else {
        $session->msg("d", "Failed to delete sale.");
    }
    redirect('sales.php');
} else {
    $session->msg("d", "Invalid sale ID.");
    redirect('sales.php');
}
?>
