function handle_cancel_order()
{
    global $path_to_root, $Ajax;
    if ($_SESSION['Items']->trans_type == ST_CUSTDELIVERY) {
        display_notification(_("Direct delivery entry has been cancelled as requested."), 1);
        submenu_option(_("Enter a New Sales Delivery"), "/sales/sales_order_entry.php?NewDelivery=1");
    } elseif ($_SESSION['Items']->trans_type == ST_SALESINVOICE) {
        display_notification(_("Direct invoice entry has been cancelled as requested."), 1);
        submenu_option(_("Enter a New Sales Invoice"), "/sales/sales_order_entry.php?NewInvoice=1");
    } elseif ($_SESSION['Items']->trans_type == ST_SALESQUOTE) {
        if ($_SESSION['Items']->trans_no != 0) {
            delete_sales_order(key($_SESSION['Items']->trans_no), $_SESSION['Items']->trans_type);
        }
        display_notification(_("This sales quotation has been cancelled as requested."), 1);
        submenu_option(_("Enter a New Sales Quotation"), "/sales/sales_order_entry.php?NewQuotation=Yes");
    } else {
        // sales order
        if ($_SESSION['Items']->trans_no != 0) {
            $order_no = key($_SESSION['Items']->trans_no);
            if (sales_order_has_deliveries($order_no)) {
                close_sales_order($order_no);
                display_notification(_("Undelivered part of order has been cancelled as requested."), 1);
                submenu_option(_("Select Another Sales Order for Edition"), "/sales/inquiry/sales_orders_view.php?type=" . ST_SALESORDER);
            } else {
                delete_sales_order(key($_SESSION['Items']->trans_no), $_SESSION['Items']->trans_type);
                display_notification(_("This sales order has been cancelled as requested."), 1);
                submenu_option(_("Enter a New Sales Order"), "/sales/sales_order_entry.php?NewOrder=Yes");
            }
        } else {
            processing_end();
            meta_forward($path_to_root . '/index.php', 'application=orders');
        }
    }
    processing_end();
    display_footer_exit();
}
    $dn =& $_SESSION['Items'];
    if ($_POST['bo_policy']) {
        $bo_policy = 0;
    } else {
        $bo_policy = 1;
    }
    $newdelivery = $dn->trans_no == 0;
    if ($newdelivery) {
        new_doc_date($dn->document_date);
    }
    $delivery_no = $dn->write($bo_policy);
    if ($delivery_no == -1) {
        display_error(_("The entered reference is already in use."));
        set_focus('ref');
    } else {
        processing_end();
        if ($newdelivery) {
            meta_forward($_SERVER['PHP_SELF'], "AddedID={$delivery_no}");
        } else {
            meta_forward($_SERVER['PHP_SELF'], "UpdatedID={$delivery_no}");
        }
    }
}
if (isset($_POST['Update']) || isset($_POST['_Location_update']) || isset($_POST['qty']) || isset($_POST['process_delivery'])) {
    $Ajax->activate('Items');
}
//------------------------------------------------------------------------------
start_form();
hidden('cart_id');
start_table(TABLESTYLE2, "width=90%", 5);
echo "<tr><td>";
function handle_cancel_order()
{
    global $path_to_root;
    if ($_POST['CancelOrder'] != "") {
        if ($_SESSION['Items']->trans_type == 13) {
            display_note(tr("Direct delivery entry has been cancelled as requested."), 1);
            hyperlink_params($path_to_root . "/sales/sales_order_entry.php", tr("Enter a New Sales Delivery"), SID . "&NewDelivery=0");
        } elseif ($_SESSION['Items']->trans_type == 10) {
            display_note(tr("Direct invoice entry has been cancelled as requested."), 1);
            hyperlink_params($path_to_root . "/sales/sales_order_entry.php", tr("Enter a New Sales Delivery"), SID . "&NewDelivery=0");
        } else {
            if ($_SESSION['Items']->trans_no != 0) {
                if (sales_order_has_deliveries(key($_SESSION['Items']->trans_no))) {
                    display_error(tr("This order cannot be cancelled because some of it has already been invoiced or dispatched. However, the line item quantities may be modified."));
                } else {
                    delete_sales_order(key($_SESSION['Items']->trans_no));
                }
            }
            display_note(tr("This sales order has been cancelled as requested."), 1);
            hyperlink_params($path_to_root . "/sales/sales_order_entry.php", tr("Enter a New Sales Order"), SID . "&NewOrder=Yes");
        }
        processing_end();
        br(1);
        end_page();
        exit;
    }
}