-2

Hi I am a newbe and writing a simple code for tax purposes. So far I have wrote a small code but I think there seems to be a problem with the function taxable_i($i) which is not letting me make $taxable_i NIL. I have tried everything. When I try to made $taxable_i NIL via function it wont let me do it. It shows me a result of £13900. I think the program is echoing $taxable_i rather then the tax payable. If I delete the above all together then the tax payable comes to -£2199.80, which is in minus. Please note that I put $taxable_i <personal_allowance so that the answer is zero but the result is not zero. I do not know why and what I am doing wrong. Any help is most welcome.'

<?php
// VAT 
define("vat_threshold",83000 );
define("vat_rate",0.2 );

// RELIEFS
define("personal_allowance",11000 );

//RATES
define("starting_rate",0 );
define("basic_rate",0.2 );
define("higher_rate",0.4 );
define("add_rate",0.45 );
define("divs_ord_rate",0.075 );
define("divs_uuper_rate",0.325 );
define("divs_add_rate",0.381 );

// THRESHOLDS
define("savings_income_band",5000 );
define("basic_rate_band",32000 );
define("higher_rate_band",150000 );
define("divs_allowance",5000 );

define ("band_0",0);
define("band_1",11000);
define("band_2",32000);
define("band_3",100000);
define("band_4",122000);
define("band_5",150000);

function taxable_i($i) {

    if ($i <= band_1 ) {
        $taxable_i = $i * 0;
        return $taxable_i;
    }
    if ($i <= band_3 ) {
        $taxable_i = $i - personal_allowance;
        return $taxable_i;
    } 
    if ($i >band_3 && $i <=band_4) {
        $taxable_i = $i - (personal_allowance-($i - band_3)/2);
        return $taxable_i;
    }
    if ($i > band_4) {
        $taxable_i = $i;
        return $taxable_i;
    }

}

$starting_income = $i = 1;
echo $i;    

$taxable_i = taxable_i($i);
echo $taxable_i;

switch ($taxable_i) {

case ($taxable_i > band_5):
    $diff = $taxable_i - band_5;
    $tax5 = $diff * add_rate;
    $taxable_i = band_5;
    $diff = $taxable_i - band_2;
    $tax4 = $diff * higher_rate;
    $taxable_i = band_2;
    $tax3 = $taxable_i * basic_rate;
    $tax_payable = $tax5 + $tax4 + $tax3;
    break;

case ($taxable_i > band_2 && $taxable_i <= band_5 ):
    $diff = $taxable_i - band_2;
    $tax4 = $diff * higher_rate;
    $taxable_i = band_2;
    $tax3 = $taxable_i * basic_rate;
    $tax_payable = $tax4 + $tax3;
    break;

case ($taxable_i < band_2):
    $tax = $taxable_i * basic_rate;
    $tax_payable = $tax;
    break;

default:    
    $taxable_i <= band_0;
    $tax_payable == 0;
    break;
}

echo $tax_payable;
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Stackoverflow is not meant for code debugging or error finding chores. Please reduce the example to the minimal required code example necessary to showcase your problem, elaborate on what you are trying to achieve (and why) and point out the expected as opposed to the actual behavior. – k0pernikus Mar 03 '17 at 00:00
  • I've been guilty of that too but... no, writing a tax programs will not help you do them. The only way to get taxes done, is to sit your sorry ass on a chair an do them :) – Félix Adriyel Gagnon-Grenier Mar 03 '17 at 00:32
  • Hi Felix - unfortunately thats what I do for living. I am a tax advisor. Coding is my hobby. I have a tax calculator on my website www.sipheraccounting.com which was originally written in Excel using if statements. I always wanted to learn programming have now I have put around 30 days to write what I wrote. Hopefully I will get it working too. – Shahzad Malik Mar 04 '17 at 13:32

1 Answers1

1

You don't put conditions in case statements, you should be using if/elseif/else there. The way switch/case works is that it compares the value of the initial switch () expression with the values of each case expression. So it's comparing $taxable_i to the value of $taxable_i > band_5, so it's comparing a number to true or false.

Replace the switch block with:

if ($taxable_i > band_5) {
    $diff = $taxable_i - band_5;
    $tax5 = $diff * add_rate;
    $taxable_i = band_5;
    $diff = $taxable_i - band_2;
    $tax4 = $diff * higher_rate;
    $taxable_i = band_2;
    $tax3 = $taxable_i * basic_rate;
    $tax_payable = $tax5 + $tax4 + $tax3;
}

elseif ($taxable_i > band_2 ) {
    $diff = $taxable_i - band_2;
    $tax4 = $diff * higher_rate;
    $taxable_i = band_2;
    $tax3 = $taxable_i * basic_rate;
    $tax_payable = $tax4 + $tax3;
}

elseif ($taxable_i >= band_0) {
    $tax = $taxable_i * basic_rate;
    $tax_payable = $tax;
}

else {    
    $tax_payable == 0;
    break;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612