KoolReport's Forum

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

DateRange - defaultParamValues: how to have previously selected dates? #2975

Open AhmedHaroon opened this topic on on Feb 8, 2023 - 8 comments

AhmedHaroon commented on Feb 8, 2023

i am using KoolReport with CodeIgniter 4.

i want to have dates in dateRange values which user entered to run report as it always initialized to defaultParamValues which is correct but How I can have the defaultParamValues of dateRange as previously entered dateRange retain within current session for all other reports which contains dateRage as parameter. please help.

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\amazing\Theme;
    use \koolreport\codeigniter\Friendship;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;

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

    protected function bindParamsToInputs()
    {
        return array(
            "dateRange",
            "riders",
        );
    }
    
    public function setup()
    {
        $node = $this->src("default")
        ->query("SELECT id, DATE(order_date) AS order_date, id order_no, orderDay, orderMonth, orderYear, orderQuarter, rider_id, rider_name, cast(ridCharges AS SIGNED) AS ridCharges, cast(gvm AS SIGNED) AS gvm, customer_name,  user_id, order_status, rider_id, rider_name
         FROM total_orders 
         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"],
            // ":customers"=>$this->params["customers"]
        ));

        $node->pipe(new ColumnMeta(array(
            "id" => array(
                "type" => "number",
                "label" => "Orders"
            ),
            "ridCharges" => array(
                "type" => "number",
                "label" => "DC"
            ),
            "gvm" => array(
                "type" => "number",
                "label" => "GVM"
            ),
        )))
        ->saveTo($node2);

        $aa = $node2->pipe(new Pivot(array(
            "dimensions" => array(
                "column" => "orderYear",
                "row" => "rider_name, customer_name, order_date",
            ),
            "aggregates" => array(
                "count" => "id",
                "sum" => array("ridCharges", "gvm"),
            ),
        )))->pipe($this->dataStore('customersData'));
        
        $this->src("default")->query("
            SELECT DISTINCT 
                rider_id,
                rider_name
            FROM
                rider_summary
            ORDER BY rider_name
        ")
        ->pipe($this->dataStore("riders"));
    }
}

regards

Sebastian Morales commented on Feb 9, 2023

If you want to retain current session's date range value pls follow these steps:

1 . Start php session at the start, for example at the beginning of index.php:

<?php
    session_start();

