I posted a solution for this a few days ago.
Please wait a day or two before evaulating. There was a bug in calculating the balance in the general ledger report from which this was based introduced by the update_payment_reference() function call. I just corrected the bug in my statement solution and I am busy testing.