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

$page_security = 'SA_DAILY_ITEM_ISSUE';
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 . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/manufacturing/includes/ui/job_cost_additions_ui.inc");
include_once($path_to_root . "/manufacturing/includes/db/job_cost_additions_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_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 . "/includes/db/inventory_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;


$type = ST_JOB_COST_ADDITION;

//set_job_cost_addition_loading_parameteres($type, $trans_no, $hid, $page_title);

page(_($help_context = $systypes_array[$type]), false, false, "", $js);

simple_page_mode(true);

$trans_no = -1;

if (isset($_GET['JCA_No']))
{
    $_POST['JCA_No'] = $_GET['JCA_No'];
    $trans_no = $_POST['JCA_No'];
    set_session_variable($type."_no", $trans_no);
}


if (isset($_GET['NewOrder']))
{
    $_POST['JCA_No'] = -1;
    set_session_variable($type."_no", $_POST['JCA_No']);
} 

if (isset($_POST['JCA_No']) && $_POST['JCA_No'] > 0){
    $trans_no = $_POST['JCA_No'];
    set_session_variable($type."_no", $_POST['JCA_No']);
}
else{
    $trans_no = get_session_variable($type."_no");
    $_POST['JCA_No'] = $trans_no;
    
}


if (isset($_GET['Processed_JC_No'])) {
    global $systypes_array;
    
    $trans_no = $_GET['Processed_JC_No'];
    $trans_type = $_GET['type'];

    $type_name = $systypes_array[$trans_type];

    $message = get_session_variable($type."_messages");
    clear_session_variable($type."_messages");
    display_notification_centered($message);
    
    display_note(get_trans_view_str($trans_type, $trans_no, _("&View this $type_name #$trans_no.")), 0, 1);
    hyperlink_params("$path_to_root/manufacturing/inquiry/job_cost_adjustments_inquiry.php", "Approve another $type_name", "type=$trans_type");
//    hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
    display_footer_exit();
}

if (isset($_POST['Approve_DIS'])) {
    $msg = save_job_cost_addition_header_only($dii_no, true);
        if ($msg['success'] !== true) {
            display_error($msg['message']);
        } 
}



if(isset($_POST['Update_Release_JCA'])){
    release_job_cost_addition_for_approval($type, $trans_no);
}


if (isset($_POST['Back_To_Originator_JCA'])){
    send_job_cost_addition_back_to_originator($type, $trans_no);  
} 

if (isset($_POST['Reject_JCA'])){
    reject_job_cost_addition($type, $trans_no);    
}



if (isset($_POST['Save_DIS_Header'])) {
    handle_save_job_cost_addition_header($type, $trans_no, $Mode);
}

if (isset($_POST['Add_DIS']) || isset($_POST['Update_DIS'])) {        
    handle_save_job_cost_addition($type, $trans_no, $selected_id, $Mode);
}

if ($Mode == 'Delete') {
    handle_delete_job_cost_addition_line($type, $selected_id, $Mode);
}

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

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

global $Ajax;

start_form();

    if (isset($_POST['NewIssue'])) {
        if (isset($dii_no) && $dii_no > 0) {
            hidden('NewIssue', $dii_no);
        } else {
            hidden('NewIssue', $_POST['NewIssue']);
            $dii_no = $_POST['NewIssue'];
        }
    } elseif(isset($_POST['ModifyIssue'])) {
        hidden('ModifyIssue', $_POST['ModifyIssue']);
    } 

    $Ajax->activate('nsit_header');
    $Ajax->activate('_page_body');
    $Ajax->activate('All_details');
    div_start('All_details');
    
        $pi_header = display_job_cost_addition_header($type, $trans_no);

        if ($trans_no > 0) {
            submit_center_first('Save_DIS_Header', _("Save Header"), '', 'default');    
            br(3);
            
            submit_center_first('Update_Release_JCA', _("Release for Approval"), '', 'default');    
            submit('Back_To_Originator_JCA', 'Back To Originator', true, '', 'default');
            submit_center_last('Reject_JCA', _("Reject"), _(""), 'default');   
            
            br(2);
        }

        display_job_cost_addition_details($type, $trans_no);

        $Ajax->activate("control_items");
        display_job_cost_addition_details_edit_controls($selected_id, $Mode);

        if ($selected_id > 0 && $Mode != 'Delete') {
            submit_center_first('Update_DIS', _("Update"), '', 'default');
            submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
            hidden('selected_id', $selected_id);

        } else {        
            submit_center_first('Add_DIS', _("ADD"), '', 'default');
            submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
        }
        
    div_end();
    
end_form(4);
end_page(@$_GET['popup'], false, false);        

?>