Question

I am working on a tax project. Taxes are broken down into quarters. the months the taxes are run are March, June, Sept, and December. once run my website displays when the taxes will run again. my problem is that in my results page when the next run date is December instead of displaying 12-2012 i get something that looks like 0-2012.

Here is my code:

<td style="white-space: nowrap;">&nbsp;#stec_mysql_search_results.cover_date#&nbsp;</td>
<td style="white-space: nowrap;">&nbsp;<cfif "" neq stec_mysql_search_results.next_run>0<cfset temp_next_run = stec_mysql_search_results.next_run MOD 4><cfswitch expression="#temp_next_run#">
<cfcase value="1">3</cfcase>
<cfcase value="2">6</cfcase>
<cfcase value="3">9</cfcase>
<cfcase value="4">12</cfcase>
</cfswitch>-<cfif 4 lt stec_mysql_search_results.next_run>#year(now())+1#<cfelse>#year(now())#</cfif></cfif>&nbsp;</td>

Here is the output when you view source:

<td style="white-space: nowrap;">&nbsp;07-16-2012&nbsp;</td>
<td style="white-space: nowrap;">&nbsp;0-2012&nbsp;</td>
Was it helpful?

Solution

cfcase will accept a list, maybe you're over complicating it, why not do this:

    <cfswitch expression="#stec_mysql_search_results.next_run#">
        <cfcase value="1,2,3">3</cfcase>
        <cfcase value="4,5,6">6</cfcase>
        <cfcase value="7,8,9">9</cfcase>
        <cfcase value="10,11,12">12</cfcase>
    </cfswitch>

OTHER TIPS

The key to the problem is your code is expecting 12 mod 4 to give 4, when it gives 0.

The code you've provided has been formatted with hardly any line-breaks in, which is a stupid way of writing code because it makes it very difficult to maintain (in terms of readability, modification, and even simple revision comparisons), especially when later developers have to come along and understand what's going on.

Make sure you use newlines - particularly if that means fixing code written by others. If the output of whitespace is an issue then the ideal solution is generally to put the logic in a function (and use output=false), though you can also use <cfsilent>..</cfsilent> blocks, appropriately placed comments <!--- --->, and other means.

Here is the relevant part of your code translated to something actually readable:

<cfif "" neq stec_mysql_search_results.next_run>
    0
    <cfset temp_next_run = stec_mysql_search_results.next_run MOD 4>
        <cfswitch expression="#temp_next_run#">
            <cfcase value="1">3</cfcase>
            <cfcase value="2">6</cfcase>
            <cfcase value="3">9</cfcase>
            <cfcase value="4">12</cfcase>
        </cfswitch>
    -
    <cfif 4 lt stec_mysql_search_results.next_run>
        #year(now())+1#
    <cfelse>
        #year(now())#
    </cfif>
</cfif>

The 0 you are seeing in your results is the hard-coded one just inside the cfif.

Because the switch doesn't have a case for 0 it is not outputting anything.

To make the existing code work, just change the cfcase for 4 to 0.


However, since this is dealing with quarters, I don't think you're calculating what you mean to be.

Here is what simply changing the cfcase from 4 to 0 would result in...

January   = January
February  = February
March     = March
April     = December
May       = January
June      = February
July      = March
August    = December
September = January
October   = February
November  = March
December  = December


When what you probably want is this:

January   = March
February  = March
March     = March
April     = June
May       = June
June      = June
July      = September
August    = September
September = September
October   = December
November  = December
December  = December


Which can be done really simply with 3*ceiling(next_run/3).

If this assumption is correct, there's a significantly better way to write your code:

<td>#calculateNextRunQuarter(stec_mysql_search_results.next_run)#</td>


<cffunction name="calculateNextRunQuarter()" returntype="String" output=false>
    <cfargument name="NextRunMonth" type="Numeric" required />

    <cfset var Quarter = 3*ceiling(Arguments.NextRunMonth/3) />
    <cfset var TheYear = Year(Now()) />

    <cfif Arguments.NextRunMonth GTE 4 >
        <cfset TheYear = TheYear + 1 />
    </cfif>

    <cfreturn Right('0'&Quarter,2) & '-' & TheYear />
</cffunction>

And because the logic is all inside a function with output=false there's no stray whitespace and the code is still perfectly readable

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top