CRM-16901 fix - Offline Recurring Payments
authormonishdeb <monish.deb@webaccessglobal.com>
Wed, 2 Dec 2015 14:49:57 +0000 (20:19 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Wed, 2 Dec 2015 14:49:57 +0000 (20:19 +0530)
https://issues.civicrm.org/jira/browse/CRM-16901

CRM/Contribute/BAO/ContributionRecur.php
CRM/Contribute/BAO/Query.php
CRM/Report/Form.php
CRM/Report/Form/Contribute/RecurSummary.php
templates/CRM/Contribute/Form/Search/ContributionRecur.tpl

index 3edeff6beb30da79b1b4e16179d324e3093e8052..ac4d40faf4c1a16a73978c19e2a005bdafebace3 100644 (file)
@@ -720,13 +720,12 @@ INNER JOIN civicrm_contribution       con ON ( con.id = mp.contribution_id )
       }
     }
 
-    $form->add('hidden', 'hidden_recurringcontribution', 1);
     // Add field to check if payment is made for recurring contribution
     $recurringPaymentOptions = array(
-      0 => ts(' All recurring contributions'),
-      1 => ts(' Recurring contributions with at least one payment'),
+      1 => ts(' All recurring contributions'),
+      2 => ts(' Recurring contributions with at least one payment'),
     );
-    $form->addRadio('contribution_recur_payment_made', ts(''), $recurringPaymentOptions, array('allowClear' => TRUE));
+    $form->addRadio('contribution_recur_payment_made', NULL, $recurringPaymentOptions, array('allowClear' => TRUE));
     CRM_Core_Form_Date::buildDateRange($form, 'contribution_recur_start_date', 1, '_low', '_high', ts('From'), FALSE, FALSE, 'birth');
     CRM_Core_Form_Date::buildDateRange($form, 'contribution_recur_end_date', 1, '_low', '_high', ts('From'), FALSE, FALSE, 'birth');
     CRM_Core_Form_Date::buildDateRange($form, 'contribution_recur_modified_date', 1, '_low', '_high', ts('From'), FALSE, FALSE, 'birth');
