ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Periodic.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
10 class Periodic
11 {
33  public static function futureValue(
34  $rate,
35  $numberOfPeriods,
36  $payment = 0.0,
37  $presentValue = 0.0,
38  $type = FinancialConstants::PAYMENT_END_OF_PERIOD
39  ) {
40  $rate = Functions::flattenSingleValue($rate);
41  $numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
42  $payment = ($payment === null) ? 0.0 : Functions::flattenSingleValue($payment);
43  $presentValue = ($presentValue === null) ? 0.0 : Functions::flattenSingleValue($presentValue);
44  $type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
45 
46  try {
48  $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
49  $payment = CashFlowValidations::validateFloat($payment);
50  $presentValue = CashFlowValidations::validatePresentValue($presentValue);
52  } catch (Exception $e) {
53  return $e->getMessage();
54  }
55 
56  return self::calculateFutureValue($rate, $numberOfPeriods, $payment, $presentValue, $type);
57  }
58 
72  public static function presentValue(
73  $rate,
74  $numberOfPeriods,
75  $payment = 0.0,
76  $futureValue = 0.0,
77  $type = FinancialConstants::PAYMENT_END_OF_PERIOD
78  ) {
79  $rate = Functions::flattenSingleValue($rate);
80  $numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
81  $payment = ($payment === null) ? 0.0 : Functions::flattenSingleValue($payment);
82  $futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
83  $type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
84 
85  try {
86  $rate = CashFlowValidations::validateRate($rate);
87  $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
88  $payment = CashFlowValidations::validateFloat($payment);
89  $futureValue = CashFlowValidations::validateFutureValue($futureValue);
91  } catch (Exception $e) {
92  return $e->getMessage();
93  }
94 
95  // Validate parameters
96  if ($numberOfPeriods < 0) {
97  return Functions::NAN();
98  }
99 
100  return self::calculatePresentValue($rate, $numberOfPeriods, $payment, $futureValue, $type);
101  }
102 
116  public static function periods(
117  $rate,
118  $payment,
119  $presentValue,
120  $futureValue = 0.0,
121  $type = FinancialConstants::PAYMENT_END_OF_PERIOD
122  ) {
123  $rate = Functions::flattenSingleValue($rate);
124  $payment = Functions::flattenSingleValue($payment);
125  $presentValue = Functions::flattenSingleValue($presentValue);
126  $futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
127  $type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
128 
129  try {
130  $rate = CashFlowValidations::validateRate($rate);
131  $payment = CashFlowValidations::validateFloat($payment);
132  $presentValue = CashFlowValidations::validatePresentValue($presentValue);
133  $futureValue = CashFlowValidations::validateFutureValue($futureValue);
135  } catch (Exception $e) {
136  return $e->getMessage();
137  }
138 
139  // Validate parameters
140  if ($payment == 0.0) {
141  return Functions::NAN();
142  }
143 
144  return self::calculatePeriods($rate, $payment, $presentValue, $futureValue, $type);
145  }
146 
147  private static function calculateFutureValue(
148  float $rate,
149  int $numberOfPeriods,
150  float $payment,
151  float $presentValue,
152  int $type
153  ): float {
154  if ($rate !== null && $rate != 0) {
155  return -$presentValue *
156  (1 + $rate) ** $numberOfPeriods - $payment * (1 + $rate * $type) * ((1 + $rate) ** $numberOfPeriods - 1)
157  / $rate;
158  }
159 
160  return -$presentValue - $payment * $numberOfPeriods;
161  }
162 
163  private static function calculatePresentValue(
164  float $rate,
165  int $numberOfPeriods,
166  float $payment,
167  float $futureValue,
168  int $type
169  ): float {
170  if ($rate != 0.0) {
171  return (-$payment * (1 + $rate * $type)
172  * (((1 + $rate) ** $numberOfPeriods - 1) / $rate) - $futureValue) / (1 + $rate) ** $numberOfPeriods;
173  }
174 
175  return -$futureValue - $payment * $numberOfPeriods;
176  }
177 
181  private static function calculatePeriods(
182  float $rate,
183  float $payment,
184  float $presentValue,
185  float $futureValue,
186  int $type
187  ) {
188  if ($rate != 0.0) {
189  if ($presentValue == 0.0) {
190  return Functions::NAN();
191  }
192 
193  return log(($payment * (1 + $rate * $type) / $rate - $futureValue) /
194  ($presentValue + $payment * (1 + $rate * $type) / $rate)) / log(1 + $rate);
195  }
196 
197  return (-$presentValue - $futureValue) / $payment;
198  }
199 }
static presentValue( $rate, $numberOfPeriods, $payment=0.0, $futureValue=0.0, $type=FinancialConstants::PAYMENT_END_OF_PERIOD)
PV.
Definition: Periodic.php:72
static futureValue( $rate, $numberOfPeriods, $payment=0.0, $presentValue=0.0, $type=FinancialConstants::PAYMENT_END_OF_PERIOD)
FV.
Definition: Periodic.php:33
$type
static calculateFutureValue(float $rate, int $numberOfPeriods, float $payment, float $presentValue, int $type)
Definition: Periodic.php:147
static calculatePeriods(float $rate, float $payment, float $presentValue, float $futureValue, int $type)
Definition: Periodic.php:181
static periods( $rate, $payment, $presentValue, $futureValue=0.0, $type=FinancialConstants::PAYMENT_END_OF_PERIOD)
NPER.
Definition: Periodic.php:116
static calculatePresentValue(float $rate, int $numberOfPeriods, float $payment, float $futureValue, int $type)
Definition: Periodic.php:163
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649