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   78273 use Moose;
  1         603186  
  1         16  
3              
4             # ABSTRACT: Play with loans, rates and repayment options
5             our $VERSION = '1.1';
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 1747 my $self = shift;
36 8   66     169 my $loan = shift // $self->loan;
37 8         344 return $loan * ($self->rate / 100 / 12);
38             }
39              
40             sub principal_per_month {
41 5     5 1 1743 my $self = shift;
42 5   33     243 my $loan = shift // $self->loan;
43              
44 5         19 my $interest = $self->interest_per_month($loan);
45              
46 5 100       222 if ($self->principal_payment) {
    100          
    100          
47 2         83 return $self->_check_payment_vs_loan($self->principal_payment, $loan);
48             }
49             elsif ($self->total_payment) {
50 1 50       46 if ($self->total_payment < $interest) {
51 0         0 return $interest + .01;
52             }
53 1         43 return $self->_check_payment_vs_loan(
54             $self->total_payment - $interest,
55             $loan
56             );
57             }
58             elsif ($self->interest_off) {
59 1         44 my $new_loan = (($interest - $self->interest_off) * 12)
60             / ($self->rate / 100);
61 1         7 return $self->_check_payment_vs_loan($loan - $new_loan, $loan);
62             }
63 1         5 return 0;
64             }
65              
66             sub _check_payment_vs_loan {
67 4     4   14 my ($self, $payment, $loan) = @_;
68 4 100       24 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.1
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
178              
179             =head1 LICENSE and COPYRIGHT
180              
181             Wesley Schwengle, 2017.
182              
183             =head1 AUTHOR
184              
185             Wesley Schwengle <waterkip@cpan.org>
186              
187             =head1 COPYRIGHT AND LICENSE
188              
189             This software is Copyright (c) 2017 by Wesley Schwengle.
190              
191             This is free software, licensed under:
192              
193             The MIT (X11) License
194              
195             =cut