Skip to content

Error in COUPNCD #2119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -635,46 +635,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Calculation/Financial/CashFlow/Variable/Periodic.php

-
message: "#^Binary operation \"\\*\" between float\\|string and int results in an error\\.$#"
count: 2
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php

-
message: "#^Binary operation \"\\*\" between float\\|string and int\\|string results in an error\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Coupons\\:\\:couponFirstPeriodDate\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Coupons\\:\\:couponFirstPeriodDate\\(\\) has parameter \\$maturity with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Coupons\\:\\:couponFirstPeriodDate\\(\\) has parameter \\$next with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Coupons\\:\\:couponFirstPeriodDate\\(\\) has parameter \\$settlement with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Coupons\\:\\:validateCouponPeriod\\(\\) has parameter \\$maturity with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Coupons\\:\\:validateCouponPeriod\\(\\) has parameter \\$settlement with no typehint specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Depreciation\\:\\:validateCost\\(\\) has parameter \\$cost with no typehint specified\\.$#"
count: 1
Expand Down
32 changes: 19 additions & 13 deletions src/PhpSpreadsheet/Calculation/Financial/Coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\Financial;

use DateTime;
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
Expand Down Expand Up @@ -73,7 +74,7 @@ public static function COUPDAYBS(
return abs((float) DateTimeExcel\Days::between($prev, $settlement));
}

return DateTimeExcel\YearFrac::fraction($prev, $settlement, $basis) * $daysPerYear;
return (float) DateTimeExcel\YearFrac::fraction($prev, $settlement, $basis) * $daysPerYear;
}

/**
Expand Down Expand Up @@ -208,7 +209,7 @@ public static function COUPDAYSNC(
}
}

return DateTimeExcel\YearFrac::fraction($settlement, $next, $basis) * $daysPerYear;
return (float) DateTimeExcel\YearFrac::fraction($settlement, $next, $basis) * $daysPerYear;
}

/**
Expand Down Expand Up @@ -322,7 +323,7 @@ public static function COUPNUM(
FinancialConstants::BASIS_DAYS_PER_YEAR_NASD
);

return (int) ceil($yearsBetweenSettlementAndMaturity * $frequency);
return (int) ceil((float) $yearsBetweenSettlementAndMaturity * $frequency);
}

/**
Expand Down Expand Up @@ -379,28 +380,33 @@ public static function COUPPCD(
return self::couponFirstPeriodDate($settlement, $maturity, $frequency, self::PERIOD_DATE_PREVIOUS);
}

private static function couponFirstPeriodDate($settlement, $maturity, int $frequency, $next)
private static function monthsDiff(DateTime $result, int $months, string $plusOrMinus, int $day, bool $lastDayFlag): void
{
$result->setDate((int) $result->format('Y'), (int) $result->format('m'), 1);
$result->modify("$plusOrMinus $months months");
$daysInMonth = (int) $result->format('t');
$result->setDate((int) $result->format('Y'), (int) $result->format('m'), $lastDayFlag ? $daysInMonth : min($day, $daysInMonth));
}

private static function couponFirstPeriodDate(float $settlement, float $maturity, int $frequency, bool $next): float
{
$months = 12 / $frequency;

$result = Date::excelToDateTimeObject($maturity);
$maturityEoM = Helpers::isLastDayOfMonth($result);
$day = (int) $result->format('d');
$lastDayFlag = Helpers::isLastDayOfMonth($result);

while ($settlement < Date::PHPToExcel($result)) {
$result->modify('-' . $months . ' months');
self::monthsDiff($result, $months, '-', $day, $lastDayFlag);
}
if ($next === true) {
$result->modify('+' . $months . ' months');
}

if ($maturityEoM === true) {
$result->modify('-1 day');
self::monthsDiff($result, $months, '+', $day, $lastDayFlag);
}

return Date::PHPToExcel($result);
return (float) Date::PHPToExcel($result);
}

private static function validateCouponPeriod($settlement, $maturity): void
private static function validateCouponPeriod(float $settlement, float $maturity): void
{
if ($settlement >= $maturity) {
throw new Exception(Functions::NAN());
Expand Down
14 changes: 14 additions & 0 deletions tests/data/Calculation/Financial/COUPDAYBS.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,18 @@
4,
4,
],
[
5,
'05-Apr-2019',
'30-Sep-2021',
2,
0,
],
[
5,
'05-Oct-2019',
'31-Mar-2022',
2,
0,
],
];
14 changes: 14 additions & 0 deletions tests/data/Calculation/Financial/COUPDAYS.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,18 @@
4,
4,
],
[
180,
'05-Apr-2019',
'30-Sep-2021',
2,
0,
],
[
180,
'05-Oct-2019',
'31-Mar-2022',
2,
0,
],
];
17 changes: 17 additions & 0 deletions tests/data/Calculation/Financial/COUPDAYSNC.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,21 @@
4,
4,
],
[
175,
'05-Apr-2019',
'30-Sep-2021',
2,
0,
],
// Excel and LibreOffice return 175 for the following calculation.
// Gnumeric returns 176.
// My hand calculation, hardly guaranteed, agrees with Gnumeric.
[
176,
'05-Oct-2019',
'31-Mar-2022',
2,
0,
],
];
28 changes: 28 additions & 0 deletions tests/data/Calculation/Financial/COUPNCD.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,32 @@
4,
4,
],
[
44651,
'30-Sep-2021',
'31-Mar-2022',
2,
0,
],
[
44834,
'31-Mar-2022',
'30-Sep-2022',
2,
0,
],
[
43738,
'05-Apr-2019',
'30-Sep-2021',
2,
0,
],
[
43921,
'05-Oct-2019',
'31-Mar-2022',
2,
0,
],
];
14 changes: 14 additions & 0 deletions tests/data/Calculation/Financial/COUPPCD.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,18 @@
4,
4,
],
[
43555,
'05-Apr-2019',
'30-Sep-2021',
2,
0,
],
[
43738,
'05-Oct-2019',
'31-Mar-2022',
2,
0,
],
];