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;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   8  
   9  class Single
  10  {
  11      /**
  12       * FVSCHEDULE.
  13       *
  14       * Returns the future value of an initial principal after applying a series of compound interest rates.
  15       * Use FVSCHEDULE to calculate the future value of an investment with a variable or adjustable rate.
  16       *
  17       * Excel Function:
  18       *        FVSCHEDULE(principal,schedule)
  19       *
  20       * @param mixed $principal the present value
  21       * @param float[] $schedule an array of interest rates to apply
  22       *
  23       * @return float|string
  24       */
  25      public static function futureValue($principal, $schedule)
  26      {
  27          $principal = Functions::flattenSingleValue($principal);
  28          $schedule = Functions::flattenArray($schedule);
  29  
  30          try {
  31              $principal = CashFlowValidations::validateFloat($principal);
  32  
  33              foreach ($schedule as $rate) {
  34                  $rate = CashFlowValidations::validateFloat($rate);
  35                  $principal *= 1 + $rate;
  36              }
  37          } catch (Exception $e) {
  38              return $e->getMessage();
  39          }
  40  
  41          return $principal;
  42      }
  43  
  44      /**
  45       * PDURATION.
  46       *
  47       * Calculates the number of periods required for an investment to reach a specified value.
  48       *
  49       * @param mixed $rate Interest rate per period
  50       * @param mixed $presentValue Present Value
  51       * @param mixed $futureValue Future Value
  52       *
  53       * @return float|string Result, or a string containing an error
  54       */
  55      public static function periods($rate, $presentValue, $futureValue)
  56      {
  57          $rate = Functions::flattenSingleValue($rate);
  58          $presentValue = Functions::flattenSingleValue($presentValue);
  59          $futureValue = Functions::flattenSingleValue($futureValue);
  60  
  61          try {
  62              $rate = CashFlowValidations::validateRate($rate);
  63              $presentValue = CashFlowValidations::validatePresentValue($presentValue);
  64              $futureValue = CashFlowValidations::validateFutureValue($futureValue);
  65          } catch (Exception $e) {
  66              return $e->getMessage();
  67          }
  68  
  69          // Validate parameters
  70          if ($rate <= 0.0 || $presentValue <= 0.0 || $futureValue <= 0.0) {
  71              return ExcelError::NAN();
  72          }
  73  
  74          return (log($futureValue) - log($presentValue)) / log(1 + $rate);
  75      }
  76  
  77      /**
  78       * RRI.
  79       *
  80       * Calculates the interest rate required for an investment to grow to a specified future value .
  81       *
  82       * @param float $periods The number of periods over which the investment is made
  83       * @param float $presentValue Present Value
  84       * @param float $futureValue Future Value
  85       *
  86       * @return float|string Result, or a string containing an error
  87       */
  88      public static function interestRate($periods = 0.0, $presentValue = 0.0, $futureValue = 0.0)
  89      {
  90          $periods = Functions::flattenSingleValue($periods);
  91          $presentValue = Functions::flattenSingleValue($presentValue);
  92          $futureValue = Functions::flattenSingleValue($futureValue);
  93  
  94          try {
  95              $periods = CashFlowValidations::validateFloat($periods);
  96              $presentValue = CashFlowValidations::validatePresentValue($presentValue);
  97              $futureValue = CashFlowValidations::validateFutureValue($futureValue);
  98          } catch (Exception $e) {
  99              return $e->getMessage();
 100          }
 101  
 102          // Validate parameters
 103          if ($periods <= 0.0 || $presentValue <= 0.0 || $futureValue < 0.0) {
 104              return ExcelError::NAN();
 105          }
 106  
 107          return ($futureValue / $presentValue) ** (1 / $periods) - 1;
 108      }
 109  }