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

Add freeform relative date for 'This Fiscal Year' #14894

Merged
merged 1 commit into from
Oct 21, 2019
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
61 changes: 34 additions & 27 deletions CRM/Utils/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -1114,13 +1114,6 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
switch ($unit) {
case 'year':
switch ($relativeTerm) {
case 'this':
$from['d'] = $from['M'] = 1;
$to['d'] = 31;
$to['M'] = 12;
$to['Y'] = $from['Y'] = $now['year'];
break;

case 'previous':
$from['M'] = $from['d'] = 1;
$to['d'] = 31;
Expand Down Expand Up @@ -1227,15 +1220,29 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
break;

default:
if ($relativeTermPrefix === 'ending') {
$to['d'] = $now['mday'];
$to['M'] = $now['mon'];
$to['Y'] = $now['year'];
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
$from = self::intervalAdd('year', -$relativeTermSuffix, $to);
$from = self::intervalAdd('second', 1, $from);
switch ($relativeTermPrefix) {

case 'ending':
$to['d'] = $now['mday'];
$to['M'] = $now['mon'];
$to['Y'] = $now['year'];
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
$from = self::intervalAdd('year', -$relativeTermSuffix, $to);
$from = self::intervalAdd('second', 1, $from);
break;

case 'this':
$from['d'] = $from['M'] = 1;
$to['d'] = 31;
$to['M'] = 12;
$to['Y'] = $from['Y'] = $now['year'];
if (is_numeric($relativeTermSuffix)) {
$from['Y'] -= ($relativeTermSuffix - 1);
}
break;
}
break;
}
break;

Expand All @@ -1249,10 +1256,15 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$from['Y'] = $fYear;
$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'];
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
if (is_numeric($relativeTermSuffix)) {
$from = self::intervalAdd('year', (-$relativeTermSuffix), $to);
$from = self::intervalAdd('second', 1, $from);
}
break;

case 'previous':
Expand All @@ -1263,6 +1275,8 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$to['d'] = $fiscalEnd['2'];
$to['M'] = $fiscalEnd['1'];
$to['Y'] = $fiscalEnd['0'];
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
}
else {
$from['Y'] = $fYear - $relativeTermSuffix;
Expand All @@ -1271,6 +1285,8 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$to['d'] = $fiscalEnd['2'];
$to['M'] = $fiscalEnd['1'];
$to['Y'] = $fYear;
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
}
break;

Expand Down Expand Up @@ -1856,17 +1872,8 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
break;
}

foreach ([
'from',
'to',
] as $item) {
if (!empty($$item)) {
$dateRange[$item] = self::format($$item);
}
else {
$dateRange[$item] = NULL;
}
}
$dateRange['from'] = empty($from) ? NULL : self::format($from);
$dateRange['to'] = empty($to) ? NULL : self::format($to);
return $dateRange;
}

Expand Down
57 changes: 55 additions & 2 deletions tests/phpunit/CRM/Utils/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,23 @@ public function testRelativeToAbsoluteFiscalYear() {
Civi::settings()->set('fiscalYearStart', ['M' => 7, 'd' => 1]);
$fiscalYearStartYear = (strtotime('now') > strtotime((date('Y-07-01')))) ? date('Y') : (date('Y') - 1);

// this_2 = 'These 2 Fiscal years'
$date = CRM_Utils_Date::relativeToAbsolute('this_2', 'fiscal_year');
$this->assertEquals([
'from' => ($fiscalYearStartYear - 1) . '0701000000',
'to' => ($fiscalYearStartYear + 1) . '0630235959',
], $date, 'relative term is this_2.fiscal_year');

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

$fiscalYearStartYear--;
}

}

/**
Expand All @@ -126,6 +134,14 @@ public function testRelativeToAbsoluteYear() {

$year--;
}

// this_2 = 'These 2 years'
$date = CRM_Utils_Date::relativeToAbsolute('this_2', 'year');
$thisYear = date('Y');
$this->assertEquals([
'from' => ($thisYear - 1) . '0101',
'to' => $thisYear . '1231',
], $date, 'relative term is this_2 year');
}

/**
Expand Down Expand Up @@ -165,6 +181,43 @@ public function testRelativeEnding() {
], $date, 'relative term is ending.week');
}

/**
* Test relativeToAbsolute function on a range of year options.
*
* Go backwards one year at a time through the sequence.
*/
public function testRelativeThisFiscal() {
$relativeDateValues = [
'ending.week' => '- 6 days',
'ending_30.day' => '- 29 days',
'ending.year' => '- 1 year + 1 day',
'ending_90.day' => '- 89 days',
'ending_60.day' => '- 59 days',
'ending_2.year' => '- 2 years + 1 day',
'ending_3.year' => '- 3 years + 1 day',
'ending_18.year' => '- 18 years + 1 day',
'ending_18.quarter' => '- 54 months + 1 day',
'ending_18.week' => '- 18 weeks + 1 day',
'ending_18.month' => '- 18 months + 1 day',
'ending_18.day' => '- 17 days',
];

foreach ($relativeDateValues as $key => $value) {
$parts = explode('.', $key);
$date = CRM_Utils_Date::relativeToAbsolute($parts[0], $parts[1]);
$this->assertEquals([
'from' => date('Ymd000000', strtotime($value)),
'to' => date('Ymd235959'),
], $date, 'relative term is ' . $key);
}

$date = CRM_Utils_Date::relativeToAbsolute('ending', 'month');
$this->assertEquals([
'from' => date('Ymd000000', strtotime('- 29 days')),
'to' => date('Ymd235959'),
], $date, 'relative term is ending.week');
}

/**
* Test relativeToAbsolute function on a range of year options.
*
Expand Down Expand Up @@ -205,7 +258,7 @@ public function testRelativeToAbsoluteFiscalYearRange() {
$offset = (substr($relativeString, -1, 1));
$this->assertEquals([
'from' => $lastFiscalYearEnd - $offset . '0701',
'to' => $lastFiscalYearEnd . '0630',
'to' => $lastFiscalYearEnd . '0630235959',
], $date, 'relative term is ' . $relativeString);
}
}
Expand Down