Order report by customer magento2

<?php


use Magento\Framework\App\Bootstrap;


require __DIR__ . '/app/bootstrap.php';


$params = $_SERVER;


$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$now = new \DateTime();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//echo "<pre>"; print_r($orderCollection->getData());
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 echo '<a href ="vermacsv.html"> BACK </a> to change the date';

     $date1 = $_POST['from'];
     $date2 = $_POST['to'];

}else{
 echo '<a href ="vermacsv.html">Click here </a> to print the details';die;
}
try

  $time = time();
  $to = date($date1);
  $from = date($date2);
  $customercoll = $objectManager->create('\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory');
  $customerObj = $customercoll->create()->addFieldToSelect(array('*'));
  $customerObj->addFieldToFilter('created_at', ['lteq' => $from])->addFieldToFilter('created_at', ['gteq' => $to]);
 ?> 
 <style type="text/css">
  table thead th{
   background: #333333;
   color: #ffffff;   
  }
  table thead th,
  table tbody td{
   font-size: 12px;
  }
 </style>
 <table width="700px" cellpadding="1" cellspacing="0" border="1">
  <thead>
   <tr>
    <th>Email</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Uid</th>
    <th>Zip</th>
    <th>City</th>
    <th>State</th>
    <th>Country</th>
    <th>Dob</th>
    <th>Doby</th>
    <th>Gender</th>
    <th>Age</th>
    <th>Value</th>
   </tr>
  </thead>
  <tbody>
   <?php
   $order_skus_array = array();  
   foreach ($customerObj as $customer_data) {
     $cemail = $customer_data->getData('email');
     $OrderFactory = $objectManager->create('Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
     $orderCollection = $OrderFactory->create()->addFieldToSelect(array('*'));
     $orderCollection->addAttributeToFilter('customer_email', $cemail);
     $grandtotal =[];
        foreach ($orderCollection as $order_data) {
          $grandtotal[] = $order_data['grand_total'];
        }
     $finalValue = array_sum($grandtotal);
     $gender = $customer_data->getData('gender');
    if($gender == '0'){
     $gender = '-';
    }elseif ($gender == '1') {
     $gender = 'Male';
   
    }elseif ($gender == '2') {
     $gender = 'Female';
    
    }else{
     $gender = '-';
    }    
    $addresses = $customer_data->getAddresses();
          $country =  '';
          $city =  '';
          $region =  '';
          $state =  '';
          $zip =  '';
    if(count($addresses)) {
      foreach($addresses as $addresse) {
          $country =  $addresse->getCountryId();
          $city =  $addresse->getCity();
          $region =  $addresse->getRegionId();
          $state =  $addresse->getRegion();
          $zip =  $addresse->getPostcode();
       }
    }    

   ?>
    <tr>
     <td><?php echo $customer_data->getData('email'); ?></td>
     <td><?php echo $customer_data->getData('firstname'); ?></td>
     <td><?php echo $customer_data->getData('lastname'); ?></td>
     <td><?php echo $customer_data->getData('entity_id'); ?></td>
     <td><?php echo $zip; ?></td>
     <td><?php echo $city; ?></td>
     <td><?php echo $state; ?></td>
     <td><?php echo $country; ?></td>
     <td><?php echo $customer_data->getData('dob'); ?></td>
     <td><?php echo $customer_data->getData('dob'); ?></td>
     <td><?php echo $gender; ?></td>
     <td><?php echo ''; ?></td>
     <td><?php echo $finalValue; ?></td>
    </tr>
   <?php
   unset($order_skus_array);   
   }
   ?>
  </tbody>
 </table>  
 <?php        
}catch (Exception $e){
 //Mage::log($e->getMessage(),null,'custom_order_export.log');
 print_r($e->getMessage());
}
?>

Comments