File Coverage

blib/lib/Finance/Loan/Repayment.pm
Criterion Covered Total %
statement 18 19 94.7
branch 9 10 90.0
condition 3 6 50.0
subroutine 4 4 100.0
pod 2 2 100.0
total 36 41 87.8


line stmt bran cond sub pod time code
1             package Finance::Loan::Repayment;
2 1     1   46729 use Moose;
  1         366570  
  1         7  
3              
4             # ABSTRACT: Play with loans, rates and repayment options
5             our $VERSION = '1.4';
6              
7             has loan => (
8             is => 'rw',
9             isa => 'Num',
10             required => 1,
11             );
12              
13             has rate => (
14             is => 'ro',
15             isa => 'Num',
16             required => 1,
17             );
18              
19             has principal_payment => (
20             is => 'ro',
21             isa => 'Num',
22             );
23              
24             has interest_off => (
25             is => 'ro',
26             isa => 'Num',
27             );
28              
29             has total_payment => (
30             is => 'ro',
31             isa => 'Num',
32             );
33              
34             sub interest_per_month {
35 8     8 1 1110 my $self = shift;
36 8   66     109 my $loan = shift // $self->loan;
37 8         210 return $loan * ($self->rate / 100 / 12);
38             }
39              
40             sub principal_per_month {
41 5     5 1 1078 my $self = shift;
42 5   33     153 my $loan = shift // $self->loan;
43              
44 5         14 my $interest = $self->interest_per_month($loan);
45              
46 5 100       129 if ($self->principal_payment) {
    100          
    100          
47 2         49 return $self->_check_payment_vs_loan($self->principal_payment, $loan);
48             }
49             elsif ($self->total_payment) {
50 1 50       31 if ($self->total_payment < $interest) {
51 0         0 return $interest + .01;
52             }
53 1         41 return $self->_check_payment_vs_loan(
54             $self->total_payment - $interest,
55             $loan
56             );
57             }
58             elsif ($self->interest_off) {
59 1         23 my $new_loan = (($interest - $self->interest_off) * 12)
60             / ($self->rate / 100);
61 1         4 return $self->_check_payment_vs_loan($loan - $new_loan, $loan);
62             }
63 1         3 return 0;
64             }
65              
66             sub _check_payment_vs_loan {
67 4     4   9 my ($self, $payment, $loan) = @_;
68 4 100       16 return ($payment > $loan) ? $loan : $payment;
69             }
70              
71              
72             __PACKAGE__->meta->make_immutable;
73              
74             __END__
75              
76             =pod
77              
78             =encoding UTF-8
79              
80             =head1 NAME
81              
82             Finance::Loan::Repayment - Play with loans, rates and repayment options
83              
84             =head1 VERSION
85              
86             version 1.4
87              
88             =head1 SYNOPSIS
89              
90             use Finance::Loan::Repayment;
91              
92             my $calc = Finance::Loan::Repayment->new(
93             loan => 100,
94             rate => 5,
95              
96             # The following parameters are optional
97             # Reduce interest by 1 each month
98             interest_off => 1,
99              
100             # Principal payment per month
101             principal_payment => 30,
102              
103             # Total amount to pay per month
104             total_payment => 30,
105              
106             );
107              
108             =head1 DESCRIPTION
109              
110             A module to calculate interest per month and principal payments per month
111              
112             =head1 ATTRIBUTES
113              
114             =head2 loan
115              
116             The loan amount, required.
117              
118             =head2 rate
119              
120             The interest rate of the loan, required.
121              
122             =head1 Attributes changing the way the C<principal_payment_per_month> functions works
123              
124             The following attributes will alter how the principal payment per month
125             function will work.
126              
127             =head2 principal_payment
128              
129             The amount you want to pay off your loan each month. This changes the
130             total costs per month and the interest you pay.
131              
132             =head2 interest_off
133              
134             The amount you want to pay off your interest each month. This changes the
135             total costs per month and the interest you pay. This will make your
136             additional payment steady.
137              
138             =head2 total_payment
139              
140             The amount you want to pay off each month. This will influence the interest
141             you pay and the principal payment.
142              
143             =head1 METHODS
144              
145             =head2 interest_per_month
146              
147             $calc->interest_per_month();
148             $calc->interest_per_month(1000);
149              
150             Calculates the interest amount per month on the loan. An optional loan
151             parameter can be used.
152              
153             =head2 principal_per_month()
154              
155             $calc->principal_per_month();
156             $calc->principal_per_month(1000);
157              
158             Calculates the principal payments per month based on the constructor
159             arguments. An optional loan parameter can be used.
160              
161             =head1 SEE ALSO
162              
163             =over
164              
165             =item L<Finance::Amortization>
166              
167             This does more or less the same thing as this module
168              
169             =item L<Finance::Loan>
170              
171             =item L<Finance::Loan::Private>
172              
173             =back
174              
175             =head1 AUTHOR
176              
177             Wesley Schwengle <waterkip@cpan.org>
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             This software is Copyright (c) 2017 by Wesley Schwengle.
182              
183             This is free software, licensed under:
184              
185             The MIT (X11) License
186              
187             =cut