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


function get_sql_for_asset_issue_inquiry($project_id, $asset_issue_id, $employee_code,  
                                            $issuing_location, $from_date, $to_date, $group_company_code )
{
    $sql = "SELECT ai.id asset_issue_id,
                    ai.reference,
                    pm.description project_description,
                    ai.tran_date,
                    ai.employee_code,
                    ai.company_code,
                    lo.location_name,
                    ai.comments,
                    ai.fully_returned                    

            FROM ".TB_PREF."tool_issues ai 
                    LEFT JOIN ".TB_PREF."projects_master pm ON
                        ai.project_id = pm.project_id 

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

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

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



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

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

            WHERE ai.id=".db_escape($asset_issue_id);
    
    $result = db_query($sql);
    return db_fetch($result);

}


function view_tool_issue_details($asset_issue_id)
{
    $sql = "SELECT aid.*,
                    am.asset_code,
                    am.asset_id,
                    am.description asset_description,
                    ar.description rate_description,
                    pt.description task_description,
                    ps.description site_name
                    
            FROM ".TB_PREF."tool_issue_details aid
                LEFT JOIN ".TB_PREF."asset_master am ON
                    aid.asset_id = am.asset_id
                
                LEFT JOIN ".TB_PREF."tool_rates ar ON
                    aid.rate_category_id = ar.id
                    
                LEFT JOIN ".TB_PREF."project_tasks pt ON
                    aid.task_id = pt.task_id

                LEFT JOIN ".TB_PREF."project_sites ps ON
                    aid.site_id = ps.id

            WHERE aid.ai_id=".db_escape($asset_issue_id);
    
    return db_query($sql);
                
}
        
function view_tool_returns_for_tool_issue($asset_issue_id)
{
    $sql = "SELECT ar.*,
                    lo.location_name
        
            FROM ".TB_PREF."tool_returns 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, "view_tool_returns_for_tool_issue");

}

 
 
?>