<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_JOB_CARD_ENTRY';
$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc"); 
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/services/includes/ui/job_order_entry_ui.inc"); 
include_once($path_to_root . "/services/includes/db/job_orders_db.inc"); 
include_once($path_to_root . "/admin/db/list_values_db.inc"); 
include_once($path_to_root . "/admin/db/transactions_db.inc");  
include_once($path_to_root . "/assets/includes/db/assets_db.inc");   
include_once($path_to_root . "/projects/includes/db/approval_group_db.inc");  
include_once($path_to_root . "/projects/includes/db/approvers_db.inc");  
include_once($path_to_root . "/includes/db/approval_functions_db.inc");  
include_once($path_to_root . "/assets/includes/db/assets_db.inc");   
include_once($path_to_root . "/payroll/includes/db/employees_db.inc");  
include_once($path_to_root . "/projects/includes/db/projects_db.inc");  

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

page(_($help_context = "Sub Job Card Entry"), false, false, "", $js);

simple_page_mode(true);
global $Ajax;

$state = true;

if (isset($_GET['p_jo_id'])) {
    $_POST['p_jo_id'] = $_GET['p_jo_id'];
} elseif (isset($_POST['p_jo_id'])) {
    $_POST['p_jo_id'] = $_POST['p_jo_id'];
} else {
    $_POST['p_jo_id'] = 0;
}

if ($_POST['p_jo_id'] > 0) {
    $p_job_rec = get_job_order_record($_POST['p_jo_id']);
        if (! is_array($p_job_rec)) {
            display_error(("Could not find parent job card #".$_POST['p_jo_id']));
            display_footer_exit();
        } elseif ($p_job_rec['parent_jo_id'] > 0) {
            display_error(("Job card #".$_POST['p_jo_id']." is a sub job. Sub job cannot create any sub job."));
            display_footer_exit();
        }
} 

if (isset($_GET['trans_no'])) {
    $jo_id = $_GET['trans_no'];
} elseif(isset($_POST['jo_id'])) {
    $jo_id = $_POST['jo_id'];
} else {
    $jo_id  = -1;
}

if (isset($_POST['SAVE_JO_HEADER'])) {
    $msg = handle_save_job_order_header($jo_id, $row_state, $_POST['p_jo_id']);
        if ($msg['success'] === false) {
            if (isset($msg['focus_to'])) {
                set_focus($msg['focus_to']);
            }
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            clear_job_order_inputs($selected_id);
            $Mode = 'RESET';
        }
        
}

if (isset($_POST['DELETE_HEADER'])) {
//    $msg = handle_delete_job_order_header($jo_id);
//        if ($msg['success'] === false) {
//            display_error($msg['message']);
//        } else {
//            display_notification($msg['message']);
//        }
}

if (isset($_POST['ADD_JO_REPAIR']) || isset($_POST['UPDATE_JO_REPAIR'])) {
    if (isset($_POST['ADD_JO_REPAIR'])) {
        $selected_id = -1;
    }
    
    include_once($path_to_root."/services/includes/ui/job_order_repairs_ui.inc"); 
    include_once($path_to_root."/services/includes/db/job_order_repairs_db.inc"); 
    
    $msg = handle_save_job_order_repair($jo_id, $selected_id);
        if ($msg['success'] === false) {
            if (isset($msg['focus_to'])) {
                set_focus($msg['focus_to']);
            }
            display_error($msg['message']);
            $state = false;
        } else {
            clear_job_order_inputs($selected_id);
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

if (isset($_POST['ADD_JO_LABOUR']) || isset($_POST['UPDATE_JO_LABOUR'])) {
    include_once($path_to_root."/services/includes/ui/job_order_labours_ui.inc"); 
    include_once($path_to_root."/services/includes/db/job_order_labours_db.inc"); 
    include_once($path_to_root."/payroll/includes/db/employee_rate_categories_db.inc");
    include_once($path_to_root."/payroll/includes/db/employee_rates_db.inc");
    
    $msg = handle_save_job_order_labour($jo_id, $selected_id);
        if ($msg['success'] === false) {
            if (isset($msg['focus_to'])) {
                set_focus($msg['focus_to']);
            }
            display_error($msg['message']);
            $state = false;
        } else {
            clear_job_order_inputs($selected_id);
            display_notification($msg['message']);
            $Mode = 'RESET';
        }
}

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

start_form();

    if (isset($_POST['p_jo_id'])) {
        hidden('p_jo_id', $_POST['p_jo_id']);
    }

    if ($jo_id > 0) {
        hidden('jo_id', $jo_id);
    }
    
    if ($selected_id > 0) {
        hidden('selected_id', $selected_id);
    }
    
    $Ajax->activate('sub_job_order_entry');
    div_start('sub_job_order_entry');
        display_job_order_header($jo_id, $_POST['p_jo_id']);
        display_job_order_tabs($Mode, $selected_id, $jo_id, $path_to_root, $state);
    div_end();
    
end_form(2);
end_page();

?>