Skip to content

Financial functions more rationalization #1990

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 35 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c27c5a8
Additional unit tests and rationalisation for Financial Functions
Apr 6, 2021
82e65d4
Remove debug
Apr 6, 2021
324e122
Merge branch 'master' into Financial-Functions-More-Rationalization
Apr 6, 2021
21677c2
Rationalise validations
Apr 7, 2021
7772791
Missed price validation
Apr 7, 2021
66256c3
Missed price validation
Apr 7, 2021
47c579b
Tighten up some validations
Apr 7, 2021
02dccd6
Providing a series of sample files for Financial functions
Apr 10, 2021
bc31e97
Additional sample files for Financial functions
Apr 11, 2021
7198b40
PHPCS Cleanup
Apr 11, 2021
8dbd0f5
Merge branch 'master' into Financial-Functions-More-Rationalization
Apr 11, 2021
50ea7c3
Parameter types for validations
Apr 11, 2021
23402af
More PHPSatan appeasements
Apr 11, 2021
e427b64
More PHPSatan appeasements
Apr 11, 2021
a1455ba
Some more examples for Financial functions
Apr 11, 2021
1ece53c
Additional unit tests and rationalisation for Financial Functions
Apr 6, 2021
e27fb8b
Remove debug
Apr 6, 2021
b0204e5
Rationalise validations
Apr 7, 2021
7251be0
Missed price validation
Apr 7, 2021
c5b4b7d
Missed price validation
Apr 7, 2021
aeb58b3
Tighten up some validations
Apr 7, 2021
9167c75
Providing a series of sample files for Financial functions
Apr 10, 2021
6bce2b4
Additional sample files for Financial functions
Apr 11, 2021
cbe255e
PHPCS Cleanup
Apr 11, 2021
bc25f0e
Parameter types for validations
Apr 11, 2021
ea5e874
More PHPSatan appeasements
Apr 11, 2021
ae4f2a9
More PHPSatan appeasements
Apr 11, 2021
ecc49e0
Some more examples for Financial functions
Apr 11, 2021
dbe1d2a
Remove PHPStan errors that no longer exists
PowerKiKi Apr 12, 2021
ba7e4b1
Merge remote-tracking branch 'origin/Financial-Functions-More-Rationa…
Apr 12, 2021
f52049c
Yet more examples for Financial functions
Apr 12, 2021
c62e324
Refactor the last of the existing Financial functions
Apr 12, 2021
f4afd7f
Some more unit tests with default assignments from null arguments
Apr 12, 2021
087edbe
Yet more unit tests with default assignments from null arguments
Apr 12, 2021
e3a280c
Removed fixed errors from phpstan baseline
Apr 12, 2021
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
940 changes: 45 additions & 895 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions samples/Calculations/Financial/ACCRINT.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the accrued interest for a security that pays periodic interest.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Issue Date', DateHelper::getDateValue('01-Jan-2012')],
['First Interest Date', DateHelper::getDateValue('01-Apr-2012')],
['Settlement Date', DateHelper::getDateValue('31-Dec-2013')],
['Annual Coupon Rate', 0.08],
['Par Value', 10000],
['Frequency', 4],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1:B3')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('0.00%');
$worksheet->getStyle('B5')->getNumberFormat()->setFormatCode('$#,##0.00');

// Now the formula
$worksheet->setCellValue('B10', '=ACCRINT(B1, B2, B3, B4, B5, B6)');
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');

