Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\Constant\Periodic;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\CashFlowValidations;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   9  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  10  
  11  class Payments
  12  {
  13      /**
  14       * PMT.
  15       *
  16       * Returns the constant payment (annuity) for a cash flow with a constant interest rate.
  17       *
  18       * @param mixed $interestRate Interest rate per period
  19       * @param mixed $numberOfPeriods Number of periods
  20       * @param mixed $presentValue Present Value
  21       * @param mixed $futureValue Future Value
  22       * @param mixed $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
  23       *
  24       * @return float|string Result, or a string containing an error
  25       */
  26      public static function annuity(
  27          $interestRate,
  28          $numberOfPeriods,
  29          $presentValue,
  30          $futureValue = 0,
  31          $type = FinancialConstants::PAYMENT_END_OF_PERIOD
  32      ) {
  33          $interestRate = Functions::flattenSingleValue($interestRate);
  34          $numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
  35          $presentValue = Functions::flattenSingleValue($presentValue);
  36          $futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
  37          $type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
  38  
  39          try {
  40              $interestRate = CashFlowValidations::validateRate($interestRate);
  41              $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
  42              $presentValue = CashFlowValidations::validatePresentValue($presentValue);
  43              $futureValue = CashFlowValidations::validateFutureValue($futureValue);
  44              $type = CashFlowValidations::validatePeriodType($type);
  45          } catch (Exception $e) {
  46              return $e->getMessage();
  47          }
  48  
  49          // Calculate
  50          if ($interestRate != 0.0) {
  51              return (-$futureValue - $presentValue * (1 + $interestRate) ** $numberOfPeriods) /
  52                  (1 + $interestRate * $type) / (((1 + $interestRate) ** $numberOfPeriods - 1) / $interestRate);
  53          }
  54  
  55          return (-$presentValue - $futureValue) / $numberOfPeriods;
  56      }
  57  
  58      /**
  59       * PPMT.
  60       *
  61       * Returns the interest payment for a given period for an investment based on periodic, constant payments
  62       *         and a constant interest rate.
  63       *
  64       * @param mixed $interestRate Interest rate per period
  65       * @param mixed $period Period for which we want to find the interest
  66       * @param mixed $numberOfPeriods Number of periods
  67       * @param mixed $presentValue Present Value
  68       * @param mixed $futureValue Future Value
  69       * @param mixed $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
  70       *
  71       * @return float|string Result, or a string containing an error
  72       */
  73      public static function interestPayment(
  74          $interestRate,
  75          $period,
  76          $numberOfPeriods,
  77          $presentValue,
  78          $futureValue = 0,
  79          $type = FinancialConstants::PAYMENT_END_OF_PERIOD
  80      ) {
  81          $interestRate = Functions::flattenSingleValue($interestRate);
  82          $period = Functions::flattenSingleValue($period);
  83          $numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
  84          $presentValue = Functions::flattenSingleValue($presentValue);
  85          $futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
  86          $type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
  87  
  88          try {
  89              $interestRate = CashFlowValidations::validateRate($interestRate);
  90              $period = CashFlowValidations::validateInt($period);
  91              $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
  92              $presentValue = CashFlowValidations::validatePresentValue($presentValue);
  93              $futureValue = CashFlowValidations::validateFutureValue($futureValue);
  94              $type = CashFlowValidations::validatePeriodType($type);
  95          } catch (Exception $e) {
  96              return $e->getMessage();
  97          }
  98  
  99          // Validate parameters
 100          if ($period <= 0 || $period > $numberOfPeriods) {
 101              return ExcelError::NAN();
 102          }
 103  
 104          // Calculate
 105          $interestAndPrincipal = new InterestAndPrincipal(
 106              $interestRate,
 107              $period,
 108              $numberOfPeriods,
 109              $presentValue,
 110              $futureValue,
 111              $type
 112          );
 113  
 114          return $interestAndPrincipal->principal();
 115      }
 116  }