<?php
/**********************************************************************
***********************************************************************/

$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/site_material_returns_cart.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/inventory/includes/lots_validation.inc");
include_once($path_to_root . "/inventory/includes/serial_no_validation.inc");
include_once($path_to_root . "/inventory/includes/db/serial_no_adjustments_db.inc");
include_once($path_to_root . "/inventory/includes/db/serial_no_db.inc");
include_once($path_to_root . "/projects/includes/db/project_sites_db.inc");
include_once($path_to_root . "/inventory/includes/site_material_returns_ui.inc");
include_once($path_to_root . "/inventory/includes/db/site_material_returns_db.inc");
include_once($path_to_root . "/includes/db/comments_db.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/includes/ui/ui_view.inc");
include_once($path_to_root . "/includes/types.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/includes/ui/ui_lists.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/includes/references.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");
include_once($path_to_root . "/inventory/includes/db/lot_adjustments_db.inc");
include_once($path_to_root . "/includes/db/audit_trail_db.inc");
include_once($path_to_root . "/includes/db/project_cost_functions_db.inc");
include_once($path_to_root . "/purchasing/includes/db/pr_issues_db.inc");


global $systypes_array;
$order_name = 'site_material_returns';

$order_no = 0;

if(isset($_GET['NewOrder'])){
    unset($_SESSION[$order_name]);
    set_session_variable("ST_SITE_MATERIAL_RETURN_no", $order_no);
}

if(isset($_GET['OrderNo']) && $_GET['OrderNo'] > 0){
    unset($_SESSION[$order_name]);
    $order_no = $_GET['OrderNo'];
    set_session_variable("ST_SITE_MATERIAL_RETURN_no", $order_no);
} 

if(!($order_no > 0)){
    $order_no = get_session_variable("ST_SITE_MATERIAL_RETURN_no");
}

$js = "";
if($use_popup_windows){
    $js .= get_js_open_window(800, 500);
}

if($use_date_picker){
    $js .= get_js_date_picker();
}

    $page_security = 'SA_SITE_MATERIAL_RETURN';
    page(_($help_context = "Site Material Returns"), false, false, "", $js);

//-----------------------------------------------------------------------------------------------

if(isset($_GET['ProcessID'])){
    global $systypes_array;

    $trans_no = $_GET['ProcessID'];
    $message = get_session_variable('ST_SITE_MATERIAL_RETURN_messages');
    clear_session_variable('ST_SITE_MATERIAL_RETURN_messages');

    display_notification_centered($message);
    br(1);
//    display_note(get_trans_view_str(ST_SITE_MATERIAL_RETURN, $trans_no, _("&View this ".$systypes_array[ST_SITE_MATERIAL_RETURN])), 0, 1);
//    display_note(print_document_link($trans_no, _("&Print This $type_name"), true, $trans_type), 0, 0);
    hyperlink_no_params("$path_to_root/inventory/inquiry/site_material_returns_inquiry.php", _("Select another ".$systypes_array[ST_SITE_MATERIAL_RETURN]));
//    hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
    display_footer_exit();
}

//-----------------------------------------------------------------------------------------------

load_existing_or_new_site_material_return_order($order_no, $order_name);


if(isset($_POST['Save_Header'])){
    $order_no = handle_save_site_material_return_header($order_name);
    $_SESSION[$order_name]->trans_no = $order_no;
    $id = find_submit("Edit");
    unset($_POST["Edit$id"]);
    unset($_POST['previous_selected_id']);
}

if(isset($_POST['Save_&_Process'])){
    $msg = handle_save_and_process_site_material_return($order_name);
    if($msg["success"] !== true){
        display_error($msg["message"]);
    }else{
        unset($_SESSION[$order_name]);
        set_session_variable('ST_SITE_MATERIAL_RETURN_messages', $msg["message"]);
        meta_forward($_SERVER['PHP_SELF'], "ProcessID=$order_no");
    }
}

if(isset($_POST['AddItem'])){
    $order_no = handle_new_site_material_return_item($order_name);
}
    
if(isset($_POST['UpdateItem'])){
    $order_no = handle_update_site_material_return_item($order_name);
}

$id = find_submit('Delete');
if($id != -1){
    $msg = handle_delete_site_material_return_item($order_name, $id);
    if($msg['success'] !== true){
        display_error($msg['message']);
    }else{
        display_notification($msg['message']);
    }
}

if(isset($_POST['CancelItemChanges'])){
    $id = find_submit("Edit");
    unset($_POST["Edit$id"]);
    unset($_POST['previous_selected_id']);
    unset($_POST['from_task_id']);
    unset($_POST['to_task_id']);
    unset($_POST['from_project_site_id']);
    unset($_POST['to_project_site_id']);
}

//-----------------------------------------------------------------------------------------------

load_existing_or_new_site_material_return_order($order_no, $order_name);

//-----------------------------------------------------------------------------------------------

    start_form();
    
        $Ajax->activate("details");
        div_start("details");
            hidden("guid",substr(md5(microtime()),rand(0,26),15));
            unset($msgss);

            if($order_no > 0){
                $msgss = $systypes_array[ST_SITE_MATERIAL_RETURN] ." #".$order_no;
            }else{
                $msgss = "New ".$systypes_array[ST_SITE_MATERIAL_RETURN];
            }
            br();

            display_site_material_return_header($_SESSION[$order_name], $msgss);
            
            if($order_no > 0){
                submit_center_first('Save_Header', _("Save"), _("Save header"), '', ICON_SUBMIT);
                submit_center_last('Save_&_Process', _("Save & Process"), _("Save & Process Site Material Returns"), '', ICON_SUBMIT);                
            }
            
            br(1);

            display_site_material_return_items(_("Items"), $_SESSION[$order_name]);

            br(2);
        
        div_end();
    end_form();
end_page();

?>