$helper->log($worksheet->getCell('B10')->getValue());
$helper->log('ACCRINT() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
33 changes: 33 additions & 0 deletions samples/Calculations/Financial/ACCRINTM.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the accrued interest for a security that pays interest at maturity.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Issue Date', DateHelper::getDateValue('01-Jan-2012')],
['Settlement Date', DateHelper::getDateValue('31-Dec-2012')],
['Annual Coupon Rate', 0.08],
['Par Value', 10000],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('0.00%');
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('$#,##0.00');

// Now the formula
$worksheet->setCellValue('B6', '=ACCRINTM(B1, B2, B3, B4)');
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('$#,##0.00');

$helper->log($worksheet->getCell('B6')->getValue());
$helper->log('ACCRINTM() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
38 changes: 38 additions & 0 deletions samples/Calculations/Financial/AMORDEGRC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the prorated linear depreciation of an asset for a specified accounting period.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Cost', 150.00],
['Date Purchased', DateHelper::getDateValue('01-Jan-2015')],
['First Period Date', DateHelper::getDateValue('30-Sep-2015')],
['Salvage Value', 20.00],
['Number of Periods', 1],
['Depreciation Rate', 0.20],
['Basis', FinancialConstants::BASIS_DAYS_PER_YEAR_360_EUROPEAN],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('$#,##0.00');
$worksheet->getStyle('B2:B3')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('$#,##0.00');
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('0.00%');

// Now the formula
$worksheet->setCellValue('B10', '=AMORDEGRC(B1, B2, B3, B4, B5, B6, B7)');
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');

$helper->log($worksheet->getCell('B10')->getValue());
$helper->log('AMORDEGRC() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
38 changes: 38 additions & 0 deletions samples/Calculations/Financial/AMORLINC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the prorated linear depreciation of an asset for a specified accounting period.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Cost', 150.00],
['Date Purchased', DateHelper::getDateValue('01-Jan-2015')],
['First Period Date', DateHelper::getDateValue('30-Sep-2015')],
['Salvage Value', 20.00],
['Period', 1],
['Depreciation Rate', 0.20],
['Basis', FinancialConstants::BASIS_DAYS_PER_YEAR_360_EUROPEAN],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('$#,##0.00');
$worksheet->getStyle('B2:B3')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');
$worksheet->getStyle('B4')->getNumberFormat()->setFormatCode('$#,##0.00');
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('0.00%');

// Now the formula
$worksheet->setCellValue('B10', '=AMORLINC(B1, B2, B3, B4, B5, B6, B7)');
$worksheet->getStyle('B10')->getNumberFormat()->setFormatCode('$#,##0.00');

$helper->log($worksheet->getCell('B10')->getValue());
$helper->log('AMORLINC() Result is ' . $worksheet->getCell('B10')->getFormattedValue());
29 changes: 29 additions & 0 deletions samples/Calculations/Financial/COUPDAYBS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the number of days from the beginning of a coupon\'s period to the settlement date.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
['Frequency', 4],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');

// Now the formula
$worksheet->setCellValue('B6', '=COUPDAYBS(B1, B2, B3)');

$helper->log($worksheet->getCell('B6')->getValue());
$helper->log('COUPDAYBS() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
29 changes: 29 additions & 0 deletions samples/Calculations/Financial/COUPDAYS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the number of days in the coupon period that contains the settlement date.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
['Frequency', 4],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');

// Now the formula
$worksheet->setCellValue('B6', '=COUPDAYS(B1, B2, B3)');

$helper->log($worksheet->getCell('B6')->getValue());
$helper->log('COUPDAYS() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
29 changes: 29 additions & 0 deletions samples/Calculations/Financial/COUPDAYSNC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the number of days from the settlement date to the next coupon date.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
['Frequency', 4],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');

// Now the formula
$worksheet->setCellValue('B6', '=COUPDAYSNC(B1, B2, B3)');

$helper->log($worksheet->getCell('B6')->getValue());
$helper->log('COUPDAYSNC() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
30 changes: 30 additions & 0 deletions samples/Calculations/Financial/COUPNCD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the next coupon date, after the settlement date.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
['Frequency', 4],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');

// Now the formula
$worksheet->setCellValue('B6', '=COUPNCD(B1, B2, B3)');
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');

$helper->log($worksheet->getCell('B6')->getValue());
$helper->log('COUPNCD() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
30 changes: 30 additions & 0 deletions samples/Calculations/Financial/COUPNUM.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the number of coupons payable, between a security\'s settlement date and maturity date,');
$helper->log('rounded up to the nearest whole coupon.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
['Frequency', 4],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');

// Now the formula
$worksheet->setCellValue('B6', '=COUPNUM(B1, B2, B3)');

$helper->log($worksheet->getCell('B6')->getValue());
$helper->log('COUPNUM() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
30 changes: 30 additions & 0 deletions samples/Calculations/Financial/COUPPCD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Helpers as DateHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the previous coupon date, before the settlement date for a security.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Settlement Date', DateHelper::getDateValue('01-Jan-2011')],
['Maturity Date', DateHelper::getDateValue('25-Oct-2012')],
['Frequency', 4],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1:B2')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');

// Now the formula
$worksheet->setCellValue('B6', '=COUPPCD(B1, B2, B3)');
$worksheet->getStyle('B6')->getNumberFormat()->setFormatCode('dd-mmm-yyyy');

$helper->log($worksheet->getCell('B6')->getValue());
$helper->log('COUPPCD() Result is ' . $worksheet->getCell('B6')->getFormattedValue());
38 changes: 38 additions & 0 deletions samples/Calculations/Financial/CUMIPMT.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';

$helper->log('Returns the cumulative interest paid on a loan or investment, between two specified periods.');

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$arguments = [
['Interest Rate (per period)', 0.05 / 12],
['Number of Periods', 5 * 12],
['Present Value', 50000],
];

// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('0.00%');
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('$#,##0.00');

// Now the formula
$baseRow = 5;
for ($year = 1; $year <= 5; ++$year) {
$row = (string) ($baseRow + $year);
$yearStartPeriod = (int) $year * 12 - 11;
$yearEndPeriod = (int) $year * 12;

$worksheet->setCellValue("A{$row}", "Yr {$year}");
$worksheet->setCellValue("B{$row}", "=CUMIPMT(\$B\$1, \$B\$2, \$B\$3, {$yearStartPeriod}, {$yearEndPeriod}, 0)");
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');

$helper->log($worksheet->getCell("B{$row}")->getValue());
$helper->log("CUMIPMT() Year {$year} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
}
Loading