Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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(mixed $settlement, mixed $maturity, mixed $discount): string|float
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(mixed $settlement, mixed $maturity, mixed $discount): string|float
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
     */
120
    public static function yield(mixed $settlement, mixed $maturity, $price): string|float
121
    {
122
        $settlement = Functions::flattenSingleValue($settlement);
123
        $maturity = Functions::flattenSingleValue($maturity);
124
        $price = Functions::flattenSingleValue($price);
125
 
126
        try {
127
            $settlement = FinancialValidations::validateSettlementDate($settlement);
128
            $maturity = FinancialValidations::validateMaturityDate($maturity);
129
            $price = FinancialValidations::validatePrice($price);
130
        } catch (Exception $e) {
131
            return $e->getMessage();
132
        }
133
 
134
        $daysBetweenSettlementAndMaturity = $maturity - $settlement;
135
        $daysPerYear = Helpers::daysPerYear(
136
            Functions::scalar(DateTimeExcel\DateParts::year($maturity)),
137
            FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL
138
        );
139
 
140
        if ($daysBetweenSettlementAndMaturity > $daysPerYear || $daysBetweenSettlementAndMaturity < 0) {
141
            return ExcelError::NAN();
142
        }
143
 
144
        return ((100 - $price) / $price) * (360 / $daysBetweenSettlementAndMaturity);
145
    }
146
}