KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Pivot report: facing problem to have figures #2948

Open AhmedHaroon opened this topic on on Jan 17, 2023 - 6 comments

AhmedHaroon commented on Jan 17, 2023

i am struggling to learn KoolReport and created 2 reports like OrderList examples with my own tables and added daterange and customer parameters to filter data. now i am trying to create a Pivot report refering example report Bun Template , this is first attempt and definitely i have to consult here with seniors but before asking i have to struggle first to achieve. please check code below and advise as it render but only showing order_date on row other 3 columns are null, and also showing error below first page "Sorry, we found no orders." (it is custom message in case no data found). hope i am going in right direction with the help of seniors here. please help.

current output is as below:

the required result is in screenshot:

RiderSummary.php

<?php
namespace App\Reports;

require_once ROOTPATH . "load.koolreport.php";

use \koolreport\pivot\processes\Pivot;
use \koolreport\pivot\processes\PivotExtract;
use \koolreport\processes\ColumnMeta;
use \koolreport\processes\Filter;

use \koolreport\KoolReport;
use \koolreport\processes\Sort;

class RiderSummary extends \koolreport\KoolReport
{
    //use \koolreport\bootstrap4\Theme;
    use \koolreport\amazing\Theme;
    use \koolreport\codeigniter\Friendship;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;

    protected function defaultParamValues()
    {
        return array(
            "dateRange"=>array(
                "2022-12-01",
                "2022-12-10",
            ),
            "riders"=>NULL,
        );
    }

    protected function bindParamsToInputs()
    {
        return array(
            "dateRange"=>"dateRange",
            "riders"=>"riders",
        );
    }

