PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Calculation / Financial / TreasuryBill.php

TreasuryBill.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/Financial/TreasuryBill.php

148 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial;
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
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 float|string $price The Treasury bill's price per $100 face value
119 * @return string|float
120 */
121 public static function yield($settlement, $maturity, $price)
122 {
123 $settlement = Functions::flattenSingleValue($settlement);
124 $maturity = Functions::flattenSingleValue($maturity);
125 $price = Functions::flattenSingleValue($price);
126
127 try {
128 $settlement = FinancialValidations::validateSettlementDate($settlement);
129 $maturity = FinancialValidations::validateMaturityDate($maturity);
130 $price = FinancialValidations::validatePrice($price);
131 } catch (Exception $e) {
132 return $e->getMessage();
133 }
134
135 $daysBetweenSettlementAndMaturity = $maturity - $settlement;
136 $daysPerYear = Helpers::daysPerYear(
137 Functions::scalar(DateTimeExcel\DateParts::year($maturity)),
138 FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL
139 );
140
141 if ($daysBetweenSettlementAndMaturity > $daysPerYear || $daysBetweenSettlementAndMaturity < 0) {
142 return ExcelError::NAN();
143 }
144
145 return ((100 - $price) / $price) * (360 / $daysBetweenSettlementAndMaturity);
146 }
147 }
148