You are here

class MortgageCalculatorCalculation in Real Estate Mortgage Calculator 8

MortgageCalculatorCalculation.

Hierarchy

Expanded class hierarchy of MortgageCalculatorCalculation

1 file declares its use of MortgageCalculatorCalculation
mortgage_calculator.module in ./mortgage_calculator.module
Mortgage Calculator module file.

File

src/MortgageCalculatorCalculation.php, line 8

Namespace

Drupal\mortgage_calculator
View source
class MortgageCalculatorCalculation {

  /**
   * A loan amount.
   *
   * @var string
   */
  private $loanAmount;

  /**
   * A mortgage rate.
   *
   * @var string
   */
  private $mortgageRate;

  /**
   * Years to pay.
   *
   * @var string
   */
  private $yearsToPay;

  /**
   * Possible values 'monthly' or 'yearly'.
   *
   * @var string
   */
  private $desiredDisplay;

  /**
   * Construct.
   *
   * @Param int $loan_amount
   *   - a loan amount
   * @Param int $mortgage_rate
   *   - a mortgage rate
   * @Param int $years_to_pay
   *   - years to pay
   * @Param string $desired_display
   *   - possible values 'monthly' or 'yearly'
   */
  public function __construct($loan_amount, $mortgage_rate, $years_to_pay, $desired_display) {
    $this->loanAmount = $loan_amount;
    $this->mortgageRate = $mortgage_rate;
    $this->yearsToPay = $years_to_pay;
    $this->desiredDisplay = $desired_display;
  }

  /**
   * Calculation function.
   *
   * @return array
   *   - a key 'rows' contains array of rows with $desired_display mortgage
   *     calculations
   *   - a key 'number_of_payments' - a number of payments
   *   - a key 'payment' - an amount of payments
   */
  public function calculate() {
    if ($this->desiredDisplay == 'monthly') {
      $rate_per = $this->mortgageRate / 100 / 12;
      $number_of_payments = $this->yearsToPay * 12;
    }
    else {
      $rate_per = $this->mortgageRate / 100;
      $number_of_payments = $this->yearsToPay;
    }
    if ($this->mortgageRate != 0) {
      $payment = $this->loanAmount * pow(1 + $rate_per, $number_of_payments) * $rate_per / (pow(1 + $rate_per, $number_of_payments) - 1);
    }
    else {
      $payment = $this->loanAmount / $number_of_payments;
    }
    $rows = [];
    $beginning_balance = $this->loanAmount;
    for ($i = 1; $i <= $number_of_payments; $i++) {
      $interest = $rate_per * $beginning_balance;
      $rows[] = [
        $i,
        round($beginning_balance),
        round($interest),
        round($payment),
        abs(round($beginning_balance - ($payment - $interest))),
      ];
      $beginning_balance -= $payment - $interest;
    }
    return [
      'rows' => $rows,
      'row' => [
        'number_of_payments' => $number_of_payments,
        'payment' => round($payment),
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MortgageCalculatorCalculation::$desiredDisplay private property Possible values 'monthly' or 'yearly'.
MortgageCalculatorCalculation::$loanAmount private property A loan amount.
MortgageCalculatorCalculation::$mortgageRate private property A mortgage rate.
MortgageCalculatorCalculation::$yearsToPay private property Years to pay.
MortgageCalculatorCalculation::calculate public function Calculation function.
MortgageCalculatorCalculation::__construct public function Construct.