KoolReport's Forum

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

Eloquent Accessor Returns a number but will not sum in Group #3020

Open april bigby opened this topic on on Mar 28, 2023 - 4 comments

april bigby commented on Mar 28, 2023

I am using Laravel Eloquent Class for my datasource. The class has an accessor, adjusted_amount, which takes the difference between two fields. I would like to group by and sum adjusted_amount but from what I can tell Koolreport thinks that it is a string and will not sum the value.

Any recommendations?

april bigby commented on Mar 28, 2023

I also own the pro license. just fyi

april bigby commented on Mar 28, 2023

ok i m trying new way of doing the actual calculation in koolreport iteself and it gives me an interesting error.

here is the code: $cust_payments_source = $this->src("fmd")->query(

        \App\CustomerPayment::where('deleted_at', '=', null)
            ->FilterBetweenMonths(
                $start_date,
                $end_date
            )
    )
        ->pipe(new Sort(array(
            "name" => "asc"
        )))
        ->pipe(new DateTimeFormat(array(
            "payment_date" => "Y-m",
        )))

        ->pipe(new CalculatedColumn(array(
            "customer_payment_amount" => function ($data) {
                if ($data["payment_status"] === 'success') {
                    return $data["payment_amount"] - $data["amount_refunded"];
                }
            },
        )))
        ->pipe(new \koolreport\processes\ColumnMeta([
            "customer_payment_amount" => [
                "type" => "number",
                "decimals" => 2,

            ],
        ]))
        ->pipe(new Group(array(
            "by" => array("customer_id", "payment_date"),
            "sum" => array(
                "adjusted_amount",
                "payment_amount",
                "customer_payment_amount"
            ),
        )))
        ->pipe(new OnlyColumn(array(
            "customer_id", "payment_date",
            "adjusted_amount",
            "payment_amount",
            "customer_payment_amount"
        )));
    //$cust_payments_source->pipe($this->dataStore('temp'));

    $join = new Join($customers_source, $cust_payments_source, array("customer_id" => "customer_id"));
    $join2 = new Join($salespeople_source, $join, array("salesperson_id" => "salesperson_id"));
    $join3 = new Join($contractors_source, $join2, array("contractor_id" => "contractor_id"));

    //$join3->pipe($this->dataStore('temp'));
    $join3
        ->pipe(new Group(array(
            "by" => array("contractor_id", "contractor_name"),
            "sum" => array(
                "payment_amount",
                "customer_payment_amount"
            ),
        )))
        ->saveTo($all_data_source);
    $all_data_source->pipe($this->dataStore('temp'));
april bigby commented on Mar 28, 2023

here is the error: Undefined array key "customer_payment_amount"

it is like it does not understand that this is a number.

Sebastian Morales commented on Mar 29, 2023

April, thanks for your information. It seems like "customer_payment_amount" is a calculated column and isn't always available in data rows. Pls try to use the following updated CalculatedColumn process and let us know if it solves the case:

        ...
        ->pipe(new CalculatedColumn(array(
            "customer_payment_amount" => function ($data) {
                if ($data["payment_status"] === 'success') {
                    return $data["payment_amount"] - $data["amount_refunded"];
                } else {
                    return 0; // in case payment_status !== 'success' set customer_payment_amount = 0
                } 
            },
        )))
        ...

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