atk4/atk4 — pull request #562 by DarkSide666 (2014-07-21 15:11:18+00:00)
Title: To correctly calculate grandTotals

To correctly calculate grandTotals in some cases when you don't have completely distinct records in your Lister or more likely - Grid. For example, if Grid Model has joined table etc.

Before:
```
select DISTINCT SQL_CALC_FOUND_ROWS
sum((select sum(`invoice`.`sum_unpaid`) `sum_unpaid` from `invoice` where `invoice`.`client_id` = `client`.`id` )) `sum_unpaid`,
count(*) `total_cnt`
from `client`
left join `invoice` as `_i` on `_i`.`client_id` = `client`.`id`
where
...
```
if you have more than one invoice for a client (rows are not distinct anymore), then SUM will be wrong.

After:
```
select
sum(`sum_unpaid`) `sum_unpaid`,
count(*) `total_cnt`
from (
select DISTINCT
...,
(select sum(`invoice`.`sum_unpaid`) `sum_unpaid` from `invoice` where `invoice`.`client_id` = `client`.`id` ) `sum_unpaid`,
...
from `client`
left join `invoice` as `_i` on `_i`.`client_id` = `client`.`id`
where ...
) `grandTotals`
```
now you shouldn't care are your rows distinct or not or whatever because SUM are calculated using sub-select.

That way it will work a bit slower, but always correctly what is more important I guess :)