1441 |
ariadna |
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 |
|
|
|
11 |
class Rates
|
|
|
12 |
{
|
|
|
13 |
/**
|
|
|
14 |
* DISC.
|
|
|
15 |
*
|
|
|
16 |
* Returns the discount rate for a security.
|
|
|
17 |
*
|
|
|
18 |
* Excel Function:
|
|
|
19 |
* DISC(settlement,maturity,price,redemption[,basis])
|
|
|
20 |
*
|
|
|
21 |
* @param mixed $settlement The security's settlement date.
|
|
|
22 |
* The security settlement date is the date after the issue
|
|
|
23 |
* date when the security is traded to the buyer.
|
|
|
24 |
* @param mixed $maturity The security's maturity date.
|
|
|
25 |
* The maturity date is the date when the security expires.
|
|
|
26 |
* @param mixed $price The security's price per $100 face value
|
|
|
27 |
* @param mixed $redemption The security's redemption value per $100 face value
|
|
|
28 |
* @param mixed $basis The type of day count to use.
|
|
|
29 |
* 0 or omitted US (NASD) 30/360
|
|
|
30 |
* 1 Actual/actual
|
|
|
31 |
* 2 Actual/360
|
|
|
32 |
* 3 Actual/365
|
|
|
33 |
* 4 European 30/360
|
|
|
34 |
*/
|
|
|
35 |
public static function discount(
|
|
|
36 |
mixed $settlement,
|
|
|
37 |
mixed $maturity,
|
|
|
38 |
mixed $price,
|
|
|
39 |
mixed $redemption,
|
|
|
40 |
mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
|
|
|
41 |
): float|string {
|
|
|
42 |
$settlement = Functions::flattenSingleValue($settlement);
|
|
|
43 |
$maturity = Functions::flattenSingleValue($maturity);
|
|
|
44 |
$price = Functions::flattenSingleValue($price);
|
|
|
45 |
$redemption = Functions::flattenSingleValue($redemption);
|
|
|
46 |
$basis = ($basis === null)
|
|
|
47 |
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
|
|
|
48 |
: Functions::flattenSingleValue($basis);
|
|
|
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 $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 |
*/
|
|
|
96 |
public static function interest(
|
|
|
97 |
mixed $settlement,
|
|
|
98 |
mixed $maturity,
|
|
|
99 |
mixed $investment,
|
|
|
100 |
mixed $redemption,
|
|
|
101 |
mixed $basis = FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
|
|
|
102 |
): float|string {
|
|
|
103 |
$settlement = Functions::flattenSingleValue($settlement);
|
|
|
104 |
$maturity = Functions::flattenSingleValue($maturity);
|
|
|
105 |
$investment = Functions::flattenSingleValue($investment);
|
|
|
106 |
$redemption = Functions::flattenSingleValue($redemption);
|
|
|
107 |
$basis = ($basis === null)
|
|
|
108 |
? FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
|
|
|
109 |
: Functions::flattenSingleValue($basis);
|
|
|
110 |
|
|
|
111 |
try {
|
|
|
112 |
$settlement = SecurityValidations::validateSettlementDate($settlement);
|
|
|
113 |
$maturity = SecurityValidations::validateMaturityDate($maturity);
|
|
|
114 |
SecurityValidations::validateSecurityPeriod($settlement, $maturity);
|
|
|
115 |
$investment = SecurityValidations::validateFloat($investment);
|
|
|
116 |
$redemption = SecurityValidations::validateRedemption($redemption);
|
|
|
117 |
$basis = SecurityValidations::validateBasis($basis);
|
|
|
118 |
} catch (Exception $e) {
|
|
|
119 |
return $e->getMessage();
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
if ($investment <= 0) {
|
|
|
123 |
return ExcelError::NAN();
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
$daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis));
|
|
|
127 |
if (!is_numeric($daysBetweenSettlementAndMaturity)) {
|
|
|
128 |
// return date error
|
|
|
129 |
return $daysBetweenSettlementAndMaturity;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
return (($redemption / $investment) - 1) / ($daysBetweenSettlementAndMaturity);
|
|
|
133 |
}
|
|
|
134 |
}
|