Magento2 Join sql query

<?php

namespace Greenhonchos\GstInvoice\Controller\Adminhtml\Shipment;

//use Magento\Framework\Model\Resource\Db\Collection\AbstractCollection;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Backend\App\Action\Context;
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Framework\App\Filesystem\DirectoryList;
class Mainifest extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
{
  const ENCLOSURE = '"';
  const DELIMITER = ',';

  protected $_directoryList;
  public $_resource;
  private $deploymentConfig;
  private $objectManager;
  public function __construct(Context $context,
  ResourceConnection $resource,
  Filter $filter, CollectionFactory $collectionFactory,DeploymentConfig $deploymentConfig,DirectoryList $directory_list)
    {

    $this->_resource = $resource;
    parent::__construct($context , $filter);
    $this->deploymentConfig = $deploymentConfig;
    $this->collectionFactory = $collectionFactory;
    $this->_directoryList = $directory_list;
$this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();

  }
    /**
     * 
     *
     * @param AbstractCollection $collection
     * @return \Magento\Backend\Model\View\Result\Redirect
     */
    protected function massAction(AbstractCollection $collection)
    {
      $data = $this->getRequest()->getParams();
      $fromdate = $data['filters']['created_at']['from'];
      $todate = $data['filters']['created_at']['to'];
     
     $toarray = explode("/",$todate);
     $fromarray = explode("/",$fromdate);
     $to = $toarray[2].'-'.$toarray[0].'-'.$toarray[1].' '.'23:59:59';
     $from = $fromarray[2].'-'.$fromarray[0].'-'.$fromarray[1].' '.'00:00:01';
     // echo $to.'==='.$from;die;
      
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
       $connection = $resource->getConnection();
       $select = $connection->select()
           ->from(
               ['mt' => 'sales_shipment_grid'],['increment_id','order_increment_id','shipping_name','shipping_address','payment_method','shipping_address'])->join(['so'=>'sales_order'],
           "mt.order_id = so.entity_id",
           [  
               'brand_box' => 'so.brand_box',
               'ship_weight' => 'so.ship_weight',
               'cod_amount' => 'so.msp_cod_amount',
           ]
       )->join(['sst'=>'sales_shipment_track'],
          "mt.entity_id = sst.parent_id",
           [
              'awb' => 'sst.track_number',
           ]
       )->join(['ssi'=>'sales_shipment_item'],
           "mt.entity_id = ssi.parent_id",
           [
              'name' => 'ssi.name',
              'price' => 'ssi.price',
           ]
       )->join(['soa'=>'sales_order_address'],
           "mt.customer_email = soa.email",
           [
              'city' => 'soa.city',
              'region' => 'soa.region',
              'country' => 'soa.country_id',
              'postcode' => 'soa.postcode',
              'telephone' => 'soa.telephone',
              'mobile' => 'soa.telephone',
           ]
       )->where('mt.created_at <= ?', $to)->where('mt.created_at >= ?',$from);
       
      
       $data = $connection->fetchAll($select);
     
       $newAr = [];
       foreach ($data as $values) {
         $newAr[$values['increment_id']][0] = $values;
       }
        $add = array('RETURN ADDRES'=>'Test address','LENGTH  (inch)'=>'','BREATH (inch)'=>'','HEIGHT (inch)'=>'');
       $output = fopen("php://output",'w') or die("Can't open php://output");
       header("Content-Type:application/csv"); 
       header("Content-Disposition:attachment;filename=Mainifest.csv"); 
       fputcsv($output, array('Shipment ID','Order ID','CONSGNEE NAME','ADDRESS','PAYMENT MODE (COD/PREPAID)','Brand Box','Ship weight','COD AMOUNT','WAYBILL ORDER NO','PRODUCT','PACKAGE AMOUNT','CITY','STATE','COUNTRY','PINCODE','PHONE','MOBILE','RETURN ADDRES','LENGTH  (inch)','BREATH (inch)','HEIGHT (inch)'));
      
        foreach ($newAr as $key => $newVal) {
         

           foreach ($newVal as $newv) {
             $finalarr = array_merge($newv,$add);
             fputcsv($output, $finalarr);
            }
         
        }

       fclose($output) or die("Can't close php://output"); 

    }
}

Comments