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

$page_security = 'SA_MATERIAL_LIST';
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/material_lists_ui.inc");
include_once($path_to_root . "/inventory/includes/db/material_lists_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/approval_functions.inc"); 

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

global $systypes_array;

if (isset($_GET['BackedOrgID'])) {
    page(_($help_context = $systypes_array[ST_MATERIAL_LIST]), false, false, "", $js);
    
    global $systypes_array;
    
    $trans_no = $_GET['BackedOrgID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('ml_bo_messages');
    clear_session_variable('ml_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['RejectedID'])) {
    page(_($help_context = $systypes_array[ST_MATERIAL_LIST]), false, false, "", $js);
    
    $trans_no = $_GET['RejectedID'];
    $trans_type = $_GET['type'];
    $msg = get_session_variable('ml_rj_messages');
    clear_session_variable('ml_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['ml_no']) && is_numeric($_GET['ml_no'])) || (isset($_POST['ml_no']) && is_numeric($_POST['ml_no']))) {
    $ml_no = $_POST['ml_no'] = isset($_GET['ml_no']) ? $_GET['ml_no'] : $_POST['ml_no'];
    
    $page_title = _($systypes_array[ST_MATERIAL_LIST]); 
    
} elseif (isset($_GET['ModifyML_No']) || isset($_POST['ModifyML_No'])) {
    $_POST['ModifyML_No'] = isset($_GET['ModifyML_No']) ? $_GET['ModifyML_No'] : $_POST['ModifyML_No'];
    $ml_no = $_POST['ModifyML_No'];
    
    $header = get_material_list_header_record($ml_no);
        if (is_array($header)) {
            if ($header['type'] != ST_MATERIAL_LIST) {
                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_MATERIAL_LIST]." #$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 = _($systypes_array[ST_MATERIAL_LIST]); 
    $ml_no = 0;
    
} 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($_POST['REJECT_ML'])) {
    $msg = reject_material_list($ml_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['BACK_TO_ORIGINATOR_ML'])) {
    $msg = back_to_originator_material_list($ml_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

if (isset($_POST['Save_ML_Header'])) {
    $msg = save_material_list_header_only($ml_no);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if (isset($_POST['Approve_ML'])) {
    $msg = save_material_list_header_only($ml_no, true);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if (isset($_POST['Add_ML']) || isset($_POST['Update_ML'])) {        
    $msg = save_material_list($ml_no, $selected_id);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } else {
            $ml_no = $msg['ml_no'];
            display_notification($msg['message']);
            clear_material_list_details();
            $Mode = 'RESET';
        }
}

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

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

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

global $Ajax;

start_form();

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

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

        $header = get_material_list_header($ml_no);
            if (is_array($header)) {
                if (in_array($header['approval_status'], array(AS_APPROVED, AS_COMPLETED))) {
                    view_material_list_header($header['type'], $header['trans_no']);
                } elseif (in_array($header['approval_status'], array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR))) {
                    display_material_list_header($ml_no);

                    submit_center_first('Save_ML_Header', _("Save Header"), '', 'default');    
                    
                    submit('Approve_ML', _("Release for Approval"), true, _("Release for Approval"), false, ICON_SUBMIT);
            
                    submit_center_last('Refresh', 'Refresh', 'Refresh', false, ICON_SUBMIT);                br(2);
                    
                    start_table();
                        submit_center_first('REJECT_ML', _("Reject ".$systypes_array[ST_MATERIAL_LIST]), '', true, ICON_DELETE);  
                        
                        submit('BACK_TO_ORIGINATOR_ML', 'Back To Originator', true, 'Back To Originator', false, ICON_SUBMIT);
                    end_table(2);
                }
            } else {
                display_material_list_header($ml_no);
            } 

        display_material_list_details($ml_no, $selected_id, $Mode);

        if (is_array($header) && in_array($header['approval_status'], array(AS_APPROVED))) {
            start_table();
                $params = "ml_no=$ml_no&type=".ST_JOB_MATERIAL_REQ;
                hyperlink_params_separate($path_to_root . "/inventory/material_list_jmr_dpr.php", 'Create Job Material Req.', $params);   br(2);
                $params = "ml_no=$ml_no&type=".ST_DIRECT_PURCHASE_REQ;
                hyperlink_params_separate($path_to_root . "/inventory/material_list_jmr_dpr.php", 'Create Direct Purchase Req.', $params); 
            end_table(2);
        }
        
    div_end();
    
end_form();
end_page(@$_GET['popup'], false, false);        

?>