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


function view_tool_usage_charge_header($selected_id)
{
    $sql = "SELECT auc.*,
                    u.real_name user_name 
            
            FROM ".TB_PREF."tool_usage_charges auc 
                JOIN ".TB_PREF."users u ON 
                    auc.created_by = u.id 

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

function view_tool_issue_detail($selected_id)
{
    $sql = "SELECT tid.*,
                    am.asset_code, am.description asset_description 
            
            FROM ".TB_PREF."tool_issue_details tid 
                JOIN ".TB_PREF."asset_master am 
                    ON am.asset_id = tid.asset_id 

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

function view_tool_usage_charge_details($auc_no)
{
    $sql = "SELECT aucd.*,
                    am.asset_code,
                    am.asset_id,
                    am.description asset_description,
                    ar.description rate_description,
                    pt.description task_description,
                    ps.description site_name,
                    aid.from_date issued_date,
                    aid.quantity issued_qty  
                    
            FROM ".TB_PREF."tool_usage_charge_details aucd 
                JOIN ".TB_PREF."tool_issue_details aid ON 
                    aucd.issue_detail_id = aid.id 
                JOIN ".TB_PREF."asset_master am ON
                    aid.asset_id = am.asset_id
                JOIN ".TB_PREF."project_tasks pt ON
                    aid.task_id = pt.task_id
                LEFT JOIN ".TB_PREF."tool_rates ar ON
                    aid.rate_category_id = ar.id                     
                LEFT JOIN ".TB_PREF."project_sites ps ON
                    aid.site_id = ps.id

            WHERE aucd.id=".db_escape($auc_no);
    
    return db_query($sql);
                
}
  

function view_tool_issue_detail_charge_details($auc_no)
{
    $sql = "SELECT tucd.*,
                    tuc.cutoff_date   
                    
            FROM ".TB_PREF."tool_usage_charge_details tucd  
                JOIN ".TB_PREF."tool_usage_charges  tuc ON 
                    tucd.charge_no = tuc.id 
                 

            WHERE tucd.issue_detail_id=".db_escape($auc_no);
    
    return db_query($sql);
                
}
        

function get_sql_for_tool_usage_charges_inquiry($project_id, $asset_issue_id, $from_date, $to_date)
{
    $sql = "SELECT tuc.id, tuc.issue_id, tuc.cutoff_date, 
                    tucd.issue_detail_id, tucd.from_date, pm.description project_description, 
                    
                    tucd.issue_detail_id, tucd.no_of_units, tucd.rate, tucd.no_of_units * tucd.rate total,
                    am.asset_id, am.asset_code, am.description 
                     
                     
            FROM ".TB_PREF."tool_usage_charges tuc 
                JOIN ".TB_PREF."tool_usage_charge_details tucd 
                    ON tuc.id = tucd.charge_no 
                JOIN ".TB_PREF."tool_issue_details tid 
                    ON tid.id = tucd.issue_detail_id 
                JOIN ".TB_PREF."asset_master am 
                    ON am.asset_id = tid.asset_id                 
                JOIN ".TB_PREF."tool_issues ti  
                    ON ti.id = tid.ai_id                 
                JOIN ".TB_PREF."projects_master pm   
                    ON pm.project_id = ti.project_id                 

            WHERE 1=1 "; 
    
    if($asset_issue_id > 0){
        $sql .= "AND ti.id=".db_escape($asset_issue_id);
    }
    else{
        $sql .= " AND  tuc.cutoff_date >=".db_escape(date2sql($from_date))." AND  tuc.cutoff_date <=".db_escape(date2sql($to_date));
      
        if($project_id > 0){
            $sql .= "AND ti.project_id=".db_escape($project_id);
        }

        
    }
    
    return $sql;    
}
        
function get_tool_issue_detail_usage_total_charge($isset_detail_id)
{
    $sql = "SELECT SUM(tucd.no_of_units * tucd.rate) total 
            FROM ".TB_PREF."tool_usage_charge_details tucd 
            WHERE tucd.issue_detail_id = ".db_escape($isset_detail_id);
    
    $result = db_query($sql, "get_tool_issue_detail_usage_total_charge");
    return db_result_value($result, "total");
}
?>