Magento – How to Filter Orders by Date on Custom Page

dateordersscript

magento site : www.1234.com

we are displaying all the orders in page : www.1234.com/php/site6/date.php , user can able to see all orders only when user login to date.php page.

in date.php page, we are displaying Order Id & Order Date & succesfully filtering orders based on entered order_id in textfield with help of below code.

php

function getDesignerCollection()
{
    $user_home = new USER();
    require_once '../../app/Mage.php';
    Mage::app(); 
    $stmts = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
    $stmts->execute(array(":uid"=>$_SESSION['userSession']));
    $rows = $stmts->fetch(PDO::FETCH_ASSOC);

    $accountType=$rows['type'];
    if($accountType=="admin"){

        $order = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('designer_id',array('nin'=>'0'));

    }
    else
    {
        $order = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('designer_id',array('like' => '%'.$_SESSION['userSession'].'%'));
    }

    $i=0;
    foreach ($order as $orderData)  
    {

        $orderitems=$orderData['dproduct_id'];
        $orderitemsarray=explode(",",$orderitems);
        $k=0;

        $oDate = new DateTime($orderData['created_at']);
        $sDate = $oDate->format("Y-m-d");
        while($k < count($orderitemsarray))
        {
            if($orderitemsarray[$k]!='0')
            {

                $stmtorders = $user_home->runQuery("SELECT * FROM order_details WHERE designerorder_id=:designerorder_id");
                $stmtorders->execute(array(":designerorder_id"=>$orderData['entity_id']));

                $roworders = $stmtorders->fetch(PDO::FETCH_ASSOC);

                if($roworders['designerorder_id']==''){$dorderStatus="Unpaid";}else{$dorderStatus=$roworders['paid_status'];}

                $productdetail=Mage::getModel('catalog/product')->load($orderitemsarray[$k]); 

                $responce[]=array($orderData->getIncrementId(),$orderData->getIncrementId(),$orderitemsarray[$k],$productdetail->getName(),$designerName,$orderData['status'],$orderData['grand_total'],$orderData['customer_email'],$orderData['shipping_description'],$dorderStatus,$sDate);

            }
            $k++; $i++;
        }

    }       echo json_encode($responce);    

}       

html

<select id="f_value2">
            <option value="entity_id">Order Id</option>             
        </select>
        <input type="text" onkeyup="doFilter()" value="" id="f_value1" >

<form name="frmSearch" method="post" action="">
<input type="text" placeholder="From Date" id="post_at" 
value="" name="post_at" />       
<input type="text" placeholder="To Date" id="post_at_to_date" 
value="" name="post_at_to_date" />

</form>

script

function doFilter() {
        var filterInfo=[
            {
                fieldName : $('select[id=f_value2]').val(),
                logic : "startWith",
                value : Sigma.Util.getValue("f_value1")
            },
            {
                fieldName : Sigma.Util.getValue("f_fieldName12"),
                logic : "greatEqual",
                value : Sigma.Util.getValue("f_value12")
            }
        ]
        var grid=Sigma.$grid("myGrid");
        var rowNOs=grid.applyFilter(filterInfo); 
    }

now we want to filter orders by "From & To Date".

enter image description here

i am trying below php code [not js ] for filtering with From & TO date, i can able to display From & TO Date textfields with datepicker , but when i click on search button its not working.

php

$post_at = '0000-00-00'; 
$post_at_todate = '0000-00-00';

$queryCondition = "";

    if( !empty( $_POST[ 'post_at' ] ) ) 
{           
    $post_at        = date( 'Y-m-d', strtotime( $_POST[ 'post_at' ] ) );
    $post_at_todate = date( 'Y-m-d' );
    if( !empty( $_POST[ 'post_at_to_date' ] ) )
    {
        $post_at_to_date = date( 'Y-m-d', strtotime( $_POST[ 'post_at_to_date' ] ) );
        $post_at_todate = $post_at_to_date;
    }
    $queryCondition .= "WHERE DATE( created_at ) >= '" . $post_at . "' AND DATE( created_at ) <= '" . $post_at_todate . "'";
}

$sqlquery = "SELECT * FROM order_details $queryCondition";
//$sqlquery = "SELECT * FROM order_details where created_at >= '2016-11-17' AND created_at < = '2016-11-19'";
var_dump( $sqlquery );  




    $result = $conn->query($sqlquery);

    if ($result->num_rows > 0) {
        echo $result->num_rows.'result found'."<br>";
        while($row = $result->fetch_assoc()) {
            echo "id: " . $row["id"]. " - date: " . $row["post_at"];
            echo "<br>";
        }
    } else {
        echo "0 results found <br>";
    }
$conn->close();

Edit 2

I got that i am doing mistake that i am fetching Date table from magento db & trying to filter values using custom db, now please help me to filter values with help of magento db [ as similar to filtering through entering order id in search box ] , for orders also i am using magento db only.

custom db : order_details

enter image description here

Best Answer

try this query

$queryCondition .= "WHERE post_at >= '" . $from_date . "' AND post_at <= '" . $to_date . "'";





$reg_user = new USER();
$stmt = $reg_user->runQuery("SELECT * FROM order_details ".$queryCondition);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->execute();

updated code

$post_at = '0000-00-00 00:00:00'; 
$post_at_todate = '0000-00-00 00:00:00';

$queryCondition = "";

    if( !empty( $_POST[ 'post_at' ] ) ) 
{           
    $post_at        = date( 'Y-m-d  h:i:s', strtotime( $_POST[ 'post_at' ] ) );
    $post_at_todate = date( 'Y-m-d' );
    if( !empty( $_POST[ 'post_at_to_date' ] ) )
    {
        $post_at_to_date = date( 'Y-m-d h:i:s', strtotime( $_POST[ 'post_at_to_date' ] ) );
        $post_at_todate = $post_at_to_date;
    }
    $queryCondition .= "WHERE  created_at  >= '" . $post_at . "' AND  created_at  <= '" . $post_at_todate . "'";
}

$sqlquery = "SELECT * FROM order_details $queryCondition";
//$sqlquery = "SELECT * FROM order_details where created_at >= '2016-11-17' AND created_at < = '2016-11-19'";
var_dump( $sqlquery ); 
Related Topic