Merge pull request #1824 from dlobo/CRM-13577
[civicrm-core.git] / CRM / Report / Form / Member / ContributionDetail.php
index 2c150088a7ba74aa34efebd7b79d8e1cc8c2cb30..442c6f856c8a9b3b0767934a970d65f3608b8035 100644 (file)
@@ -42,6 +42,7 @@ class CRM_Report_Form_Member_ContributionDetail extends CRM_Report_Form {
   protected $_nameFieldHonor = FALSE;
 
   protected $_summary = NULL;
+  protected $_allBatches = NULL;
 
   protected $_customGroupExtends = array(
     'Contribution', 'Membership');
@@ -77,6 +78,11 @@ class CRM_Report_Form_Member_ContributionDetail extends CRM_Report_Form {
           array('title' => ts('Contact Type'),
             'no_repeat' => TRUE,
           ),
+          'contact_sub_type' =>
+          array(
+            'title' => ts('Contact SubType'),
+            'no_repeat' => TRUE,
+          ),
           'do_not_email' =>
           array('title' => ts('Do Not Email'),
             'no_repeat' => TRUE,
@@ -415,8 +421,8 @@ class CRM_Report_Form_Member_ContributionDetail extends CRM_Report_Form {
     $this->_tagFilter = TRUE;
 
     // Don't show Batch display column and filter unless batches are being used
-    $this->_closedBatches = CRM_Batch_BAO_Batch::getBatches();
-    if (!empty($this->_closedBatches)) {
+    $this->_allBatches = CRM_Batch_BAO_Batch::getBatches();
+    if (!empty($this->_allBatches)) {
       $this->_columns['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch';
       $this->_columns['civicrm_batch']['fields']['batch_id'] = array(
         'name' => 'id',
@@ -427,7 +433,7 @@ class CRM_Report_Form_Member_ContributionDetail extends CRM_Report_Form {
         'title' => ts('Batch Name'),
         'type' => CRM_Utils_Type::T_INT,
         'operatorType' => CRM_Report_Form::OP_MULTISELECT,
-        'options' => $this->_closedBatches,
+        'options' => $this->_allBatches,
       );
       $this->_columns['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch';
       $this->_columns['civicrm_entity_batch']['fields']['entity_batch_id'] = array(
@@ -570,12 +576,14 @@ class CRM_Report_Form_Member_ContributionDetail extends CRM_Report_Form {
                          {$this->_aliases['civicrm_phone']}.is_primary = 1)";
     }
     //for contribution batches
-    if ($this->_closedBatches &&
+    if ($this->_allBatches &&
       (CRM_Utils_Array::value('batch_id', $this->_params['fields']) || !empty($this->_params['bid_value']))) {
       $this->_from .= "
+                LEFT JOIN civicrm_entity_financial_trxn tx ON (tx.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
+                   tx.entity_table = 'civicrm_contribution')
                  LEFT JOIN  civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']}
-                        ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = {$this->_aliases['civicrm_contribution']}.id AND
-                        {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_contribution')
+                        ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id AND
+                        {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn')
                  LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']}
                         ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id";
     }
@@ -678,6 +686,7 @@ class CRM_Report_Form_Member_ContributionDetail extends CRM_Report_Form {
     $sql = "{$select} {$this->_from} {$this->_where} {$group}";
 
     $dao = CRM_Core_DAO::executeQuery($sql);
+    $totalAmount = $average = array();
     while ($dao->fetch()) {
       $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency)."(".$dao->count.")";
       $average[] =   CRM_Utils_Money::format($dao->avg, $dao->currency);