script/loan.pl untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 160 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2 use warnings;
3 use strict;
4  
5 # ABSTRACT: A simple loan calculator
6 # PODNAME: loan.pl
7  
8 use Getopt::Long;
9 use Pod::Usage;
10 use Config::Any;
11 use File::Spec::Functions qw(catfile);
12 use Finance::Loan::Repayment;
13  
14 my %opts = (
15 help => 0,
16 config => catfile($ENV{HOME}, qw (.config finance-loan-repayment loan.conf)),
17 );
18  
19 {
20 local $SIG{__WARN__};
21 my $ok = eval {
22 GetOptions(
23 \%opts, qw(
24 help
25 rate=s
26 loan=s
27 total=s
28 principal=s
29 interest=s
30 duration=i
31 interest-only
32 no_pay=i
33 config=s
34 summary
35 )
36 );
37 };
38 if (!$ok) {
39 die($@);
40 }
41 }
42  
43 pod2usage(0) if ($opts{help});
44  
45 if (-f $opts{config}) {
46 my $config = Config::Any->load_files({
47 files => [$opts{config}],
48 use_ext => 1,
49 flatten_hash => 1,
50  
51 })->[0]{$opts{config}};
52  
53 my @keys = qw(interest principal interest-only total);
54 foreach my $key (@keys) {
55 if ($opts{$key}) {
56 delete $config->{$_} for @keys;
57 last;
58 }
59 }
60 foreach (keys %opts) {
61 delete $config->{$_};
62 }
63  
64 foreach (keys %$config) {
65 # If an option is set multiple times in the config file, take
66 # the last value and work with that
67 $opts{$_} ||= ref $config->{$_} eq 'ARRAY'
68 ? $config->{$_}[-1]
69 : $config->{$_};
70 }
71 }
72  
73 my @required = qw(loan rate);
74 foreach (@required) {
75 if (!defined $opts{$_} || $opts{$_} <= 0) {
76 pod2usage(1);
77 }
78 }
79  
80 my $ok = 0;
81 my @optional = qw(total principal interest interest-only duration);
82 foreach (@optional) {
83 if (defined $opts{$_} && $opts{$_} > 0) {
84 $ok++;
85 }
86 }
87 if (!$ok) {
88 pod2usage(1);
89 }
90  
91 my $summary = $opts{summary};
92  
93 my %args = (
94 rate => $opts{rate},
95 loan => $opts{loan},
96 );
97  
98 if ($opts{'interest-only'}) {
99 my $calc = Finance::Loan::Repayment->new(%args);
100  
101 printf("Loan: %.2f\tRate: %.2f\tInterest: %.2f\n",
102 $calc->loan, $calc->rate, $calc->interest_per_month);
103  
104 exit 0;
105 }
106  
107 if ($opts{principal}) {
108 $args{principal_payment} = $opts{principal};
109 }
110 elsif ($opts{interest}) {
111 $args{interest_off} = $opts{interest};
112 }
113 elsif ($opts{total}) {
114 $args{total_payment} = $opts{total};
115 }
116 elsif ($opts{duration}) {
117 $args{duration} = $opts{duration};
118 }
119  
120 my $calc = Finance::Loan::Repayment->new(%args);
121  
122 my $loan = $calc->loan;
123 my $total_interest = 0;
124 my $total_payments = 0;
125 my $total_principal = 0;
126 my $payment = 0;
127 my $principal = 0;
128 my $interest = 0;
129 my $orig_loan = $loan;
130 my $counter = 0;
131 my $duration = $opts{duration};
132  
133 while ($calc->loan > 0) {
134  
135 $interest = $calc->interest_per_month($loan);
136 $principal = $calc->principal_per_month($loan);
137 $payment = $interest + $principal;
138  
139 $total_interest += $interest;
140 $total_payments += $payment;
141 $total_principal += $principal;
142 $loan -= $principal;
143  
144 printf("Loan: %.2f\tInterest: %.2f\tPrincipal: %.2f\tPay per month: %.2f\n",
145 $calc->loan, $interest, $principal, $payment) unless $summary;
146  
147 $calc->loan($loan);
148  
149 $counter++;
150 last if ($duration && $counter >= $duration);
151 if ($counter % 12 == 0) {
152 printf("%.2f years\tInterest: %.2f\tPrincipal: %.2f\tPaid in total: %.2f\n",
153 $counter / 12,
154 $total_interest,
155 $total_principal,
156 $total_payments,
157 );
158 }
159 }
160  
161 print "\n" unless $summary;
162 if ($duration) {
163 printf("Duration: %dm\tInterest: %.2f\tPrincipal: %.2f\tPaid in total: %.2f\n",
164 $counter,
165 $total_interest,
166 $total_principal,
167 $total_payments,
168 );
169 }
170 else {
171 if ($counter / 12 > 1) {
172 printf("%.2f years\tInterest: %.2f\tPrincipal: %.2f\tPaid in total: %.2f\n",
173 $counter / 12,
174 $total_interest,
175 $total_principal,
176 $total_payments,
177 );
178 }
179 else {
180 printf("%d months\tInterest: %.2f\tPrincipal: %.2f\tPaid in total: %.2f\n",
181 $counter,
182 $total_interest,
183 $total_principal,
184 $total_payments,
185 );
186 }
187 }
188  
189 __END__