Improve relationship report by adding in end_date check for active relationships...
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 17 Dec 2015 19:57:50 +0000 (19:57 +0000)
committerSeamus Lee <seamuslee001@gmail.com>
Sat, 19 Dec 2015 23:57:01 +0000 (23:57 +0000)
Switch to using is_valid test as per 4.7

CRM/Report/Form/Contact/Relationship.php

index 97e49c6760c4494de0d86c55537929e16f3846b8..04b34278eb25e9e3088df632003e2478e591427b 100644 (file)
@@ -219,6 +219,16 @@ class CRM_Report_Form_Contact_Relationship extends CRM_Report_Form {
             ),
             'type' => CRM_Utils_Type::T_INT,
           ),
+          'is_valid' => array(
+            'title' => ts('Relationship Dates Validity'),
+            'operatorType' => CRM_Report_Form::OP_SELECT,
+            'options' => array(
+              NULL => ts('- Any -'),
+              1 => ts('Not expired'),
+              0 => ts('Expired'),
+            ),
+            'type' => CRM_Utils_Type::T_INT,
+          ),
           'relationship_type_id' => array(
             'title' => ts('Relationship'),
             'operatorType' => CRM_Report_Form::OP_SELECT,
@@ -228,6 +238,14 @@ class CRM_Report_Form_Contact_Relationship extends CRM_Report_Form {
             CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
             'type' => CRM_Utils_Type::T_INT,
           ),
+          'start_date' => array(
+            'title' => ts('Start Date'),
+            'type' => CRM_Utils_Type::T_DATE,
+          ),
+          'end_date' => array(
+            'title' => ts('End Date'),
+            'type' => CRM_Utils_Type::T_DATE,
+          ),
         ),
         'grouping' => 'relation-fields',
       ),
@@ -420,13 +438,17 @@ class CRM_Report_Form_Contact_Relationship extends CRM_Report_Form {
                 }
               }
               else {
-
-                $clause = $this->whereClause($field,
-                  $op,
-                  CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
-                  CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
-                  CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
-                );
+                if ($fieldName == 'is_valid') {
+                  $clause = $this->buildValidityQuery(CRM_Utils_Array::value("{$fieldName}_value", $this->_params));
+                }
+                else {
+                  $clause = $this->whereClause($field,
+                    $op,
+                    CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
+                    CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
+                    CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
+                  );
+                }
               }
             }
           }
@@ -629,4 +651,22 @@ class CRM_Report_Form_Contact_Relationship extends CRM_Report_Form {
     }
   }
 
+  /**
+   * @param $valid bool - set to 1 if we are looking for a valid relationship, 0 if not
+   *
+   * @return array
+   */
+  public function buildValidityQuery($valid) {
+    $clause = NULL;
+    if ($valid == '1') {
+      // relationships dates are not expired
+      $clause = "((start_date <= CURDATE() OR start_date is null) AND (end_date >= CURDATE() OR end_date is null))";
+    }
+    elseif ($valid == '0') {
+      // relationships dates are expired or has not started yet
+      $clause = "(start_date >= CURDATE() OR end_date < CURDATE())";
+    }
+    return $clause;
+  }
+
 }