<?php //
/**********************************************************************
***********************************************************************/
function add_project_approval_path($project_id, $approval_path_type, $approval_path) {   
    $created_by = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
                
        $sql = "INSERT INTO ".TB_PREF."project_approval_paths (project_id, approval_path_type, approval_path_id, created_by, created_on) 
                    VALUES (".db_escape($project_id).", "
                    .db_escape($approval_path_type).","
                    .db_escape($approval_path).","
                    .db_escape($created_by).","
                    .db_escape($datetime).")";

            db_query($sql, "add_project_approval_path");	
            return db_insert_id();
}

function update_project_approval_path($selected_id, $approval_path) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."project_approval_paths 
                    SET approval_path_id = ".db_escape($approval_path).",                                                                                    
                        updated_by = ".db_escape($updated_by).", 
                        updated_on = ".db_escape($datetime)."
                WHERE id = ".db_escape($selected_id);

            db_query($sql, "update_project_approval_path");	
            return db_num_affected_rows();
}
//
//function delete_project_site($project_id, $selected_id)
//{
//	$sql="DELETE FROM ".TB_PREF."project_sites WHERE project_id= ".db_escape($project_id).
//                "AND id = ".db_escape($selected_id);
//	db_query($sql,"task could not be deleted");			
//}
//
//
function get_project_approval_path_details($project_id)
{
     
        $sql = "SELECT pu.id,
                        pu.approval_path_type,
                        pu.approval_path_id,
                        lv.value_name,
                        ap.approval_path_name
                FROM ".TB_PREF."project_approval_paths pu,                        
                        ".TB_PREF."projects_master pm,
                        ".TB_PREF."list_values as lv,
                        ".TB_PREF."approval_path ap    
                WHERE pu.project_id = ".db_escape($project_id). "
                        AND pu.project_id = pm.project_id 
                        AND lv.value_code=pu.approval_path_type 
                        AND ap.approval_path_id=pu.approval_path_id 
                        AND lv.list_code='PROJ_APP_PATH_TYPES' 
                ORDER BY lv.value_name";                     

                    
               
	return db_query($sql,"Approval path details could not be retreived");
    
}

function get_approval_paths($site_id)
{
	$sql = "SELECT ap.*, aptn.value_name approval_path_type_name 
            FROM ".TB_PREF."project_approval_paths ap, 
                    ".TB_PREF."list_values aptn  
            WHERE ap.approval_path_type = aptn.value_code AND aptn.list_code = 'PROJ_APP_PATH_TYPES' AND 
                    ap.id=".db_escape($site_id);
	
	$result = db_query($sql,"Approval path details could not be retreived");
	
	return db_fetch($result);
}
//function get_task_approval_path($task_id)
//{
//    $sql = "SELECT pt.approval_path_id, ap.approval_path_name 
//                FROM ".TB_PREF."project_tasks pt 
//                    LEFT JOIN ".TB_PREF."approval_path ap ON pt.approval_path_id = ap.approval_path_id 
//                        WHERE pt.task_id = ".  db_escape($task_id);
//    
//    $result = db_query($sql,"approval path could not be retreived");
//    return db_fetch($result);
//}

function get_project_approval_path_record($project_id,$approval_path_type)
{
     
     $sql = "SELECT *                   
            FROM ".TB_PREF."project_approval_paths  
            WHERE project_id = ".db_escape($project_id)." 
                AND approval_path_type=".db_escape($approval_path_type);
     
    $result = db_query($sql,"get_project_approval_path_record");
    
    return db_fetch($result);
}

function get_project_approval_path_types_by_project_ids($project_ids_array=array()) {
    $sql = "SELECT DISTINCT apt.id ap_tp_id, apt.value_name path_tp_name  
            FROM ".TB_PREF."project_approval_paths pap                  
                JOIN ".TB_PREF."list_values apt 
                    ON apt.value_code = pap.approval_path_type 
                        AND apt.list_code = 'PROJ_APP_PATH_TYPES' 
            WHERE pap.project_id IN (".implode(',', $project_ids_array).")";                     
    
	return db_query($sql, "get_project_approval_path_types_by_project_ids");
}

function create_project_approval_path_types_array($to_project_id, $from_project_id) {
    $project_ids_array = array($to_project_id, $from_project_id);
    
    $ap_path_types_array = array();
    
        $result = get_project_approval_path_types_by_project_ids($project_ids_array);
            while ($row = db_fetch($result)) {
                $ap_path_types_array[$row['ap_tp_id']] = $row['path_tp_name'];
            }
            
            return $ap_path_types_array;
}

function get_copy_project_approval_path_details($path_type_id, $project_id) {
    $sql = "SELECT pap.id,
                pap.approval_path_type,
                pap.approval_path_id,
                apt.value_name ap_path_type_name,
                ap.approval_path_name
            FROM ".TB_PREF."list_values apt
                JOIN ".TB_PREF."project_approval_paths pap                   
                    ON apt.value_code = pap.approval_path_type 
                        AND apt.list_code = 'PROJ_APP_PATH_TYPES' 
                JOIN ".TB_PREF."approval_path ap    
                    ON pap.approval_path_id = ap.approval_path_id
            WHERE apt.id = ".db_escape($path_type_id)." 
                AND pap.project_id = ".db_escape($project_id); 

        $result = db_query($sql, "get_copy_project_approval_path_details");
        return db_fetch($result);
}

?>