index 7dd498ffc0ed43554797256ec58230f7bb5224ae..c9597e8fca9ce600ae05c9b8b232c4f22609c9c6 100644 (file)
@@ -519,7 +519,7 @@ class CRM_Contribute_BAO_Query {
 
       case 'contribution_recur_payment_made':
         $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution_recur.id", 'IS NOT EMPTY');
-        if ($value) {
+        if ($value == 2) {
           $query->_qill[$grouping][] = ts("Recurring contributions with at least one payment");
           self::$_contribRecurPayment = TRUE;
         }
@@ -645,7 +645,7 @@ class CRM_Contribute_BAO_Query {
           // 'Made payment for the recurring contributions?' is ticked yes
           $from = " $side JOIN civicrm_contribution_recur ON contact_a.id = civicrm_contribution_recur.contact_id ";
           if (self::$_contribRecurPayment == TRUE) {
-            $from .= " AND civicrm_contribution.contribution_recur_id = civicrm_contribution_recur.id ";
+            $from .= " INNER JOIN civicrm_contribution cr ON cr.contribution_recur_id = civicrm_contribution_recur.id ";
           }
         }
         else {
index b55a970b6708b6f90aa4de915ef4b274d5104dc8..afe13b7ceef67d4505c5b389512ec6d14744158f 100644 (file)
@@ -3078,7 +3078,8 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
     foreach ($this->_columns as $tableName => $table) {
       if (array_key_exists('filters', $table)) {
         foreach ($table['filters'] as $fieldName => $field) {
-          if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE &&
+          if ((CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE ||
+              CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_TIME) &&
             CRM_Utils_Array::value('operatorType', $field) !=
             CRM_Report_Form::OP_MONTH
           ) {
index c066da48fc066b0a4307821fd70b8b7e85c3080c..0133db518a21685ceba52d2273764a9fe66b11e0 100644 (file)
@@ -74,8 +74,8 @@ class CRM_Report_Form_Contribute_RecurSummary extends CRM_Report_Form {
         'filters' => array(
           'start_date' => array(
             'title' => ts('Start Date'),
-            'operatorType' => CRM_Report_Form::OP_DATE,
-            'type' => CRM_Utils_Type::T_DATE,
+            'operatorType' => CRM_Report_Form::OP_DATETIME,
+            'type' => CRM_Utils_Type::T_TIME,
           ),
         ),
       ),
@@ -241,15 +241,15 @@ class CRM_Report_Form_Contribute_RecurSummary extends CRM_Report_Form {
 
     $entryFound = FALSE;
 
-    $startDateFrom = $startDateTo = '';
+    $startDateFrom = CRM_Utils_Array::value("start_date_to", $this->_params);
+    $startDateTo = CRM_Utils_Array::value("start_date_from", $this->_params);
+    $startDateRelative = CRM_Utils_Array::value("start_date_relative", $this->_params);
 
-    if (isset($_POST['start_date_from']) && !empty($_POST['start_date_from'])) {
-      $startDateFrom = CRM_Utils_Date::processDate($_POST['start_date_from']);
-    }
+    $startedDateSql = $this->dateClause('start_date', $startDateRelative, $startDateFrom, $startDateTo);
+    $startedDateSql = $startedDateSql ? $startedDateSql : " ( 1 ) ";
 
-    if (isset($_POST['start_date_to']) && !empty($_POST['start_date_to'])) {
-      $startDateTo = CRM_Utils_Date::processDate($_POST['start_date_to']);
-    }
+    $cancelledDateSql = $this->dateClause('cancel_date', $startDateRelative, $startDateFrom, $startDateTo);
+    $cancelledDateSql = $cancelledDateSql ? $cancelledDateSql : " ( cancel_date IS NOT NULL ) ";
 
     $started = $cancelled = $active = $total = 0;
 
@@ -261,13 +261,7 @@ class CRM_Report_Form_Contribute_RecurSummary extends CRM_Report_Form {
       $rows[$rowNum]['civicrm_contribution_recur_cancel_date'] = 0;
       $rows[$rowNum]['civicrm_contribution_recur_contribution_status_id'] = 0;
 
-      $startedSql = "SELECT count(*) as count FROM civicrm_contribution_recur WHERE payment_instrument_id = $paymentInstrumentId";
-      if (!empty($startDateFrom)) {
-        $startedSql .= " AND start_date >= '{$startDateFrom}'";
-      }
-      if (!empty($startDateTo)) {
-        $startedSql .= " AND start_date <= '{$startDateTo}'";
-      }
+      $startedSql = "SELECT count(*) as count FROM civicrm_contribution_recur WHERE payment_instrument_id = $paymentInstrumentId AND $startedDateSql ";
 
       $startedDao = CRM_Core_DAO::executeQuery($startedSql);
       $startedDao->fetch();
@@ -275,13 +269,7 @@ class CRM_Report_Form_Contribute_RecurSummary extends CRM_Report_Form {
       $rows[$rowNum]['civicrm_contribution_recur_start_date'] = $startedDao->count;
       $started = $started + $startedDao->count;
 
-      $cancelledSql = "SELECT count(*) as count FROM civicrm_contribution_recur WHERE payment_instrument_id = $paymentInstrumentId";
-      if (!empty($startDateFrom)) {
-        $cancelledSql .= " AND cancel_date >= '{$startDateFrom}'";
-      }
-      if (!empty($startDateTo)) {
-        $cancelledSql .= " AND cancel_date <= '{$startDateTo}'";
-      }
+      $cancelledSql = "SELECT count(*) as count FROM civicrm_contribution_recur WHERE payment_instrument_id = $paymentInstrumentId AND $cancelledDateSql ";
 
       $cancelledDao = CRM_Core_DAO::executeQuery($cancelledSql);
       $cancelledDao->fetch();
@@ -291,13 +279,17 @@ class CRM_Report_Form_Contribute_RecurSummary extends CRM_Report_Form {
       $cancelled = $cancelled + $cancelledDao->count;
 
       $activeSql = "SELECT count(*) as count FROM civicrm_contribution_recur WHERE payment_instrument_id = $paymentInstrumentId";
-      if (!empty($startDateFrom)) {
-        //$activeSql .= " AND start_date >= '{$startDateFrom}'";
-        //$activeSql .= " AND end_date >= '{$startDateFrom}'";
+      list($from, $to) = $this->getFromTo($startDateRelative, $startDateFrom, $startDateTo);
+      // To find active recurring contribution start date must be >= to start of selected date-range AND
+      // end date or cancel date must be >= to end of selected date-range if NOT null OR end date is null
+      if (!empty($from)) {
+        $activeSql .= " AND start_date >= '{$from}'";
       }
-      if (!empty($startDateTo)) {
-        $activeSql .= " AND start_date <= '{$startDateTo}'";
-        $activeSql .= " AND (end_date <= '{$startDateTo}' OR end_date IS NULL)";
+      if (!empty($to)) {
+        $activeSql .= " AND (
+        ( end_date >= '{$to}' AND end_date IS NOT NULL ) OR
+        ( cancel_date >= '{$to}' AND cancel_date IS NOT NULL ) OR
+        end_date IS NULL )";
       }
 
       $activeDao = CRM_Core_DAO::executeQuery($activeSql);
@@ -311,14 +303,8 @@ class CRM_Report_Form_Contribute_RecurSummary extends CRM_Report_Form {
       $amountSql = "
        SELECT SUM(cc.total_amount) as amount FROM `civicrm_contribution` cc
        INNER JOIN civicrm_contribution_recur cr ON (cr.id = cc.contribution_recur_id AND cr.payment_instrument_id = {$paymentInstrumentId})
-       WHERE cc.contribution_status_id = 1 AND cc.is_test = 0";
-      if (!empty($startDateFrom)) {
-        $amountSql .= " AND cc.`receive_date` >= '{$startDateFrom}'";
-      }
-      if (!empty($startDateTo)) {
-        $amountSql .= " AND cc.`receive_date` <= '{$startDateTo}'";
-      }
-
+       WHERE cc.contribution_status_id = 1 AND cc.is_test = 0 AND ";
+      $amountSql .= str_replace("start_date", "cc.`receive_date`", $startedDateSql);
       $amountDao = CRM_Core_DAO::executeQuery($amountSql);
       $amountDao->fetch();
       if ($amountDao->amount) {
index 23f49337c8ebd51b0dbb2d148c200943901dc42f..a01cad2e31a534a58624cc462ca34c90a8d3bf12 100644 (file)
@@ -83,4 +83,3 @@
 </div><!-- /.crm-accordion-wrapper -->
 
 
-