Skip to content

Commit a34695e

Browse files
Mark BakerPowerKiKi
Mark Baker
andauthored
Financial functions more rationalization (#1990)
* Additional unit tests and rationalisation for Financial Functions * Providing a series of sample files for Financial functions * Refactor the last of the existing Financial functions * Some more unit tests with default assignments from null arguments Co-authored-by: Adrien Crivelli <adrien.crivelli@gmail.com>
1 parent 49f87de commit a34695e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2234
-1571
lines changed

phpstan-baseline.neon

Lines changed: 45 additions & 895 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$helper->log('Returns the accrued interest for a security that pays periodic interest.');
9+
10+
// Create new PhpSpreadsheet object
11+
$spreadsheet = new Spreadsheet();
12+
$worksheet = $spreadsheet->getActiveSheet();
13+
14+
// Add some data
15+
$arguments = [
16+
['Issue Date', DateHelper::getDateValue('01-Jan-2012')],
17+
['First Interest Date', DateHelper::getDateValue('01-Apr-2012')],
18+
['Settlement Date', DateHelper::getDateValue('31-Dec-2013')],
19+
['Annual Coupon Rate', 0.08],
20+
['Par Value', 10000],
21+
['Frequency', 4],
22+
];
23+
24+
// Some basic formatting for the data
25+
$worksheet->fromArray($arguments, null, 'A1');
26+
$worksheet->getStyle('B1:B3')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
27+
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('0.00%');
28+
$worksheet->getStyle('B5')->getNumberFormat()->setFormatCode('$#,##0.00');
29+
30+
// Now the formula
31+
$worksheet->setCellValue('B10', '=ACCRINT(B1, B2, B3, B4, B5, B6)');
32+
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');
33+
34+
$helper->log($worksheet->getCell('B10')->getValue());
35+
$helper->log('ACCRINT() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$helper->log('Returns the accrued interest for a security that pays interest at maturity.');
9+
10+
// Create new PhpSpreadsheet object
11+
$spreadsheet = new Spreadsheet();
12+
$worksheet = $spreadsheet->getActiveSheet();
13+
14+
// Add some data
15+
$arguments = [
16+
['Issue Date', DateHelper::getDateValue('01-Jan-2012')],
17+
['Settlement Date', DateHelper::getDateValue('31-Dec-2012')],
18+
['Annual Coupon Rate', 0.08],
19+
['Par Value', 10000],
20+
];
21+
22+
// Some basic formatting for the data
23+
$worksheet->fromArray($arguments, null, 'A1');
24+
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
25+
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('0.00%');
26+
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('$#,##0.00');
27+
28+
// Now the formula
29+
$worksheet->setCellValue('B6', '=ACCRINTM(B1, B2, B3, B4)');
30+
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('$#,##0.00');
31+
32+
$helper->log($worksheet->getCell('B6')->getValue());
33+
$helper->log('ACCRINTM() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
5+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
7+
require __DIR__ . '/../../Header.php';
8+
9+
$helper->log('Returns the prorated linear depreciation of an asset for a specified accounting period.');
10+
11+
// Create new PhpSpreadsheet object
12+
$spreadsheet = new Spreadsheet();
13+
$worksheet = $spreadsheet->getActiveSheet();
14+
15+
// Add some data
16+
$arguments = [
17+
['Cost', 150.00],
18+
['Date Purchased', DateHelper::getDateValue('01-Jan-2015')],
19+
['First Period Date', DateHelper::getDateValue('30-Sep-2015')],
20+
['Salvage Value', 20.00],
21+
['Number of Periods', 1],
22+
['Depreciation Rate', 0.20],
23+
['Basis', FinancialConstants::BASIS_DAYS_PER_YEAR_360_EUROPEAN],
24+
];
25+
26+
// Some basic formatting for the data
27+
$worksheet->fromArray($arguments, null, 'A1');
28+
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('$#,##0.00');
29+
$worksheet->getStyle('B2:B3')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
30+
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('$#,##0.00');
31+
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('0.00%');
32+
33+
// Now the formula
34+
$worksheet->setCellValue('B10', '=AMORDEGRC(B1, B2, B3, B4, B5, B6, B7)');
35+
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');
36+
37+
$helper->log($worksheet->getCell('B10')->getValue());
38+
$helper->log('AMORDEGRC() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
5+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
7+
require __DIR__ . '/../../Header.php';
8+
9+
$helper->log('Returns the prorated linear depreciation of an asset for a specified accounting period.');
10+
11+
// Create new PhpSpreadsheet object
12+
$spreadsheet = new Spreadsheet();
13+
$worksheet = $spreadsheet->getActiveSheet();
14+
15+
// Add some data
16+
$arguments = [
17+
['Cost', 150.00],
18+
['Date Purchased', DateHelper::getDateValue('01-Jan-2015')],
19+
['First Period Date', DateHelper::getDateValue('30-Sep-2015')],
20+
['Salvage Value', 20.00],
21+
['Period', 1],
22+
['Depreciation Rate', 0.20],
23+
['Basis', FinancialConstants::BASIS_DAYS_PER_YEAR_360_EUROPEAN],
24+
];
25+
26+
// Some basic formatting for the data
27+
$worksheet->fromArray($arguments, null, 'A1');
28+
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('$#,##0.00');
29+
$worksheet->getStyle('B2:B3')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
30+
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('$#,##0.00');
31+
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('0.00%');
32+
33+
// Now the formula
34+
$worksheet->setCellValue('B10', '=AMORLINC(B1, B2, B3, B4, B5, B6, B7)');
35+
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');
36+
37+
$helper->log($worksheet->getCell('B10')->getValue());
38+
$helper->log('AMORLINC() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$helper->log('Returns the number of days from the beginning of a coupon\'s period to the settlement date.');
9+
10+
// Create new PhpSpreadsheet object
11+
$spreadsheet = new Spreadsheet();
12+
$worksheet = $spreadsheet->getActiveSheet();
13+
14+
// Add some data
15+
$arguments = [
16+
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
17+
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
18+
['Frequency', 4],
19+
];
20+
21+
// Some basic formatting for the data
22+
$worksheet->fromArray($arguments, null, 'A1');
23+
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
24+
25+
// Now the formula
26+
$worksheet->setCellValue('B6', '=COUPDAYBS(B1, B2, B3)');
27+
28+
$helper->log($worksheet->getCell('B6')->getValue());
29+
$helper->log('COUPDAYBS() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$helper->log('Returns the number of days in the coupon period that contains the settlement date.');
9+
10+
// Create new PhpSpreadsheet object
11+
$spreadsheet = new Spreadsheet();
12+
$worksheet = $spreadsheet->getActiveSheet();
13+
14+
// Add some data
15+
$arguments = [
16+
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
17+
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
18+
['Frequency', 4],
19+
];
20+
21+
// Some basic formatting for the data
22+
$worksheet->fromArray($arguments, null, 'A1');
23+
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
24+
25+
// Now the formula
26+
$worksheet->setCellValue('B6', '=COUPDAYS(B1, B2, B3)');
27+
28+
$helper->log($worksheet->getCell('B6')->getValue());
29+
$helper->log('COUPDAYS() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$helper->log('Returns the number of days from the settlement date to the next coupon date.');
9+
10+
// Create new PhpSpreadsheet object
11+
$spreadsheet = new Spreadsheet();
12+
$worksheet = $spreadsheet->getActiveSheet();
13+
14+
// Add some data
15+
$arguments = [
16+
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
17+
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
18+
['Frequency', 4],
19+
];
20+
21+
// Some basic formatting for the data
22+
$worksheet->fromArray($arguments, null, 'A1');
23+
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
24+
25+
// Now the formula
26+
$worksheet->setCellValue('B6', '=COUPDAYSNC(B1, B2, B3)');
27+
28+
$helper->log($worksheet->getCell('B6')->getValue());
29+
$helper->log('COUPDAYSNC() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$helper->log('Returns the next coupon date, after the settlement date.');
9+
10+
// Create new PhpSpreadsheet object
11+
$spreadsheet = new Spreadsheet();
12+
$worksheet = $spreadsheet->getActiveSheet();
13+
14+
// Add some data
15+
$arguments = [
16+
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
17+
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
18+
['Frequency', 4],
19+
];
20+
21+
// Some basic formatting for the data
22+
$worksheet->fromArray($arguments, null, 'A1');
23+
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
24+
25+
// Now the formula
26+
$worksheet->setCellValue('B6', '=COUPNCD(B1, B2, B3)');
27+
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
28+
29+
$helper->log($worksheet->getCell('B6')->getValue());
30+
$helper->log('COUPNCD() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$helper->log('Returns the number of coupons payable, between a security\'s settlement date and maturity date,');
9+
$helper->log('rounded up to the nearest whole coupon.');
10+
11+
// Create new PhpSpreadsheet object
12+
$spreadsheet = new Spreadsheet();
13+
$worksheet = $spreadsheet->getActiveSheet();
14+
15+
// Add some data
16+
$arguments = [
17+
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
18+
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
19+
['Frequency', 4],
20+
];
21+
22+
// Some basic formatting for the data
23+
$worksheet->fromArray($arguments, null, 'A1');
24+
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
25+
26+
// Now the formula
27+
$worksheet->setCellValue('B6', '=COUPNUM(B1, B2, B3)');
28+
29+
$helper->log($worksheet->getCell('B6')->getValue());
30+
$helper->log('COUPNUM() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$helper->log('Returns the previous coupon date, before the settlement date for a security.');
9+
10+
// Create new PhpSpreadsheet object
11+
$spreadsheet = new Spreadsheet();
12+
$worksheet = $spreadsheet->getActiveSheet();
13+
14+
// Add some data
15+
$arguments = [
16+
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
17+
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
18+
['Frequency', 4],
19+
];
20+
21+
// Some basic formatting for the data
22+
$worksheet->fromArray($arguments, null, 'A1');
23+
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
24+
25+
// Now the formula
26+
$worksheet->setCellValue('B6', '=COUPPCD(B1, B2, B3)');
27+
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
28+
29+
$helper->log($worksheet->getCell('B6')->getValue());
30+
$helper->log('COUPPCD() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$helper->log('Returns the cumulative interest paid on a loan or investment, between two specified periods.');
8+
9+
// Create new PhpSpreadsheet object
10+
$spreadsheet = new Spreadsheet();
11+
$worksheet = $spreadsheet->getActiveSheet();
12+
13+
// Add some data
14+
$arguments = [
15+
['Interest Rate (per period)', 0.05 / 12],
16+
['Number of Periods', 5 * 12],
17+
['Present Value', 50000],
18+
];
19+
20+
// Some basic formatting for the data
21+
$worksheet->fromArray($arguments, null, 'A1');
22+
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('0.00%');
23+
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('$#,##0.00');
24+
25+
// Now the formula
26+
$baseRow = 5;
27+
for ($year = 1; $year <= 5; ++$year) {
28+
$row = (string) ($baseRow + $year);
29+
$yearStartPeriod = (int) $year * 12 - 11;
30+
$yearEndPeriod = (int) $year * 12;
31+
32+
$worksheet->setCellValue("A{$row}", "Yr {$year}");
33+
$worksheet->setCellValue("B{$row}", "=CUMIPMT(\$B\$1, \$B\$2, \$B\$3, {$yearStartPeriod}, {$yearEndPeriod}, 0)");
34+
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
35+
36+
$helper->log($worksheet->getCell("B{$row}")->getValue());
37+
$helper->log("CUMIPMT() Year {$year} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
38+
}

0 commit comments

Comments
 (0)