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