| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | privateloan.pl - Calculate repayments on a private loan under UK law | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | privateloan.pl --conf some-configuration-file | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This script calculates the repayments and tax schedule for a private | ||
| 14 | loan under UK law. The output of the script is a repayment table with | ||
| 15 | a tax summary at the end of each finacial year. | ||
| 16 | |||
| 17 | At the time of writing, interest payments on a private loan (not from | ||
| 18 | a bank or building society) must be have tax deducted from them by the | ||
| 19 | borrower. The borrower is responsible for paying this tax to HMRC. | ||
| 20 | |||
| 21 | If the loan is for business purposes the borrower can then reclaim the | ||
| 22 | whole amount of interest paid (net amount paid to the lender plus the | ||
| 23 | tax paid) as a business expense. | ||
| 24 | |||
| 25 | The premium that is calculated by this module is not exact. It may over- | ||
| 26 | or under-pay slightly and an adjustment will be necessary in the | ||
| 27 | last few months. A configuration file enables you to set the tax rate, | ||
| 28 | interest rate, period of loan and the amount. The tax rate and the amount | ||
| 29 | lent may be subject to change during the period of the loan. Monthly | ||
| 30 | interest is calculated as 1/12 of the nominal annual rate; this is | ||
| 31 | not mathematically acccurate, but it is the usual convention in the | ||
| 32 | financial world. | ||
| 33 | |||
| 34 | |||
| 35 | =head1 CONFIGURATION | ||
| 36 | |||
| 37 | The script uses Config::General. The format of a configuration files is the same | ||
| 38 | as for Apache. | ||
| 39 | |||
| 40 | <taxrate> and <loan> blocks may be repeated any number of times. They do | ||
| 41 | not have to be in date order, the program will sort them. In each block only | ||
| 42 | the date is mandatory; if any other field is omitted it will remain at | ||
| 43 | its previously set value. | ||
| 44 | |||
| 45 | Changes in amounts or rates that are made part way through a month are treated | ||
| 46 | as happening at the start of the month. We make no attempt to calculate daily | ||
| 47 | interest rates. | ||
| 48 | |||
| 49 | The loan period is used only for estimating the initial premium. The | ||
| 50 | actual period of the loan will only approximate to this period. A premium that | ||
| 51 | is defined in the configuration file overrides the calculated figure. | ||
| 52 | |||
| 53 | If there are advances on the loan during its period it is advisable to | ||
| 54 | set the premium manually, otherwise there is a risk that the loan will | ||
| 55 | never be paid off. | ||
| 56 | |||
| 57 | yearend is used to determine when to print summaries. | ||
| 58 | |||
| 59 | For a bank loan, where you pay gross interest, set the tax rate to 0. | ||
| 60 | |||
| 61 | All dates must be in the yyyy-mm-dd format. | ||
| 62 | |||
| 63 | =head1 SAMPLE CONNFIGURATION FILE | ||
| 64 | |||
| 65 | <taxrate> | ||
| 66 | date 2012-04-05 | ||
| 67 | rate 20 # Correct rate at the time of writing | ||
| 68 | </taxrate> | ||
| 69 | |||
| 70 | # Initial loan amount | ||
| 71 | <loan> | ||
| 72 | date 2012-05-20 | ||
| 73 | amount 20000 | ||
| 74 | rate 3 | ||
| 75 | premium 200 | ||
| 76 | </loan> | ||
| 77 | |||
| 78 | # Part way through change the tax and interest rates, and advance some more cash | ||
| 79 | <loan> | ||
| 80 | date 2015-07-20 | ||
| 81 | amount 500 | ||
| 82 | rate 10 | ||
| 83 | premium 250 | ||
| 84 | </loan> | ||
| 85 | |||
| 86 | #Approximate loan period in years | ||
| 87 | period 10 | ||
| 88 | |||
| 89 | # Start/end of the financial year, usually 5 April. | ||
| 90 | yearend 2012-04-05 | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | use strict; | ||
| 95 | use DateTime; | ||
| 96 | use DateTime::Duration; | ||
| 97 | use DateTime::Format::ISO8601; | ||
| 98 | use Getopt::Long; | ||
| 99 | use Config::General qw(ParseConfig); | ||
| 100 | use Finance::Loan::Private qw(premium sorter); | ||
| 101 | |||
| 102 | # Some useful intervals | ||
| 103 | my $Month = DateTime::Duration->new(months=>1); | ||
| 104 | my $Year = DateTime::Duration->new(years=>1); | ||
| 105 | |||
| 106 | # Working variales with default values. | ||
| 107 | my $Today = DateTime->today(); | ||
| 108 | my $TaxRate = 20; | ||
| 109 | my $Period = 10; | ||
| 110 | my $Principal = 10000; | ||
| 111 | my $InterestRate = 3.0; | ||
| 112 | my $ConfigFile = "/dev/null"; | ||
| 113 | |||
| 114 | my $rc = GetOptions( | ||
| 115 | "conf:s" => \$ConfigFile, | ||
| 116 | ); | ||
| 117 | if (!$rc) { | ||
| 118 | print "Usage: privateloan.pl [ --conf /dev/null ]\n"; | ||
| 119 | die "Bad command line options"; | ||
| 120 | } | ||
| 121 | my $Config = Config::General->new(-ConfigFile=>$ConfigFile, | ||
| 122 | -LowerCaseNames=> 1, | ||
| 123 | ); | ||
| 124 | my %Config = $Config->getall; | ||
| 125 | my $TaxRates = (ref($Config{taxrate}) eq 'ARRAY') ? $Config{taxrate} : | ||
| 126 | [ $Config{taxrate} ]; | ||
| 127 | my $Advances = (ref($Config{loan}) eq 'ARRAY') ? $Config{loan} : | ||
| 128 | [ $Config{loan} ]; | ||
| 129 | $Period = $Config{period} if ($Config{period}); | ||
| 130 | my $YearEnd = DateTime::Format::ISO8601->parse_datetime($Config{yearend}); | ||
| 131 | |||
| 132 | # Get things into date order | ||
| 133 | my @TaxRates = sorter($TaxRates); | ||
| 134 | my @Advances = sorter($Advances); | ||
| 135 | my ($Premium, $totalTax, $totalInterest); | ||
| 136 | |||
| 137 | # Now do the real work | ||
| 138 | main(); | ||
| 139 | exit 0; | ||
| 140 | |||
| 141 | sub main { | ||
| 142 | my ($nextTaxDate, $nextLoanDate); | ||
| 143 | |||
| 144 | # Get the loan details | ||
| 145 | $Principal = $Advances[0]->{amount}; | ||
| 146 | $Today = DateTime::Format::ISO8601->parse_datetime($Advances[0]->{date}); | ||
| 147 | $InterestRate = $Advances[0]->{rate}; | ||
| 148 | $Premium = int premium($Principal, $InterestRate, $Period); | ||
| 149 | $Premium = $Advances[0]->{premium} if exists $Advances[0]->{premium}; | ||
| 150 | shift @Advances; | ||
| 151 | if (@Advances) { | ||
| 152 | $nextLoanDate = DateTime::Format::ISO8601->parse_datetime($Advances[0]->{date}); | ||
| 153 | } | ||
| 154 | |||
| 155 | # Get the tax details | ||
| 156 | $TaxRate = $TaxRates[0]->{rate}; | ||
| 157 | shift @TaxRates; | ||
| 158 | if (@TaxRates) { | ||
| 159 | $nextTaxDate = DateTime::Format::ISO8601->parse_datetime($TaxRates[0]->{date}); | ||
| 160 | } | ||
| 161 | while ($Today > $YearEnd) { | ||
| 162 | $YearEnd += $Year; | ||
| 163 | } | ||
| 164 | |||
| 165 | printf("Loan amount\t\t%d\n", $Principal); | ||
| 166 | printf("Gross interest rate\t%.2f%%\n", $InterestRate); | ||
| 167 | printf("Tax rate\t\t%.2f%%\n", $TaxRate); | ||
| 168 | printf("Period\t\t\t%d years\n", $Period); | ||
| 169 | print "\n"; | ||
| 170 | print "Date\t\tPrincipal\tGross interest due\tTax due\tAmount paid\tPrincipal repaid\n"; | ||
| 171 | while (1) { | ||
| 172 | printf("%s\t%8.2f\t", $Today->ymd(), $Principal); | ||
| 173 | my $interest = $Principal*$InterestRate/1200.0; | ||
| 174 | my $tax = $interest*$TaxRate/100.0; | ||
| 175 | $totalTax += $tax; | ||
| 176 | $totalInterest += $interest; | ||
| 177 | my $repayment = $Premium -$interest + $tax; | ||
| 178 | if ($repayment >= $Principal) { | ||
| 179 | $Premium = $Principal + $interest - $tax; | ||
| 180 | $repayment = $Principal; | ||
| 181 | printf("%8.2f\t\t%6.2f\t%6.2f\t\t%8.2f\n", $interest, $tax, $Premium, $repayment); | ||
| 182 | last; | ||
| 183 | } | ||
| 184 | printf("%8.2f\t\t%6.2f\t%6.2f\t\t%8.2f\n", $interest, $tax, $Premium, $repayment); | ||
| 185 | $Principal -= $repayment; | ||
| 186 | $Today += $Month; | ||
| 187 | yearEndSummary() if ($Today >= $YearEnd); | ||
| 188 | updateLoan() if ($nextLoanDate && $Today >= $nextLoanDate); | ||
| 189 | updateTax() if ($nextTaxDate && $Today >= $nextTaxDate); | ||
| 190 | } | ||
| 191 | |||
| 192 | yearEndSummary(); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub yearEndSummary { | ||
| 196 | printf ("Gross interest paid in the past year\t%8.2f\n", $totalInterest); | ||
| 197 | printf("Total tax deducted in the past year\t%8.2f\n", $totalTax); | ||
| 198 | print "\n"; | ||
| 199 | $totalTax = 0; | ||
| 200 | $totalInterest =0; | ||
| 201 | $YearEnd += $Year; | ||
| 202 | print "Date\t\tPrincipal\tGross interest due\tTax due\tAmount paid\tPrincipal repaid\n"; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub updateLoan { | ||
| 206 | $InterestRate = $Advances[0]->{rate} if exists($Advances[0]->{rate}); | ||
| 207 | $Principal += $Advances[0]->{amount} if exists($Advances[0]->{amount}); | ||
| 208 | $Premium = $Advances[0]->{premium} if exists($Advances[0]->{premium}); | ||
| 209 | shift @Advances; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub updateTax { | ||
| 213 | $TaxRate = $TaxRates[0]->{rate} if exists($TaxRates[0]->{rate}); | ||
| 214 | shift @TaxRates; | ||
| 215 | } | ||
| 216 | |||
| 217 | =head1 AUTHOR | ||
| 218 | |||
| 219 | Raphael Mankin <rapmankin@cpan.org> |