coldfusion subtract from last iteration in cfloop

240 views Asked by At

I am a PHP fellow trapped in coldfusionland, I need some help and I thank you in advance.

I am trying to set up payment schedules based on amount owed, number of payments per year.

I have a method in a componant:

<!--- declare method vars --->
    <cfset begBal = 1200>
    <cfset curBal = "1000">
    <cfset pymntsPerYear = 12>
    <cfset pymntAmnt = 0>
    <cfset pymntsLeft = "">
    <cfset prettyPymntsPerYear = "">
    <cfset movingBal = 0>
    <!--- set numerical value for frequency --->
    <cfswitch expression=#pymntsPerYear#>
        <cfcase value="52">
            <cfset pymntsPerYear = "52">
            <cfset prettyPymntsPerYear = "Weekly">
        </cfcase>
        <cfcase value="12">
            <cfset pymntsPerYear = "12">
            <cfset prettyPymntsPerYear = "Monthly">
        </cfcase>
        <cfcase value="24">
            <cfset pymntsPerYear = "24">
            <cfset prettyPymntsPerYear = "Twice Monthly">
        </cfcase>
        <cfcase value="26">
            <cfset pymntsPerYear = "26">
            <cfset prettyPymntsPerYear = "Every 2 Weeks">
        </cfcase>
        <cfcase value="1">
            <cfset pymntsPerYear = "1">
            <cfset prettyPymntsPerYear = "Quarterly">
        </cfcase>
    </cfswitch>
    <!--- set balance, if we have payments that have already been made, we wouldnt want to start
        the new payment schedule at the original balance --->

    <!--- loop through and do the math for the new schedule --->
    <cfset pymntAmnt = begBal / pymntsPerYear>
    <cfloop from="1" to="#pymntsPerYear#" index="i">
        <cfset movingBal += movingBal - (movingBal - pymntAmnt)>
        PAYMENT NUMBER: #i#  #dollarFormat(pymntAmnt)#, balance = #dollarFormat(movingBal)#<br>
    </cfloop>
        <cfoutput>The new schedule will be #pymntsPerYear# #prettyPymntsPerYear# Payments @ #dollarFormat(pymntAmnt)#</cfoutput>

But the output is unexpected. I am trying to get the new balance after each payment, but it seems to work backwards, ie:(100,200,300) not what I was expecting ie:(300,200,100,0)

See the output

PAYMENT NUMBER: 1 $100.00, balance = $100.00
PAYMENT NUMBER: 2 $100.00, balance = $200.00
PAYMENT NUMBER: 3 $100.00, balance = $300.00
PAYMENT NUMBER: 4 $100.00, balance = $400.00
PAYMENT NUMBER: 5 $100.00, balance = $500.00
PAYMENT NUMBER: 6 $100.00, balance = $600.00
PAYMENT NUMBER: 7 $100.00, balance = $700.00
PAYMENT NUMBER: 8 $100.00, balance = $800.00
PAYMENT NUMBER: 9 $100.00, balance = $900.00
PAYMENT NUMBER: 10 $100.00, balance = $1,000.00
PAYMENT NUMBER: 11 $100.00, balance = $1,100.00
PAYMENT NUMBER: 12 $100.00, balance = $1,200.00
The new schedule will be 12 Monthly Payments @ $100.00

How do I make it "desc" and end at Balance = 0?

1

There are 1 answers

2
KFNinja On BEST ANSWER

Maybe like this?

<!--- declare method vars --->
<cfset begBal = 1200>
<cfset curBal = "1000">
<cfset pymntsPerYear = 12>
<cfset pymntAmnt = 0>
<cfset pymntsLeft = "">
<cfset prettyPymntsPerYear = "">
<cfset movingBal = begBal>
<!--- set numerical value for frequency --->
<cfswitch expression=#pymntsPerYear#>
    <cfcase value="52">
        <cfset pymntsPerYear = "52">
        <cfset prettyPymntsPerYear = "Weekly">
    </cfcase>
    <cfcase value="12">
        <cfset pymntsPerYear = "12">
        <cfset prettyPymntsPerYear = "Monthly">
    </cfcase>
    <cfcase value="24">
        <cfset pymntsPerYear = "24">
        <cfset prettyPymntsPerYear = "Twice Monthly">
    </cfcase>
    <cfcase value="26">
        <cfset pymntsPerYear = "26">
        <cfset prettyPymntsPerYear = "Every 2 Weeks">
    </cfcase>
    <cfcase value="1">
        <cfset pymntsPerYear = "1">
        <cfset prettyPymntsPerYear = "Quarterly">
    </cfcase>
</cfswitch>
<!--- set balance, if we have payments that have already been made, we wouldnt want to start
    the new payment schedule at the original balance --->

<!--- loop through and do the math for the new schedule --->
<cfset pymntAmnt = begBal / pymntsPerYear>
<cfoutput>
<cfloop from="1" to="#pymntsPerYear#" index="i" >
    <cfset movingBal -= movingBal - (movingBal - pymntAmnt)>
    PAYMENT NUMBER: #i#  #dollarFormat(pymntAmnt)#, balance = #dollarFormat(movingBal)#<br>
</cfloop>
The new schedule will be #pymntsPerYear# #prettyPymntsPerYear# Payments @ #dollarFormat(pymntAmnt)#
</cfoutput>