From: Sunil Pawar Date: Fri, 8 Jan 2021 10:03:21 +0000 (+0530) Subject: Show Contact age on deceased date X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=786bde06a8d8b226f214bf3514c0dbd61e6e399b;p=civicrm-core.git Show Contact age on deceased date --- diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 0edeb2f09a..e90708118d 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -2599,7 +2599,11 @@ LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) if ($contact->birth_date) { $birthDate = CRM_Utils_Date::customFormat($contact->birth_date, '%Y%m%d'); if ($birthDate < date('Ymd')) { - $age = CRM_Utils_Date::calculateAge($birthDate); + $deceasedDate = NULL; + if (!empty($contact->is_deceased) && !empty($contact->deceased_date)) { + $deceasedDate = $contact->deceased_date; + } + $age = CRM_Utils_Date::calculateAge($birthDate, $deceasedDate); $values['age']['y'] = $age['years'] ?? NULL; $values['age']['m'] = $age['months'] ?? NULL; } diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index d3b8074022..f92c8cfc2a 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -893,11 +893,13 @@ class CRM_Utils_Date { * * @param date $birthDate * Birth Date. + * @param date $targetDate + * Target Date. (show age on specific date) * * @return int * array $results contains years or months */ - public static function calculateAge($birthDate) { + public static function calculateAge($birthDate, $targetDate = NULL) { $results = []; $formatedBirthDate = CRM_Utils_Date::customFormat($birthDate, '%Y-%m-%d'); @@ -905,23 +907,27 @@ class CRM_Utils_Date { $birthYear = $bDate[0]; $birthMonth = $bDate[1]; $birthDay = $bDate[2]; - $year_diff = date("Y") - $birthYear; + if (empty($targetDate)) { + $targetDate = date('Y-m-d'); + } + $targetDate = strtotime($targetDate); + + $year_diff = date("Y", $targetDate) - $birthYear; // don't calculate age CRM-3143 if ($birthYear == '1902') { return $results; } - switch ($year_diff) { case 1: - $month = (12 - $birthMonth) + date("m"); + $month = (12 - $birthMonth) + date("m", $targetDate); if ($month < 12) { - if (date("d") < $birthDay) { + if (date("d", $targetDate) < $birthDay) { $month--; } $results['months'] = $month; } - elseif ($month == 12 && (date("d") < $birthDay)) { + elseif ($month == 12 && (date("d", $targetDate) < $birthDay)) { $results['months'] = $month - 1; } else { @@ -930,13 +936,13 @@ class CRM_Utils_Date { break; case 0: - $month = date("m") - $birthMonth; + $month = date("m", $targetDate) - $birthMonth; $results['months'] = $month; break; default: $results['years'] = $year_diff; - if ((date("m") < $birthMonth) || (date("m") == $birthMonth) && (date("d") < $birthDay)) { + if ((date("m", $targetDate) < $birthMonth) || (date("m", $targetDate) == $birthMonth) && (date("d", $targetDate) < $birthDay)) { $results['years']--; } } diff --git a/templates/CRM/Contact/Page/Inline/Demographics.tpl b/templates/CRM/Contact/Page/Inline/Demographics.tpl index dee1ed5c2d..72247a2fb4 100644 --- a/templates/CRM/Contact/Page/Inline/Demographics.tpl +++ b/templates/CRM/Contact/Page/Inline/Demographics.tpl @@ -34,6 +34,7 @@
{assign var="date_format" value = $fields.birth_date.smarty_view_format} {$deceased_date|crmDate:$date_format} + ({if $age.y}{ts count=$age.y plural='%count years'}%count year{/ts}{elseif $age.m}{ts count=$age.m plural='%count months'}%count month{/ts}{/if})
{else} diff --git a/tests/phpunit/CRM/Contact/BAO/ContactTest.php b/tests/phpunit/CRM/Contact/BAO/ContactTest.php index 075655206d..84689e5dcd 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactTest.php @@ -1789,4 +1789,23 @@ class CRM_Contact_BAO_ContactTest extends CiviUnitTestCase { ]; } + /** + * Show age of contact on Deceased date + */ + public function testAgeOfDeceasedContact() { + $birthDate = '1961-06-06'; + $deceasedDate = '1991-07-07'; + $age = CRM_Utils_Date::calculateAge($birthDate, $deceasedDate); + $this->assertEquals('30', $age['years']); + } + + /** + * Show age of Contact with current date + */ + public function testAgeOfNormalContact() { + $birthDate = '1961-06-06'; + $age = CRM_Utils_Date::calculateAge($birthDate); + $this->assertGreaterThanOrEqual('59', $age['years']); + } + }