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;
   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  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  10  
  11  class TreasuryBill
  12  {
  13      /**
  14       * TBILLEQ.
  15       *
  16       * Returns the bond-equivalent yield for a Treasury bill.
  17       *
  18       * @param mixed $settlement The Treasury bill's settlement date.
  19       *                                The Treasury bill's settlement date is the date after the issue date
  20       *                                    when the Treasury bill is traded to the buyer.
  21       * @param mixed $maturity The Treasury bill's maturity date.
  22       *                                The maturity date is the date when the Treasury bill expires.
  23       * @param mixed $discount The Treasury bill's discount rate
  24       *
  25       * @return float|string Result, or a string containing an error
  26       */
  27      public static function bondEquivalentYield($settlement, $maturity, $discount)
  28      {
  29          $settlement = Functions::flattenSingleValue($settlement);
  30          $maturity = Functions::flattenSingleValue($maturity);
  31          $discount = Functions::flattenSingleValue($discount);
  32  
  33          try {
  34              $settlement = FinancialValidations::validateSettlementDate($settlement);
  35              $maturity = FinancialValidations::validateMaturityDate($maturity);
  36              $discount = FinancialValidations::validateFloat($discount);
  37          } catch (Exception $e) {
  38              return $e->getMessage();
  39          }
  40  
  41          if ($discount <= 0) {
  42              return ExcelError::NAN();
  43          }
  44  
  45          $daysBetweenSettlementAndMaturity = $maturity - $settlement;
  46          $daysPerYear = Helpers::daysPerYear(
  47              Functions::scalar(DateTimeExcel\DateParts::year($maturity)),
  48              FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL
  49          );
  50  
  51          if ($daysBetweenSettlementAndMaturity > $daysPerYear || $daysBetweenSettlementAndMaturity < 0) {
  52              return ExcelError::NAN();
  53          }
  54  
  55          return (365 * $discount) / (360 - $discount * $daysBetweenSettlementAndMaturity);
  56      }
  57  
  58      /**
  59       * TBILLPRICE.
  60       *
  61       * Returns the price per $100 face value for a Treasury bill.
  62       *
  63       * @param mixed $settlement The Treasury bill's settlement date.
  64       *                                The Treasury bill's settlement date is the date after the issue date
  65       *                                    when the Treasury bill is traded to the buyer.
  66       * @param mixed $maturity The Treasury bill's maturity date.
  67       *                                The maturity date is the date when the Treasury bill expires.
  68       * @param mixed $discount The Treasury bill's discount rate
  69       *
  70       * @return float|string Result, or a string containing an error
  71       */
  72      public static function price($settlement, $maturity, $discount)
  73      {
  74          $settlement = Functions::flattenSingleValue($settlement);
  75          $maturity = Functions::flattenSingleValue($maturity);
  76          $discount = Functions::flattenSingleValue($discount);
  77  
  78          try {
  79              $settlement = FinancialValidations::validateSettlementDate($settlement);
  80              $maturity = FinancialValidations::validateMaturityDate($maturity);
  81              $discount = FinancialValidations::validateFloat($discount);
  82          } catch (Exception $e) {
  83              return $e->getMessage();
  84          }
  85  
  86          if ($discount <= 0) {
  87              return ExcelError::NAN();
  88          }
  89  
  90          $daysBetweenSettlementAndMaturity = $maturity - $settlement;
  91          $daysPerYear = Helpers::daysPerYear(
  92              Functions::scalar(DateTimeExcel\DateParts::year($maturity)),
  93              FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL
  94          );
  95  
  96          if ($daysBetweenSettlementAndMaturity > $daysPerYear || $daysBetweenSettlementAndMaturity < 0) {
  97              return ExcelError::NAN();
  98          }
  99  
 100          $price = 100 * (1 - (($discount * $daysBetweenSettlementAndMaturity) / 360));
 101          if ($price < 0.0) {
 102              return ExcelError::NAN();
 103          }
 104  
 105          return $price;
 106      }
 107  
 108      /**
 109       * TBILLYIELD.
 110       *
 111       * Returns the yield for a Treasury bill.
 112       *
 113       * @param mixed $settlement The Treasury bill's settlement date.
 114       *                                The Treasury bill's settlement date is the date after the issue date when
 115       *                                    the Treasury bill is traded to the buyer.
 116       * @param mixed $maturity The Treasury bill's maturity date.
 117       *                                The maturity date is the date when the Treasury bill expires.
 118       * @param mixed $price The Treasury bill's price per $100 face value
 119       *
 120       * @return float|string
 121       */
 122      public static function yield($settlement, $maturity, $price)
 123      {
 124          $settlement = Functions::flattenSingleValue($settlement);
 125          $maturity = Functions::flattenSingleValue($maturity);
 126          $price = Functions::flattenSingleValue($price);
 127  
 128          try {
 129              $settlement = FinancialValidations::validateSettlementDate($settlement);
 130              $maturity = FinancialValidations::validateMaturityDate($maturity);
 131              $price = FinancialValidations::validatePrice($price);
 132          } catch (Exception $e) {
 133              return $e->getMessage();
 134          }
 135  
 136          $daysBetweenSettlementAndMaturity = $maturity - $settlement;
 137          $daysPerYear = Helpers::daysPerYear(
 138              Functions::scalar(DateTimeExcel\DateParts::year($maturity)),
 139              FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL
 140          );
 141  
 142          if ($daysBetweenSettlementAndMaturity > $daysPerYear || $daysBetweenSettlementAndMaturity < 0) {
 143              return ExcelError::NAN();
 144          }
 145  
 146          return ((100 - $price) / $price) * (360 / $daysBetweenSettlementAndMaturity);
 147      }
 148  }