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


function get_sql_for_outstanding_material_requisitions_inquiry($required_by_from = null, $required_by_to = null, 
//            $created_on_from = null, $created_on_to = null,  
            $issuing_location = "",
            $mr_no = 0,  $mr_reference = "", $mr_complete = null,
            $project_id = null, $originator = null,
            $stock_item = null)
{
        $filter = "";
        
        if($required_by_from != null)
        {
            $filter = " AND mr.approved_on > ".db_escape(date2sql($required_by_from));
        }
        
        if($required_by_to != null)
        {
            $filter .= " AND mr.approved_on < ".db_escape(date2sql($required_by_to));
        }
        
//        if($created_on_from != null)
//        {
//                $filter .= " AND mr.created_on > ".db_escape(date2sql($created_on_from));
//        }
//        
//        if($created_on_to != null)
//        {
//                $filter .= " AND mr.created_on < ".db_escape(date2sql($created_on_to));
//        }
        
        if($issuing_location != "")
        {
                $filter .= " AND mr.issuing_location = ".db_escape($issuing_location);            
        }
        
        if ($mr_no != 0)
        {
                $filter .= " AND mr.mr_no = ".$mr_no;
        }
        
        if ($project_id > 0)
        {
                $filter .= " AND mr.project_id = ".db_escape($project_id); 
        }
        
        if ($originator > 0)
        {
                $filter .= " AND mr.created_by = ".db_escape($originator); 
        }
        
        $filter .= " AND mr.mr_complete = ".db_escape($mr_complete);   

        
        //$filter = "";
        
 
        $sql = "SELECT 
                    mr.mr_no,
                    mr.reference, 
                    il.location_name issuing_location ,
                    p.description,
                    mr.mr_date mr_required_date,
                    u.real_name requestor_name,
                    mr.approval_status,
                    mr.approval_status,
                    mr.created_on,
                    mr.approved_on,
                    mr.closed_for_prs 
                FROM 
                    ".TB_PREF."material_requisitions mr,
                    ".TB_PREF."projects_master as p, 
                    ".TB_PREF."locations il,
                    ".TB_PREF."users u 
                WHERE  mr.project_id = p.project_id AND 
                    mr.issuing_location = il.loc_code AND 
                    mr.created_by = u.id AND
                        (mr.approval_status=".AS_APPROVED." OR mr.approval_status=".AS_ISSUED.")"                                       

                    .$filter." 
                ORDER BY
                   mr.approved_on";
        
        

	return $sql;
}





        
?>