| 1 | package Finance::Loan::Repayment; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Play with loans, rates and repayment options | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = '1.5'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | has loan => ( | |||||||||||||||||||||||||||||||||||||||||
| 8 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 9 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 10 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | has rate => ( | |||||||||||||||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 15 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | has duration => ( | |||||||||||||||||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | has principal_payment => ( | |||||||||||||||||||||||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | has interest_off => ( | |||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | has total_payment => ( | |||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 6 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 6 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 6 | ▶ | if ($args{rate} && exists $args{duration} && !$args{total_payment}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$args{"rate"} and exists $args{"duration"} and not $args{"total_payment"}
$args{"rate"} and exists $args{"duration"}
MC/DC33%1/3
$args{"rate"} and exists $args{"duration"} and not $args{"total_payment"}
$args{"rate"}
exists $args{"duration"}
not $args{"total_payment"}
Truth table50%2/4
A and B and not(C)
A= $args{"rate"}B= exists $args{"duration"}C= not $args{"total_payment"}
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | my $monthly_rate = $args{rate} / 100 / 12; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | my $duration = $args{duration}; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | my $a = $args{loan} * ($monthly_rate * ((1 + $monthly_rate) ** $duration)); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | my $b = ((1 + $monthly_rate) ** $duration) - 1; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | $args{total_payment} = $a / $b; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 6 | return \%args; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | ▶ | sub _interest_per_month { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_interest_per_month: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | my $loan = shift; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 10 | my $rate = shift; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | return $loan * ($rate / 100 / 12); | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 10 | ▶ | sub interest_per_month { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
interest_per_month: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 10 | ▶ | return _interest_per_month(shift // $self->loan, $self->rate); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
shift() // $self->loan
MC/DC0%0/2
shift() // $self->loan
shift()
$self->loan
Truth table66%2/3
A // B
A= shift()B= $self->loan
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 6 | ▶ | sub principal_per_month { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
principal_per_month: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 6 | ▶ | my $loan = shift // $self->loan; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() // $self->loan
MC/DC0%0/2
shift() // $self->loan
shift()
$self->loan
Truth table33%1/3
A // B
A= shift()B= $self->loan
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | my $interest = $self->interest_per_month($loan); | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 6 | ▶ | if ($self->principal_payment) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | return $self->_check_payment_vs_loan($self->principal_payment, $loan); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | elsif ($self->total_payment) { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | if ($self->total_payment > $interest) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | return $self->_check_payment_vs_loan( | |||||||||||||||||||||||||||||||||||||||||
| 76 | $self->total_payment - $interest, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | $loan | ||||||||||||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | die "Total payment is too small to cover interest"; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | elsif ($self->interest_off) { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | my $new_loan = (($interest - $self->interest_off) * 12) | |||||||||||||||||||||||||||||||||||||||||
| 84 | / ($self->rate / 100); | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | return $self->_check_payment_vs_loan($loan - $new_loan, $loan); | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 5 | ▶ | sub _check_payment_vs_loan { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_payment_vs_loan: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 5 | my ($self, $payment, $loan) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 5 | ▶ | return ($payment > $loan) ? $loan : $payment; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||||||||||||||||||||||