KoolReport's Forum

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

DrillDown data and columns needs to fetch outside DrillDown class but it's show variable not defined error. #3061

Open Ankit Agarwal opened this topic on on May 30, 2023 - 2 comments

Ankit Agarwal commented on May 30, 2023
$query_result1= DB::select(DB::raw("SELECT SalesMen.team, SUM(amount) AS Sale,salesmen.teamcolour as Colour
                        FROM dbo.sales AS sales 
                        INNER JOIN dbo.SalesMen AS SalesMen ON sales.salesman = SalesMen.id 
                        left JOIN dbo.tbl_user_sexe ON dbo.tbl_user_sexe.s_exe = salesmen.id
                        cross join universal.dbo.tbl_user 
                        WHERE salesmen.enabled = 'yes' AND salesmen.team != 'NULL'
                        GROUP BY salesmen.team, salesmen.teamcolour"));
$query_result2= DB::select(DB::raw("SELECT salesmen.name, DATENAME(month, sales.docdate) AS Month, SUM(amount) AS Sale,salesmen.teamcolour as Colour
                        FROM dbo.sales AS sales 
                        INNER JOIN dbo.SalesMen AS SalesMen ON sales.salesman = SalesMen.id 
                        left JOIN dbo.tbl_user_sexe ON dbo.tbl_user_sexe.s_exe = salesmen.id
                        cross join universal.dbo.tbl_user 
                        WHERE salesmen.enabled = 'yes' AND salesmen.team != 'NULL' and salesmen.team like '%" . $team . "%'
                        GROUP BY salesmen.team, salesmen.name, DATENAME(month, sales.docdate),salesmen.teamcolour"));
$query_result3 = DB::select(DB::raw("SELECT salesmen.name, DATENAME(month, sales.docdate) AS Month, SUM(amount) AS Sale,salesmen.teamcolour as Colour
                        FROM dbo.sales AS sales 
                        INNER JOIN dbo.SalesMen AS SalesMen ON sales.salesman = SalesMen.id 
                        left JOIN dbo.tbl_user_sexe ON dbo.tbl_user_sexe.s_exe = salesmen.id
                        cross join universal.dbo.tbl_user 
                        WHERE salesmen.enabled = 'yes' AND salesmen.team != 'NULL' and salesmen.name like '%" . $name . "%'
                        GROUP BY DATENAME(month, sales.docdate),salesmen.teamcolour, salesmen.name"));


DrillDown::create(array(
        "name"=>"saleDrillDown",
        "title"=>"Team Sale Report",
        "levels"=>array(
            array(
                "title"=>"All Month",
                "content"=>function($params,$scope)
                {
                    ColumnChart::create(array(
                        "dataSource"=>$query_result1,
                        "columns"=>array(
                            "team"=>array(
                                "label"=>"Team",
                                "type"=>"string",
                            ),
                            "Sale"=>array(
                                "label"=>"Sale Amount",
                                "type"=>"number",
                            )
                        ),
                        "clientEvents"=>array(
                            "itemSelect"=>"function(params){
                                saleDrillDown.next({team:params.selectedRow[0]});
                            }",
                        )
                    ));
                }
            ),
            array(
                "title"=>"Sales Man Sale Report",
                
                "content" => function ($params, $scope) {
                $team = $params["team"];
                ColumnChart::create(array(
                        "dataSource"=>$query_result2,
                        "columns"=>array(
                             "name"=>array(
                                "label"=>"Name",
                                "type"=>"string",
                            ),
                            "Sale"=>array(
                                "label"=>"Sale Amount",
                                "type"=>"number",
                            )
                        ),
                        "clientEvents"=>array(
                            "itemSelect"=>"function(params){
                                 saleDrillDown.next({name:params.selectedRow[0]});
                            }",
                        )
                    ));
                }        
            ),
            array(
                "title"=>"Sales man Month Report",
                "content"=>function($params,$scope)
                {
                    $name = $params["name"];
                    ColumnChart::create(array(
                        "dataSource"=> $query_result3,
                        "columns"=>array(
                            "Month"=>array(
                                "label"=>"Month",
                                "type"=>"string",
                            ),
                            "Sale"=>array(
                                "label"=>"Sale Amount",
                                "type"=>"number",
                            )
                        ),
                    ));
                }                
            )
        ),
    ));
KoolReport commented on May 31, 2023

Please do this:

"content"=>function($params,$scope) use ($query_result1) {
        ...
   }

This is called function scope. You use the same for $query_result2.

Let us know if you need further assistance.

Ankit Agarwal commented on Jun 6, 2023

Drilldown report queries getting from db level1 it's working but level2 also it's trying load when page loads. Suggest me how it should be work. below is my code.

$graphClassName = "\koolreport\drilldown\\" . $graph_name;
    $query1_result = DB::select(DB::raw($query));
    $query1_keys = array_keys((array)$query1_result[0]);
    $param_value1 = $query1_keys[1];
    $query1_columns = [];

    foreach ($query1_keys as $index => $key) {
        $query1_columns[$key] = [
            "label" => ucfirst($key),
            "type" => ($index === 0) ? "string" : "number",
        ];
        }
    $levels = [
    [
        "title" => "All Month",
        "content" => function ($params, $scope) use ($query1_result, $graph_name,$query1_columns,$param_value1) {
            ColumnChart::create([
                "dataSource" => $query1_result,
                "columns" => $query1_columns,
                "clientEvents" => [
                    "itemSelect" => "function(params) {
                        $graph_name.next({'$param_value'1: params.selectedRow[0]});
                    }",
                ],
            ]);
        },
    ],
];

foreach ($drilldown as $drill) {
    $query_result = $drill->drilldown_query;
    $query_keys = array_keys((array)$query_result[0]);
    $param_value = $query_keys[1];
    $query_columns = [];

    foreach ($query_keys as $index => $key) {
        $query_columns[$key] = [
            "label" => ucfirst($key),
            "type" => ($index === 0) ? "string" : "number",
        ];
    }
    $level = [
        "title" => "",
        "content" => function ($params, $scope) use ($query_result, $graph_name,$param_value) {
            ColumnChart::create([
                "dataSource" => $query_result,
                "columns" => $query_columns,
                "clientEvents" => [
                    "itemSelect" => "function(params) {
                        $graph_name.next({'$param_value': params.selectedRow[0]});
                    }",
                ],
            ]);
        },
    ];

    $levels[] = $level;
}

DrillDown::create([
    "name" => $graph_name,
    "title" => "Team Sale Report",
    "levels" => $levels,
]);

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