If I understand your algorithm correctly, this should do the job:
#AL doAccounts {
#DBLOAD Ac
#QUERY ((&ShortName = "%-1")|(&LongName = "%-1")) All 1
#MATH temp (&AmountCredit + &AmountCollected - &AmountPastDue)
&AmountPastDue = %max(0, %eval(&AmountPastDue - &AmountCredit - &AmountCollected))
#IF (@temp > 0) {
#MATH temp2 (@temp - &AmountDue)
&AmountDue = %max(0, %eval(&AmountDue - @temp))
#IF (@temp2 > 0) {&AmountCredit = %eval(&AmountCredit + @temp2)}
}
}
Troubadour