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


function add_asset_issue_header($ar_no, $reference, $project_id, $company_code, $employee_code,
                                    $location, $date, $comment) 
                                             
{ 
    global $Refs;
       
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."tool_issues
                  ( ar_id, reference, project_id,  
                    company_code, employee_code, location,
                    tran_date, comments,
                    created_by, created_on )  
                          
                VALUES 
                
                (".db_escape($ar_no).", ".db_escape($reference).", ".db_escape($project_id).", 
                    ".db_escape($company_code).", ".db_escape($employee_code).", ".db_escape($location).",
                    ".db_escape(date2sql($date)).",
                    ".db_escape($comment).",                   
                    ".db_escape($created_by).", ".db_escape($created_on).")";
    
    db_query($sql);   
    
    $ai_id = db_insert_id();
    
    $Refs->save(ST_TOOL_ISSUE, $ai_id, $reference);
       
    return $ai_id;            
  
}


function add_asset_issue_line($source_line_id, $ai_id, $asset_id,
                                $rate_category_id, $rate,  
                                $task_id, $site_id, $from_date,
                                $to_date, $no_of_days, 
                                $issuing_comment, $project_asset, $issuing_qty)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."tool_issue_details (
                ai_id, asset_id, 
                rate_category_id, rate, 
                task_id, site_id, 
                from_date, to_date, no_of_days,
                line_comment, project_asset, quantity,
                ar_line_id,
                created_by, created_on )
               
            VALUES (
                ".db_escape($ai_id).", ".db_escape($asset_id).", 
                ".db_escape($rate_category_id).", ".db_escape($rate).", 
                ".db_escape($task_id).", ".db_escape($site_id).",
                ".db_escape(date2sql($from_date)).", ".db_escape(date2sql($to_date)).", ".db_escape($no_of_days).", 
                ".db_escape($issuing_comment).", ".db_escape($project_asset).", ".db_escape($issuing_qty).", 
                ".db_escape($source_line_id).",  
                ".db_escape($created_by).", ".db_escape($created_on).")";
            
    db_query($sql);       
 
    return db_insert_id();
    
}


function get_asset_total_issued_qty($ar_line_id)
{
    $sql = "SELECT SUM(quantity) issued_qty
            FROM ".TB_PREF."tool_issue_details
            WHERE ar_line_id=".db_escape($ar_line_id);
    
    $result = db_query($sql, "get_asset_total_issued_qty");
    $row = db_fetch($result);
    
    if(isset($row['issued_qty']) && $row['issued_qty'] > 0){
        return $row['issued_qty'];
    }
    else{
        return 0;
    }
}


function get_asset_issue_detail_record($id)
{
   $sql = "SELECT *
            FROM ".TB_PREF."tool_issue_details
            WHERE id=".db_escape($id);
               
    $result = db_query($sql);
    return db_fetch($result);
}
   
   
function get_asset_issue_detail_records($ai_id)
{
   $sql = "SELECT *
            FROM ".TB_PREF."tool_issue_details
            WHERE ai_id=".db_escape($ai_id);
               
    return db_query($sql);

}

   
function get_open_tool_issue_detail_records_for_charging($ai_id)
{
   $sql = "SELECT *
            FROM ".TB_PREF."tool_issue_details
            WHERE fully_charged = 0 
                AND ai_id=".db_escape($ai_id);
               
    return db_query($sql);

}

function get_asset_issue_detail_records_of_tool_request_line($ar_line_id)
{
   $sql = "SELECT *
            FROM ".TB_PREF."tool_issue_details
            WHERE ar_line_id=".db_escape($ar_line_id);
               
    return db_query($sql);

}

function update_tool_issue_detail_total_returned_qty($issue_line_id, $return_qty)
{
    $sql = "UPDATE ".TB_PREF."tool_issue_details
            SET returned_qty =".db_escape($return_qty)."             
            WHERE id=".db_escape($issue_line_id);               
   
    db_query($sql, "update_asset_issue_detail_total_returned_qty");
    return db_num_affected_rows();
}

function update_tool_issue_fully_returned($asset_issue_id, $fully_returned_date_)
{
    $fully_returned_date = date2sql($fully_returned_date_);
    
    $sql = "UPDATE ".TB_PREF."tool_issues
            SET fully_returned = 1,
                fully_returned_date = ".db_escape($fully_returned_date)."          
            WHERE id=".db_escape($asset_issue_id);               
   
    db_query($sql, "update_asset_issue_fully_returned");
}

function check_tool_issue_fully_returned($asset_issue_id)
{
    $sql = "SELECT id num_recs  
            FROM ".TB_PREF."tool_issue_details  
            WHERE quantity > returned_qty   
                    AND ai_id = ".db_escape($asset_issue_id);
    
    $result = db_query($sql, "check_tool_issue_fully_returned");
    $num_recs = db_num_rows($result);
    
    if($num_recs > 0){
        return false;
    }
    else{
        return true;
    }
}

