<?php
/**********************************************************************
***********************************************************************/
//-------------------------------------------------------------------------------------------------------------

function get_projects_advance_search_sql($project_id, $code, $description, $department_id, 
                                    $project_manager_id, $customer_id, $status_id)
{

    $user_id = $_SESSION['wa_current_user']->user;
    
    $sql = "SELECT project_id, description, project_code, value_name, u.real_name pm_name, dm.name customer_name    
            FROM ".TB_PREF."projects_master pm 
                    INNER JOIN ".TB_PREF."user_data_access_groups udag ON
                            pm.dag_id = udag.dag_id  
                    LEFT JOIN ".TB_PREF."list_values lv ON 
                            pm.department_id = lv.id 
                    LEFT JOIN ".TB_PREF."debtors_master dm ON 
                            pm.customer_id = dm.debtor_no  
                    LEFT JOIN ".TB_PREF."users u ON 
                            pm.project_manager_id = u.id  
            WHERE pm.inactive = 0 
                    AND udag.user_id =".$user_id;

    if($project_id > 0){
        $sql .= " AND pm.project_id = ".db_escape($project_id);
    }
    if(strlen($code) > 0){
        $sql .= " AND pm.project_code LIKE '%".$code."%'";
    }
    if(strlen($description) > 0){
        $sql .= " AND pm.description LIKE '%".$description."%'";
    }
    if($department_id > 0){
        $sql .= " AND pm.department_id = ".db_escape($department_id);
    }
    if($project_manager_id > 0){
        $sql .= " AND pm.project_manager_id = ".db_escape($project_manager_id);
    }
    if($customer_id > 0){
        $sql .= " AND pm.customer_id = ".db_escape($customer_id);
    }
    if($status_id > 0){
        $sql .= " AND  pm.project_status_id = ".db_escape($status_id);
    }
	
	
        $sql .= " ORDER BY pm.description, pm.project_id";
        
   	return $sql;
}
?>