Shipment Export using mysql join in magento2

$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','created_at','order_increment_id','order_created_at','shipping_name','total_qty'])->join(['so'=>'sales_order'],
           "mt.order_id = so.entity_id",
           [
               'brand_box' => 'so.brand_box',
               'ship_weight' => 'so.ship_weight',
           ]
       )->join(['sst'=>'sales_shipment_track'],
           "so.entity_id = sst.order_id",
           [
               'shipper' => 'sst.title',
               'awb' => 'sst.track_number',
           ]
       );
       $data = $connection->fetchAll($select);
       $newAr = [];
       foreach ($data as $values) {
         $newAr[$values['increment_id']][0] = $values;
       }     
       $output = fopen("php://output",'w') or die("Can't open php://output");
       header("Content-Type:application/csv"); 
       header("Content-Disposition:attachment;filename=shipmentexport.csv"); 
        fputcsv($output, array('Shipment #','Date Shipped','Order #','Order Date','Ship to Name','Total Qty','Brand Box','Shipper','AWB NO.','Weight'));
       
        foreach ($newAr as $key => $newVal) {
            foreach ($newVal as $newv) {
              fputcsv($output, $newv);
            }         
        }
        fclose($output) or die("Can't close php://output");

Comments