0

"<?php namespace Rkd\test\Block\Adminhtml\Alltest\Alltest;

use Rkd\Reports\Helper\Data as ReportsHelper;

class GridExports extends \Magento\Backend\Block\Widget\Grid\Extended {

    protected $orderCollectionFactory;
    protected $orderConfig;
    protected $reportshelper;
    public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Helper\Data $backendHelper,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    \Magento\Sales\Model\Order\Config $orderConfig,
    ReportsHelper $reportshelper,
    array $data = []
) {
    $this->reportshelper = $reportshelper;
    $this->orderCollectionFactory = $orderCollectionFactory;
    $this->orderConfig = $orderConfig;
    parent::__construct($context, $backendHelper, $data);
}

public function getHelper()
{
    return $this->reportshelper;
}

public function getFormattedPrice($price)
{
    return $this->getHelper()->getFormattedPrice($price);
}

public function getDateTime() 
{
    return $this->getHelper()->getDateTime();
}

protected function _construct()
{
    parent::_construct();
    $this->setId('freightlogGrid');
    $this->setDefaultSort('freightlog_id');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(true);
}

protected function _prepareCollection()
{
    $ids = $this->getRequest()->getParam('internal_entity_id');
    $ids = explode(",", $ids);
    
    $collection = $this->orderCollectionFactory->create()
        ->join(
            array('soa' => 'sales_order_address'), 
            'soa.parent_id=main_table.entity_id and soa.address_type = "billing"', 
            array(
                'billing_company'=> 'soa.company',
                'billing_postcode'=> 'soa.postcode', 
                'billing_state'=> 'soa.region', 
                'billing_city'=> 'soa.city'
            ), 
            null,
            'left'
        )->join(
            array('soas' => 'sales_order_address'), 
            'soas.parent_id=main_table.entity_id and soas.address_type = "shipping"', 
            array(
                'shipping_company'=> 'soas.company',
                'shipping_zipcode'=> 'soas.postcode', 
                'shipping_state'=> 'soas.region', 
                'shipping_city'=> 'soas.city',
                'shipping_street'=> 'soas.street',
                'shipping_firstname'=> 'soas.firstname',
                'shipping_lastname'=> 'soas.lastname'
            ), 
            null,
            'left'
        );

    $collection->addFieldToFilter('order_source',array('in' => array(\Rkd\Salescustom\Helper\Data::ORDER_TYPE_AMZON,\Rkd\Salescustom\Helper\Data::ORDER_TYPE_FULLFILL_BY_AMZON,\Rkd\Salescustom\Helper\Data::ORDER_TYPE_FULLFILL_BY_SELLER)));
    $collection->addFieldToFilter('entity_id',array('in' => $ids));
    $returnVal = $this->setCollection($collection);
    $this->_prepareTotals('subtotal,tax_amount,shipping_amount,total_refunded,grand_total');
    return $returnVal;
}

protected function _prepareTotals($columns = null){
    $columns=explode(',',$columns);
    if(!$columns){
        return;
    }
    $this->_countTotals = true;   
    $totals = new \Magento\Framework\DataObject();
    $fields = [];
    foreach($columns as $column){
        $fields[$column]    = 0;    
    } 
    foreach ($this->getCollection() as $item) {
        foreach($fields as $field=>$value){
            $fields[$field]+=$item->getData($field);
        }
    }
    $totals->setData($fields);
    $this->setTotals($totals);
    return;
}

protected function _prepareColumns()
{
    $this->addColumn('created_at', array(
    'header' => __('Date'),
    'index' => 'created_at',
    'type' => 'date',
    'width' => '150px',
    'totals_label'      => __('Total')
    ));

    if($this->getRequest()->getActionName() == 'exportxls' || $this->getRequest()->getActionName() == 'exportcsv'  || $this->getRequest()->getActionName() == 'exportpdf') {
        $this->addColumn('real_order_id',
        array(
            'header'    => __('Job Number'),
            'align'     =>'left',
            'index'     => 'increment_id',                  
            'filter'    => false
        ));
    }
    else{
        $this->addColumn('real_order_id',
        array(
            'header'    => __('Job Number'),
            'align'     =>'left',
            'index'     => 'increment_id',
            'width'   => '80px',
            'renderer'  => \Rkd\Reports\Block\Adminhtml\Audit\Audit\Grid\Renderer\Ordernumber::class,
            'filter'    => false
        ));
    }
    $this->addColumn('heared4us',
    array(
        'header'    => __('Reference Number'),
        'align'     =>'left',
        'index'     => 'heared4us',
        'filter'    => false
    ));  

    $this->addColumn('billing_company', array(
        'header' => __('Customer Billing'),
        'index' => 'billing_company',
        'width' => '200px',
        'filter_index'=>'soa.company',
        'renderer'  => \Rkd\Reports\Block\Adminhtml\Audit\Audit\Grid\Renderer\Billname::class,
        'filter'    => false
    ));

    $this->addColumn('shipping_company', array(
        'header' => __('Customer Shipping'),
        'index' => 'shipping_company',
        'width' => '200px',
        'renderer'  => \Rkd\test\Block\Adminhtml\Alltest\Grid\Renderer\Shipname::class,
        'filter'    => false
    ));

    $this->addColumn('shipping_zipcode',
        array(
              'header'    => __('Shipping Zip Code'),
              'align'     =>'left',
              'index'     => 'shipping_zipcode',
              'filter'    => false
      ));

    $this->addColumn('county', array(
        'header' => __('County'),
        'align' => 'right',
        'width' => '150px',
        'renderer'  => \Rkd\Reports\Block\Adminhtml\Audit\Audit\Grid\Renderer\Country::class,
        'filter'    => false,
        'sortable'  => false
    ));
    $this->addColumn('subtotal', array(
        'header' => __('Material Amount'),
        'type' => 'price',
        'index' => 'subtotal',
        'filter'    => false,
        'currency_code' => 'USD'
    ));
    $this->addColumn('tax_amount', array(
        'header' => __('Taxable Amount'),
        'type' => 'price',
        'index' => 'tax_amount',
        'filter'    => false,
        'currency_code' => 'USD'
    ));
    $this->addColumn('shipping_amount', array(
        'header' => __('Shipping'),
        'type' => 'price',
        'index' => 'shipping_amount',
        'filter'    => false,
        'currency_code' => 'USD'
    ));
$this->addColumn('grand_total', array(
        'header' => __('Total Amount'),
        'type' => 'price',
        'index' => 'grand_total',
        'filter'    => false,
        'currency_code' => 'USD'
    ));
    $this->addColumn('total_refunded', array(
        'header' => __('Refunds'),
        'width' => '80px',
        'type' => 'price',
        'index' => 'total_refunded',
        'filter'    => false,
        'currency_code' => 'USD'
    ));

    $this->addExportType('*/*/exportcsv', __('CSV'));
    $this->addExportType('*/*/exportxls', __('Excel'));
    $this->addExportType('*/*/exportpdf', __('PDF'));

    return parent::_prepareColumns();
}

