By aggregating before the case statement there is no need to group by amount_currency.
In earlier versions of Odoo this bug only presented in multicurrency transactions as company currency transactions
would have an amount_currency of 0.0 and group by 0.0 just fine.
In later versions of Odoo amount_currency is always set and this causes otherwise identical move lines
that should be summed to display seperately. However, amount_currency was required in groupby because it
was part of a non aggregated case statement with the sum inside.
By reversing the nesting we remove the need to group by amount_currency. This is safe because we already group by
currency_id and aggregates are not evaluated until after group by completes.