function get_tool_issue_header($pi_no)
{
    $sql = "SELECT ai.*,
                    pm.description project_description
                   
            FROM ".TB_PREF."tool_issues ai 
                    LEFT JOIN ".TB_PREF."projects_master pm ON 
                            ai.project_id = pm.project_id 
                  
            WHERE ai.id=".db_escape($pi_no);
    
    $result = db_query($sql, "Could not get_asset_issue_header.");
    return db_fetch($result);   
    
}

function get_tool_issue_header_record($pi_no)
{
    $sql = "SELECT ai.* 
            FROM ".TB_PREF."tool_issues ai 
            WHERE ai.id=".db_escape($pi_no);
    
    $result = db_query($sql, "Could not get_tool_issue_header_record.");
    return db_fetch($result);   
    
}

function get_tool_issue_details($ar_no)
{
    $sql = "SELECT aid.*,
                    am.description asset_description,
                    am.asset_code,
                    am.asset_id,
                    ar.description rate_description,
                    pt.description task_description,
                    ps.description site_description
        
            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($ar_no);
    
    return db_query($sql, "Could not get_tool_request_details");
    
}

function update_tool_issue_detail_charged_summary($asset_issue_detail_id, $charging_units, $fully_charged)
{
    $charged_units = get_tool_issue_detail_charged_units($asset_issue_detail_id);
    
    $total_units_charged = $charging_units + $charged_units;
    
    $sql = "UPDATE ".TB_PREF."tool_issue_details 
            SET units_charged =".db_escape($total_units_charged).",
                fully_charged =".db_escape($fully_charged)." 
            WHERE id=".db_escape($asset_issue_detail_id);               
   
    db_query($sql, "update_tool_issue_detail_charged_summary");

    
}


function get_tool_request_detail_charged_units($tool_request_detail_id)
{
   $sql = "SELECT SUM(units_charged) units_charged 
            FROM ".TB_PREF."tool_issue_details
            WHERE ar_line_id =".db_escape($tool_request_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 calculate_tool_issue_task_remaining_totals($asset_issue_id)
{
    $task_costs = array();

    $result = get_tool_issue_task_remaining_totals($asset_issue_id);

    while($row = db_fetch($result))
    {            
         add_to_task_cost($task_costs, $row['task_id'], $row['task_cost'], $row['task_cost'], $row['task_cost']);
    }

    get_task_cost_details($task_costs);

    return $task_costs;
}


function get_tool_issue_task_remaining_totals($asset_issue_id)
{
    $sql = "SELECT task_id, SUM(((quantity * no_of_days) - units_charged) * rate) task_cost 
            FROM ".TB_PREF."tool_issue_details 
            WHERE ai_id = ".db_escape($asset_issue_id)." 
                    AND project_asset = 0 
            GROUP BY task_id";
    
    return db_query($sql, "get_tool_issue_task_remaining_totals");
}


function get_asset_issue_tasks($ar_no)
{
    $tasks = array();
    
    $sql = "SELECT task_id 
            FROM ".TB_PREF."tool_request_details 
            WHERE ar_id = ".db_escape($ar_no)." 
                    AND project_asset = 0 ";
    
    $result = db_query($sql, "get_tool_request_tasks");
    while ($row = db_fetch($result)) {
        $tasks[] = $row['task_id'];        
    }
    
    get_task_cost_details($tasks);
    
    return $tasks;
}


function check_tool_issue_fully_charged($asset_issuse_id)
{
    $sql = "SELECT aid.id num_recs  
            FROM ".TB_PREF."tool_issue_details aid 
            WHERE aid.fully_charged = 0  
                    AND aid.project_asset = 0 
                    AND aid.ai_id = ".db_escape($asset_issuse_id);
    
    $result = db_query($sql);
    $num_recs = db_num_rows($result);
    
    if($num_recs > 0){
        return false;
    }
    else{
        return true;
    }
}
       

function update_tool_issue_charges_calculation_summary($areq_id, $charged_upto_date, $fully_charged, $charge_asset_issue, $row_state)
{
    $sql = "UPDATE ".TB_PREF."tool_issues 
            SET charged_upto_date = ".db_escape($charged_upto_date).",
                fully_charged = ".db_escape($fully_charged).",
                row_state = ". db_new_row_state($row_state)."
            WHERE id=".db_escape($areq_id)." 
                AND row_state = ".db_escape($row_state);
            
    db_query($sql, "update_tool_issue_charges_calculation_summary");
    
    return db_num_affected_rows();
}


?>