2

I'm trying to get some of the values from the collection, below is my code.

<?php 
public function RentalPartnerRevenue()
  { 
    $filter = $this->request->getParam('filter'); 
    $operator_id = $this->request->getParam('operator_id');   
    try{
    $completecollection1 = $this-&gt;_orderCollectionFactory-&gt;create()
                                         -&gt;addAttributeToSelect('*');  

    if(isset($filter) &amp;&amp; $filter  == 'totalrevenue'){
        //$completecollection1-&gt;addAttributeToSelect('grand_total')-&gt;getColumnValues('grand_total');       
        $completecollection1-&gt;getSelect()-&gt;joinLeft(
            'rider_detail as wrd',
            'wrd.booking_id = main_table.entity_id',
            ['entity_id','order_total', 'operator_id']
        )-&gt;order('wrd.created_at DESC')
         -&gt;where('booking_status =&quot;' . self::COMPLETED . '&quot;')
         -&gt;where('operator_id =&quot;' . $operator_id . '&quot;');

        echo $completecollection1-&gt;getSelect()-&gt;__toString();
        //print_r($completecollection1-&gt;getData());

    }elseif(isset($filter) &amp;&amp; $filter  == 'todaysrevenue'){ 

    }elseif(isset($filter) &amp;&amp; $filter  == 'lastmonthrevenue'){ 

    }elseif(isset($filter) &amp;&amp; $filter  == 'lastthreemonthrevenue'){ 

    }elseif(isset($filter) &amp;&amp; $filter  == 'selectcustomdate'){

    }

    if(count($completecollection1-&gt;getData())){   
       $PartnerArray = [];
        foreach ($completecollection1 as $Partnerrevenue) {  
         if(isset($PartnerArray[$Partnerrevenue-&gt;getData('operator_id')])){ 
             $PartnerArray[$Partnerrevenue-&gt;getData('operator_id')] = $PartnerArray[$Partnerrevenue-&gt;getData('operator_id')] + $Partnerrevenue-&gt;getData('grand_total');                 
           }else{
             $PartnerArray[$Partnerrevenue-&gt;getData('operator_id')] = $Partnerrevenue-&gt;getData('grand_total');
           } print_r($PartnerArray); // right data
        } 

    }
}catch (Exception $e) {
    return $this-&gt;setResponse(500, $e-&gt;getMessage(), null);
}

}

OUTPUT

Array
(
[2707173] => 700.0000
)
Array
(
[2707173] => 7500
)
Array
(
[2707173] => 12900
)
Array
(
[2707173] => 18300
)
Array
(
[2707173] => 23700
)
Array
(
[2707173] => 25500
)
Array
(
[2707173] => 35500
)
Array
(
[2707173] => 45500
)
Array
(
[2707173] => 79500
)
Array
(
[2707173] => 113500
)

here 2707173 is operator_id and => 700.000 to 113500 is order_total

now I want to get sum of order_total, but I don't know how can I sum these values. please help me with this.

thanks.

Rushikesh Solanki
  • 909
  • 12
  • 28

2 Answers2

1

Based on my understanding of your requirement I am sharing the below code check if this helps, This will sum up all the values based on the operator id.

if(count($completecollection1->getData())){
    $PartnerArray = [];
    foreach ($completecollection1 as $Partnerrevenue) {
        $PartnerArray[$Partnerrevenue->getData('operator_id')][] = $Partnerrevenue->getData('grand_total');
    }
    $orderTotal = [];
    foreach($PartnerArray as $operatorId => $data) {
        $orderTotal[$operatorId] = array_sum($data);
    }
   return $orderTotal;
}
0

Magento allow us to retrieve all value of a specific column via $collection->getColumnValues('column');, in your case we can get the sum of grand total as per following:

$sumOfTotal = array_sum($completecollection1->getColumnValues('grand_total'));
Toan Nguyen
  • 3,049
  • 1
  • 24
  • 45