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

function get_asset_advance_search_sql($id = null, $code = null, $description = null , $hired = null , 
                                    $group_id = null ,$subgroup_id = null,$category = null , $rate_category = null, 
                                    $make = null, $model = null, $serial_no = null, $type = null)
{
//   $filter = ""; 
   
//    if ($category_id == -1){
//        $filter .= "LEFT JOIN ".TB_PREF."stock_category ON 
//                    sc.category_id = sm.category_id  "; 
//    }
//    
//    if ($category_id != -1) 
//	{
//         $filter .=" ,".TB_PREF."stock_category sc ";
//	} 

    $sql = "SELECT a.asset_id, a.asset_code, a.description asset_description,
                    g.description asset_group_description, 
                    sg.description asset_subgroup_description, 
                    r.description asset_rate_description 
                    
            FROM ".TB_PREF."asset_master a 
                    LEFT JOIN ".TB_PREF."asset_groups g ON 
                            a.group_id = g.id    
                    LEFT JOIN ".TB_PREF."asset_subgroups sg ON 
                            a.subgroup_id = sg.id    
                    LEFT JOIN ".TB_PREF."asset_catergories c ON 
                            a.asset_category_id = c.id 
                    LEFT JOIN ".TB_PREF."tool_rates r ON 
                            a.rate_category_id = r.id 
                            
            WHERE 1  ";

    if($id != null){
            $sql .= " AND a.asset_id = ".db_escape($id);
    } 
    
    if($code != null){
                $code = "%".$code."%";
		$sql .= " AND a.asset_code LIKE ".db_escape($code);
	} 
    
    if ($description != null) {
		$description = "%".$description."%";
		$sql .= " AND a.description LIKE ".db_escape($description);
	} 
    
    if ($hired == 0 || $hired == 1) {
		$sql .= " AND a.is_hired = ".db_escape($hired);
	} 
    
    if ($group_id != null && $group_id > 0){
		$sql .= " AND a.group_id = ".db_escape($group_id);
	} 
    
    if ($subgroup_id != null && $subgroup_id > 0){
		$sql .= " AND a.subgroup_id = ".db_escape($subgroup_id);
	}
    
    if ($category != null && $category > 0){
		$sql .= " AND a.asset_category_id = ".db_escape($category);
	}
    
    if ($rate_category != null && $rate_category > 0){
		$sql .= " AND a.rate_category_id = ".db_escape($rate_category);
	}
    
    if ($make != null) {
		$make = "%".$make."%";
		$sql .= " AND a.make LIKE ".db_escape($make);
	}
    
    if ($model != null){
		$model = "%".$model."%";
		$sql .= " AND a.model LIKE ".db_escape($model);
	}
    
    if ($serial_no != null) {
		$serial_no = "%".$serial_no."%";
		$sql .= " AND a.serial_no LIKE ".db_escape($serial_no);
	}
    
    if ($type != null){
		$sql .= " AND a.type = ".db_escape($type);
	}
    

    $sql .= " ORDER BY a.asset_id";
        
   	return $sql;
}
?>