Show Contact age on deceased date
authorSunil Pawar <sunil@skvare.com>
Fri, 8 Jan 2021 10:03:21 +0000 (15:33 +0530)
committerSunil Pawar <sunil@skvare.com>
Fri, 8 Jan 2021 10:03:21 +0000 (15:33 +0530)
CRM/Contact/BAO/Contact.php
CRM/Utils/Date.php
templates/CRM/Contact/Page/Inline/Demographics.tpl
tests/phpunit/CRM/Contact/BAO/ContactTest.php

index 0edeb2f09a4101f923a0fc136f278a95597c85c6..e90708118de071e136f7a8bab361ad8d8db9a30f 100644 (file)
@@ -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;
         }
index d3b807402228e4a015545c33469e5594fc75d4f4..f92c8cfc2aef91396904c548c2bc09b57d020fc1 100644 (file)
@@ -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']--;
         }
     }
index dee1ed5c2d24a5dcdaf0713bfb1229c6537f6762..72247a2fb47e80facb31c45c89da15a0fd01e4c4 100644 (file)
@@ -34,6 +34,7 @@
             <div class="crm-content crm-contact-deceased_date_display">
               {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})
             </div>
           </div>
         {else}
index 075655206d1d07a26fff0e57aff91bd9bee10e2e..84689e5dcd7b843e52a26edea246769f764b4624 100644 (file)
@@ -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']);
+  }
+
 }