    public function setup()
    {
        $node = $this->src("default")
        ->query("SELECT DATE(order_date) AS order_date, rider_id, rider_name, no_of_orders, rider_charges AS dc, order_total AS gvm FROM rider_data WHERE
                    rider_id = ".($this->params["riders"] == '' ? 'rider_id' : ':riders')."
                    AND
                    order_date >= :start
                    AND
                    order_date <= :end")
        ->params(array(
            ":start"=>$this->params["dateRange"][0],
            ":end"=>$this->params["dateRange"][1],
            ":riders"=>$this->params["riders"]
        ));
        // $node->pipe(new Filter(array(
        //     array('order_date', '>', '0'),
        //     array('rider_name', '>', 'A'),
        // )))
        $node->pipe(new ColumnMeta(array(
            "no_of_orders" => array(
                'type' => 'number',
            ),
            "dc" => array(
                'type' => 'number',
            ),
            "gvm" => array(
                'type' => 'number',
            ),
        )))
        ->saveTo($node2);

        $node2->pipe(new Pivot(array(
            "dimensions" => array(
                "column" => "rider_name",
                "row" => "order_date",
            ),
            "aggregates" => array(
                "count" => "no_of_orders",
                "sum" => "dc",
                "sum" => "gvm",
            ),
        )))->pipe($this->dataStore('riderSales'));

        $this->src("default")->query("
            SELECT DISTINCT 
                rider_id,
                rider_name
            FROM
                rider_summary
            ORDER BY rider_name
        ")
        ->pipe($this->dataStore("riders"));
        // echo $this->endSection();
    }
}

RiderSummary.view.php

<?php
use \koolreport\pivot\widgets\PivotTable;
use \koolreport\pivot\widgets\PivotMatrix;

//use \koolreport\datagrid\DataTables;
//use \koolreport\bootstrap4\Theme;

//use \koolreport\amazing\Theme;
// use \koolreport\clients\Bootstrap;
use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\DateRangePicker;
use \koolreport\inputs\Select2;

?>
<style>
    .amazing {
        width: 40%;
    }
    .select2 {
        width: 100% !important;
    }

    .sidebar {
        background: #ffffff !important;
    }
</style>
<div class='report-content'>
    <div class="text-center">
        <h2>Rider Summary</h2>
    </div>
    
    <form method="post">
        <div class="row">
            <div class="col-md-8 offset-md-2">
                <div class="form-group" style="display: flex; flex-direction: row; flex-wrap: nowrap; align-content: center; justify-content: center; align-items: center;">
                    <?php
                        DateRangePicker::create(array(
                            "name"=>"dateRange",
                            "format"=>"YYYY-MM-DD"
                        ))
                    ?>
                <!-- </div>
                <div class="form-group" style="display: flex; flex-direction: column; flex-wrap: nowrap; align-content: center; justify-content: center; align-items: center;"> -->
                <strong>Select Rider</strong>
                <?php
                Select2::create(array(
                    "name"=>"riders",
                    "dataStore"=>$this->dataStore("riders"),
                    "defaultOption"=>array(
                        "All"=>NULL
                    ),
                    "dataBind"=>array(
                        "text"=>"rider_name",
                        "value"=>"rider_id",
                    ),
                    "attributes"=>array(
                        "class"=>"form-control",
                    )
                ));
                ?>

                <div class="form-group text-center">
                    <button class="btn btn-success"><i class="glyphicon glyphicon-refresh"></i> Load</button>
                </div>
                </div>
            </div>
        </div>
    </form>
</div>
<div>
    <?php
    if($this->dataStore("riderSales")->countData()>0)
    {
                    $dataStore = $this->dataStore('riderSales');
                    PivotTable::create(array(
                        "name" => "PivotTable1",
                        'template' => 'PivotTable-Bun',
                        "dataStore" => $dataStore,
                        "rowDimension" => "row",
                        "columnDimension" => "column",
                        "measures"=>array(
                            "no_of_orders - count",
                            "dc - sum",
                            "gvm - sum",
                        ),
                        'rowSort' => array(
                            'order_date' => 'desc',
                        ),
                        'rowCollapseLevels' => array(1),
                        'columnCollapseLevels' => array(0),
                        'width' => '100%',
                    ));
    }
   else
    {
        ?>
            <div class="alert alert-warning">
                <i class="glyphicon glyphicon-info-sign"></i> Sorry, we found no orders.
            </div>
        <?php    
    }
    ?>
</div>

regards

Sebastian Morales commented on Jan 17, 2023

Pls remove the "measures" property in PivotTable for all the measures to be automatically shown:

                        // "measures"=>array(
                            // "no_of_orders",
                            // "dc",
                            // "gvm",
                        // ),

In case you want to show specific measures, its name should be in the format "{field_name} - {aggregate}". For example:

                        "measures"=>array(
                            "no_of_orders - count",
                            "dc - sum",
                            "gvm - sum",
                        ),
AhmedHaroon commented on Jan 17, 2023

@Sebastian Morales thanks for this help. now it is showing data but column dc is still null and also on bottom of page showing error "Sorry, we found no orders.", can i show the modified code? may be this will help. regards

AhmedHaroon commented on Jan 17, 2023

now that error on bottom of page has gone but column dc is still null... any idea? i have updated my code in OP here.

last output is here and can be seen that for riders there is center column ( which is dc ) is null and off course in Total it is null too.

AhmedHaroon commented on Jan 18, 2023

i have checked the column dc in SQLyog, it contains data, data is numeric and coming from a view but not showing in report. please help where i am making mistake?

regards

Sebastian Morales commented on Jan 18, 2023

Pls make sure the column "dc" has non null values. I would suggest piping to a datastore directly without a Pivot process. Then print out that datastore's data to check for "dc" column value.

AhmedHaroon commented on Jan 18, 2023

thank you @Sebastian Morales for your contnuous support here to guide us.

problem resolved with modification of code as below: i used both column array in "sum" = array()

"aggregates" => array(
                "count" => "no_of_orders",
                "sum" => array("dc", "gvm"),
            ),

now it is showing data for dc column.

regards

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
solved

None