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