tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Calculation
/
Financial
/
CashFlow
/
Constant
/
Periodic
/
Cumulative.php
Cumulative.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/Financial/CashFlow/Constant/Periodic/Cumulative.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 Cumulative |
| 12 | { |
| 13 | /** |
| 14 | * CUMIPMT. |
| 15 | * |
| 16 | * Returns the cumulative interest paid on a loan between the start and end periods. |
| 17 | * |
| 18 | * Excel Function: |
| 19 | * CUMIPMT(rate,nper,pv,start,end[,type]) |
| 20 | * |
| 21 | * @param mixed $rate The Interest rate |
| 22 | * @param mixed $periods The total number of payment periods |
| 23 | * @param mixed $presentValue Present Value |
| 24 | * @param mixed $start The first period in the calculation. |
| 25 | * Payment periods are numbered beginning with 1. |
| 26 | * @param mixed $end the last period in the calculation |
| 27 | * @param mixed $type A number 0 or 1 and indicates when payments are due: |
| 28 | * 0 or omitted At the end of the period. |
| 29 | * 1 At the beginning of the period. |
| 30 | * @return string|float|int |
| 31 | */ |
| 32 | public static function interest( |
| 33 | $rate, |
| 34 | $periods, |
| 35 | $presentValue, |
| 36 | $start, |
| 37 | $end, |
| 38 | $type = FinancialConstants::PAYMENT_END_OF_PERIOD |
| 39 | ) { |
| 40 | $rate = Functions::flattenSingleValue($rate); |
| 41 | $periods = Functions::flattenSingleValue($periods); |
| 42 | $presentValue = Functions::flattenSingleValue($presentValue); |
| 43 | $start = Functions::flattenSingleValue($start); |
| 44 | $end = Functions::flattenSingleValue($end); |
| 45 | $type = Functions::flattenSingleValue($type) ?? FinancialConstants::PAYMENT_END_OF_PERIOD; |
| 46 | |
| 47 | try { |
| 48 | $rate = CashFlowValidations::validateRate($rate); |
| 49 | $periods = CashFlowValidations::validateInt($periods); |
| 50 | $presentValue = CashFlowValidations::validatePresentValue($presentValue); |
| 51 | $start = CashFlowValidations::validateInt($start); |
| 52 | $end = CashFlowValidations::validateInt($end); |
| 53 | $type = CashFlowValidations::validatePeriodType($type); |
| 54 | } catch (Exception $e) { |
| 55 | return $e->getMessage(); |
| 56 | } |
| 57 | |
| 58 | // Validate parameters |
| 59 | if ($start < 1 || $start > $end) { |
| 60 | return ExcelError::NAN(); |
| 61 | } |
| 62 | |
| 63 | // Calculate |
| 64 | $interest = 0; |
| 65 | for ($per = $start; $per <= $end; ++$per) { |
| 66 | $ipmt = Interest::payment($rate, $per, $periods, $presentValue, 0, $type); |
| 67 | if (is_string($ipmt)) { |
| 68 | return $ipmt; |
| 69 | } |
| 70 | |
| 71 | $interest += $ipmt; |
| 72 | } |
| 73 | |
| 74 | return $interest; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * CUMPRINC. |
| 79 | * |
| 80 | * Returns the cumulative principal paid on a loan between the start and end periods. |
| 81 | * |
| 82 | * Excel Function: |
| 83 | * CUMPRINC(rate,nper,pv,start,end[,type]) |
| 84 | * |
| 85 | * @param mixed $rate The Interest rate |
| 86 | * @param mixed $periods The total number of payment periods as an integer |
| 87 | * @param mixed $presentValue Present Value |
| 88 | * @param mixed $start The first period in the calculation. |
| 89 | * Payment periods are numbered beginning with 1. |
| 90 | * @param mixed $end the last period in the calculation |
| 91 | * @param mixed $type A number 0 or 1 and indicates when payments are due: |
| 92 | * 0 or omitted At the end of the period. |
| 93 | * 1 At the beginning of the period. |
| 94 | * @return string|float|int |
| 95 | */ |
| 96 | public static function principal( |
| 97 | $rate, |
| 98 | $periods, |
| 99 | $presentValue, |
| 100 | $start, |
| 101 | $end, |
| 102 | $type = FinancialConstants::PAYMENT_END_OF_PERIOD |
| 103 | ) { |
| 104 | $rate = Functions::flattenSingleValue($rate); |
| 105 | $periods = Functions::flattenSingleValue($periods); |
| 106 | $presentValue = Functions::flattenSingleValue($presentValue); |
| 107 | $start = Functions::flattenSingleValue($start); |
| 108 | $end = Functions::flattenSingleValue($end); |
| 109 | $type = Functions::flattenSingleValue($type) ?? FinancialConstants::PAYMENT_END_OF_PERIOD; |
| 110 | |
| 111 | try { |
| 112 | $rate = CashFlowValidations::validateRate($rate); |
| 113 | $periods = CashFlowValidations::validateInt($periods); |
| 114 | $presentValue = CashFlowValidations::validatePresentValue($presentValue); |
| 115 | $start = CashFlowValidations::validateInt($start); |
| 116 | $end = CashFlowValidations::validateInt($end); |
| 117 | $type = CashFlowValidations::validatePeriodType($type); |
| 118 | } catch (Exception $e) { |
| 119 | return $e->getMessage(); |
| 120 | } |
| 121 | |
| 122 | // Validate parameters |
| 123 | if ($start < 1 || $start > $end) { |
| 124 | return ExcelError::VALUE(); |
| 125 | } |
| 126 | |
| 127 | // Calculate |
| 128 | $principal = 0; |
| 129 | for ($per = $start; $per <= $end; ++$per) { |
| 130 | $ppmt = Payments::interestPayment($rate, $per, $periods, $presentValue, 0, $type); |
| 131 | if (is_string($ppmt)) { |
| 132 | return $ppmt; |
| 133 | } |
| 134 | |
| 135 | $principal += $ppmt; |
| 136 | } |
| 137 | |
| 138 | return $principal; |
| 139 | } |
| 140 | } |
| 141 |