<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_APPROVE_PURCH_ORDER';
$path_to_root = "..";
include($path_to_root . "/purchasing/includes/po_class.inc");

include($path_to_root . "/includes/session.inc");
include($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");
include($path_to_root . "/purchasing/includes/db/purchase_requisitions_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/purchasing/includes/ui/approve_purchase_orders_ui.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/purchasing/includes/db/po_db.inc");

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
page(_($help_context = "Approve Purchase Order"), false, false, "", $js);


if (isset($_GET['ReleasedID'])) 
{
	$order_no = $_GET['ReleasedID'];
	$trans_type = ST_PURCHORDER;	

        if(isset($_SESSION['po_messages'])){
            $result = get_session_variable('po_messages');
            clear_session_variable('po_messages');
            
            if($result['success'] === true){
                display_notification_centered($result['message']);
            }
            else{
                display_error($result['message']);
            }
            
            br();
        }

	display_note(get_trans_view_str($trans_type, $order_no, _("&View this order")), 0, 1);

	hyperlink_params($path_to_root."/purchasing/po_entry_items.php", _("Enter &Another Purchase Order"), "NewOrder=Yes");

	hyperlink_no_params($path_to_root."/purchasing/inquiry/po_approve_search.php", _("Approve Another Purchase Order"));

	display_footer_exit();	
}


start_form();

    if(isset($_GET['trans_no']) && $_GET['trans_no'] > 0){
        $_POST['trans_no'] = $_GET['trans_no'];
        hidden('selected_trans_no', $_POST['trans_no']);
        $purchase_order = new purch_order;
        read_po($_POST['trans_no'], $purchase_order);
        
    }elseif(isset($_POST['selected_trans_no']) && $_POST['selected_trans_no'] > 0){ 
        $_GET['trans_no'] = $_POST['selected_trans_no'];
        $purchase_order = new purch_order;
        read_po($_GET['trans_no'], $purchase_order);
        
    }else{
        die ("<br>" . _("This page must be called with a purchase order number to review."));
    }
    
        $project_id = $purchase_order->project_id;
        $user_id = $_SESSION['wa_current_user']->user;
        $cap = can_access_project($project_id, $user_id);
        if($cap['success'] !== true){
            display_error($cap["message"]);
            set_focus('project_id');
            display_footer_exit();
            exit;
        }

        display_heading(_("Purchase Order") . " #" . $_GET['trans_no']);
        br();
        display_approve_purchase_order_summary($purchase_order, true);
        display_heading2(_("Line Details"));
        display_approve_purchase_order_details($purchase_order);
        
        

        if (isset($_POST['Release'])){
            release_approve_purchase_order($purchase_order, true);
        }
        if (isset($_POST['Back_To_Originator'])){
            send_approve_purchase_order_back_to_originator($purchase_order);
        }
        if (isset($_POST['Reject_PO'])){
            reject_approve_purchase_order($purchase_order);
        } 

        br();
//        $cspoi_msg = check_saved_purchase_order_integrity($purchase_order->order_no);
//        if($cspoi_msg['success'] !== true){
//                display_error($cspoi_msg['message']);
//        }
//        else{
                submit_center_first('Release', 'Release', '', 'default');
                submit('Back_To_Originator', "Back To Originator", true, '', 'default');
                submit_center_last('Reject_PO', "Reject", '', 'default');
//        }
//            }

    end_form();
    
    br();
    
end_page();

?>
