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


function get_asset_movements($asset_id, $from_date, $to_date, $project_id, $location_code, $incl_tool_issues=true)                                           
{
    $sql = "SELECT amo.*,
                    am.asset_code,
                    am.description asset_description, 
                    pm.description project_description, 
                    al.location_name asset_location_name                     
            FROM ".TB_PREF."asset_moves amo
                JOIN ".TB_PREF."asset_master am ON
                    amo.asset_id = am.asset_id  
                JOIN ".TB_PREF."asset_locations al ON
                    amo.loc_code = al.loc_code   
                LEFT JOIN ".TB_PREF."projects_master pm ON
                    amo.project_id = pm.project_id   
                                                                                                       
            WHERE  amo.tran_date >=".db_escape(date2sql($from_date))." AND amo.tran_date <=".db_escape(date2sql($to_date));

            $sql .= " AND amo.asset_id=".db_escape($asset_id);
            
            
    if(!$incl_tool_issues){
        $sql .= " AND amo.trans_type NOT IN (".db_escape(ST_TOOL_ISSUE).", ".db_escape(ST_TOOL_RETURN).", ".db_escape(ST_DAILY_TOOL_ISSUE).", ".db_escape(ST_DAILY_TOOL_RETURN).")";
    }        

    if($project_id > 0){
        $sql .= " AND amo.project_id=".db_escape($project_id);
    }
    
    if(strlen($location_code) > 0){
        $sql .= " AND amo.loc_code=".db_escape($location_code);
    }

    $sql .= " ORDER BY amo.id";
    
    return db_query($sql);
    
}
        

function get_asset_balance($asset_id, $balance_upto_date, 
                           $project_id, $location_code)                                           
{
    $sql = "SELECT SUM(amo.qty) balance                  
                                        
            FROM ".TB_PREF."asset_moves amo
                JOIN ".TB_PREF."asset_master am ON
                    amo.asset_id = am.asset_id   
                LEFT JOIN ".TB_PREF."projects_master pm ON
                    amo.project_id = pm.project_id  
                    
            WHERE  amo.tran_date <".db_escape(date2sql($balance_upto_date));

            $sql .= " AND amo.asset_id=".db_escape($asset_id);
        

        if($project_id > 0){
            $sql .= " AND amo.project_id=".db_escape($project_id);
        }

        if(strlen($location_code) > 0){
            $sql .= " AND amo.loc_code=".db_escape($location_code);
        }
    

    $result = db_query($sql);
    $row = db_fetch($result);
    
    if(isset($row['balance'])){
        return $row['balance'];
    }
    else{
        return 0;
    }
    
}
        

function view_asset_transfer($asset_material_issue_id)
{
    $sql = "SELECT at.*,
                    am.asset_code,
                    am.description asset_description,
                    am.asset_id,
                    fpm.description from_project_description,
                    fpt.description from_budget_description,
                    fde.value_name from_department_name,
                    tpm.description to_project_description,
                    tpt.description to_budget_description,
                    tde.value_name to_department

            
            FROM ".TB_PREF."asset_transfer at
                    LEFT JOIN ".TB_PREF."asset_master am ON
                        at.asset_id = am.asset_id
                
                    LEFT JOIN ".TB_PREF."projects_master fpm ON
                        at.from_project_id = fpm.project_id                
         
                    LEFT JOIN ".TB_PREF."project_tasks fpt ON
                        at.from_budget_id = fpt.task_id

                    LEFT JOIN ".TB_PREF."list_values fde ON
                        at.from_department_id = fde.id                

                    LEFT JOIN ".TB_PREF."projects_master tpm ON
                        at.to_project_id = tpm.project_id

                    LEFT JOIN ".TB_PREF."project_tasks tpt ON
                        at.to_budget_id = tpt.task_id

                    LEFT JOIN ".TB_PREF."list_values tde ON
                        at.to_department_id = tde.id
              
            WHERE at.id=".db_escape($asset_material_issue_id);
    
    $result = db_query($sql);
    return db_fetch($result);

}



 
 
?>