KoolReport's Forum

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

CSV export with exception #2924

Closed Christoph Paschen opened this topic on on Dec 30, 2022 - 3 comments

Christoph Paschen commented on Dec 30, 2022

Hi there!

PDF and image exports are working fine. Now I am trying to get the csv and excel export working. I'm getting an Exception("Call undefined csv() method") and I don't know exactly why. Can you please show me where my mistake is? My App (in parts):

class App extends Application
{
    ...

    protected function export()
    {
        return ExportHandler::create()
            ->storage(dirname(__DIR__) . "/tmp")
            ->pdfEngine(
                LocalExport::create()
                    ->defaultConfig([
                        "format" => "A4",
                        "margin" => "1in",
                        "orientation" => "portrait"
                    ])
            )
            ->jpgEngine(
                LocalExport::create()
            )
            ->pngEngine(
                LocalExport::create()
            )
            ->csvEngine(
                CSVEngine::create()
                ->defaultConfig([
                    "BOM" => false,
                    'useLocalTempFolder' => true,
                ])
            )
            ->csvEngine(
                XLSXEngine::create()
            );
    }
}

my Report.class

class SalesCommonReport extends KoolReport 
{
    use \koolreport\excel\CSVExportable;
    use \koolreport\excel\ExcelExportable;

    protected function settings()
    {
        return array(
            'dataSources' => array(
                'db' => array( ... )
            )
        );
    }

    protected function setup() 
    {
        $p = $this->params;

        $dateStart = date('Y-m-d').' 00:00:00';
        $dateEnd = date('Y-m-d').' 23:59:59';
        if(isset($p["dateRange"]) && is_array($p["dateRange"])) {
            if(isset($p["dateRange"][0])) {
                $dateStart = date('Y-m-d H:i:s', strtotime($p["dateRange"][0]));
            }
            if(isset($p["dateRange"][1])) {
                $dateEnd = date('Y-m-d H:i:s', strtotime($p["dateRange"][1]));
            }
        }

        ...

        $this->src('db')->query("
        SELECT DISTINCT
        ...
        ")->pipe(
            new CalculatedColumn(array(
                ...
            ))
        )->pipe(
            $this->dataStore('_salesCommonResult')
        );
    }
}

my widget

class SalesCommonResult extends KWidget 
{
    //protected $allowExport = false;

    protected function dataSource()
    {
        $report = new SalesCommonReport(array(
            "dateRange" => $this->sibling("VDateRangePicker")->value(),
        ));
        return $report->run()->dataStore("_salesCommonResult");
    }

    protected function onCreated()
    {
        $this
        ->use(\koolreport\widgets\koolphp\Table::class)
        ->settings([
            "cssClass"=>array(
                "table"=>"table table-bordered table-sm"
            ),
            "emptyValue" => "",
            "tableSmall"=>true,
            "showFooter"=>true,
            "columns"=>array(
                "articleName"=>array(
                    "label"=>"Artikel",
                    "footerText"=>"<b>Gesamt</b>",
                ),
                ...
                "amountGross"=>array(
                    "label"=>"Brutto",
                    'valType' => 'float',
                    'type' => 'number',
                    "decimals"=>2,
                    "cssStyle"=>"text-align:right",
                    "footer"=>"sum",
                    "footerText"=>"<b>@value</b>"
                )
            ),
            'grouping' => array(
                "groupName" => array(
                    "calculate"=>array(
                        "{sumAmountGross}"=>array("sum","amountGross")
                    ),
                    "top"=>"<b>{groupName}</b>",
                    "bottom"=>'<td colspan="8"><b>Gesamt</b></td>
                        <td style="text-align:right"><b>{sumAmountGross}</b></td>'
                ),
            ),
        ]);
    }

    protected function fields() {
        return array(
            Text::create("Artikel")
                ->colName("articleName"),
            ...
            Number::create("Brutto")
                ->colName("amountGross")
                ->decimals(2)
        );
    }

    public function dataView()
    {
        $dataView = parent::dataView();
        $fields = $this->fields();
        return $dataView->fields($fields);
    }    
}

my board

class SalesCommonBoard extends Dashboard
{
    protected function content()
    {
        return [
            Row::create()->sub([
                Panel::create()
                ->header("<b>".Lang::t("Filter options")."</b>")
                ->sub([
                    Row::create([
                        [
                            Html::label(Lang::t("Time period")),
                            VDateRangePicker::create(),
                        ],
                    ]),
                    Row::create([
                        Inline::create([
                            RequestResultButton::create()
                                ->type("primary")
                                ->icon("fa fa-eye")
                                ->text(Lang::t("View"))
                                ->laddaOnAction(true),

                            Dropdown::create("exporting")
                            ->title("Export")
                            ->type("success")
                            ->icon("fa fa-file-download")
                            ->items([
                                Dropdown::menuItem()
                                    ->text("PDF")
                                    ->icon("fa fa-file-pdf")
                                    ->onClick(
                                        Client::widget("Result")->exportToPDF()
                                    ),
                                Dropdown::menuItem()
                                    ->text("Excel")
                                    ->icon("fa fa-file-excel")
                                    ->onClick(
                                        Client::widget("Result")->exportToXLSX("SalesCommon")
                                    ),
                                Dropdown::menuItem()
                                    ->text("CSV")
                                    ->icon("fa fa-file-csv")
                                    ->onClick(
                                        Client::widget("Result")->exportToCSV("SalesCommon")
                                    ),
                                Dropdown::menuItem()
                                    ->text("Image PNG")
                                    ->icon("fa fa-file-image")
                                    ->onClick(
                                        Client::widget("Result")->exportToPNG()
                                    ),
                                Dropdown::menuItem()
                                    ->text("Image JPG")
                                    ->icon("fa fa-file-image")
                                    ->onClick(
                                        Client::widget("Result")->exportToJPG()
                                    ),
                            ]),
                        ]),
                    ]),
                ]), 
            ]),
            Row::create()->sub([
                Panel::create()
                ->sub([
                    Row::create([
                        SalesCommonResult::create("Result")
                        ->pdfExportable( [
                            "format"=>"A4",
                            "orientation"=>"landscape",
                            "zoom" => 0.5,
                            ]
                        )
                        ->pngExportable(true)
                        ->jpgExportable(true)
                        ->csvExportable(true)
                        ->xlsxExportable(true)
                    ]),
                ]),
            ]),
        ];
    }
}

Thank you in advance

Sebastian Morales commented on Jan 3, 2023

I think there's some typo in App's export function:

            ->csvEngine(
                CSVEngine::create()
                ->defaultConfig([
                    "BOM" => false,
                    'useLocalTempFolder' => true,
                ])
            )
            ->csvEngine(
                XLSXEngine::create()
            ); 

csvEngine is duplicated and in the later call it uses an XLSXEngine which should not be used for CSV export. Pls fix this (either remove the duplicate or change it to xlsxEngine(...)) and see if CSV export works. Tks,

Christoph Paschen commented on Jan 3, 2023

Thank you very much! Renaming the second "csvEngine" entry was the trick. Now the CSV and Excel exports works. currently with an empty file, but i think this is caused by the kind of field declaration. i will try to change it.

PS: Please have a look at and maybe change your documentation at https://www.koolreport.com/docs/dashboard/export/#setup-exporting-xlsxengine. In the code exaple the Excel-engine will declared with "->csvEngine".

Sebastian Morales commented on Jan 4, 2023

Christoph, it's great to know you fixed the issue! And thank a lot for your feedback, the Dashboard export documentation has been updated accordingly.

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

Dashboard