#!/usr/bin/perl

use CGI;

my $q = new CGI();

print $q->header();

print `cat header.html`;

my $years = 3;
my $salary = 25000;
my $increase = 0.03;

if ($q->param("years")) {
$years = $q->param("years");
$salary = $q->param("salary");
$increase = scalar($q->param("increase"))/100;
}
print <<EOF;
<h1>Student Loan Payment Calculator</h1>
<form>
Total Years studied : <input type="text" name="years" value="$years"></br>
Starting Graduate salary : <input type="text" name="salary" value="$salary"></br>
Salary increase per year : <select name="increase">
<option value="0">Inflation</option>
<option value="3" selected>3% above inflation</option>
<option value="6">6% above inflation</option>
<option value="10">10% above inflation</option>
</select>
<br/>
<input type="submit" name="submit" value="submit">
</form>

EOF
;


print "<table cellpadding=\"5\">";
print "<tr><th>Age</th><th>Gross Salary</th>
<th>Employers NI</th><th>NI</th><th>Tax</th><th>Net pay</th>
<th>Loan repaid<br/>(pre
1998)</th><th>Loan repaid<br/>(1998-2005)</th><th>Loan
repaid<br/>(2006-2010)</th><th>Loan repaid<br/>(2011+)</th></tr>";

#Each of the schemes,
#Pre 1998
#Borrowed 2000/year for maintenance
#Repayments in 5 years
#Threshold 26k

my $loan1998 = 2000 * $years;

#1998-2005
#Borrowed 4000/year for mainance + 1000 for fees
#9% of salary above 15k
my $loan2005 = 5175 * $years;

#2006-2010
#Borrowed 4000/year for maintanence + 3000/year for fees
#9% of salary above 15k
my $loan2010 = 7000 * $years;

#2011+
#Borrowed 4000/year for mainteancen + 6000/year for fees
#9% of salary above 21k
#interest 3% above inflation when salary is over 21k
my $loan2011 = 10000 * $years;

my $repaid1998 = 0;
my $repaid2005 = 0;
my $repaid2010 = 0;
my $repaid2011 = 0;


for (my $age = (18 + $years + 1); $age <= 50; $age++) {

if ($age % 2 == 0) {
print "<tr>"
}
else {
print "<tr bgcolor=\"lightgrey\">";
}
print "<td>$age</td>";
print "<td>" . int($salary) . "</td>";


#Approximate tax NI
#allowance 6475
# 20% of the next 37400
# 40% of the next 56125
# 60% of the next 12950
# 40% of the next 37050
# 50% thereafter

# NI
# LEL 5044
# 11% to 40040
# 1% over

# Employers NI
# LEL 5044
# 12.5% over this
my $net = 0;
my $tax = 0;
my $ni = 0;
my $eni = 0;

#Tax
if ($salary > 6475) {
   if ($salary > (37400 + 6475)) {
      if ($salary > (37400 + 6475 + 56125)) {
        if ($salary > (37400 + 6475 + 56125 + 12950)) {
		  if ($salary > (37400 + 6475 + 56125 + 12950 + 37050)) {
            $tax += ($salary - 37400 - 6475 - 56125 - 12950 - 37050) * 0.5;
			$tax += 37050 * 0.4;
		  }
		  else {
            $tax += ($salary - 37400 - 6475 - 56125 - 12950) * 0.4;
		  }
          $tax += 12950 * 0.6;
        }
		else {
          $tax += ($salary - 56125 - 37400 - 6475) * 0.6;
		}
        $tax += 56125 * 0.4
	  }
	  else {
        $tax += ($salary - 37400 - 6475) * 0.4;
	  }
      $tax += (37400 * 0.2);
   }
   else {
      $tax += ($salary - 6475) * 0.2;
   }
}

#NI
if ($salary > 5720) {
  $eni += ($salary - 5720) * .125;
  if ($salary > 38168 + 5720) {
    $ni += 38168 * .11;
	$ni += ($salary - 38168 - 5720) * 0.01;
  }
  else {
    $ni += ($salary - 5720) * 0.11;
  }
}

print "<td>" . int($eni) . "</td>";
print "<td>" . int($ni) . "</td>";
print "<td>"  . int($tax) . "</td>";
print "<td>" . int($salary - $ni - $tax) . "</td>";

#Work out the repayments
#pre 1998
if (($salary > 26000) && (($loan1998-1) >= $repaid1998)) {
   $repaid1998 += $loan1998/5;
}

print "<td>$repaid1998</td>\n";

#1998-2005
if (($salary > 15000) && (($loan2005) >= $repaid2005)) {
    my $repayment = ($salary - 15000) * 0.09;
	if ($repayment > (($loan2005-$repaid2005))) {
	    $repaid2005 = $loan2005;
	}
	else {
        $repaid2005 += $repayment;
	}
}

print "<td>" . int($repaid2005) . "</td>";

#2005-2010
if (($salary > 15000) && (($loan2010) >= $repaid2010)) {
    my $repayment = ($salary - 15000) * 0.09;
    if ($repayment > (($loan2010-$repaid2010))) {
        $repaid2010 = $loan2010;
    }
    else {
        $repaid2010 += $repayment;
    }
}

print "<td>" . int($repaid2010) . "</td>";

#2011
if (($salary > 21000) && (($loan2011) >= $repaid2011)) {
    #Interest
    $loan2011 += (($loan2011-$repaid2011) * 0.03);
    my $repayment = ($salary - 21000) * 0.09;
    if ($repayment > (($loan2011-$repaid2011))) {
        $repaid2011 = $loan2011;
    }
    else {
        $repaid2011 += $repayment;
    }
}

print "<td>" . int($repaid2011) . "</td>";



print "</tr>\n";

#Payrise
$salary += ($salary * $increase);
}

