PHP code example of phelix / loan-amortization

1. Go to this page and download the library: Download phelix/loan-amortization library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phelix / loan-amortization example snippets



 

    use Phelix\LoanAmortization\ScheduleGenerator;

    $interestCalculator = new ScheduleGenerator();
    
    $interestCalculator
                ->setPrincipal(50000)
                ->setInterestRate(12, "yearly", ScheduleGenerator::FLAT_INTEREST) // note the interest type
                ->setLoanDuration(1, "weeks") // could be "years" to have it in years or "months" to have it in months
                ->setRepayment(1,2, "days")
                ->generate();
    
    print "\nTotal Interest = {$interestCalculator->interest} \n";
    print "Effective Interest Rate = {$interestCalculator->effective_interest_rate} \n";
    print "Total Repayable amount = {$interestCalculator->amount} \n";
    print "Number of Installments = {$interestCalculator->no_installments} \n";
    print "Repayment Frequency = {$interestCalculator->repayment_frequency} \n";
    
    print "Amortization Schedule: \n";
    
    print_r($this->interestCalculator->amortization_schedule);
    
    // Sample Schedule Response:
    
    Array
    (
        [0] => Array
            (
                [principal_repayment] => 14285.71,
                [interest_repayment] => 32.88,
                [total_amount_repayment] => 14318.59,
                [principal_repayment_balance] => 35714.29
            ),
    
        [1] => Array
            (
                [principal_repayment] => 14285.71,
                [interest_repayment] => 32.88,
                [total_amount_repayment] => 14318.59,
                [principal_repayment_balance] => 21428.58,
            ),
    
        [2] => Array
            (
                [principal_repayment] => 14285.71,
                [interest_repayment] => 32.88,
                [total_amount_repayment] => 14318.59,
                [principal_repayment_balance] => 7142.87,
            ),
    
        [3] => Array
            (
                [principal_repayment] => 7142.87,
                [interest_repayment] => 16.43,
                [total_amount_repayment] => 7159.3,
                [principal_repayment_balance] => 0
            )
    
    );



 

    use Phelix\LoanAmortization\ScheduleGenerator;

    $interestCalculator = new ScheduleGenerator();
    
    $interestCalculator
                ->setPrincipal(50000)
                ->setInterestRate(12, "yearly", ScheduleGenerator::INTEREST_ON_REDUCING_BALANCE) // note the interest type
                ->setLoanDuration(1, "weeks")
                ->setRepayment(1,2, "days")
                ->setAmortization(ScheduleGenerator::EVEN_PRINCIPAL_REPAYMENT) // note the amortization type
                ->generate();
    
    print "\nTotal Interest = {$interestCalculator->interest} \n";
    print "Effective Interest Rate = {$interestCalculator->effective_interest_rate} \n";
    print "Total Repayable amount = {$interestCalculator->amount} \n";
    print "Number of Installments = {$interestCalculator->no_installments} \n";
    print "Repayment Frequency = {$interestCalculator->repayment_frequency} \n";
    
    print "Amortization Schedule: \n";
    
    print_r($this->interestCalculator->amortization_schedule);
    
     // Sample Response
     Array
     (
         [0] => Array
             (
                 [principal_repayment] => 21428.57,
                 [interest_repayment] => 49.32,
                 [total_amount_repayment] => 21477.89,
                 [principal_repayment_balance] => 28571.43
             ),
     
         [1] => Array
             (
                 [principal_repayment] => 21428.57,
                 [interest_repayment] => 28.18,
                 [total_amount_repayment] => 21456.75,
                 [principal_repayment_balance] => 7142.86
             ),
     
         [2] => Array
             (
                 [principal_repayment] => 7142.86,
                 [interest_repayment] => 7.05,
                 [total_amount_repayment] => 7149.91,
                 [principal_repayment_balance] => 0
             )
     
     );




 

    use Phelix\LoanAmortization\ScheduleGenerator;

    $interestCalculator = new ScheduleGenerator();
    
    $interestCalculator
                ->setPrincipal(50000)
                ->setInterestRate(12, "yearly", ScheduleGenerator::INTEREST_ON_REDUCING_BALANCE) // note the interest type
                ->setLoanDuration(1, "weeks")
                ->setRepayment(1,2, "days")
                ->setAmortization(ScheduleGenerator::EVEN_INSTALLMENT_REPAYMENT) // note the amortization type
                ->generate();
    
    print "\nTotal Interest = {$interestCalculator->interest} \n";
    print "Effective Interest Rate = {$interestCalculator->effective_interest_rate} \n";
    print "Total Repayable amount = {$interestCalculator->amount} \n";
    print "Number of Installments = {$interestCalculator->no_installments} \n";
    print "Repayment Frequency = {$interestCalculator->repayment_frequency} \n";
    
    print "Amortization Schedule: \n";
    
    print_r($this->interestCalculator->amortization_schedule);
    
    // Sample Response
    Array
    (
        [0] => Array
            (
                [principal_repayment] => 16650.23,
                [interest_repayment] => 49.32,
                [total_amount_repayment] => 16699.55,
                [principal_repayment_balance] => 33349.77
            ),
    
        [1] => Array
            (
                [principal_repayment] => 16666.66,
                [interest_repayment] => 32.89,
                [total_amount_repayment] => 16699.55,
                [principal_repayment_balance] => 16683.11,
            ),
    
        [2] => Array
            (
                [principal_repayment] => 16683.11,
                [interest_repayment] => 16.45,
                [total_amount_repayment] => 16699.55,
                [principal_repayment_balance] => 0
            )
    
    );



 

    use Phelix\LoanAmortization\ScheduleGenerator;

    $interestCalculator = new ScheduleGenerator();
    
    $interestCalculator
                ->setPrincipal(50000)
                ->setInterestRate(12, "yearly", ScheduleGenerator::INTEREST_ON_REDUCING_BALANCE) // note the interest type
                ->setLoanDuration(1, "weeks")
                ->setRepayment(1,2, "days")
                ->setAmortization(ScheduleGenerator::EVEN_INSTALLMENT_REPAYMENT) // note the amortization type
                ->setGraceOnPrincipalRepayment(2) // note the grace period. First two principals' payment deferred
                ->setGraceOnInterestRepayment(1) // note the grace period. First month interest payment is deferred
                ->setGraceOnInterest(5, "days", 2, "days") // not how this is set. It reads: If you repay entire loan within 5 days, then no interest will be charged for the first two days
                ->generate();
    
    print "\nTotal Interest = {$interestCalculator->interest} \n";
    print "Effective Interest Rate = {$interestCalculator->effective_interest_rate} \n";
    print "Total Repayable amount = {$interestCalculator->amount} \n";
    print "Number of Installments = {$interestCalculator->no_installments} \n";
    print "Repayment Frequency = {$interestCalculator->repayment_frequency} \n";
    
    print "Amortization Schedule: \n";
    
    print_r($this->interestCalculator->amortization_schedule);