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

function get_open_tool_issues_for_cost_calculation($calculation_date)
{    
    $sql = "SELECT * 
            FROM ".TB_PREF."tool_issues 
            WHERE fully_charged = 0 
                    AND charged_upto_date < ".db_escape($calculation_date)." 
                     LIMIT 100";
    
    return db_query($sql);
    
}

function create_next_tool_usage_charge_no($cutoff_date, $asset_issue_id)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."tool_usage_charges(
                        cutoff_date, 
                        issue_id,
                        created_by, created_on)

            VALUES (".db_escape($cutoff_date).", "
                        .db_escape($asset_issue_id).", "
                        .db_escape($id_user).", ".db_escape($datetime).") ";

    db_query($sql, "Could not create_next_tool_usage_charge_no");

    return db_insert_id();
}


function save_tool_usage_charge_details($charge_no, $asset_issue_detail_id, $calculated_from_fdate, $charging_units, $rate)
{
    $sql = "INSERT INTO ".TB_PREF."tool_usage_charge_details(
                        charge_no, issue_detail_id, 
                        from_date, no_of_units,
                        rate)

            VALUES (".db_escape($charge_no).", ".db_escape($asset_issue_detail_id).", "
                        .db_escape($calculated_from_fdate).", ".db_escape($charging_units).", "
                        .db_escape($rate).") ";

    db_query($sql, "Could not save_tool_usage_charge_details");

}

function add_to_tool_usage_charges_log($type, $asset_issue_id, $issue_detail_id, 
                                        $return_detail_id, $charge_no, 
                                        $from_date, $cal_from_date, $to_date, 
                                        $days, $quantity, $asset_rate)

{

    $sql = "INSERT INTO ".TB_PREF."tool_usage_charges_log(
                    type, issue_id, 
                    issue_detail_id, 
                    return_detail_id, asset_charge_no,
                    from_date, cal_from_date, 
                    to_date, days, 
                    quantity, rate)

            VALUES (".db_escape($type).", ".db_escape($asset_issue_id).", "
                    .db_escape($issue_detail_id).", "
                    .db_escape($return_detail_id).", ".db_escape($charge_no).", "
                    .db_escape($from_date).", ".db_escape($cal_from_date).", "
                    .db_escape($to_date).", ".db_escape($days).", "
                    .db_escape($quantity).", ".db_escape($asset_rate).") ";

    db_query($sql, "Could not add_to_tool_usage_charges_log");

}

function get_tool_issue_detail_charged_units($asset_issue_detail_id)
{
   $sql = "SELECT SUM(no_of_units) units_charged 
            FROM ".TB_PREF."tool_usage_charge_details
            WHERE issue_detail_id =".db_escape($asset_issue_detail_id);
               
    $result = db_query($sql);
    $row = db_fetch($result);
    
    if(isset($row['units_charged']) && $row['units_charged'] > 0){
        return $row['units_charged'];
    }
    else{
        return 0;
    }

}


function get_open_tool_usage_charge_nos($asset_issue_id)
{    
    $sql = "SELECT auc.id 
            FROM ".TB_PREF."tool_usage_charges auc                       
            WHERE auc.charged = 0 
                    AND auc.issue_id = ".db_escape($asset_issue_id);
    
    return db_query($sql);
    
}

function get_tool_usage_charges_task_totals($asset_usage_charge_id)
{    
    $sql = "SELECT SUM(aucd.no_of_units * aucd.rate) task_cost, aid.task_id   
            FROM ".TB_PREF."tool_usage_charge_details aucd 
                    JOIN ".TB_PREF."tool_issue_details aid ON 
                            aucd.issue_detail_id = aid.id 
            WHERE aucd.charge_no = ".db_escape($asset_usage_charge_id)." 
                    GROUP BY aid.task_id ";
    
    return db_query($sql);
    
}


function update_tool_usage_costs_charged($auc_id)
{
    $sql = "UPDATE ".TB_PREF."tool_usage_charges 
            SET charged = 1             
            WHERE charged = 0 
                AND id =".db_escape($auc_id);
   
    db_query($sql);   
    return db_num_affected_rows();
}

function check_tool_request_fully_charged($tool_request_id)
{
    $sql = "SELECT aid.id num_recs  
            FROM ".TB_PREF."tool_issue_details aid 
                    JOIN ".TB_PREF."tool_request_details areqd ON 
                            aid.ar_line_id = areqd.id 
            WHERE aid.fully_charged = 0  
                    AND areqd.project_asset = 0 
                    AND areqd.ar_id = ".db_escape($tool_request_id);
    
    $result = db_query($sql);
    $num_recs = db_num_rows($result);
    
    if($num_recs > 0){
        return false;
    }
    else{
        return true;
    }
}
        
?>