print "</table>";

print <<EOF

<h2>
Method
</h2>

<p>Pre 1998, I've assumed the student borrows &pound;2,000 (approximately
half the living costs of the student) with the other half having been
provided by a grant. They then pay this back in exactly five years once
their salary exceeds &pound;26,000. This is an inflation adjusted
version of the scheme that was in place at the time. The scheme did
change thresholds yearly with inflation so this is a fairly true
reflection.</p>

<p>1998 - 2005, I've assumed that the student borrows &pound;4,000 for
living costs and &pound;1,000 for fees and pays back at 9% of their
salary above &pound;15,000. Inflation adjusting doesn't work properly in
this case because the threshold doesn't change with inflation, the
living costs do and the fees obey different rules entirely. This figure
is definitely wrong, but the amount wrong depends on exactly which year
the student studied.</p>

<p>2005 - 2010, This is the same as the case for 1998 - 2005 but with
fees of &pound;3,000. The same criticism applies.</p>

<p>2011 onwards, I've assumed the student borrows &pound;4,000 for
living costs and &pound;6,000 for fees. They repay at 9% of their salary
over &pound;21,000, but when their salary is in excess of &pound;21,000
the loan incurs an interest rate of 3%.</p>

<p>These calculations account for inflation because a simple model of
inflation should treat salary and loan as rising at the same rate.</p>

<p>I've calculated Tax and NI using figures from the inland revenues
site,

<pre>
#Approximate tax & NI
#allowance 6475
# 20% of the next 37400
# 40% of the next 56125
# 60% of the next 12950
# 40% of the next 37050
# 50% thereafter

# NI
# LEL 5720
# 11% to 38168
# 1% over

# Employers NI
# LEL 5720
# 12.5% over this
</pre>
These figures disagree slightly with <a
href="http://listentotaxman.com/index.php">Listen To Taxman</a> who's
almost certainly got the calculation right, but I'm not quite sure why.
</p>

<p>Requests for source code, comments, whinges, flames etc. <a
href="mailto:pete\@ex-parrot.com">pete\@ex-parrot.com</a>.</p>

EOF
;
print `cat footer.html`;
