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


function get_sql_for_material_issues_inquiry($from_date, $to_date, $receiving_location,
                                              $mr_no, $im_no, $project_id)                                                    
{
    $sql = "SELECT im.im_no,
                    im.reference,
                    l.location_name,
                    im.im_date,
                    pm.description project_description,
                    im.source_mr_no,
                    gc.company_name,
                    im.company_code, 
                    im.employee_code 

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

//function view_materials_return($rm_no)
//{
//    $sql = "SELECT rm.*,
//                   pm.project_name 
//        
//            FROM ".TB_PREF."return_materials rm,
//                ".TB_PREF."projects_master pm
//                
//            WHERE rm.project_id = pm.project_id AND
//                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
//        
//            FROM ".TB_PREF."return_material_details rmd,
//                    ".TB_PREF."stock_master sm
//
//            WHERE rmd.item_code = sm.stock_id AND
//                rmd.rm_no=".db_escape($id);
//    
//   return db_query($sql);
//   
//}


function view_issue_material_header($im_no)
{
    $sql = "SELECT im.*, 
                    il.location_name issuing_location_name,
                    pm.description project_description,
                    u.real_name created_user_name,
                    gc.company_name,
                    mv.name movement_type_name 
            FROM ".TB_PREF."issue_materials im
                    LEFT JOIN ".TB_PREF."locations il ON
                            im.issuing_location = il.loc_code 
                    LEFT JOIN ".TB_PREF."projects_master pm ON
                            pm.project_id = im.project_id
                    LEFT JOIN ".TB_PREF."movement_types mv ON
                            mv.id = im.movement_type  
                    LEFT JOIN ".TB_PREF."users u ON
                            im.created_by = u.id 
                    LEFT JOIN group_companies gc ON
                            im.company_code = gc.company_code 
            WHERE im_no = '".$im_no."'";
    
    $result = db_query($sql);
    return db_fetch($result);
    
}

function view_issue_material_details($im_no)
{
    $sql = "SELECT  imd.*,
                    sm.description item_description,
                    pt.description task_description,
                    ps.description site_description,
                    u.name unit_name,
                    u.decimals 
            FROM ".TB_PREF."issue_material_details imd 
                    LEFT JOIN ".TB_PREF."stock_master sm ON  
                            imd.item_code = sm.stock_id 
                    LEFT JOIN ".TB_PREF."project_tasks pt ON  
                            imd.task_id = pt.task_id  
                    LEFT JOIN ".TB_PREF."project_sites ps ON  
                            imd.site_id = ps.id 
                    LEFT JOIN ".TB_PREF."item_units u ON  
                            imd.tran_unit = u.abbr  
            WHERE imd.im_no = '".$im_no."'";
    
    $result = db_query($sql);
    
    return $result;
    
}

        
?>