ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Payments.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
10 class Payments
11 {
25  public static function annuity(
26  $interestRate,
27  $numberOfPeriods,
28  $presentValue,
29  $futureValue = 0,
30  $type = FinancialConstants::PAYMENT_END_OF_PERIOD
31  ) {
32  $interestRate = Functions::flattenSingleValue($interestRate);
33  $numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
34  $presentValue = Functions::flattenSingleValue($presentValue);
35  $futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
36  $type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
37 
38  try {
39  $interestRate = CashFlowValidations::validateRate($interestRate);
40  $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
41  $presentValue = CashFlowValidations::validatePresentValue($presentValue);
42  $futureValue = CashFlowValidations::validateFutureValue($futureValue);
44  } catch (Exception $e) {
45  return $e->getMessage();
46  }
47 
48  // Calculate
49  if ($interestRate != 0.0) {
50  return (-$futureValue - $presentValue * (1 + $interestRate) ** $numberOfPeriods) /
51  (1 + $interestRate * $type) / (((1 + $interestRate) ** $numberOfPeriods - 1) / $interestRate);
52  }
53 
54  return (-$presentValue - $futureValue) / $numberOfPeriods;
55  }
56 
72  public static function interestPayment(
73  $interestRate,
74  $period,
75  $numberOfPeriods,
76  $presentValue,
77  $futureValue = 0,
78  $type = FinancialConstants::PAYMENT_END_OF_PERIOD
79  ) {
80  $interestRate = Functions::flattenSingleValue($interestRate);
81  $period = Functions::flattenSingleValue($period);
82  $numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods);
83  $presentValue = Functions::flattenSingleValue($presentValue);
84  $futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue);
85  $type = ($type === null) ? FinancialConstants::PAYMENT_END_OF_PERIOD : Functions::flattenSingleValue($type);
86 
87  try {
88  $interestRate = CashFlowValidations::validateRate($interestRate);
89  $period = CashFlowValidations::validateInt($period);
90  $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods);
91  $presentValue = CashFlowValidations::validatePresentValue($presentValue);
92  $futureValue = CashFlowValidations::validateFutureValue($futureValue);
94  } catch (Exception $e) {
95  return $e->getMessage();
96  }
97 
98  // Validate parameters
99  if ($period <= 0 || $period > $numberOfPeriods) {
100  return Functions::NAN();
101  }
102 
103  // Calculate
104  $interestAndPrincipal = new InterestAndPrincipal(
105  $interestRate,
106  $period,
107  $numberOfPeriods,
108  $presentValue,
109  $futureValue,
110  $type
111  );
112 
113  return $interestAndPrincipal->principal();
114  }
115 }
$type
static annuity( $interestRate, $numberOfPeriods, $presentValue, $futureValue=0, $type=FinancialConstants::PAYMENT_END_OF_PERIOD)
PMT.
Definition: Payments.php:25
static interestPayment( $interestRate, $period, $numberOfPeriods, $presentValue, $futureValue=0, $type=FinancialConstants::PAYMENT_END_OF_PERIOD)
PPMT.
Definition: Payments.php:72
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649