public function getGridUrl()
{
    return $this->getUrl('*/*/grid', array('_current' => true));
}

public function getRowUrl($row)
{
    
}

public function getExcelData(){
    $this->_isExport = true;
    $this->_prepareGrid();
    $this->getCollection()->getSelect()->limit();
    $this->getCollection()->setPageSize(0);
    $this->getCollection()->load();
    $this->_afterLoadCollection();
    $headers = array();
    $data = array();
    foreach ($this->getColumns() as $column) {
        if (!$column->getIsSystem()) {
            $headers[] = $column->getHeader();
        }
    }
    
    $material = 0.00;
    $tax = 0.00;
    $freight = 0.00;
    $refund = 0.00;
    foreach ($this->getCollection() as $item) {
        $row = array();
        $material = $material + $item->getSubtotal();
        $tax = $tax + $item->getTaxAmount();
        $freight = $freight + $item->getShippingAmount();
        $refund = $refund + $item->getTotalRefunded();
        foreach ($this->getColumns() as $column) {
            if (!$column->getIsSystem()) {
                $row[] = $column->getRowField($item);
            }
        }
        $data[] = $row;
    }
    $data[] = array('Total', '','','','','','','','$'.$material,'$'.$tax,'$'.$freight,'$'.$refund);

    if(count($row))
    {
      $fileName = "test.xls";
      $helper = $this->getHelper();
      $helper->initMemberData();
      $helper->addHeader($headers);
      foreach($data as $xl){
        $newRow = array();
        foreach ($xl as $key => $value) {
          array_push($newRow, $value);
        }
        $helper->addRow($newRow);
        unset($newRow);        
      }
      $helper->sendFile($fileName);
      unset($helper);
      
    }
}

public function getPdfData(){
    $this->_isExport = true;
    $this->_prepareGrid();
    $this->getCollection()->getSelect()->limit();
    $this->getCollection()->setPageSize(0);
    $this->getCollection()->load();
    $this->_afterLoadCollection();
    $headers = array();
    $data = array();
    foreach ($this->getColumns() as $column) {
        if (!$column->getIsSystem()) {
            $headers[] = $column->getHeader();
        }
    }
    $material = 0.00;
    $tax = 0.00;
    $freight = 0.00;
    $refund = 0.00;
    $grandtotal = 0.00;
    foreach ($this->getCollection() as $item) {
        $row = array();
        $material = $material + $item->getSubtotal();
        $tax = $tax + $item->getTaxAmount();
        $freight = $freight + $item->getShippingAmount();
        $refund = $refund + $item->getTotalRefunded();
        $grandtotal = $grandtotal + $item->getGrandTotal();
        foreach ($this->getColumns() as $column) {
            if (!$column->getIsSystem()) {
                $row[] = $column->getRowField($item);
            }
        }
        $data[] = $row;
    }
    $data[] = array('Total','','','','','','','$'.$material,'$'.$tax,'$'.$freight,'$'.$grandtotal,'$'.$refund);
   return array($headers,$data);
}

protected function _prepareMassaction()
{
    $this->setMassactionIdField('entity_id');
    $this->getMassactionBlock()->setFormFieldName('entity_id');
    $this->getMassactionBlock()->addItem(
         'addressupdate',
            [
            'label' => __('Import address from PO'),
            'url' => $this->getUrl('test/alltest/orderaddress'),
            'confirm'  => ('Are you sure?')
            ]
     );
     return $this;
}

}"

1 Answer 1

0

You just need to change the explode as php 8.1 doesn't allow null values to be explode as you used two explode in this class:

1. From:

$ids = explode(",", $ids);

To:

$ids = explode(",", $ids ? $ids: "");

2. From:

$columns=explode(',',$columns);

To:

$columns=explode(',',$columns ? $columns: "");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.