<?php
/**********************************************************************
***********************************************************************/
function write_approval_path_configuration($selected_id, $approval_path_id, $position_seq_no, 
                            $position_code, $position_description, $approval_group_id, 
                            $next_position_message, $next_position_email_template_id, $previous_position_email_template_id,
                            $rejection_email_template_id, $display_status)
{
    if($selected_id > 0){
		$sql = "UPDATE ".TB_PREF."approval_path_configuration SET
                        approval_path_id = ".db_escape($approval_path_id).",
                        position_seq_no = ".db_escape($position_seq_no).",
                        position_code = ".db_escape($position_code).",
                        position_description = ".db_escape($position_description).",
                        approval_group_id = ".db_escape($approval_group_id).",
                        display_status = ".db_escape($display_status).",
                        next_position_message = ".db_escape($next_position_message).",
                        next_position_email_template_id = ".db_escape($next_position_email_template_id).",
                        previous_position_email_template_id = ".db_escape($previous_position_email_template_id).",
                        rejection_email_template_id = ".db_escape($rejection_email_template_id)." 
        	WHERE id = ".db_escape($selected_id);
    }                
    else{
		$sql = "INSERT INTO ".TB_PREF."approval_path_configuration(
                        approval_path_id, position_seq_no,
                        position_code, position_description,
                        approval_group_id,  display_status, next_position_message,
                        next_position_email_template_id,previous_position_email_template_id,
                        rejection_email_template_id) 
                VALUES( ".db_escape($approval_path_id).", ".db_escape($position_seq_no).
                        ", ".db_escape($position_code).", ".db_escape($position_description).
                        " , ".db_escape($approval_group_id)." , ".db_escape($display_status)." , ".db_escape($next_position_message).
                        ", ".db_escape($next_position_email_template_id)." , ".db_escape($previous_position_email_template_id).
                        ", ".db_escape($rejection_email_template_id)."     )";
    }

	db_query($sql,"An approval path configuration could not be updated");
}
//
function delete_approval_path_configuration($id)
{
	$sql="DELETE FROM ".TB_PREF."approval_path_configuration WHERE id=".db_escape($id);

	db_query($sql,"An approval path could not be deleted");
}
//
function get_approval_configure_details($id)
{
	$sql = "SELECT ag.approval_group_description,
                        email_next.template_description next_email_template_description,
                        apc.* ,
                        email_previous.template_description previous_email_template_description,
                        email_reject.template_description rejection_email_template_description
            FROM ".TB_PREF."approval_path_configuration apc 
                    INNER JOIN ".TB_PREF."approval_group ag ON 
                            apc.approval_group_id = ag.approval_group_id 
                    LEFT JOIN ".TB_PREF."email_template email_next ON 
                            apc.next_position_email_template_id = email_next.template_id 
                    LEFT JOIN ".TB_PREF."email_template email_previous ON 
                            apc.previous_position_email_template_id = email_previous.template_id 
                    LEFT JOIN ".TB_PREF."email_template email_reject ON 
                            apc.rejection_email_template_id = email_reject.template_id 
		WHERE apc.id = $id ";
                
        $result = db_query($sql,"An approval path configuration could not be retrieved");

	return db_fetch($result);


}

function get_approval_path_configuration($approval_path_id) {
   
	$sql = "SELECT ag.approval_group_description,
                    email_next.template_description next_email_template_description,
                    apc.* ,
                    email_previous.template_description previous_email_template_description,
                    email_reject.template_description rejection_email_template_description
            FROM ".TB_PREF."approval_path_configuration apc 
                    LEFT JOIN ".TB_PREF."approval_group ag ON 
                            apc.approval_group_id = ag.approval_group_id 
                    LEFT JOIN ".TB_PREF."email_template email_next ON 
                            apc.next_position_email_template_id = email_next.template_id 
                    LEFT JOIN ".TB_PREF."email_template email_previous ON 
                            apc.previous_position_email_template_id = email_previous.template_id 
                    LEFT JOIN ".TB_PREF."email_template email_reject ON 
                            apc.rejection_email_template_id = email_reject.template_id 
		WHERE apc.approval_path_id = $approval_path_id 
                ORDER BY position_seq_no ";
        
    return  db_query($sql, "could not get approval path configurations");
    
}


function get_existing_approval_path_configuration_code($position_code, $id)
{
    $sql="SELECT position_code FROM ".TB_PREF."approval_path_configuration 
            WHERE position_code = ".db_escape($position_code)." AND 
                    id <> ".db_escape($id);

	$result = db_query($sql,"Could not get_existing_approval_path_configuration_code");

	return db_fetch($result);
}

?>