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

$page_security = 'SA_OUTSIDE_MATERIAL_REQ';
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_requisitions_ui.inc");
include_once($path_to_root . "/inventory/includes/db/outside_material_requisitions_db.inc");
include_once($path_to_root . "/services/includes/db/job_orders_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/approval_functions.inc"); 

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

global $systypes_array;

if ((isset($_GET['omr_no']) && is_numeric($_GET['omr_no'])) || (isset($_POST['omr_no']) && is_numeric($_POST['omr_no']))) {
    $omr_no = $_POST['omr_no'] = isset($_GET['omr_no']) ? $_GET['omr_no'] : $_POST['omr_no'];
    
    $page_title = _($systypes_array[ST_OUTSIDE_MATERIAL_REQ]); 
    
} elseif (isset($_GET['ModifyOMR_No']) || isset($_POST['ModifyOMR_No'])) {
    $_POST['ModifyOMR_No'] = isset($_GET['ModifyOMR_No']) ? $_GET['ModifyOMR_No'] : $_POST['ModifyOMR_No'];
    $omr_no = $_POST['ModifyOMR_No'];
    
    $header = get_outside_material_requisition_header_record($omr_no);
        if (is_array($header)) {
            if ($header['type'] != ST_OUTSIDE_MATERIAL_REQ) {
                page(_($help_context = ''), false, false, "", $js);
                display_error(("Invalid transaction type."));
                display_footer_exit();
            } 
            
            $_POST['jo_id'] = $header['jo_id'];
            $trans_no = $header['trans_no'];
        } else {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Invalid transaction ID."));
            display_footer_exit();
        }
    
    $page_title = _('Modify '.$systypes_array[ST_OUTSIDE_MATERIAL_REQ]." #$trans_no" );
    
} elseif ((isset($_GET['jo_id']) && is_numeric($_GET['jo_id'])) || (isset($_POST['jo_id']) && is_numeric($_POST['jo_id']))) {
    $_POST['jo_id'] = isset($_GET['jo_id']) ? $_GET['jo_id'] : $_POST['jo_id'];
    
    $page_title = _('New '.$systypes_array[ST_OUTSIDE_MATERIAL_REQ]); 
    $omr_no = 0;
    
} elseif (isset($_GET['type']) && (isset($_GET['ApprovedID']) || isset($_GET['RejectedID']) || isset($_GET['BackedOrgID']))) { 
    $page_title = _($systypes_array[$_GET['type']]);
} else {
    page(_($help_context = ''), false, false, "", $js);
    display_error(("Invalid transaction type."));
    display_footer_exit();
}

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

simple_page_mode(true);

if (isset($_GET['ApprovedID'])) {
    global $systypes_array;
    
    $trans_no = $_GET['ApprovedID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('jmr_messages');
    clear_session_variable('jmr_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'])) {
    global $systypes_array;
    
    $trans_no = $_GET['RejectedID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('omr_rj_messages');
    clear_session_variable('omr_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'])) {
    global $systypes_array;
    
    $trans_no = $_GET['BackedOrgID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('omr_bo_messages');
    clear_session_variable('omr_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($_POST['Approve_JMR'])) {
    $msg = save_outside_material_requisition_header_only($omr_no, true);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['REJECT_OMR'])) {
    $msg = reject_outside_material_requisition($omr_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['BACK_TO_ORIGINATOR_OMR'])) {
    $msg = back_to_originator_outside_material_requisition($omr_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['Save_JMR_Header'])) {
    $msg = save_outside_material_requisition_header_only($omr_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if (isset($_POST['Add_JMR']) || isset($_POST['Update_JMR'])) {        
    $msg = save_outside_material_requisition($omr_no, $selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_outside_material_requisition_details();
            $Mode = 'RESET';
        }
}

if ($Mode == 'Delete') {
    $msg = delete_outside_material_requisition_line($omr_no, $selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_outside_material_requisition_details();
            $Mode = 'RESET';
        }
}

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

if ($Mode == 'RESET') {
    $selected_id = -1;
}

global $Ajax;

start_form();

    if (isset($_POST['jo_id'])) {
        hidden('jo_id', $_POST['jo_id']);
    }    
        
    if (isset($omr_no) && $omr_no > 0) {
        hidden('omr_no', $omr_no);
    } elseif (isset($_POST['omr_no']) && $_POST['omr_no'] > 0) {
        hidden('omr_no', $_POST['omr_no']);
        $omr_no = $_POST['omr_no'];
    } elseif(isset($_POST['ModifyOMR_No'])) {
        hidden('ModifyOMR_No', $_POST['ModifyOMR_No']);
        $omr_no = $_POST['ModifyOMR_No'];
    } 

    $Ajax->activate('nsit_header');
    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        display_outside_material_requisition_header($omr_no);

        $header = get_outside_material_requisition_header($omr_no);
            if (is_array($header)) {
                submit_center_first('Save_JMR_Header', _("Save Header"), '', 'default');    

                submit('Approve_JMR', _("Release for Approval"), true, _("Release for Approval"), false, ICON_SUBMIT);  
                
                submit_center_last('Refresh', _("Refresh"), _("Release for Approval"), 'default');   br();
                
                start_table();
                    submit_center_first('REJECT_OMR', _("Reject ".$systypes_array[ST_OUTSIDE_MATERIAL_REQ]), '', true, ICON_DELETE);    

                    submit('BACK_TO_ORIGINATOR_OMR', 'Back To Originator', true, 'Back To Originator', false, ICON_SUBMIT); 
                end_table(2);
            }

        display_outside_material_requisition_details($omr_no, $selected_id, $Mode);

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

?>