-
Notifications
You must be signed in to change notification settings - Fork 0
/
loans.pl
executable file
·263 lines (221 loc) · 6.87 KB
/
loans.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/perl
#This program does loan calculations.
#It takes a "banking statement" (list of transactions) and outputs
#a banking statement of the same form, with interest amounts and loan
#amounts inserted.
#To calculate the interest earned in a tax year, use the -t option.
use warnings; use strict; use Data::Dumper;
use Time::Piece;
use Time::Seconds;
use POSIX;
my $compoundedEvery = 3; #interest is compounded every 3 months (quarterly)
my $interestRate = 0; #The interest rate on the loan
my $loan = 0; #The amount of the loan up to the point we've processed
my $currentDate = localtime;
#Only care about the current day, not the time
$currentDate = Time::Piece->strptime($currentDate->dmy, "%d-%m-%Y");
my $taxYear = 0; #Year for which to calculate the interest earned, for taxes
if (scalar(@ARGV) == 2)
{
my ($thearg) = shift;
if ($thearg eq "-y")
{
#Compound interest yearly instead of the default
$compoundedEvery = 12;
}
elsif ($thearg =~ /([0-9A-Za-z-]*)/)
{
#Pretend the current date is something else (mainly for debugging)
$currentDate = Time::Piece->strptime($1, "%d-%b-%Y");
}
}
elsif (scalar(@ARGV) == 3)
{
if ($ARGV[0] eq "-t")
{
shift;
$taxYear = $ARGV[0] + 0;
shift;
}
else
{
die "Usage: $0 FILENAME\n";
}
}
my $filename = shift or die "Usage: $0 FILENAME\n";
open(DATA, "<" . $filename) or die "File not found.\n";
#Read interest rate from the top of the input
$_ = <DATA>;
if ($_ && /^InterestRate (.*)/)
{
$interestRate = $1 + 0;
print;
}
else
{
die "Error: invalid interest rate at start\n";
}
#Convert from dollars in the input file to pennies for calculations
sub toPennies {
my $amt = shift;
return floor($amt * 100 + 0.5);
}
#Convert from pennies to dollars for printing
sub printMoney {
my $amtInPennies = shift;
return $amtInPennies / 100.0;
}
my $dateItr; #Iterator in the below loop
#Read loan amount on the second line
$_ = <DATA>;
if($_ && /^([0-9.]*) (.*)/)
{
#Amount of the loan on a particular date
$loan = toPennies($1);
$dateItr = Time::Piece->strptime($2, "%d-%b-%Y");
print;
}
else
{
die "Error: invalid second line\n";
}
my $targetDate = $dateItr; #The date we have to process interest up until,
#before moving to the next line of the input.
#The date we have to accumulate interest until, at which point we
#pay out (compound) the interest and reset it.
my $nextInterestPayment = $dateItr->add_months($compoundedEvery);
my $lineno = 2; #Line number for error reporting
my $interest = 0; #Interest accumulated
my $endOfInput = 0; #Boolean value indicating whether we've reached the end of the input
my $paidBackRep = 0;
my $paidBackRepAmt = 0;
my $totalInterest = 0;
#Pay out interest when it's reached the compounding date
sub payoutInterest {
$interest = floor($interest); #Discard fractions of pennies
if ($interest == 0)
{
return;
}
print "Interest " . printMoney($interest) . "\n";
if ($dateItr->year == ($taxYear))
{
$totalInterest += $interest;
}
$loan += $interest;
$interest = 0;
print printMoney($loan) . " " . $dateItr->strftime("%d-%b-%Y") . "\n";
}
while ($dateItr < $currentDate)
{
#These are the values for PaidBack lines
my $paidBackAmt = 0; #The amount paid back
if ($paidBackRep != 0)
{
#Regular repayments - keep accumulating them
$paidBackAmt = $paidBackRepAmt;
if ($paidBackRep != -1)
{
--$paidBackRep;
}
$targetDate = $targetDate->add_months(1);
}
elsif (!$endOfInput and ($_ = <DATA>))
{
++$lineno;
if(/^([0-9.-]*) (.*)/)
{
#Amount of the loan on a particular date
#Ignore these lines - we'll be outputting this ourself
#They're here so that the bank statement can be re-input into the program again
}
elsif (/^Interest (.*)/)
{
#Interest lines - also ignored for the same reason
}
elsif(/^PaidBack ([0-9.-]*) (.*)/)
{
$targetDate = Time::Piece->strptime($2, "%d-%b-%Y");
$paidBackAmt = $1;
#Paid back amounts printed and handled below
}
elsif(/^PayBackRegular ([0-9.-]*) (\d*) ([0-9A-Za-z-]*)/)
{
#Pay back regular amounts for a set number of payments
$paidBackRepAmt = $1;
$paidBackRep = $2;
$targetDate = Time::Piece->strptime($3, "%d-%b-%Y");
$paidBackAmt = $paidBackRepAmt;
--$paidBackRep;
}
elsif(/^PayBackRegular ([0-9.-]*) ([0-9A-Za-z-]*)/)
{
#Pay back regular amounts forever
$paidBackRepAmt = $1;
$paidBackRep = -1;
$targetDate = Time::Piece->strptime($2, "%d-%b-%Y");
$paidBackAmt = $paidBackRepAmt;
}
elsif ($_ eq "\n")
{
#Ignore blank lines
}
else
{
die "Error: invalid entry at line " . $lineno . "\n";
}
}
else
{
#No more input - but keep processing and adding interest up to the current date.
$endOfInput = 1;
$targetDate = $currentDate;
}
#Keep increasing the date, calculating interest as you go
while ($dateItr < $targetDate and $dateItr < $currentDate)
{
$dateItr += ONE_DAY;
#Accumulate interest for one day
if ($loan > 0)
{
$interest += $loan * ($interestRate / 100.0) / 365.25;
}
if ($dateItr == $nextInterestPayment)
{
payoutInterest;
#Set the next point where interest is added
$nextInterestPayment = $nextInterestPayment->add_months($compoundedEvery);
}
}
if ($paidBackAmt != 0 and $targetDate <= $currentDate)
{
#An amount was paid back
$loan -= toPennies($paidBackAmt);
print "PaidBack " . $paidBackAmt . " "
. $targetDate->strftime("%d-%b-%Y") . "\n";
#Print the current loan value
print printMoney($loan) . " " . $dateItr->strftime("%d-%b-%Y") . "\n";
}
}
#Put the regular payments at the end of the file if there are still some
if ($paidBackRep == -1)
{
#$targetDate = $targetDate->add_months(1);
print "PayBackRegular " . $paidBackRepAmt . " "
. $targetDate->strftime("%d-%b-%Y") . "\n";
}
elsif ($paidBackRep != 0)
{
$paidBackRep = $paidBackRep + 1;
print "PayBackRegular " . $paidBackRepAmt . " " . $paidBackRep . " "
. $targetDate->strftime("%d-%b-%Y") . "\n";
}
#Print the final amount, adding the fraction of interest that has accumulated since
#the last interest payout.
payoutInterest;
if ($taxYear > 0)
{
print "Interest for year " . $taxYear . ": " . printMoney($totalInterest) . "\n";
}
#Print a newline at the end, to make the output text file nicer
print "\n";