<?php
/**********************************************************************
***********************************************************************/
function write_approvers($selected, $approval_path_id, $user_id, $approval_limit, $over_budget_limit,$over_budget_percentage)
{
    if($selected!='')
		$sql = "UPDATE ".TB_PREF."approvers SET
	 	approval_group_id = ".db_escape($approval_path_id).",
                    approval_limit = ".db_escape($approval_limit).",
                        over_budget_limit = ".db_escape($over_budget_limit).",
                         over_budget_percentage=".db_escape($over_budget_percentage).",   
	 	user_id = ".db_escape($user_id)."
        	WHERE id = ".db_escape($selected);
    else
		$sql = "INSERT INTO ".TB_PREF."approvers
			(approval_group_id, user_id,approval_limit,over_budget_limit,over_budget_percentage) 
                        VALUES(".db_escape($approval_path_id).", ".db_escape($user_id).
                                    ",".db_escape($approval_limit).","
            .db_escape($over_budget_limit).","
            .db_escape($over_budget_percentage).")";
    db_query($sql,"An approver could not be updated");
}
//
function delete_approver($id)
{
	$sql="DELETE FROM ".TB_PREF."approvers WHERE id=".db_escape($id);

	db_query($sql,"An approver could not be deleted");
}
////
function get_approvers_details($id)
{
	$sql = "SELECT  ".TB_PREF."approval_group.approval_group_description, "
                .TB_PREF."approval_group.approval_group_id, "
                .TB_PREF."approvers.* 
		FROM ".TB_PREF."approvers, ".TB_PREF."approval_group
		WHERE ".TB_PREF."approvers.id = $id ";

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

	return db_fetch($result);
}

function get_all_approvers($approval_group_id) 
{
    $sql = "SELECT ag.approval_group_description,
                     u.real_name,
                        a.* 
		FROM ".TB_PREF."approval_group ag, "
                        .TB_PREF."users u,  "
                        .TB_PREF."approvers a 
		WHERE a.approval_group_id = ag.approval_group_id
                        AND a.user_id = u.id
                        AND ag.approval_group_id =".db_escape($approval_group_id);
                
    
     return  db_query($sql, "Could not get approvers");   
}

function get_all_inactive_approvers($approval_group_id) 
{
    $sql = "SELECT ag.approval_group_description,
                     u.real_name,
                        a.* 
		FROM ".TB_PREF."approval_group ag, "
                        .TB_PREF."users u,  "
                        .TB_PREF."approvers a 
		WHERE a.approval_group_id = ag.approval_group_id
                        AND a.user_id = u.id
                        AND ag.approval_group_id =".db_escape($approval_group_id)." 
                        AND u.inactive = 1 ";
                
    
     return  db_query($sql, "get_all_inactive_approvers");   
}

function get_all_active_approvers($approval_group_id) 
{
    $sql = "SELECT ag.approval_group_description,
                     u.real_name,
                        a.* 
		FROM ".TB_PREF."approval_group ag, "
                        .TB_PREF."users u,  "
                        .TB_PREF."approvers a 
		WHERE a.approval_group_id = ag.approval_group_id
                        AND a.user_id = u.id
                        AND ag.approval_group_id =".db_escape($approval_group_id)." 
                        AND u.inactive = 0 ";
                
    
     return  db_query($sql, "get_all_inactive_approvers");   
}


function view_approval_group_approvers($approval_group_id, $inactive=false)
{
    $sql = "SELECT u.real_name,
                   ap.*
            FROM ".TB_PREF."approvers ap
            LEFT JOIN ".TB_PREF."users u ON
                u.id=ap.user_id
            WHERE ap.approval_group_id =".db_escape($approval_group_id)." 
                    AND u.inactive = ".db_escape($inactive);
        
    return db_query($sql,"view_approval_group_approvers");
    
}

?>