KoolReport's Forum

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

Calculated Colum errors #2766

Open Francois DISSAIT opened this topic on on Jul 26, 2022 - 2 comments

Francois DISSAIT commented on Jul 26, 2022

Hello, I created (with great help from you) a report using a query with parameters, then Cube. After I pipe to Calculated Columns. I use the long form (function for calculation). When the query parameter is % , large array of data, no problem. If i use a query parameter who gives a smaller array of data, I have msg of error about undefined indexes coming from Cube in the result of Calculated Column *Notice: Undefined index: Detruit in /home/u521225422/domains/phammadmin.com/public_html/reports_phamm/reports/_phamm/test/test.php on line 84

Notice: Undefined index: Perdu in /home/u521225422/domains/phammadmin.com/public_html/reports_phamm/reports/_phamm/test/test.php on line 84

Notice: Undefined index: RemisAMembre in /home/u521225422/domains/phammadmin.com/public_html/reports_phamm/reports/_phamm/test/test.php on line 84* But I get the result after the messages! I thought that using long form with fonction should avoid that issue. Is It an error from me ? Or a bug ? Here is my php program :

public function setup()

{
    $this->src('phamm')
    ->query("
		SELECT
		items.item as Item,
		items.balance as Stock,
		CONCAT (items.item,'. Stock : ',items.balance) as Article,
		batches.quantity AS Quantite,
		batches.etat as Etat
		FROM
		items
		INNER JOIN batches ON (items.id = batches.item)
		WHERE 
		items.item LIKE :Sel_Article
		AND 
		manufacturing_date BETWEEN :start and :end  
		GROUP BY
		items.item,
		batches.etat
		")			
	->params(array(
		":Sel_Article"=>$this->params["Sel_Article"],
		":start"=>$this->params["dateRange"][0],
        ":end"=>$this->params["dateRange"][1],
		))	
	->pipe(new Sort(array(
	"Article"=>"asc"
	)))		
	->pipe(new Cube(array(
    "rows" => "Article", 
    "columns" => "Etat",
    "sum" => "Quantite",
	)))		
__	->pipe(new CalculatedColumn(array(
		"Sortis"=>array(
		"exp"=>function($data){
		return $data["Expedie"]+$data["Detruit"]+$data["Perdu"]+$data["RemisAMembre"];
			},
		"type"=>"string",
		)
			)))__
	->pipe(new RemoveColumn(array(
        "Expedie","Detruit","Perdu","RemisAMembre","{{all}}"
    )))
	->pipe(new CalculatedColumn(array(
		"Balance"=>array(
		"exp"=>function($data){
		return $data["Prepare"]-$data["Sortis"];
			},
		"type"=>"string",
		)
			)))
	->pipe(new ColumnMeta(array(		
        "Prepare"=>array(
            "type"=>"number"),
		"Promis"=>array(
            "type"=>"number"),			
		"Sortis"=>array(
            "type"=>"number"),
		"Balance"=>array(
            "type"=>"number"),
		                )))
	
	->pipe(new \koolreport\processes\Map(array(
	"{meta}" => function($meta) {			
        $meta["columns"]["Prepare"] = ["label" => "Rentres"];
        $meta["columns"]["Promis"] = ["label" => "Promis"];
		$meta["columns"]["Sortis"] = ["label" => "Sortis"];
		$meta["columns"]["Balance"] = ["label" => "Balance"];
    return $meta;				
    }
)))
	->pipe($this->dataStore("stocks"));
}

}

I tried type "number" for the calculated column "Sortis", no way !

There is no emergency with this issue ... enjoy summer ! Kind Regards François

Sebastian Morales commented on Jul 27, 2022

Hi François, columns after Cube process are created dynamically based on data. If you use fewer data rows, there's a chance some column such as "Detruit" won't be created. To solve this case you only need to check for isset($data["Detruit"]) or use null coalescing operator (??) like this:

		return ($data["Expedie"] ?? 0)+($data["Detruit"] ?? 0)+($data["Perdu"] ?? 0)+($data["RemisAMembre"] ?? 0);
Francois DISSAIT commented on Jul 28, 2022

Hi Sebastian, It works perfectly. Thanks ...

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
solved

None