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


$page_security = 'SA_APPROVE_WORK_ORDER';
$path_to_root = "..";
include($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/manufacturing/includes/db/manufacturing_requirements_db.inc");
include_once ($path_to_root . "/trace.php");
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
	
page(_($help_context = "Approve Workorder"), @$_REQUEST['popup'], false, "", $js);

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/approval_functions.inc");

include_once($path_to_root . "/manufacturing/includes/work_order_validations.inc");
include_once($path_to_root . "/manufacturing/includes/workorder_detail_view.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/includes/project_cost_functions.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");


if (isset($_GET['trans_no']))
{
	$_POST['wo_id'] = $_GET['trans_no'];
}

function wo_has_changed($wo_id)
{
    return false;
}

function can_approve_workorder($woh)
{
    if($woh['closed'] == 1){
        display_error("This workorder has been closed and it can not be approved.");
        return false;
    }
    
    if($woh['approval_status'] == AS_APPROVED){
        display_error("This workorder has been approved already.");
        return false;
    }
    
    if($woh['approval_status'] == AS_REJECTED){
        display_error("This workorder has been rejected and it can not be approved.");
        return false;
    }
    
    if(!($woh['id'] > 0)){
        display_error("This workorder is not yet confirmed and it can not be approved.");
        return false;
    }
    
    if($woh['released'] == 1){
        display_error("This workorder has been released and it can not be approved.");
        return false;
    }
    
    return true;
    
}


function can_reject_workorder($woh)
{
    if($woh['closed'] == 1){
        display_error("This workorder has been closed and it can not be rejected.");
        return false;
    }
    
    if($woh['approval_status'] == AS_APPROVED){
        display_error("This workorder has been approved and it can not be rejected.");
        return false;
    }
    
    if($woh['approval_status'] == AS_REJECTED){
        display_error("This workorder has been rejected already.");
        return false;
    }
    
    if($woh['released'] == 1){
        display_error("This workorder has been released and it can not be rejected.");
        return false;
    }
    
    return true;
    
}


function can_reopen_workorder($woh)
{
    if($woh['closed'] == 1){
        display_error("This workorder has been closed and it can not be reopened.");
        return false;
    }

    if($woh['approval_status'] == AS_REJECTED){
        display_error("This workorder has been rejected and it can not be reopened.");
        return false;
    }
    
    if(!($woh['id'] > 0)){
        display_error("This workorder is not yet confirmed and it can not be reopened.");
        return false;
    }
    
    return true;
    
}

function can_send_back_workorder($woh)
{
    if($woh['closed'] == 1){
        display_error("This workorder has been closed and it can not be amended.");
        return false;
    }
    
    if($woh['approval_status'] == AS_APPROVED){
        display_error("This workorder has been approved and it has to be reopened to send it back to originator.");
        return false;
    }
    
    if($woh['approval_status'] == AS_REJECTED){
        display_error("This workorder has been rejected and it can not be amended.");
        return false;
    }
    
    if(!($woh['id'] > 0)){
        display_error("This workorder is not yet confirmed and it is with the originator still.");
        return false;
    }
    
    if($woh['released'] == 1){
        display_error("This workorder has been released and has to be reopened to send it back to originator.");
        return false;
    }
    
    return true;
    
}


function change_wo_approval_status($change_type, $woh)
{
    //$woh = get_work_order_header($_POST['wo_id']);
    $wo['wo_project_id'] = $woh['project_id'];
    $wo['wo_task_id'] = $woh['task_id'];
    $wo['wo_created_by'] = $woh['created_by'];
    $wo['wo_id'] = $_POST['wo_id'];
    $wo['wo_total'] = input_num('total_wo_cost');
    $wo['wo_next_approval_group_id'] = $woh['next_approval_group_id'];
    $wo['wo_released'] = $woh['released'];
    $wo['wo_row_state'] = $woh['row_state'];
    
    if(wo_has_changed($_POST['wo_id'])){
        return;
    }
    
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    switch ($change_type) {
        case "APPROVE":
            $cwoaps = approve_work_order($wo, $current_user_id);
            break;
        case "REOPEN":
            $cwoaps = reopen_work_order($wo, $current_user_id, false);
            break;
        case "BACK_TO_ORIGINATOR":
            $cwoaps = send_work_order_back_to_originator($wo, $current_user_id);
            break;
        default:
            $cwoaps = reject_work_order($wo, $current_user_id);
            break;                
    }
    //$cwoas = change_work_order_approval_status($wo, $change_type, $current_user_id);
    if($cwoaps['success'] !== true){
        display_error($cwoaps['message']);
        return false;
    }
    else{
        display_notification($cwoaps['message']);
        return true;
    }
}

if (isset($_POST['Back_To_Originator'])){

    $woh = get_work_order_header($_POST['wo_id']);
    if(can_send_back_workorder($woh)){
        change_wo_approval_status("BACK_TO_ORIGINATOR", $woh);
    }
        
} 

if(isset($_POST['ReleaseForApproval'])){
    
    $woh = get_work_order_header($_POST['wo_id']);
    if(can_approve_workorder($woh)){
        change_wo_approval_status("APPROVE", $woh);
    }
    
}

if(isset($_POST['RejectWO'])){
    
    $woh = get_work_order_header($_POST['wo_id']);
    if(can_reject_workorder($woh)){
        change_wo_approval_status("REJECT", $woh);
    }
    
}

if(isset($_POST['ReOpenWO'])){
    
    $woh = get_work_order_header($_POST['wo_id']);
    if(can_reopen_workorder($woh)){
        change_wo_approval_status("REOPEN", $woh);
    }
    
}

if(isset($_POST['CloseWO'])){
    
    $cwo_msg = close_work_order($_POST['wo_id']);
    if($cwo_msg['success'] !== true){
        display_error($cwo_msg['message']);
    }
    else{
        display_notification("Workorder ".$_POST["wo_id"]." has been closed.");
    }
    
}

//------------------------------------------------------------------------------------
if(isset($_GET['ModifyMRNumber']) && isset($_GET['ModifyMRNumber']) > 0)
{
    $id_mno=$_GET['ModifyMRNumber'];
}


//------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------- 

start_form(true);

div_start('details');

$manuf_req_id = 0;
if(isset($_POST['id']) && $_POST['id'] > 0){
    $manuf_req_id = $_POST['id'];
}
elseif(get_post('mid') > 0){
    $manuf_req_id = get_post('mid');
}

$total_wo_cost = display_costed_wo($_POST['wo_id']);

hidden('wo_id', $_POST['wo_id']);
hidden('total_wo_cost', $total_wo_cost);

div_end();

submit_center_first('ReleaseForApproval', _("Approve"), '', 'default');
br(2);
submit_center_first('Back_To_Originator', _("Back to Originator"), 'Back to Originator', '');
submit('Back_To_Originator', _("Back to Originator"), 'Back to Originator', '', true);
submit('ReOpenWO', _("Reopen"), 'Reopen', '', true);
submit('RejectWO', _("Reject"), _("Reject"), '', true);
submit_center_last('CloseWO', _("Close"), '', '');

hidden('popup', @$_REQUEST['popup']);

end_form();

//------------------------------------------------------------------------------------

end_page(@$_REQUEST['popup']);
?>
