<?php
/**********************************************************************
***********************************************************************/
function update_approval_path($selected, $approval_path_name, $approval_path_description)
{
        $sql = "UPDATE ".TB_PREF."approval_path SET
            approval_path_name = ".db_escape($approval_path_name).", 
            approval_path_description = ".db_escape($approval_path_description)."
            WHERE approval_path_id = ".db_escape($selected);

	db_query($sql,"An approval path could not be updated");
}
function add_approval_path($selected, $approval_path_name, $approval_path_description)
{

        $sql = "INSERT INTO ".TB_PREF."approval_path
                (approval_path_name, approval_path_description) 
                VALUES( ".db_escape($approval_path_name)." , ".db_escape($approval_path_description).")";

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

	db_query($sql,"An approval path could not be deleted");
}
//
function get_approval_details($id)
{
	$sql="SELECT * FROM ".TB_PREF."approval_path WHERE approval_path_id=".db_escape($id);

	$result = db_query($sql,"An approval path  could not be retrieved");

	return db_fetch($result);
}

function get_approval($id) {
    $sql = "SELECT * FROM ".TB_PREF."approval_path WHERE approval_path_id = $id ";
    
    $result = db_query($sql, "Could not get approval path");
    
    return db_fetch($result);
}

function delete_approval_path($id)
{
	$sql="DELETE FROM ".TB_PREF."approval_path WHERE approval_path_id=".db_escape($id);

	db_query($sql,"An approval group could not be deleted");
}




function get_approval_paths_groups_and_approver_details($approval_path_id=0, $approval_group_id=0, $user_id=0)
{
    $sql = "SELECT ap.approval_path_id, ap.approval_path_name,
            apc.position_seq_no, apc.position_code, apc.position_description, 
            ag.approval_group_id, ag.approval_group_code, ag.approval_group_description,
            apr.approval_limit, apr.over_budget_limit, apr.over_budget_percentage,
            u.user_id, u.real_name, u.inactive user_inactive  

            FROM ".TB_PREF."approval_path ap 
                JOIN ".TB_PREF."approval_path_configuration apc 
                    ON apc.approval_path_id = ap.approval_path_id 
                JOIN ".TB_PREF."approval_group ag 
                    ON ag.approval_group_id = apc.approval_group_id 
                JOIN ".TB_PREF."approvers apr 
                    ON apr.approval_group_id = ag.approval_group_id 
                JOIN ".TB_PREF."users u 
                    ON u.id = apr.user_id 
                   
            WHERE 1 = 1 ";
    
        if($approval_path_id > 0){
            $sql .= " AND ap.approval_path_id = ".db_escape($approval_path_id);
        }
        
        if($approval_group_id > 0){
            $sql .= " AND ag.approval_group_id = ".db_escape($approval_group_id);
        }
        if($user_id > 0){
            $sql .= " AND u.id = ".db_escape($user_id);
        }
        
        $sql .= " ORDER BY ap.approval_path_name, apc.position_seq_no, u.inactive, u.real_name";
        
        return db_query($sql,"get_approval_paths_groups_and_approver_details");
}