Summing amounts with results corresponding to end of month

Hi Experts,

I and tracking arrears in a report. There is a minimum monthly payment amount one has to pay each months for a mortgage. If the person pays less than the minimum amount, the difference becomes an arrears with negative and if pay more the difference becomes an over payment with positive.

i want to accumulate this arrears and over payment column. I want the accumulate results be correspond to the end of the months. There can be more than one payment a month and the arrears should be added and the result be placed against the end/ or last payment date in the month and the other arrears payment for the other payment date in the month be zero.

This is the code that tracks the arrears:
Code:

ArreasVar:= -Customer.“Monthly Install” - “Cust. Ledger Entry”.Amount;
ArreasVarTotal:=ArreasVarTotal+ArreasVar;

Can someone help me on this please?
Thanks