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

$page_security = 'SA_JOB_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/job_material_requisition_dpr_ui.inc");
include_once($path_to_root . "/inventory/includes/ui/job_material_requisitions_ui.inc");
include_once($path_to_root . "/inventory/includes/db/job_material_requisitions_db.inc"); 
include_once($path_to_root . "/purchasing/includes/ui/direct_purchase_requisitions_ui.inc");
include_once($path_to_root . "/purchasing/includes/db/direct_purchase_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 $Ajax, $systypes_array;

if ((isset($_GET['jmr_no']) && is_numeric($_GET['jmr_no'])) || (isset($_POST['jmr_no']) && is_numeric($_POST['jmr_no']))) {
    $jmr_no = $_POST['jmr_no'] = isset($_GET['jmr_no']) ? $_GET['jmr_no'] : $_POST['jmr_no'];
    
    $header = get_job_material_requisition_header($jmr_no);
        if (! is_array($header)) {
            page(_($help_context = ''), false, false, "", $js);
            display_error(("Could not find ".$systypes_array[ST_JOB_MATERIAL_REQ]." #$jmr_no"));
            display_footer_exit();
        }
        
    $jmr_trans_no = $header['trans_no'];   
} 

if ((isset($_GET['type']) && is_numeric($_GET['type'])) || (isset($_POST['type']) && is_numeric($_POST['type']))) {
    $type = $_POST['type'] = isset($_GET['type']) ? $_GET['type'] : $_POST['type'];
    
    $page_title = _($systypes_array[ST_JOB_MATERIAL_REQ].": ".$systypes_array[$type]); 
} elseif (isset($_GET['type']) && isset($_GET['ApprovedID'])) {
    $page_title = _($systypes_array[ST_JOB_MATERIAL_REQ].": ".$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['SavedID'])) {
    $trans_no = $_GET['SavedID'];
    $trans_type = $_GET['type'];

    $type_name = $systypes_array[$trans_type];

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

if (isset($_POST['Save_JMR_DPR'])) {        
    $msg = validate_and_save_job_material_requisition_dpr($jmr_no, $type);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}

start_form();

    if (isset($_POST['jmr_no']) && $_POST['jmr_no'] > 0) {
        hidden('jmr_no', $_POST['jmr_no']);
    }    

    if (isset($_POST['type']) && $_POST['type'] > 0) {
        hidden('type', $_POST['type']);
    }    
        
    $Ajax->activate('nsit_header');
    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        display_heading($systypes_array[ST_JOB_MATERIAL_REQ]." Header");
        view_job_material_requisition_header(ST_JOB_MATERIAL_REQ, $jmr_trans_no);
    
        display_heading($systypes_array[$type]." Header");
        display_job_material_requisition_dpr_header($jmr_no, $type);

        display_heading($systypes_array[$type]." Lines");
        display_job_material_requisition_dpr_details($jmr_no);

        submit_center_first('Save_JMR_DPR', _("Save $systypes_array[$type]"), '', 'default');   
        
    div_end();
    
end_form(2);

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

?>