Skip to content
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

Extend fiscal year relative options to better match other periods #12137

Merged
merged 1 commit into from
May 22, 2018
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
33 changes: 23 additions & 10 deletions CRM/Utils/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,8 @@ public static function datePluginToPHPFormats() {
/**
* Resolves the given relative time interval into finite time limits.
*
* @param array $relativeTerm
* Relative time frame like this, previous, etc.
* @param string $relativeTerm
* Relative time frame: this, previous, previous_1.
* @param int $unit
* Frequency unit like year, month, week etc.
*
Expand All @@ -1039,6 +1039,9 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$now = getdate();
$from = $to = $dateRange = array();
$from['H'] = $from['i'] = $from['s'] = 0;
$relativeTermParts = explode('_', $relativeTerm);
$relativeTermPrefix = $relativeTermParts[0];
$relativeTermSuffix = isset($relativeTermParts[1]) ? $relativeTermParts[1] : '';

switch ($unit) {
case 'year':
Expand Down Expand Up @@ -1162,7 +1165,7 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$from['d'] = $config->fiscalYearStart['d'];
$from['M'] = $config->fiscalYearStart['M'];
$fYear = self::calculateFiscalYear($from['d'], $from['M']);
switch ($relativeTerm) {
switch ($relativeTermPrefix) {
case 'this':
$from['Y'] = $fYear;
$fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1);
Expand All @@ -1174,12 +1177,22 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
break;

case 'previous':
$from['Y'] = $fYear - 1;
$fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1);
$fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
$to['d'] = $fiscalEnd['2'];
$to['M'] = $fiscalEnd['1'];
$to['Y'] = $fiscalEnd['0'];
if (!is_numeric($relativeTermSuffix)) {
$from['Y'] = ($relativeTermSuffix === 'before') ? $fYear - 2 : $fYear - 1;
$fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1);
$fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
$to['d'] = $fiscalEnd['2'];
$to['M'] = $fiscalEnd['1'];
$to['Y'] = $fiscalEnd['0'];
}
else {
$from['Y'] = $fYear - $relativeTermSuffix;
$fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1);
$fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
$to['d'] = $fiscalEnd['2'];
$to['M'] = $fiscalEnd['1'];
$to['Y'] = $fYear;
}
break;

case 'next':
Expand Down Expand Up @@ -1745,7 +1758,7 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
* Fiscal Start Month.
*
* @return int
* $fy Current Fiscl Year
* $fy Current Fiscal Year
*/
public static function calculateFiscalYear($fyDate, $fyMonth) {
$date = date("Y-m-d");
Expand Down
86 changes: 86 additions & 0 deletions tests/phpunit/CRM/Utils/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,90 @@ public function testGetFromTo($expectedFrom, $expectedTo, $relative, $from, $to)
$this->assertEquals($expectedTo, $calculatedTo);
}

/**
* Test relativeToAbsolute function on a range of fiscal year options.
*
* Go backwards one year at a time through the sequence.
*/
public function testRelativeToAbsoluteFiscalYear() {
$sequence = ['this', 'previous', 'previous_before'];
Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
$fiscalYearStartYear = (strtotime('now') > strtotime((date('Y-07-01')))) ? date('Y') : (date('Y') - 1);

foreach ($sequence as $relativeString) {
$date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
$this->assertEquals([
'from' => $fiscalYearStartYear . '0701',
'to' => ($fiscalYearStartYear + 1) . '0630'
], $date, 'relative term is ' . $relativeString);

$fiscalYearStartYear--;
}
}

/**
* Test relativeToAbsolute function on a range of year options.
*
* Go backwards one year at a time through the sequence.
*/
public function testRelativeToAbsoluteYear() {
$sequence = ['this', 'previous', 'previous_before'];
$year = date('Y');

foreach ($sequence as $relativeString) {
$date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year');
$this->assertEquals([
'from' => $year . '0101',
'to' => $year . '1231',
], $date, 'relative term is ' . $relativeString);

$year--;
}
}

/**
* Test relativeToAbsolute function on a range of year options.
*
* Go backwards one year at a time through the sequence.
*/
public function testRelativeToAbsoluteYearRange() {
$sequence = ['previous_2'];
$lastYear = (date('Y') - 1);

foreach ($sequence as $relativeString) {
$date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'year');
// For previous 2 years the range is e.g 2016-01-01 to 2017-12-31 so we have to subtract
// one from the range count to reflect the calendar year being one less apart due
// to it being from the beginning of one to the end of the next.
$offset = (substr($relativeString, -1, 1)) - 1;
$this->assertEquals([
'from' => $lastYear - $offset . '0101',
'to' => $lastYear . '1231',
], $date, 'relative term is ' . $relativeString);
}
}

/**
* Test relativeToAbsolute function on a range of year options.
*
* Go backwards one year at a time through the sequence.
*/
public function testRelativeToAbsoluteFiscalYearRange() {
$sequence = ['previous_2', 'previous_3', 'previous_4'];
Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
$lastFiscalYearEnd = (strtotime('now') > strtotime((date('Y-07-01')))) ? (date('Y')) : (date('Y') - 1);

foreach ($sequence as $relativeString) {
$date = CRM_Utils_Date::relativeToAbsolute($relativeString, 'fiscal_year');
// For previous 2 years the range is e.g 2015-07-01 to 2017-06-30 so we have to subtract
// one from the range count to reflect the calendar year being one less apart due
// to it being from the beginning of one to the end of the next.
$offset = (substr($relativeString, -1, 1));
$this->assertEquals([
'from' => $lastFiscalYearEnd - $offset . '0701',
'to' => $lastFiscalYearEnd . '0630',
], $date, 'relative term is ' . $relativeString);
}
}

}