How best to write this using C/AL.

asemberengasembereng Member Posts: 220
function income_tax(val){
start=625.01;
tax=0;
incr=5.0;
tax_add=0.50;
itax=array("0"=>"0");
boundary=1455;

for(i=start;i<=boundary;i+=incr){

itax=tax;
tax+=tax_add;
}

start=1455.01;
tax=83.33;
incr=5;
tax_add=0.75;
boundary=2288;

for(i=start;i<=boundary;i+=incr){
itax=tax;

tax+=tax_add;

}

start=2288.01;
tax=208.67;
tax_add=1;
boundary=3123;
incr=5;

for(i=start;i<=boundary;i+=incr){
itax=tax;
tax+=tax_add;

}


start=3123.01;
tax=375.83;
incr=5;
tax_add=1.25;
boundary=3958;

for(i=start;i<=boundary;i+=incr){
itax=tax;
tax+=tax_add;

}
last='0';

foreach(itax as amount => tax){
if(val<amount){
tax=itax[last];
break;
}
last=amount;
}
return itax[last];
}

Can someone help me on this?

Comments

  • kinekine Member Posts: 12,562
    It is not good example for C/AL code. Not because soe technical issues, but because heavy Magic Constants usage. Have this code some meaning? The values you used are just example?
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • asemberengasembereng Member Posts: 220
    This is code used to income tax calculation. I was hoping someone can write a navision version. the constant define are ok..
  • gerdhuebnergerdhuebner Member Posts: 155
    edited 2009-02-21
    At first, could you please explain how to understand an expression like tax[625.01] or tax, because arrays in NAV can only have integer arguments greater than or equal to 1, for example tax[5] or
    tax[round(625.01,1,'<')] may be valid expressions, but not tax[625.01]

    Furthermore it is not clear for me, what the statement
    itax=array("0"=>"0");
    means.
    Perhaps you should give some information about the programming language.
  • kinekine Member Posts: 12,562
    The whole code looks like precalculating array of values for all possible amounts and after that the correct amount is selected by selecting the correct part from the array - because the input parameter is used only in last section...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • gerdhuebnergerdhuebner Member Posts: 155
    kine wrote:
    The whole code looks like precalculating array of values for all possible amounts and after that the correct amount is selected by selecting the correct part from the array - because the input parameter is used only in last section...
    Can only be something like that...
    I think, the best way to implement this kind of calculation in NAV is to use some table (tax table) with the fields "Taxable Amount min." and "Income Tax Amount" and may be "Year" (This has the advantage to have a history if the calculation formulas change and on the other hand, the calculation is more transparent, because you can check the result by simply taking a look at the table). Primary key would be "Year" + "Taxable Amount min." - To find the tax, you simply have to put a filter on "Taxable Amount min." (and may be "Year" of course)
    SETFILTER("Taxable Amount min.",'..%1',val) and find the last record. In order to create the tax table once a year, you may use some parts of the original code.
Sign In or Register to comment.