Consolidate use of constants

Remove link to deprecated method in favour of new method
This commit is contained in:
MarkBaker 2021-04-06 22:57:35 +02:00
parent 37ebc99a5a
commit 02f37d4f7e
4 changed files with 29 additions and 24 deletions

View File

@ -4,6 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\CashFlow\Constant\Perio
use PhpOffice\PhpSpreadsheet\Calculation\Exception; use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Financial; use PhpOffice\PhpSpreadsheet\Calculation\Financial;
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Securities\Constants;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class Cumulative class Cumulative
@ -30,7 +31,7 @@ class Cumulative
* *
* @return float|string * @return float|string
*/ */
public static function interest($rate, $periods, $presentValue, $start, $end, $type = 0) public static function interest($rate, $periods, $presentValue, $start, $end, $type = Constants::END_OF_PERIOD)
{ {
$rate = Functions::flattenSingleValue($rate); $rate = Functions::flattenSingleValue($rate);
$periods = Functions::flattenSingleValue($periods); $periods = Functions::flattenSingleValue($periods);
@ -51,7 +52,7 @@ class Cumulative
} }
// Validate parameters // Validate parameters
if ($type !== 0 && $type !== 1) { if ($type !== Constants::END_OF_PERIOD && $type !== Constants::BEGINNING_OF_PERIOD) {
return Functions::NAN(); return Functions::NAN();
} }
if ($start < 1 || $start > $end) { if ($start < 1 || $start > $end) {
@ -61,7 +62,7 @@ class Cumulative
// Calculate // Calculate
$interest = 0; $interest = 0;
for ($per = $start; $per <= $end; ++$per) { for ($per = $start; $per <= $end; ++$per) {
$ipmt = Financial::IPMT($rate, $per, $periods, $presentValue, 0, $type); $ipmt = Interest::payment($rate, $per, $periods, $presentValue, 0, $type);
if (is_string($ipmt)) { if (is_string($ipmt)) {
return $ipmt; return $ipmt;
} }

View File

@ -23,12 +23,12 @@ class Interest
* Excel Function: * Excel Function:
* IPMT(rate,per,nper,pv[,fv][,type]) * IPMT(rate,per,nper,pv[,fv][,type])
* *
* @param float $interestRate Interest rate per period * @param mixed $interestRate Interest rate per period
* @param int $period Period for which we want to find the interest * @param mixed $period Period for which we want to find the interest
* @param int $numberOfPeriods Number of periods * @param mixed $numberOfPeriods Number of periods
* @param float $presentValue Present Value * @param mixed $presentValue Present Value
* @param float $futureValue Future Value * @param mixed $futureValue Future Value
* @param int $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period * @param mixed $type Payment type: 0 = at the end of each period, 1 = at the beginning of each period
* *
* @return float|string * @return float|string
*/ */
@ -81,13 +81,10 @@ class Interest
* Excel Function: * Excel Function:
* =ISPMT(interest_rate, period, number_payments, pv) * =ISPMT(interest_rate, period, number_payments, pv)
* *
* interest_rate is the interest rate for the investment * @param mixed $interestRate is the interest rate for the investment
* * @param mixed $period is the period to calculate the interest rate. It must be betweeen 1 and number_payments.
* period is the period to calculate the interest rate. It must be betweeen 1 and number_payments. * @param mixed $numberOfPeriods is the number of payments for the annuity
* * @param mixed $principleRemaining is the loan amount or present value of the payments
* number_payments is the number of payments for the annuity
*
* pv is the loan amount or present value of the payments
*/ */
public static function schedulePayment($interestRate, $period, $numberOfPeriods, $principleRemaining) public static function schedulePayment($interestRate, $period, $numberOfPeriods, $principleRemaining)
{ {
@ -189,17 +186,17 @@ class Interest
return $close ? $rate : Functions::NAN(); return $close ? $rate : Functions::NAN();
} }
private static function rateNextGuess($rate, $nper, $pmt, $pv, $fv, $type) private static function rateNextGuess($rate, $numberOfPeriods, $payment, $presentValue, $futureValue, $type)
{ {
if ($rate == 0) { if ($rate == 0) {
return Functions::NAN(); return Functions::NAN();
} }
$tt1 = ($rate + 1) ** $nper; $tt1 = ($rate + 1) ** $numberOfPeriods;
$tt2 = ($rate + 1) ** ($nper - 1); $tt2 = ($rate + 1) ** ($numberOfPeriods - 1);
$numerator = $fv + $tt1 * $pv + $pmt * ($tt1 - 1) * ($rate * $type + 1) / $rate; $numerator = $futureValue + $tt1 * $presentValue + $payment * ($tt1 - 1) * ($rate * $type + 1) / $rate;
$denominator = $nper * $tt2 * $pv - $pmt * ($tt1 - 1) * ($rate * $type + 1) / ($rate * $rate) $denominator = $numberOfPeriods * $tt2 * $presentValue - $payment * ($tt1 - 1)
+ $nper * $pmt * $tt2 * ($rate * $type + 1) / $rate * ($rate * $type + 1) / ($rate * $rate) + $numberOfPeriods
+ $pmt * ($tt1 - 1) * $type / $rate; * $payment * $tt2 * ($rate * $type + 1) / $rate + $payment * ($tt1 - 1) * $type / $rate;
if ($denominator == 0) { if ($denominator == 0) {
return Functions::NAN(); return Functions::NAN();
} }

View File

@ -291,7 +291,11 @@ class Coupons
return $e->getMessage(); return $e->getMessage();
} }
$yearsBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::funcYearFrac($settlement, $maturity, 0); $yearsBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::funcYearFrac(
$settlement,
$maturity,
Helpers::DAYS_PER_YEAR_NASD
);
return (int) ceil($yearsBetweenSettlementAndMaturity * $frequency); return (int) ceil($yearsBetweenSettlementAndMaturity * $frequency);
} }

View File

@ -7,4 +7,7 @@ class Constants
public const FREQUENCY_ANNUAL = 1; public const FREQUENCY_ANNUAL = 1;
public const FREQUENCY_SEMI_ANNUAL = 2; public const FREQUENCY_SEMI_ANNUAL = 2;
public const FREQUENCY_QUARTERLY = 4; public const FREQUENCY_QUARTERLY = 4;
public const END_OF_PERIOD = 0;
public const BEGINNING_OF_PERIOD = 1;
} }