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


function get_sql_for_asset_return_inquiry($asset_return_id , $from_date, $to_date, $location = null,
                                        $project_id = 0, $company_code = null, $employee_code = null)
{
    $sql = "SELECT ar.id,
                    ar.reference,
                    ar.tran_date,
                    ar.comment,
                    pm.description project_description,
                    lo.location_name,
                    ar.company_code,
                    ar.employee_code 

            FROM ".TB_PREF."tool_returns ar 
                    LEFT JOIN ".TB_PREF."projects_master pm ON
                        ar.project_id = pm.project_id 

                    LEFT JOIN ".TB_PREF."asset_locations lo ON
                        ar.location = lo.loc_code 
                 
            WHERE 1=1 ";
   
    if($asset_return_id > 0){
        $sql .= " AND ar.id =".db_escape($asset_return_id);
    }
    else{
        $sql .= " AND ar.tran_date >=".db_escape(date2sql($from_date))." AND  ar.tran_date <=".db_escape(date2sql($to_date));
      
        if($location != null){
            $sql .= " AND ar.location=".db_escape($location);
        } 

        if($project_id > 0){
            $sql .= " AND ar.project_id=".db_escape($project_id);
        }

        if($company_code != ''){
            $sql .= " AND ar.company_code=".db_escape($company_code);
        }

        if(strlen($employee_code) > 2){
            $sql .= " AND ar.employee_code=".db_escape($employee_code);
        }

        if($location != ''){
            $sql .= " AND ar.location=".db_escape($location);
        }   
    }
   
    return $sql;
    
}

        
//function view_tool_request($tool_request_id)
//{
//    $sql = "SELECT ar.*,
//                    pm.project_name,
//                    em.last_name employee_last_name,
//                    lo.location_name
//                            
//            FROM ".TB_PREF."tool_requests ar
//                    LEFT JOIN ".TB_PREF."projects_master pm ON
//                        ar.project_id = pm.project_id
//                        
//                    LEFT JOIN ".TB_PREF."employee_master em ON
//                        ar.employee_code = em.employee_code
//                        
//                    LEFT JOIN ".TB_PREF."asset_locations lo ON
//                        ar.location = lo.loc_code
//                
//            WHERE ar.id =".db_escape($tool_request_id);
//    
//    $result = db_query($sql);
//    return db_fetch($result);
//    
//}
//
//
//function view_tool_request_details($tool_request_id)
//{
//      $sql = "SELECT ard.*,
//                    am.asset_code,
//                    am.description asset_description,
//                    am.asset_id,
//                    ar.description asset_rate_description,
//                    pt.task_code,
//                    pt.description task_description,
//                    ps.description site_description
//        
//
//            FROM ".TB_PREF."tool_request_details ard 
//                LEFT JOIN ".TB_PREF."asset_master am ON 
//                    ard.asset_id = am.asset_id
//                 
//                LEFT JOIN ".TB_PREF."tool_rates ar ON
//                    ard.rate_category_id = ar.id
//                    
//                LEFT JOIN ".TB_PREF."project_tasks pt ON
//                    pt.task_id = ard.task_id
//                    
//                LEFT JOIN ".TB_PREF."project_sites ps ON
//                    ard.site_id = ps.id
//                    
//                
//            WHERE ard.ar_id=".db_escape($tool_request_id);
//    
//    return db_query($sql, "Could not get_tool_request_details");
//  
//    
//}

        
function view_tool_return($selected_id)
{
    $sql = "SELECT ar.*,
                    pm.project_name,
                    em.last_name employee_last_name,                   
                    em.initials,                   
                    lo.location_name
            
            FROM ".TB_PREF."tool_returns ar
                LEFT JOIN ".TB_PREF."projects_master pm ON
                    ar.project_id = pm.project_id
                        
                LEFT JOIN ".TB_PREF."employee_master em ON
                    ar.employee_code = em.employee_code
                        
                LEFT JOIN ".TB_PREF."asset_locations lo ON
                    ar.location = lo.loc_code

            WHERE ar.id=".db_escape($selected_id);
    
    $result = db_query($sql);
    return db_fetch($result);
}


function view_tool_return_details($asset_return_id)
{
    $sql = "SELECT ard.*,
                    am.asset_code,
                    am.asset_id,
                    am.description asset_description,                                    
                    ps.description site_name
                    
            FROM ".TB_PREF."tool_return_details ard
                LEFT JOIN ".TB_PREF."asset_master am ON
                    ard.asset_id = am.asset_id
                              
                LEFT JOIN ".TB_PREF."project_sites ps ON
                    ard.site_id = ps.id

            WHERE ard.asset_return_id=".db_escape($asset_return_id);
    
    return db_query($sql);                
}
        
function view_asset_issue_for_asset_return($asset_issue_id)
{
    $sql = "SELECT ar.*,
                    lo.location_name
        
            FROM ".TB_PREF."asset_return ar
                LEFT JOIN ".TB_PREF."asset_locations lo ON
                    ar.location = lo.loc_code
                
            WHERE ar.asset_issue_id=".db_escape($asset_issue_id);
    
    return db_query($sql);

    
}
        
?>