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\CashFlow;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8
 
9
class Single
10
{
11
    /**
12
     * FVSCHEDULE.
13
     *
14
     * Returns the future value of an initial principal after applying a series of compound interest rates.
15
     * Use FVSCHEDULE to calculate the future value of an investment with a variable or adjustable rate.
16
     *
17
     * Excel Function:
18
     *        FVSCHEDULE(principal,schedule)
19
     *
20
     * @param mixed $principal the present value
21
     * @param float[] $schedule an array of interest rates to apply
22
     */
23
    public static function futureValue(mixed $principal, array $schedule): string|float
24
    {
25
        $principal = Functions::flattenSingleValue($principal);
26
        $schedule = Functions::flattenArray($schedule);
27
 
28
        try {
29
            $principal = CashFlowValidations::validateFloat($principal);
30
 
31
            foreach ($schedule as $rate) {
32
                $rate = CashFlowValidations::validateFloat($rate);
33
                $principal *= 1 + $rate;
34
            }
35
        } catch (Exception $e) {
36
            return $e->getMessage();
37
        }
38
 
39
        return $principal;
40
    }
41
 
42
    /**
43
     * PDURATION.
44
     *
45
     * Calculates the number of periods required for an investment to reach a specified value.
46
     *
47
     * @param mixed $rate Interest rate per period
48
     * @param mixed $presentValue Present Value
49
     * @param mixed $futureValue Future Value
50
     *
51
     * @return float|string Result, or a string containing an error
52
     */
53
    public static function periods(mixed $rate, mixed $presentValue, mixed $futureValue): string|float
54
    {
55
        $rate = Functions::flattenSingleValue($rate);
56
        $presentValue = Functions::flattenSingleValue($presentValue);
57
        $futureValue = Functions::flattenSingleValue($futureValue);
58
 
59
        try {
60
            $rate = CashFlowValidations::validateRate($rate);
61
            $presentValue = CashFlowValidations::validatePresentValue($presentValue);
62
            $futureValue = CashFlowValidations::validateFutureValue($futureValue);
63
        } catch (Exception $e) {
64
            return $e->getMessage();
65
        }
66
 
67
        // Validate parameters
68
        if ($rate <= 0.0 || $presentValue <= 0.0 || $futureValue <= 0.0) {
69
            return ExcelError::NAN();
70
        }
71
 
72
        return (log($futureValue) - log($presentValue)) / log(1 + $rate);
73
    }
74
 
75
    /**
76
     * RRI.
77
     *
78
     * Calculates the interest rate required for an investment to grow to a specified future value .
79
     *
80
     * @param mixed $periods The number of periods over which the investment is made, expect array|float
81
     * @param mixed $presentValue Present Value, expect array|float
82
     * @param mixed $futureValue Future Value, expect array|float
83
     *
84
     * @return float|string Result, or a string containing an error
85
     */
86
    public static function interestRate(mixed $periods = 0.0, mixed $presentValue = 0.0, mixed $futureValue = 0.0): string|float
87
    {
88
        $periods = Functions::flattenSingleValue($periods);
89
        $presentValue = Functions::flattenSingleValue($presentValue);
90
        $futureValue = Functions::flattenSingleValue($futureValue);
91
 
92
        try {
93
            $periods = CashFlowValidations::validateFloat($periods);
94
            $presentValue = CashFlowValidations::validatePresentValue($presentValue);
95
            $futureValue = CashFlowValidations::validateFutureValue($futureValue);
96
        } catch (Exception $e) {
97
            return $e->getMessage();
98
        }
99
 
100
        // Validate parameters
101
        if ($periods <= 0.0 || $presentValue <= 0.0 || $futureValue < 0.0) {
102
            return ExcelError::NAN();
103
        }
104
 
105
        return ($futureValue / $presentValue) ** (1 / $periods) - 1;
106
    }
107
}