2 . In your report file, use session's date range if available in function defaultParamValues() and set the session value in function setup():

    protected function defaultParamValues()
    {
        $start = isset($_SESSION["startDate"]) ? $_SESSION["startDate"] : date("2022-12-01");
        $end = isset($_SESSION["startDate"]) ? $_SESSION["endDate"] : date("2022-12-01");
        return array(
            "dateRange"=>array($start ,$end),
            "riders"=>NULL,
        );
    }

    public function setup()
    {
        $_SESSION["startDate"] = $this->params["dateRange"][0];
        $_SESSION["endDate"] = $this->params["dateRange"][1];
        ...

Hope this helps.

AhmedHaroon commented on Feb 9, 2023

thanks Sebastian Morales

please check code below if it is correct and will work ( especially query's where clause and below the query statement ), please guide if anything wrong here.

    protected function defaultParamValues()
    {
        $start = isset($_SESSION["startDate"]) ? $_SESSION["startDate"] : date("2022-12-01");
        $end = isset($_SESSION["startDate"]) ? $_SESSION["endDate"] : date("2022-12-01");
        return array(
            "dateRange"=>array($start ,$end),
            "riders"=>NULL,
        );
    }

    protected function bindParamsToInputs()
    {
        return array(
            "dateRange",
            "riders",
        );
    }
    
    public function setup()
    {
        $node = $this->src("default")
		$_SESSION["startDate"] = $this->params["dateRange"][0];
        $_SESSION["endDate"] = $this->params["dateRange"][1];
        ->query("SELECT id, DATE(order_date) AS order_date, id order_no, orderDay, orderMonth, orderYear, orderQuarter, rider_id, rider_name, cast(ridCharges AS SIGNED) AS ridCharges, cast(gvm AS SIGNED) AS gvm, customer_name,  user_id, order_status, rider_id, rider_name
         FROM total_orders 
         WHERE
                    rider_id = ".($this->params["riders"] == '' ? 'rider_id' : ':riders')."
                    AND
                    order_date >= :startDate
                    AND
                    order_date <= :endDate")
        ->params(array(
            ":startDate"=>$this->params["dateRange"][0],
            ":endDate"=>$this->params["dateRange"][1],
            ":riders"=>$this->params["riders"],
        ));

regards

AhmedHaroon commented on Feb 9, 2023

thanks a bunch Sebastian Morales the code is working fine on my local Windows machine, will deploy on staging server and hope all things will work fine. just a query, is there any chance of problem of PHP version? i mean this will work on all PHP + CI4 or there is any version restriction? regards

AhmedHaroon commented on Feb 9, 2023

very strange error... after successfully run of above RiderSummarry report i modified another report CustomerSummary for same, when deployed on staging server it is showing as below ( screenshot ). i failed to understand, it was showing data before these changes implemented. i have no clue. above mentioned report is working fine on staging server ( it has Ubuntu OS ), please check below my code and screenshot.

CustomerSummary.php

<?php
namespace App\reports;

// we have copied below file in root path of our app 
require_once ROOTPATH . "load.koolreport.php";

use \koolreport\KoolReport;
use \koolreport\processes\Sort;
use \koolreport\processes\Map;
use \koolreport\processes\Limit;
use \koolreport\processes\Filter;
use \koolreport\cube\processes\Cube;
use \koolreport\pivot\processes\Pivot;

class CustomerSummary extends \koolreport\KoolReport
{
    //use \koolreport\bootstrap4\Theme;
    use \koolreport\amazing\Theme;
    use \koolreport\codeigniter\Friendship;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
    use \koolreport\export\Exportable;
    use \koolreport\excel\ExcelExportable;
    use \koolreport\excel\BigSpreadsheetExportable;

    protected function defaultParamValues()
    {
        $start = isset($_SESSION["startDate"]) ? $_SESSION["startDate"] : date("2022-12-01");
        $end = isset($_SESSION["startDate"]) ? $_SESSION["endDate"] : date("2022-12-01");
        return array(
            "dateRange"=>array($start ,$end),
            "customers"=>NULL,
        );
    }

    protected function bindParamsToInputs()
    {
        return array(
            "dateRange",
            "customers",
        );
    }

    function setup()
    {
        $_SESSION["startDate"] = $this->params["dateRange"][0];
        $_SESSION["endDate"] = $this->params["dateRange"][1];
        $this->src("default")
        ->query("SELECT DATE(order_date) AS order_date, user_id, customer_name, rides,rider_charges, order_total FROM customer_summary WHERE
                    user_id = ".($this->params["customers"] == '' ? 'user_id' : ':customers')."
                    AND
                    order_date >= :startDate
                    AND
                    order_date <= :endDate")
        ->params(array(
            ":startDate"=>$this->params["dateRange"][0],
            ":endDate"=>$this->params["dateRange"][1],
            ":customers"=>$this->params["customers"],
        ))
        ->pipe(Sort::process([
            "order_date"=>"desc"
        ]))
        ->pipe($this->dataStore("result"));
        $this->src("default")->query("
            SELECT DISTINCT 
                user_id,
                customer_name
            FROM
                customer_summary
            ORDER BY customer_name
        ")
        ->pipe($this->dataStore("customers"));
    }
}

Screenshot:

regards

Sebastian Morales commented on Feb 10, 2023

This is a report's data dumping page which most likely happens when a report view could not be found. In case you use Linux servers pls make sure the report view file name matches its setup file name case sensitively. For example: a "SalesReport.php" class file must have a "SalesReport.view.php" view file, not "Salesreport.view.php". In linux, a.php is different from A.php.

AhmedHaroon commented on Feb 12, 2023

thank you @Sebastian Morales

as mentioned, this report was running / rendering perfectly before this change, so, there is no filename issue.

i just modified the report as per your guidance to have dateRange in session variables for next use within current session and nothing else. my report OrderSummary with same changes is rendered perfectly and showing the requested data but CustomerSummary is showing this (screenshot above completely - $this->dataStore('result') in screenshot is to render all customer related data and $this->dataStore('customers') is to show list of customers in dropdown list input- ) instead of showing the data.

hope that cleared. regards

Sebastian Morales commented on Feb 14, 2023
AhmedHaroon commented on Feb 14, 2023

@Sebastian Morales thank, will check and confirm on that post. 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
help needed

None