tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Calculation
/
Financial
/
CashFlow
/
Constant
/
Periodic
/
Payments.php
Payments.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic/Payments.php
| 1 | <?php |
| 2 | |
| 3 | namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\Constant\Periodic; |
| 4 | |
| 5 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 6 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\CashFlowValidations; |
| 7 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants; |
| 8 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 9 | use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 10 | |
| 11 | class Payments |
| 12 | { |
| 13 | /** |
| 14 | * PMT. |
| 15 | * |
| 16 | * Returns the constant payment (annuity) for a cash flow with a constant interest rate. |
| 17 | * |
| 18 | * @param mixed $interestRate Interest rate per period |
| 19 | * @param mixed $numberOfPeriods Number of periods |
| 20 | * @param mixed $presentValue Present Value |
| 21 | * @param mixed $futureValue Future Value |
| 22 | * @param mixed $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period |
| 23 | * |
| 24 | * @return float|string Result, or a string containing an error |
| 25 | */ |
| 26 | public static function annuity( |
| 27 | $interestRate, |
| 28 | $numberOfPeriods, |
| 29 | $presentValue, |
| 30 | $futureValue = 0.0, |
| 31 | $type = FinancialConstants::PAYMENT_END_OF_PERIOD |
| 32 | ) { |
| 33 | $interestRate = Functions::flattenSingleValue($interestRate); |
| 34 | $numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods); |
| 35 | $presentValue = Functions::flattenSingleValue($presentValue); |
| 36 | $futureValue = Functions::flattenSingleValue($futureValue) ?? 0.0; |
| 37 | $type = Functions::flattenSingleValue($type) ?? FinancialConstants::PAYMENT_END_OF_PERIOD; |
| 38 | |
| 39 | try { |
| 40 | $interestRate = CashFlowValidations::validateRate($interestRate); |
| 41 | $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods); |
| 42 | $presentValue = CashFlowValidations::validatePresentValue($presentValue); |
| 43 | $futureValue = CashFlowValidations::validateFutureValue($futureValue); |
| 44 | $type = CashFlowValidations::validatePeriodType($type); |
| 45 | } catch (Exception $e) { |
| 46 | return $e->getMessage(); |
| 47 | } |
| 48 | |
| 49 | // Calculate |
| 50 | if ($interestRate != 0.0) { |
| 51 | return (-$futureValue - $presentValue * (1 + $interestRate) ** $numberOfPeriods) |
| 52 | / (1 + $interestRate * $type) / (((1 + $interestRate) ** $numberOfPeriods - 1) / $interestRate); |
| 53 | } |
| 54 | |
| 55 | return (-$presentValue - $futureValue) / $numberOfPeriods; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * PPMT. |
| 60 | * |
| 61 | * Returns the interest payment for a given period for an investment based on periodic, constant payments |
| 62 | * and a constant interest rate. |
| 63 | * |
| 64 | * @param mixed $interestRate Interest rate per period |
| 65 | * @param mixed $period Period for which we want to find the interest |
| 66 | * @param mixed $numberOfPeriods Number of periods |
| 67 | * @param mixed $presentValue Present Value |
| 68 | * @param mixed $futureValue Future Value |
| 69 | * @param mixed $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period |
| 70 | * |
| 71 | * @return float|string Result, or a string containing an error |
| 72 | */ |
| 73 | public static function interestPayment( |
| 74 | $interestRate, |
| 75 | $period, |
| 76 | $numberOfPeriods, |
| 77 | $presentValue, |
| 78 | $futureValue = 0, |
| 79 | $type = FinancialConstants::PAYMENT_END_OF_PERIOD |
| 80 | ) { |
| 81 | $interestRate = Functions::flattenSingleValue($interestRate); |
| 82 | $period = Functions::flattenSingleValue($period); |
| 83 | $numberOfPeriods = Functions::flattenSingleValue($numberOfPeriods); |
| 84 | $presentValue = Functions::flattenSingleValue($presentValue); |
| 85 | $futureValue = ($futureValue === null) ? 0.0 : Functions::flattenSingleValue($futureValue); |
| 86 | $type = Functions::flattenSingleValue($type) ?? FinancialConstants::PAYMENT_END_OF_PERIOD; |
| 87 | |
| 88 | try { |
| 89 | $interestRate = CashFlowValidations::validateRate($interestRate); |
| 90 | $period = CashFlowValidations::validateInt($period); |
| 91 | $numberOfPeriods = CashFlowValidations::validateInt($numberOfPeriods); |
| 92 | $presentValue = CashFlowValidations::validatePresentValue($presentValue); |
| 93 | $futureValue = CashFlowValidations::validateFutureValue($futureValue); |
| 94 | $type = CashFlowValidations::validatePeriodType($type); |
| 95 | } catch (Exception $e) { |
| 96 | return $e->getMessage(); |
| 97 | } |
| 98 | |
| 99 | // Validate parameters |
| 100 | if ($period <= 0 || $period > $numberOfPeriods) { |
| 101 | return ExcelError::NAN(); |
| 102 | } |
| 103 | |
| 104 | // Calculate |
| 105 | $interestAndPrincipal = new InterestAndPrincipal( |
| 106 | $interestRate, |
| 107 | $period, |
| 108 | $numberOfPeriods, |
| 109 | $presentValue, |
| 110 | $futureValue, |
| 111 | $type |
| 112 | ); |
| 113 | |
| 114 | return $interestAndPrincipal->principal(); |
| 115 | } |
| 116 | } |
| 117 |