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


function add_tool_return_header($ai_id, $reference, $project_id, 
                                    $company_code, $employee_code,
                                    $location, $date, $comment)                                             
{
    begin_transaction();
    
    global $Refs;
     
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."tool_returns
                  ( asset_issue_id, reference, project_id,  
                    company_code, employee_code, location,
                    tran_date, comment,
                    created_by, created_on )  
                          
                VALUES 
                
                (".db_escape($ai_id).", ".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, "add_tool_return_header");    
    $artn_id = db_insert_id(); 
    
    $Refs->save(ST_TOOL_RETURN, $ai_id, $reference);
    commit_transaction();

    return $artn_id;       
   
}


function add_tool_return_line($ai_line_id, $asset_return_id, $asset_id, 
                                $site_id, $qty, $returned_date, 
                                $lost, $comment)
{
    
    $sql = "INSERT INTO ".TB_PREF."tool_return_details
              ( ai_line_id, asset_return_id, 
                asset_id, site_id, 
                quantity, returned_date, 
                lost, line_comment)
               
            VALUES
                (".db_escape($ai_line_id).",  ".db_escape($asset_return_id).", 
                    ".db_escape($asset_id).", ".db_escape($site_id).",  
                    ".db_escape($qty).", ".db_escape(date2sql($returned_date)).", 
                    ".db_escape($lost).", ".db_escape($comment).")";
            
    db_query($sql);  
    
    return db_insert_id();

}


function get_tool_issue_detail_total_returned_qty($issue_line_id)
{
    $sql = "SELECT SUM(quantity) returned_qty
            FROM ".TB_PREF."tool_return_details
            WHERE ai_line_id=".db_escape($issue_line_id);
    
    $result = db_query($sql, "get_tool_issue_detail_total_returned_qty");
    $row = db_fetch($result);
    
    if(isset($row['returned_qty']) && $row['returned_qty'] > 0){
        return $row['returned_qty'];
    }else{
        return 0;
    }
    
}





//function get_tool_request_detail_line($selected_id)
//{
//    $sql = "SELECT ard.*,
//                    ar.id
//        
//            FROM ".TB_PREF."tool_request_details ard
//                LEFT JOIN ".TB_PREF."tool_rates ar ON
//                    ard.rate_category_id = ar.id
//
//            WHERE ard.id=".db_escape($selected_id);
//    
//    $result = db_query($sql);
//    return db_fetch($result);    
//}


function get_tool_return_line_record($selected_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."tool_return_details
            WHERE ai_line_id=".db_escape($selected_id);
    
    $result = db_query($sql);
    return db_fetch($result);    
}

function get_tool_return_detail_records_of_tool_issue_line($ai_line_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."tool_return_details
            WHERE ai_line_id=".db_escape($ai_line_id);
    
    return db_query($sql);
}


?>