How best to write this using C/AL.

asembereng
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?
0
Comments
-
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?0
-
This is code used to income tax calculation. I was hoping someone can write a navision version. the constant define are ok..0
-
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.New kits on the blog: https://massivedynamicsblog.wordpress.com0 -
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...0
-
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...
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.New kits on the blog: https://massivedynamicsblog.wordpress.com0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions