diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 74aa2e35caec..b2316eca75d9 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -1209,6 +1209,17 @@ public static function relativeToAbsolute($relativeTerm, $unit) { $to['M'] = $now['mon']; $to['Y'] = $now['year'] + 1; 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); + } } break; @@ -1433,6 +1444,17 @@ public static function relativeToAbsolute($relativeTerm, $unit) { $to = self::intervalAdd('day', 90, $from); $to = self::intervalAdd('second', -1, $to); 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('month', -($relativeTermSuffix * 3), $to); + $from = self::intervalAdd('second', 1, $from); + } } break; @@ -1602,6 +1624,17 @@ public static function relativeToAbsolute($relativeTerm, $unit) { $to = self::intervalAdd('day', 60, $from); $to = self::intervalAdd('second', -1, $to); 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($unit, -$relativeTermSuffix, $to); + $from = self::intervalAdd('second', 1, $from); + } } break; @@ -1719,6 +1752,17 @@ public static function relativeToAbsolute($relativeTerm, $unit) { $to = self::intervalAdd('day', 7, $from); $to = self::intervalAdd('second', -1, $to); 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($unit, -$relativeTermSuffix, $to); + $from = self::intervalAdd('second', 1, $from); + } } break; @@ -1782,6 +1826,16 @@ public static function relativeToAbsolute($relativeTerm, $unit) { $from['Y'] = $to['Y']; 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($unit, -$relativeTermSuffix, $to); + $from = self::intervalAdd('second', 1, $from); + } } break; } diff --git a/tests/phpunit/CRM/Utils/DateTest.php b/tests/phpunit/CRM/Utils/DateTest.php index faa661c9e21b..d930c360c7a9 100644 --- a/tests/phpunit/CRM/Utils/DateTest.php +++ b/tests/phpunit/CRM/Utils/DateTest.php @@ -128,6 +128,43 @@ public function testRelativeToAbsoluteYear() { } } + /** + * Test relativeToAbsolute function on a range of year options. + * + * Go backwards one year at a time through the sequence. + */ + public function testRelativeEnding() { + $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. * diff --git a/xml/templates/civicrm_data.tpl b/xml/templates/civicrm_data.tpl index d20de95e1c7a..5c256e5143b7 100644 --- a/xml/templates/civicrm_data.tpl +++ b/xml/templates/civicrm_data.tpl @@ -984,9 +984,9 @@ VALUES (@option_group_id_date_filter, '{ts escape="sql"}Previous fiscal year{/ts}', 'previous.fiscal_year', 'previous.fiscal_year', NULL, NULL, NULL,11, NULL, 0, 0, 1, NULL, NULL, NULL), (@option_group_id_date_filter, '{ts escape="sql"}Previous calendar year{/ts}', 'previous.year', 'previous.year', NULL, NULL, NULL,12, NULL, 0, 0, 1, NULL, NULL, NULL), (@option_group_id_date_filter, '{ts escape="sql"}Last 7 days including today{/ts}', 'ending.week', 'ending.week', NULL, NULL, NULL,13, NULL, 0, 0, 1, NULL, NULL, NULL), - (@option_group_id_date_filter, '{ts escape="sql"}Last 30 days including today{/ts}', 'ending.month', 'ending.month', NULL, NULL, NULL,14, NULL, 0, 0, 1, NULL, NULL, NULL), - (@option_group_id_date_filter, '{ts escape="sql"}Last 60 days including today{/ts}', 'ending_2.month', 'ending_2.month', NULL, NULL, NULL,15, NULL, 0, 0, 1, NULL, NULL, NULL), - (@option_group_id_date_filter, '{ts escape="sql"}Last 90 days including today{/ts}', 'ending.quarter', 'ending.quarter', NULL, NULL, NULL,16, NULL, 0, 0, 1, NULL, NULL, NULL), + (@option_group_id_date_filter, '{ts escape="sql"}Last 30 days including today{/ts}', 'ending_30.day', 'ending.month', NULL, NULL, NULL,14, NULL, 0, 0, 1, NULL, NULL, NULL), + (@option_group_id_date_filter, '{ts escape="sql"}Last 60 days including today{/ts}', 'ending_60.day', 'ending_2.month', NULL, NULL, NULL,15, NULL, 0, 0, 1, NULL, NULL, NULL), + (@option_group_id_date_filter, '{ts escape="sql"}Last 90 days including today{/ts}', 'ending_90.day', 'ending.quarter', NULL, NULL, NULL,16, NULL, 0, 0, 1, NULL, NULL, NULL), (@option_group_id_date_filter, '{ts escape="sql"}Last 12 months including today{/ts}', 'ending.year', 'ending.year', NULL, NULL, NULL,17, NULL, 0, 0, 1, NULL, NULL, NULL), (@option_group_id_date_filter, '{ts escape="sql"}Last 2 years including today{/ts}', 'ending_2.year', 'ending_2.year', NULL, NULL, NULL,18, NULL, 0, 0, 1, NULL, NULL, NULL), (@option_group_id_date_filter, '{ts escape="sql"}Last 3 years including today{/ts}', 'ending_3.year', 'ending_3.year', NULL, NULL, NULL,19, NULL, 0, 0, 1, NULL, NULL, NULL),