CRM-14951 fix for number of terms on IPN
authorEileen McNaughton <eileen@fuzion.co.nz>
Mon, 7 Jul 2014 12:06:04 +0000 (00:06 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Mon, 7 Jul 2014 12:07:42 +0000 (00:07 +1200)
CRM/Contribute/BAO/Contribution.php
CRM/Core/Payment/BaseIPN.php

index 6438dd0539848613d3919c90a996434b30a7f57f..d54cc25620656106c9e4692ff0a64923c2127ca5 100644 (file)
@@ -218,6 +218,31 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
     return NULL;
   }
 
+  /**
+   * Get the number of terms for this contribution for a given membership type
+   * based on querying the line item table and relevant price field values
+   * Note that any one contribution should only be able to have one line item relating to a particular membership
+   * type
+   * @param int $membershipTypeID
+   *
+   * @return int
+   */
+  public function getNumTermsByContributionAndMembershipType($membershipTypeID) {
+    if (!is_numeric($membershipTypeID)) {
+      //precautionary measure - this is being introduced to a mature release hence adding extra checks that
+      // might be removed later
+      return 1;
+    }
+    $numTerms = CRM_Core_DAO::singleValueQuery("
+      SELECT membership_num_terms FROM civicrm_line_item li
+      LEFT JOIN civicrm_price_field_value v ON li.price_field_value_id = v.id
+      WHERE entity_id = %1 AND entity_table = 'civicrm_contribution' AND membership_type_id = %2",
+      array(1 => array($this->id, 'Integer') , 2 => array($membershipTypeID, 'Integer'))
+    );
+    // default of 1 is precautionary
+    return empty($numTerms) ? 1 : $numTerms;
+  }
+
   /**
    * takes an associative array and creates a contribution object
    *
index 3976560052fb1a937debca4a8caadf044746a742..2dcac5dc4b7039f8838cc2acefee3e2b35ecd756 100644 (file)
@@ -380,9 +380,13 @@ LIMIT 1;";
                */
               CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeToday);
 
+              $num_terms = $contribution->getNumTermsByContributionAndMembershipType($membership->membership_type_id);
+              // @todo - we should pass membership_type_id instead of null here but not
+              // adding as not sure of testing
               $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id,
-                $changeToday
+                $changeToday, NULL, $num_terms
               );
+
               $dates['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format);
             }
             else {