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

$page_security = 'SA_OUTSIDE_MATERIAL_RETURN';
if (!@$_GET['popup'])
    $path_to_root = "..";
else	
    $path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/inventory/includes/ui/outside_material_returns_ui.inc");
include_once($path_to_root . "/inventory/includes/db/outside_material_receipts_db.inc");
include_once($path_to_root . "/inventory/includes/db/outside_material_requisitions_db.inc"); 
include_once($path_to_root . "/inventory/includes/db/items_db.inc"); 
include_once($path_to_root . "/inventory/includes/db/items_units_db.inc"); 
include_once($path_to_root . "/admin/db/transactions_db.inc"); 
include_once($path_to_root . "/projects/includes/db/projects_db.inc"); 
include_once($path_to_root . "/includes/project_cost_functions.inc"); 
include_once($path_to_root . "/includes/db/inventory_db.inc"); 
include_once($path_to_root . "/inventory/includes/db/lot_allocations_db.inc");  
include_once($path_to_root . "/inventory/includes/ui/lot_ui.inc");  
include_once($path_to_root . "/inventory/includes/db/lots_db.inc");   
include_once($path_to_root . "/includes/approval_functions.inc");   

$js = '';
$js .= get_js_open_window(900, 500);
$js .= get_js_date_picker();

global $systypes_array;

if (isset($_GET['ProcessedID'])) {
    page(_($help_context = $systypes_array[ST_OUTSIDE_MATERIAL_RETURN]), false, false, "", $js);
    
    global $systypes_array;
    
    $trans_no = $_GET['ProcessedID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('omrt_messages');
    clear_session_variable('omrt_messages');

    $type_name = $systypes_array[$trans_type];

    display_notification($msg);
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    display_footer_exit();
}

if (isset($_GET['RejectedID'])) {
    page(_($help_context = $systypes_array[ST_OUTSIDE_MATERIAL_RETURN]), false, false, "", $js);
    
    global $systypes_array;
    
    $trans_no = $_GET['RejectedID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('omrt_rj_messages');
    clear_session_variable('omrt_rj_messages');

    $type_name = $systypes_array[$trans_type];

    display_notification($msg);
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    display_footer_exit();
}

if (isset($_GET['BackedOrgID'])) {
    page(_($help_context = $systypes_array[ST_OUTSIDE_MATERIAL_RETURN]), false, false, "", $js);
    
    global $systypes_array;
    
    $trans_no = $_GET['BackedOrgID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('omrt_bo_messages');
    clear_session_variable('omrt_bo_messages');

    $type_name = $systypes_array[$trans_type];

    display_notification($msg);
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    display_footer_exit();
}

if ((isset($_GET['omrc_no']) && is_numeric($_GET['omrc_no'])) || (isset($_POST['omrc_no']) && is_numeric($_POST['omrc_no']))) {
    $_POST['omrc_no'] = isset($_GET['omrc_no']) ? $_GET['omrc_no'] : $_POST['omrc_no'];
    
    $header = get_outside_material_receipt_header_record($_POST['omrc_no']);
        if (is_array($header)) {
            if ($header['type'] != ST_OUTSIDE_MATERIAL_RECEIPT) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(("Invalid transaction type."));
                display_footer_exit();
            } 
        } else {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Could not find ".$systypes_array[ST_OUTSIDE_MATERIAL_RECEIPT]." record #".$_POST['omrc_no']));
            display_footer_exit();
        }
    
    $page_title = _('New '.$systypes_array[ST_OUTSIDE_MATERIAL_RETURN]);
    
} elseif ((isset($_GET['omrt_no']) && is_numeric($_GET['omrt_no'])) || (isset($_POST['omrt_no']) && is_numeric($_POST['omrt_no']))) {
    $_POST['omrt_no'] = isset($_GET['omrt_no']) ? $_GET['omrt_no'] : $_POST['omrt_no'];
    
    $header = get_outside_material_receipt_header_record($_POST['omrt_no']);
        if (is_array($header)) {
            if ($header['type'] != ST_OUTSIDE_MATERIAL_RETURN) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(("Invalid transaction type."));
                display_footer_exit();
            } 
        } else {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Could not find ".$systypes_array[ST_OUTSIDE_MATERIAL_RETURN]." record #".$_POST['omrt_no']));
            display_footer_exit();
        }
        
    $_POST['omrc_no'] = $header['omr_no'];
    $page_title = _('Modify '.$systypes_array[ST_OUTSIDE_MATERIAL_RETURN]." #".$header['trans_no'] );
    
} else {
    page(_($help_context = ''), false, false, "", $js);
    display_error(("Could not find ".$systypes_array[ST_OUTSIDE_MATERIAL_RETURN]));
    display_footer_exit();
}

page(_($help_context = $page_title), false, false, "", $js);

simple_page_mode(true);

if (isset($_POST['Process_OMRC'])) {
    $msg = save_outside_material_returns($_POST['omrc_no'], $_POST['omrt_no'], true);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['REJECT_OMRT'])) {
    $msg = reject_outside_material_return($_POST['omrc_no'], $_POST['omrt_no']);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['BACK_TO_ORIGINATOR_OMRT'])) {
    $msg = back_to_originator_outside_material_return($_POST['omrc_no'], $_POST['omrt_no']);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['Create_OMRC'])) {        
    $msg = save_outside_material_returns($_POST['omrc_no'], $_POST['omrt_no']);
        if ($msg['success'] !== true) {
            if (isset($msg["focus_to"])) {
                set_focus($msg["focus_to"]);            
            }
            $return_hd_row = get_outside_material_receipt_header_record($_POST['omrt_no']);
                if (! is_array($return_hd_row)) {
                    $omrt_no = $_POST['omrt_no'] = 0; 
                }
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_outside_material_return_details();
            $Mode = 'RESET';
        }
}

