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


function get_sql_for_material_return_inquiry($from_date, $to_date, $receiving_location,
                                              $mr_no, $rm_no, $project_id)                                                     
{
    $sql = "SELECT rm.rm_no,
                    rm.reference,
                    l.location_name,
                    rm.rm_date,
                    pm.description project_description,
                    rm.source_mr_no,
                    gc.company_name,
                    rm.company_code, 
                    rm.employee_code 

            FROM ".TB_PREF."return_materials rm 
                    INNER JOIN ".TB_PREF."projects_master pm ON 
                            rm.project_id = pm.project_id 
                    INNER JOIN ".TB_PREF."locations l ON 
                            rm.receiving_location = l.loc_code  
                    LEFT JOIN group_companies gc ON  
                            rm.company_code = gc.company_code         
                               
            WHERE rm.project_id = pm.project_id AND
                    rm.rm_date >= ".db_escape(date2sql($from_date))." AND rm.rm_date <= ".db_escape(date2sql($to_date)); 
            
    
   if($receiving_location != ''){        
        $sql .= " AND rm.receiving_location=".db_escape($receiving_location);
    }
        
        
    if($mr_no > 0){
        $sql .= " AND rm.source_mr_no=".db_escape($mr_no);
    }
   
    if($rm_no > 0){
        $sql .= " AND rm.rm_no=".db_escape($rm_no);
    }
   
    if($project_id > 0){
        $sql .= " AND rm.project_id=".db_escape($project_id);
    }
    
    return $sql;
    
}
        

function view_materials_return($rm_no)
{
    $sql = "SELECT rm.*,
                    rl.location_name receiving_location_name,
                    pm.description project_description,
                    u.real_name created_user_name,
                    gc.company_name 
        
            FROM ".TB_PREF."return_materials rm 
                    INNER JOIN ".TB_PREF."projects_master pm ON 
                            rm.project_id = pm.project_id 
                    LEFT JOIN ".TB_PREF."locations rl ON
                            rm.receiving_location = rl.loc_code 
                    LEFT JOIN ".TB_PREF."users u ON
                            rm.created_by = u.id 
                    LEFT JOIN group_companies gc ON
                            rm.company_code = gc.company_code                             
            WHERE rm.rm_no=".db_escape($rm_no);
    
    $result = db_query($sql);
    return db_fetch($result);    
    
}


function view_material_return_details($id)
{
    $sql = "SELECT rmd.*,
                    sm.description item_description,
                    pt.description task_description,
                    ps.description site_description,
                    u.name unit_name,
                    u.decimals 
        
            FROM ".TB_PREF."return_material_details rmd 
                    INNER JOIN ".TB_PREF."stock_master sm ON 
                            rmd.item_code = sm.stock_id 
                    LEFT JOIN ".TB_PREF."project_tasks pt ON  
                            rmd.task_id = pt.task_id  
                    LEFT JOIN ".TB_PREF."project_sites ps ON  
                            rmd.site_id = ps.id 
                    LEFT JOIN ".TB_PREF."item_units u ON  
                            rmd.tran_unit = u.abbr                              
            WHERE rmd.rm_no=".db_escape($id);
    
   return db_query($sql);
   
}


        
?>