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


$page_security = 'SA_CHANGE_DOC_APP_GROUP';
$path_to_root = "../..";
include($path_to_root . "/includes/session.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 = "Change Document Approval Group"), @$_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 . "/inventory/includes/inventory_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 . "/includes/approval_functions.inc");
//include_once($path_to_root . "/admin/db/list_values_db.inc");
include_once($path_to_root . "/projects/includes/db/project_approval_path_db.inc");
include_once($path_to_root . "/projects/includes/db/change_document_approval_group_db.inc");


            

$user_comp = user_company();
$new_item = get_post('approval_group_id')== -1 || get_post('cancel'); 


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

$selected_id = get_post('approval_group_id');

if (list_updated('approval_group_id')) {
	$_POST['NewProjectID'] = $project_id = get_post('approval_group_id');
    clear_data();
	$Ajax->activate('details');
	$Ajax->activate('controls');
}
$project1 = get_post('NewProjectID');
if (get_post('cancel')) {
	$_POST['NewProjectID'] = $project_id = $_POST['approval_group_id'] = '';
    clear_data();
	set_focus('approval_group_id');
	$Ajax->activate('_page_body');
}
//if (list_updated('category_id') || list_updated('mb_flag')) {
//	$Ajax->activate('details');
//}


//check_db_has_project_tasks(_("There are no project tasks defined in the system."));

function clear_data()
{
    unset($_POST['approval_group_code']);
    unset($_POST['approval_group_description']);
}


function can_change_document_approval_group()
{
	if(!($_POST['tran_type'] > 0))
	{
		display_error(_("Please select a transaction type."));
		set_focus('tran_type');
                return false;
	}
    
        if ($_POST['document_no'] == 0)
	{
		display_error(_("The document number cannot be empty."));
		set_focus('document_no');
                return false;
	}
        
        $h_rec_msg = get_document_header_record($_POST['tran_type'], $_POST['document_no'], $h_rec);
        if($h_rec_msg["success"] !== true){      
                display_error($h_rec_msg["message"]);
                return false;
        }

        if(!(in_array($h_rec['approval_status'], array(AS_NEW, AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER, AS_BACK_TO_ORIGINATOR, AS_REOPENED)))){
                display_error("Status of this document does not allow further updating.");
                return false;
        }
        
	if (!($_POST['approval_group_id'] > 0))
	{
		display_error(_("Please select the new approval group."));
		set_focus('approval_group_id');
                return false;
	}

//        if ($info['tran_type']== -99)
//	{
//                $approval_path_type = get_listvalue_by_code("PROJ_APP_PATH_TYPES",$_POST['approval_path_type']);
//		display_error(_('Could not find table information for approval path type "'.$approval_path_type['value_name']).'"');
//		set_focus('approval_group_id');
//                return false;
//	}
    
//    $code = get_existing_approval_group_code($_POST['approval_group_code'], $selected_id);
//    if(isset($code['approval_group_code'])){
//        display_error(_("The entered approval group code already exists."));
//		set_focus('approval_group_code');
//        return false;
//    }
	return true;
    
}



function change_document_approval_group()
{   
    if(!can_change_document_approval_group()){
        return false;
    }
    
    
    $info = array();
    $tran_type = $_POST['tran_type'];
    $document_no = $_POST['document_no'];
    $next_approval_group_id = $_POST['approval_group_id'];
    
    $h_rec_msg = get_document_header_record($tran_type, $document_no, $h_rec);
    if($h_rec_msg["success"] !== true){      
        end_outer_table(1);
        display_error($h_rec_msg["message"]);
        return;
    }
        
    begin_transaction();
    
    $message = "Document next approval group changed.";
    $duds_msg = update_document_next_approval_group($tran_type, $document_no, $h_rec['approval_status'], $h_rec['row_state'], 
                                $message, $h_rec['display_status'], $h_rec['project_id'], '', $next_approval_group_id);
    if($duds_msg["success"] !== true){      
        cancel_transaction();
        display_error($h_rec_msg["message"]);
        return;
    }
        
    commit_transaction();
    display_notification(_('Document approval group has been updated'));
   
}



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


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

    change_document_approval_group();
    
    clear_data();
    $Mode = 'RESET';
    
	
}
//}
//------------------------------------------------------------------------------------

function change_approve_group_settings() 
{
	global $SysPrefs, $path_to_root, $new_item, $pic_height, $Refs, $systypes_array;;

	start_outer_table(TABLESTYLE2);

	table_section(1);

	table_section_title(_("Change Document Approval Group"));
        
        
//        value_list_code_row(_("Approval Path Type:"), 'approval_path_type','PROJ_APP_PATH_TYPES',null, "Select Approval Path Type");
        systypes_list_row("Transaction Type:", 'tran_type', null, true);
        ref_row("Document No:", 'document_no', null, null, true);
        
        $h_rec_msg = get_document_header_record($_POST['tran_type'], $_POST['document_no'], $h_rec);
        if($h_rec_msg["success"] !== true){      
            end_outer_table(1);
            if($_POST['document_no'] > 0){
                display_error($h_rec_msg["message"]);
            }
            return;
        }  
        
        $view_link = "";
        if(isset($_POST['document_no']) && $_POST['document_no'] > 0){
            $view_link = get_trans_view_str($_POST['tran_type'], $_POST['document_no'], $systypes_array[$_POST['tran_type']]." #". $_POST['document_no']);
        }
        label_row("View Document:",$view_link);
        
        $project_id = $h_rec['project_id'];
        $approval_path_type = get_approval_path_type($_POST['tran_type']);
        
        $approval_path = get_project_approval_path_record($project_id,$approval_path_type);
        
        approval_path_approval_groups_list_row(_("New Approval Group:"), 'approval_group_id', $approval_path['approval_path_id'], null,_('New Approval Group'));
	end_outer_table(1);



        submit_center('change_document_approval', _("Update"), true, '', 'default');


}

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

start_form(true);

global $Ajax;

$Ajax->activate('details');

div_start('details');

change_approve_group_settings(); 

br();
//tabbed_content_end();

div_end();


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

end_form();

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

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