KoolReport's Forum

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

Problems using formatValue. $row and $cKey aren't working #2582

Open Newton opened this topic on on Feb 19, 2022 - 4 comments

Newton commented on Feb 19, 2022

I've created a table report and using 'formatValue' the only parameter that's working fine is $value. From my Laravel application, I used 'dd' to show the content form $row and $cKey, and both of them are with the value null. Could you help me, please? Bellow part of my code ...

  Table::create( [
                   "dataSource"=>$this->dataStore('vendas_por_vendedor_data'),
                   'showfooter' => true,
                   "columns"=>[
                     'Nome' => [
                             'label' => 'Vendedor',
                             'footerText' => "<b>Totais Gerais:</b>"
                     ],
                     'DataPedido' => [
                             'label' => 'Data do Pedido',
                             'formatValue' => function ($value,$row) {
                                return $value->format('d/m/Y');
                             },
                             'cssStyle' => "text-align:center",

                     ],
                     'QtdVendas' => [
                       'label' => 'Qtd Vendas',
                       'formatValue' => function ($value,$row) {
                         dd($row);
                         return formataValor($value,true,0);
                       },
                       'cssStyle' => 'text-align:center',
                       'footer' =>  function ($value) {
                         return formataValor($value->sum('QtdVendas'),true,0);
                       },
                       'footerText' =>"<b>@value</b>",
                     ],
                     'ValorTelhas' => [
                             'label' => 'Valor da Venda',
                             'formatValue' => function ($value,$row) {
                                return formataValoresEmReais($value);
                             },
                             'cssStyle' => 'text-align:right',
                             'footer' =>  function ($value) {
                               return formataValoresEmReais($value->sum('ValorTelhas'));
                             },
                             'footerText' =>"<b>@value</b>",
                       ],
                     'PesoTelhas' => [
                             'label' => 'Peso KG',
                             'formatValue' => function ($value, $row) {
                                return formataValor($value,true,2);
                             },
                             'cssStyle' => 'text-align:right',
                             'footer' =>  function ($value) {
                                return formataValor($value->sum('PesoTelhas'),true,2);
                             },
                             'footerText' => "<b>@value</b>"
                     ],
                     'TicketMedio' => [
                       'label' => 'Ticket Médio',
                       'formatValue' => function ($value, $row, $cKey) {
                         return ($row['QtdVendas'] > 0) ? formataValoresEmReais($value / $row['QtdVendas']) : 0.00 ;
                       },
                       'cssStyle' => 'text-align:right',
                       'footer' =>  function ($value) {
                         return ($value->sum('QtdVendas') > 0 ) ?
                           formataValoresEmReais($value->sum('TicketMedio') / $value->sum('QtdVendas')) :
                           0.00;
                       },
                       'footerText' => "<b>@value</b>"
                     ],
                     'PesoMedio' => [
                       'label' => 'Valor/KG',
                       'formatValue' => function ($value, $row) {
                         return ($row['QtdVendas'] > 0) ? formataValoresEmReais($value / $row['QtdVendas']) : 0.00 ;
                       },
                       'cssStyle' => 'text-align:right',
                       'footer' =>  function ($value) {
                         return ($value->sum('PesoMedio') > 0) ? formataValoresEmReais($value->sum('PesoMedio')) : 0.00;
                       },
                       'footerText' => "<b>@value</b>"
                     ],
                     'QtdVendasPago' => [
                       'label' => 'Qtd Vendas (Pg)',
                       'formatValue' => function ($value) {
                         return formataValor($value,true,0);
                       },
                       'cssStyle' => 'text-align:center',
                       'footer' =>  function ($value) {
                         return formataValor($value->sum('QtdVendasPago'),true,0);
                       },
                       'footerText' =>"<b>@value</b>",
                     ],
                     'ValorTelhasPago' => [
                       'label' => 'Valor da Venda (Pg)',
                       'formatValue' => function ($value,$row) {
                         return formataValoresEmReais($value);
                       },
                       'cssStyle' => 'text-align:right',
                       'footer' =>  function ($value) {
                         return formataValoresEmReais($value->sum('ValorTelhasPago'));
                       },
                       'footerText' =>"<b>@value</b>",
                     ],
                     'PesoTelhasPago' => [
                       'label' => 'Peso KG (Pg)',
                       'formatValue' => function ($value, $row) {
                         return formataValor($value,true,2);
                       },
                       'cssStyle' => 'text-align:right',
                       'footer' =>  function ($value) {
                         return formataValor($value->sum('PesoTelhasPago'),true,2);
                       },
                       'footerText' => "<b>@value</b>"
                     ],
                     'TicketMedioPago' => [
                       'label' => 'Ticket Médio (Pg)',
                       'formatValue' => function ($value, $row) {
                         return formataValoresEmReais($value / $row['QtdVendasPago']) ;
                       },
                       'cssStyle' => 'text-align:right',
                       'footer' =>  function ($value) {
                         return ($value->sum('QtdVendasPago') > 0.00)
                           ? formataValoresEmReais($value->sum('ValorTelhaPago') / $value->sum('QtdVendasPago'))
                           : 0.00;
                       },
                       'footerText' => "<b>@value</b>"
                     ],
                     'PesoMedioPago' => [
                       'label' => 'Valor/KG (Pg)',
                       'formatValue' => function ($value, $row) {
                         return ($row['QtdVendasPago'] > 0)
                            ? formataValoresEmReais($value / $row['QtdVendasPago'])
                            : 0.00;
                       },
                       'cssStyle' => 'text-align:right',
                       'footer' =>  function ($value) {
                         return ($value->sum('QtdVendasPago') > 0.00)
                            ? formataValoresEmReais($value->sum('PesoMedioPago') / $value->sum('QtdVendasPago'))
                            : 0.00;
                       },
                       'footerText' => "<b>@value</b>"
                     ],
                   ],
                   'grouping' => [
                         'Nome' => [
                             'calculate' => [
                                     "{sumQtdVendas}" => ['sum','QtdVendas'],
                                     "{sumValorTelhas}" => ['sum','ValorTelhas'],
                                     "{sumPesoTelhas}" => ['sum','PesoTelhas'],
                                     "{avgTicketMedio}" => ['avg','TicketMedio'],
//                                     "{avgTicketMedio}" => function ($store) {
//
//                                     },
                                     "{avgPesoMedio}" => ['avg','PesoMedio'],
                                     "{sumValorTelhasPago}" => ['sum','ValorTelhasPago'],
                                     "{sumPesoTelhasPago}" => ['sum','PesoTelhasPago'],
                                     "{avgTicketMedioPago}" => ['avg','TicketMedioPago'],
                                     "{avgPesoMedioPago}" => ['avg','PesoMedioPago'],
                             ],
                             'bottom' => "<td class='table-dark' colspan=2><b> Total do Vendedor: {Nome}</b></td>
                                          <td style='text-align: center;'><b>{sumQtdVendas}</b></td>
                                          <td style='text-align: right;'><b>{sumValorTelhas}</b></td>
                                          <td style='text-align: right;'><b>{sumPesoTelhas}</b></td>
                                          <td style='text-align: right;'><b>{avgTicketMedio}</b></td>
                                          <td style='text-align: right;'><b>{avgPesoMedio}</b></td>
                                          <td style='text-align: right;'><b>{sumValorTelhasPago}</b></td>
                                          <td style='text-align: right;'><b>{sumPesoTelhasPago}</b></td>
                                          <td style='text-align: right;'><b>{avgTicketMedioPago}</b></td>
                                          <td style='text-align: right;'><b>{avgPesoMedioPago}</b></td>",
                                 ],
                           ],
                   'sorting' => [
                        'Nome' => 'asc',
                        'DataPedido' => 'asc',
                        ],
                   "cssClass" => [
                      'table' => 'table-primary table-striped table-hover table-bordered',
                   ],
                   'groupCellsInColumns' => ['Nome'],
                 ]);


Sebastian Morales commented on Feb 21, 2022

Pls let us which version of KoolReport Core you are using. You can find it in file koolreport/core/composer.json. Rgds,

Newton commented on Feb 23, 2022

Follows

{
    "name": "koolreport/core",
    "version":"5.5.0",
    "description": "An Open Source PHP Reporting Framework for easier and faster report delivery.",
    "keywords": ["php reporting framework","php reporting tools","data processing","data visualization","charts and graphs"],
    "homepage": "https://www.koolreport.com",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "KoolPHP Inc",
            "homepage": "https://www.koolphp.net"
        }
    ], 
    "require":
    {
        "php":">=5.4"
    },
	"autoload": {
        "files": [
            "autoload.php"
        ]
    }

}
Sebastian Morales commented on Feb 24, 2022

That's strange. This version has supported the $cKey argument in formatValue function. If you don't mind pls send a screenshot of your table and the output of var_dump($row) and var_dump($cKey) for each row. Tks,

Newton commented on Feb 28, 2022

Sebastian, I don't know if there were any other mistake or misspelling that we've corrected from the other topic that I've opened but now its working fine, $row and $cKey are returning values correctly. Thanks for your help.

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

None