Question

How can I Get Refunded Orders And Canceled Orders ( Without invoice being generated ) in one grid and apply date filter on which the refund/canceled orders will be generated ?

Was it helpful?

Solution

I get the collection of canceled orders ( with no invoice ) and the refunded order by applying left join on the tables sales_order & sales_creditmemo

Here is my function in the collection class

//main_table is sales_order
protected function _beforeLoad()
{
    parent::_beforeLoad();

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->create('\Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $this->getSelect()->joinLeft(
        ['ot'=>$connection->getTableName('sales_creditmemo')],
        "main_table.entity_id = ot.order_id"
            )->reset(\Zend_Db_Select::COLUMNS)
            ->columns([
                        'main_table.entity_id as entity_id',
                        'main_table.increment_id as increment_id',
                        'main_table.updated_at',
                        'main_table.created_at',
                        'main_table.status as status'
                        ]);

    $this->getSelect()->order(array('main_table.entity_id DESC'));
    $this->getSelect()->group('main_table.entity_id');
    $status = array('canceled','closed');
    $this->addFieldToFilter('main_table.status' ,array("in" => array($status)));
    return $this;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top