<?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 = "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 . "/admin/db/list_values_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']);
}
//$info['tran_type']
function get_table_info_type($type,&$info)
{
//    get_table_info($info, $table, $num_field, $type_field = null);
    
        switch ($type) {
            case 'MR':
                $info['tran_type'] = ST_MATERIAL_REQ;
                break;
            case 'TR':
                $info['tran_type'] = ST_TRANSFERREQ;
                break;
            case 'PR':
                $info['tran_type'] = ST_PURCH_REQ;
                break;
            case 'PO':
                $info['tran_type'] = ST_PURCHORDER;
                break;
            case 'WO':
                $info['tran_type'] = ST_WORKORDER;
                break;
            case 'OU':
                $info['tran_type'] = ST_I_OWE_YOU;
                break;
            case 'EX':
                $info['tran_type'] = ST_I_OWE_YOU_BATCH;
                break;
            case 'VR':
                $info['tran_type'] = ST_VEHICLE_REQUEST;
                break;
            case 'PI':
                $info['tran_type'] = ST_PROJECT_INVOICE;
                break;
            case 'PC':
                $info['tran_type'] = ST_PROJECT_CREDIT_NOTE;
                break;
            case "STI":
                $info['tran_type'] = ST_SUPPLIER_TRANSPORT_INVOICES;
                break;
            case "SCI":
                $info['tran_type'] = ST_SUBCONTRACTOR_INVOICE;
                break;
            case "UB":
                $info['tran_type'] = ST_UTILITY_BILLS;
                break;
            case "SCPO":
                $info['tran_type'] = ST_SUBCONTRACTOR_PURCH_ORDER;
                break;
            case "AP":
                $info['tran_type'] = ST_APPROVE_PROJECT;
                break;
            default:
                $info['tran_type'] = -99;
                break;

        }
}

function can_commit($info)
{
    global $systypes_array;
    
	if ($_POST['approval_path_type'] == '-1')
	{
		display_error(_("Please select an approval path type."));
		set_focus('approval_path_type');
                return false;
	}
    
        if ($_POST['document_no'] == 0)
	{
		display_error(_("The document number cannot be empty."));
		set_focus('document_no');
                return false;
	}

	if ($_POST['approval_group_id']== '-1')
	{
		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()
{   
    $info = array();
    $type = get_post('approval_path_type');
    $document_no = get_post('document_no');
    $approval_group_id = get_post('approval_group_id');
    get_table_info_type($type,$info);

    if(!can_commit($info)){
        return false;
    }

    get_table_info($info, $table, $num_field, $type_field);
    
    
    //$selected_id = change_document_approval_group($table, $num_field,$_POST['document_no'] , $_POST['approval_group_id'] );

//    $Ajax->activate('_page_body');

    $sql = "UPDATE ".TB_PREF.$table."
            SET next_approval_group_id = ".db_escape($approval_group_id)." 
            WHERE $num_field = ".db_escape($document_no);

    db_query($sql, "could not update document approval group");
        
    $num_recs = db_num_affected_rows();
    if($num_recs == 1){
        return true;
    }
    else{
        return false;
    }
   
}
//------------------------------------------------------------------------------------


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

    if(change_document_approval_group()){
        display_notification(_('Document approval group has been updated'));
    }
        
    clear_data();
    $Mode = 'RESET';
    
	
}
//}
//------------------------------------------------------------------------------------

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

	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");
        text_row(_('Document No:'), 'document_no', null, 15, 15);
//        amount_row("Document No:", 'document_no', null, null, null, 0);
        approval_group_list_row(_("New Approval Group:"), 'approval_group_id', null,_('New Approval Group'));
	end_outer_table(1);

	div_start('controls');

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

	div_end();
}

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

start_form(true);


div_start('details');

change_approve_group_settings(); 

br();
//tabbed_content_end();

div_end();


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

end_form();

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

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