if (isset($_POST['cancel']) || isset($_POST['Refresh']) || $Mode == 'RESET') {
    clear_outside_material_return_details();
    
    $Mode = 'RESET';
    $selected_id = -1;
}

global $Ajax;

start_form();

    if (isset($_POST['omrc_no']) && $_POST['omrc_no'] > 0) {
        hidden('omrc_no', $_POST['omrc_no']);
    } else {
        $_POST['omrc_no'] = 0;
    }   
        
    if (isset($_POST['omrt_no']) && $_POST['omrt_no'] > 0) {
        hidden('omrt_no', $_POST['omrt_no']);
        $button_name = 'Save';
    } else {
        $_POST['omrt_no'] = 0;
        $button_name = 'Create';
    }

    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        $req_hd_row = display_outside_material_receipt_header_details($_POST['omrc_no'], $_POST['omrt_no']);
        
        display_outside_material_return_header($_POST['omrt_no'], $req_hd_row);

        display_outside_material_return_details($_POST['omrc_no'], $_POST['omrt_no']);

            submit_center_first('Create_OMRC', _("$button_name Outside Material Return"), '', 'default');    
            
            $header = get_outside_material_receipt_header($_POST['omrt_no']);
            if (is_array($header)) {
                submit('Process_OMRC', _("Process Outside Material Return"), true, 'Process', false, ICON_SUBMIT);
            } 
            
            submit_center_last('Refresh', 'Refresh', _("Refresh"), 'default');  
            
            br(2);
            start_table();
                if (is_array($header)) {
                    submit_center_first('REJECT_OMRT', _("Reject ".$systypes_array[ST_OUTSIDE_MATERIAL_RETURN]), '', true, ICON_DELETE);
                    
                    submit('BACK_TO_ORIGINATOR_OMRT', 'Back To Originator', true, 'Back To Originator', false, ICON_SUBMIT);
                }
            
            end_table();

    div_end();
    
end_form(2);
end_page(@$_GET['popup'], false, false);        

?>