Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\Securities;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   9  
  10  class Rates
  11  {
  12      /**
  13       * DISC.
  14       *
  15       * Returns the discount rate for a security.
  16       *
  17       * Excel Function:
  18       *        DISC(settlement,maturity,price,redemption[,basis])
  19       *
  20       * @param mixed $settlement The security's settlement date.
  21       *                              The security settlement date is the date after the issue
  22       *                                  date when the security is traded to the buyer.
  23       * @param mixed $maturity The security's maturity date.
  24       *                            The maturity date is the date when the security expires.
  25       * @param mixed $price The security's price per $100 face value
  26       * @param mixed $redemption The security's redemption value per $100 face value
  27       * @param mixed $basis The type of day count to use.
  28       *                         0 or omitted    US (NASD) 30/360
  29       *                         1               Actual/actual
  30       *                         2               Actual/360
  31       *                         3               Actual/365
  32       *                         4               European 30/360
  33       *
  34       * @return float|string
  35       */
  36      public static function discount(
  37          $settlement,
  38          $maturity,
  39          $price,
  40          $redemption,
  41          $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
  42      ) {
  43          $settlement = Functions::flattenSingleValue($settlement);
  44          $maturity = Functions::flattenSingleValue($maturity);
  45          $price = Functions::flattenSingleValue($price);
  46          $redemption = Functions::flattenSingleValue($redemption);
  47          $basis = ($basis === null)
  48              ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
  49              : Functions::flattenSingleValue($basis);
  50  
  51          try {
  52              $settlement = SecurityValidations::validateSettlementDate($settlement);
  53              $maturity = SecurityValidations::validateMaturityDate($maturity);
  54              SecurityValidations::validateSecurityPeriod($settlement, $maturity);
  55              $price = SecurityValidations::validatePrice($price);
  56              $redemption = SecurityValidations::validateRedemption($redemption);
  57              $basis = SecurityValidations::validateBasis($basis);
  58          } catch (Exception $e) {
  59              return $e->getMessage();
  60          }
  61  
  62          if ($price <= 0.0) {
  63              return Functions::NAN();
  64          }
  65  
  66          $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
  67          if (!is_numeric($daysBetweenSettlementAndMaturity)) {
  68              //    return date error
  69              return $daysBetweenSettlementAndMaturity;
  70          }
  71  
  72          return (1 - $price / $redemption) / $daysBetweenSettlementAndMaturity;
  73      }
  74  
  75      /**
  76       * INTRATE.
  77       *
  78       * Returns the interest rate for a fully invested security.
  79       *
  80       * Excel Function:
  81       *        INTRATE(settlement,maturity,investment,redemption[,basis])
  82       *
  83       * @param mixed $settlement The security's settlement date.
  84       *                              The security settlement date is the date after the issue date when the security
  85       *                                  is traded to the buyer.
  86       * @param mixed $maturity The security's maturity date.
  87       *                            The maturity date is the date when the security expires.
  88       * @param mixed $investment the amount invested in the security
  89       * @param mixed $redemption the amount to be received at maturity
  90       * @param mixed $basis The type of day count to use.
  91       *                         0 or omitted    US (NASD) 30/360
  92       *                         1               Actual/actual
  93       *                         2               Actual/360
  94       *                         3               Actual/365
  95       *                         4               European 30/360
  96       *
  97       * @return float|string
  98       */
  99      public static function interest(
 100          $settlement,
 101          $maturity,
 102          $investment,
 103          $redemption,
 104          $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
 105      ) {
 106          $settlement = Functions::flattenSingleValue($settlement);
 107          $maturity = Functions::flattenSingleValue($maturity);
 108          $investment = Functions::flattenSingleValue($investment);
 109          $redemption = Functions::flattenSingleValue($redemption);
 110          $basis = ($basis === null)
 111              ? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
 112              : Functions::flattenSingleValue($basis);
 113  
 114          try {
 115              $settlement = SecurityValidations::validateSettlementDate($settlement);
 116              $maturity = SecurityValidations::validateMaturityDate($maturity);
 117              SecurityValidations::validateSecurityPeriod($settlement, $maturity);
 118              $investment = SecurityValidations::validateFloat($investment);
 119              $redemption = SecurityValidations::validateRedemption($redemption);
 120              $basis = SecurityValidations::validateBasis($basis);
 121          } catch (Exception $e) {
 122              return $e->getMessage();
 123          }
 124  
 125          if ($investment <= 0) {
 126              return Functions::NAN();
 127          }
 128  
 129          $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis);
 130          if (!is_numeric($daysBetweenSettlementAndMaturity)) {
 131              //    return date error
 132              return $daysBetweenSettlementAndMaturity;
 133          }
 134  
 135          return (($redemption / $investment) - 1) / ($daysBetweenSettlementAndMaturity);
 136      }
 137  }