old_remaining can be slightly larger than 0 depending on how the truncation works out.
This results in the next IF statement evaluating as true when it should not:
IF old_remaining > 0 THEN
Which moves the entire original allocation into a deduction.
I think the best solution is to fix how prorated amounts are stored in the database. These should probably be stored as 2 digit rounded numbers. This means the final prorated entry will have to be massaged, so that all the prorated values equal the total value for the item they are representing.
The problem is with this line here:
curr_old_amt := trunc( curr_old_amt, 2 );
When the next line is executed:
old_remaining := old_remaining - curr_old_amt;
old_remaining can be slightly larger than 0 depending on how the truncation works out.
This results in the next IF statement evaluating as true when it should not:
IF old_remaining > 0 THEN
Which moves the entire original allocation into a deduction.
I think the best solution is to fix how prorated amounts are stored in the database. These should probably be stored as 2 digit rounded numbers. This means the final prorated entry will have to be massaged, so that all the prorated values equal the total value for the item they are representing.