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
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
10
|
|
|
use 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
|
|
|
*/ |
36
|
7 |
|
public static function discount( |
37
|
|
|
mixed $settlement, |
38
|
|
|
mixed $maturity, |
39
|
|
|
mixed $price, |
40
|
|
|
mixed $redemption, |
41
|
|
|
mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
42
|
|
|
): float|string { |
43
|
7 |
|
$settlement = Functions::flattenSingleValue($settlement); |
44
|
7 |
|
$maturity = Functions::flattenSingleValue($maturity); |
45
|
7 |
|
$price = Functions::flattenSingleValue($price); |
46
|
7 |
|
$redemption = Functions::flattenSingleValue($redemption); |
47
|
7 |
|
$basis = ($basis === null) |
48
|
1 |
|
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
49
|
6 |
|
: Functions::flattenSingleValue($basis); |
50
|
|
|
|
51
|
|
|
try { |
52
|
7 |
|
$settlement = SecurityValidations::validateSettlementDate($settlement); |
53
|
6 |
|
$maturity = SecurityValidations::validateMaturityDate($maturity); |
54
|
6 |
|
SecurityValidations::validateSecurityPeriod($settlement, $maturity); |
55
|
6 |
|
$price = SecurityValidations::validatePrice($price); |
56
|
5 |
|
$redemption = SecurityValidations::validateRedemption($redemption); |
57
|
5 |
|
$basis = SecurityValidations::validateBasis($basis); |
58
|
2 |
|
} catch (Exception $e) { |
59
|
2 |
|
return $e->getMessage(); |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
if ($price <= 0.0) { |
63
|
1 |
|
return ExcelError::NAN(); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); |
67
|
4 |
|
if (!is_numeric($daysBetweenSettlementAndMaturity)) { |
68
|
|
|
// return date error |
69
|
|
|
return StringHelper::convertToString($daysBetweenSettlementAndMaturity); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
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
|
7 |
|
public static function interest( |
98
|
|
|
mixed $settlement, |
99
|
|
|
mixed $maturity, |
100
|
|
|
mixed $investment, |
101
|
|
|
mixed $redemption, |
102
|
|
|
mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
103
|
|
|
): float|string { |
104
|
7 |
|
$settlement = Functions::flattenSingleValue($settlement); |
105
|
7 |
|
$maturity = Functions::flattenSingleValue($maturity); |
106
|
7 |
|
$investment = Functions::flattenSingleValue($investment); |
107
|
7 |
|
$redemption = Functions::flattenSingleValue($redemption); |
108
|
7 |
|
$basis = ($basis === null) |
109
|
1 |
|
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD |
110
|
6 |
|
: Functions::flattenSingleValue($basis); |
111
|
|
|
|
112
|
|
|
try { |
113
|
7 |
|
$settlement = SecurityValidations::validateSettlementDate($settlement); |
114
|
6 |
|
$maturity = SecurityValidations::validateMaturityDate($maturity); |
115
|
6 |
|
SecurityValidations::validateSecurityPeriod($settlement, $maturity); |
116
|
6 |
|
$investment = SecurityValidations::validateFloat($investment); |
117
|
6 |
|
$redemption = SecurityValidations::validateRedemption($redemption); |
118
|
5 |
|
$basis = SecurityValidations::validateBasis($basis); |
119
|
3 |
|
} catch (Exception $e) { |
120
|
3 |
|
return $e->getMessage(); |
121
|
|
|
} |
122
|
|
|
|
123
|
4 |
|
if ($investment <= 0) { |
124
|
|
|
return ExcelError::NAN(); |
125
|
|
|
} |
126
|
|
|
|
127
|
4 |
|
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); |
128
|
4 |
|
if (!is_numeric($daysBetweenSettlementAndMaturity)) { |
129
|
|
|
// return date error |
130
|
|
|
return StringHelper::convertToString($daysBetweenSettlementAndMaturity); |
131
|
|
|
} |
132
|
|
|
|
133
|
4 |
|
return (($redemption / $investment) - 1) / ($daysBetweenSettlementAndMaturity); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|