CRM-17586 - ensure total goal is accurate.
authorJamie McClelland <jm@mayfirst.org>
Tue, 17 Nov 2015 20:30:08 +0000 (15:30 -0500)
committerJamie McClelland <jm@mayfirst.org>
Fri, 20 Nov 2015 22:00:54 +0000 (17:00 -0500)
CRM/Report/Form/Contribute/PCP.php

index 93e875e1c21a12961eaf2afa849c8bb401fb2359..8ed35a035fa4bcb67f5a06df710f7513e0a60d5f 100644 (file)
@@ -264,32 +264,48 @@ LEFT JOIN civicrm_contribution_page {$this->_aliases['civicrm_contribution_page'
   public function statistics(&$rows) {
     $statistics = parent::statistics($rows);
 
+    // Calculate totals from the civicrm_contribution_soft table.
     $select
-      = "SELECT SUM({$this->_aliases['civicrm_pcp']}.goal_amount) as goal_total, " .
-        "SUM({$this->_aliases['civicrm_contribution_soft']}.amount) as committed_total, " .
+      = "SELECT SUM({$this->_aliases['civicrm_contribution_soft']}.amount) as committed_total, " .
         "COUNT({$this->_aliases['civicrm_contribution_soft']}.id) as donors_total, " .
         "SUM(IF( contribution_civireport.contribution_status_id > 1, 0, contribution_soft_civireport.amount)) AS received_total ";
     $sql = "{$select} {$this->_from} {$this->_where}";
     $dao = CRM_Core_DAO::executeQuery($sql);
     $dao->fetch();
+    $committed_total = $dao->committed_total;
+    $received_total = $dao->received_total;
+    $donors_total =  $dao->donors_total;
+
+    // Calculate goal total goal from the PCP table (we only want one result per
+    // PCP page - the query above produces one result per contribution made).
+    $sql =
+      "SELECT SUM(goal_amount) as goal_total FROM civicrm_pcp WHERE ".
+      "goal_amount IS NOT NULL AND id IN (" .
+        "SELECT DISTINCT {$this->_aliases['civicrm_pcp']}.id {$this->_from} ".
+        "{$this->_where}".
+      ")";
+    $dao = CRM_Core_DAO::executeQuery($sql);
+    $dao->fetch();
+    $goal_total = $dao->goal_total;
+
     $statistics['counts']['goal_total'] = array(
       'title' => ts('Goal Total'),
-      'value' => $dao->goal_total,
+      'value' => $goal_total,
       'type' => CRM_Utils_Type::T_MONEY,
     );
     $statistics['counts']['committed_total'] = array(
       'title' => ts('Total Committed'),
-      'value' => $dao->committed_total,
+      'value' => $committed_total,
       'type' => CRM_Utils_Type::T_MONEY,
     );
     $statistics['counts']['received_total'] = array(
       'title' => ts('Total Received'),
-      'value' => $dao->received_total,
+      'value' => $received_total,
       'type' => CRM_Utils_Type::T_MONEY,
     );
     $statistics['counts']['donors_total'] = array(
       'title' => ts('Total Donors'),
-      'value' => $dao->donors_total,
+      'value' => $donors_total,
       'type' => CRM_Utils_Type::T_INT,
     );
     return $statistics;