KoolReport's Forum

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

Grouping and sum total not working. any advice on what im doing wrong would be appreciated. #3124

Closed Jordan Taylor opened this topic on on Aug 21, 2023 - 2 comments

Jordan Taylor commented on Aug 21, 2023
<?php

    use \koolreport\widgets\koolphp\Table;
    use koolreport\core\Utility as Util;
    use koolreport\datagrid\DataTables;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
    use \koolreport\export\Exportable;
    use \koolreport\excel\ExcelExportable;

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    if (empty($_POST["datefrom"])) {
        $selected_date = NULL;
    } else {
        $selected_date = test_input($_POST["datefrom"]);
    }


?>

</div>

<div class="report-content">

    <div class="text-center">
        <h1>Invoice Report</h1>

        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <?php
            function addDayswithdate($date, $days){
                $date = strtotime("+".$days." days", strtotime($date));
                return date("Y-m-d", $date);
            }

            $today2 = date('yy-m-d');
            $today3 = DateTime::createFromFormat('Y-m-d', $today2);
            $today = $today3->format("d")."-".$today3->format("m")."-20".$today3->format("y");
            $today2 = "20".$today3->format("y")."-".$today3->format("m")."-".$today3->format("d");
            $prev_date = addDayswithdate($today2, -7);

            if ($selected_date != NULL) {
                $prev_date = $selected_date;
            }

            echo("<input type=\"date\" id=\"datefrom\" name=\"datefrom\" value=\"".$prev_date."\">");
            echo("<input type=\"date\" id=\"dateto\" name=\"dateto\" value=\"".$prev_date."\"><br>");

            require 'filters.php';
            echo("<br>");
            echo("<button class=\"button\" name=\"direction\" value=\"0\"><span>Preview</span></button>");
            echo("<button class=\"button\" name=\"Download\" value=\"Download\" type=\"button\" onClick=\"handleDownload()\"><span>Download</span></button>");

        ?>
        </form>
    </div>

    <?php
        if ($prev_date == $today2){
            $today2 = addDayswithdate($today2, +1);
        }

        DataTables::create(array(
            "excludedColumns" => array("string5"),
            "name" => "MyTable1",
            "dataStore" => $this->dataStore('sales_by_customer'),
            "themeBase" => "bs4", // Reference Bootstrap 4 theme
            "cssClass" => array("table" => "table table-bordered table-hover table-striped text-right"),
            "showFooter" => true,
            "columns" => array(
                "inv_invno" => array("label" => "Invoice Number", "class"),
                "inv_ticket" => array("label" => "Ticket No", "class"),
                "inv_date" => array("label" => "Invoice Date", "class"), 
                "inv_cust" => array("label" => "Customer", "class"),
                "inv_prod" => array("label" => "Invoice Product", "class"),
                "inv_total" => array("label" => "Invoice total", "class"),
            ),
            "grouping" => array(
                "group_by" => "inv_invno", // Group by invoice number
                "total" => array(
                    "inv_total" => "sum" // Calculate the sum of invoice total
                )
            ),
            "plugins" => array("Buttons", "FixedColumns", "FixedHeader", "KeyTable", "Responsive", "RowReorder", "Scroller", "SearchPanes", "RowGroup"),
            "buttons" => array(
                array(
                    "extend" => "pdfHtml5",
                    "orientation" => "landscape",
                    "pageSize" => "A4",
                    "title" => "Transaction Report"
                ),
                "copy", "csv", "excel", "print", "colvis"
            ),
            "options" => array(
                "paging" => true,
                "pageLength" => 200,
                "searching" => true,
                "dom" => 'Bfrtip',
                "buttons" => array("copy", "csv", "excel", "pdf", "print", "colvis"),
            ),
            "rowGroup" => array(
                "dataSrc" => "inv_invno" // Group by invoice number using "inv_invno" column
            )
        ));
        
    ?>
</div>
Sebastian Morales commented on Aug 25, 2023

If you use datagrid\DataTables widget, I think the property for row grouping is clientRowGroup instead of grouping like in core\Table widget:

DataTables' clientRowGroup

Jordan Taylor commented on Aug 30, 2023

thank you that fixed it : )

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