CRM-14197 set entity_id & entity_table to reflect membership on membership transactions
[civicrm-core.git] / CRM / Member / BAO / Membership.php
index 13b862bc53a6154751afa8840930e3b56a3dd248..c3beb0ea55a63a3aa14bc02ac5a0718bb9be3663 100644 (file)
@@ -46,6 +46,15 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
 
   static $_signupActType = NULL;
 
+  /**
+   * class constructor
+   *
+   * @access public
+   * @return \CRM_Member_DAO_Membership
+   */
+  /**
+   *
+   */
   function __construct() {
     parent::__construct();
   }
@@ -216,9 +225,13 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
   /**
    * takes an associative array and creates a membership object
    *
-   * @param array    $params      (reference ) an assoc array of name/value pairs
-   * @param array    $ids         the array that holds all the db ids
-   * @param boolean  $callFromAPI Is this function called from API?
+   * @param array $params (reference ) an assoc array of name/value pairs
+   * @param array $ids the array that holds all the db ids
+   * @param bool $skipRedirect
+   * @param string $activityType
+   *
+   * @throws CRM_Core_Exception
+   * @internal param bool $callFromAPI Is this function called from API?
    *
    * @return object CRM_Member_BAO_Membership object
    * @access public
@@ -270,7 +283,8 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
     //    CRM_Contact_BAO_Relationship::relatedMemberships()
     if (isset($params['owner_membership_id'])) {
       unset($params['max_related']);
-    } else {
+    }
+    else {
       // if membership allows related, default max_related to value in membership_type
       if (!array_key_exists('max_related', $params) && !empty($params['membership_type_id'])) {
         $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($params['membership_type_id']);
@@ -413,7 +427,9 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
    * Function to check the membership extended through relationship
    *
    * @param int $membershipId membership id
-   * @param int $contactId    contact id
+   * @param int $contactId contact id
+   *
+   * @param const $action
    *
    * @return Array    array of contact_id of all related contacts.
    * @static
@@ -477,10 +493,11 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
    * full featured over a period of time. This is the inverse function of
    * create.  It also stores all the retrieved values in the default array
    *
-   * @param array $params   (reference ) an assoc array of name/value pairs
+   * @param array $params (reference ) an assoc array of name/value pairs
    * @param array $defaults (reference ) an assoc array to hold the name / value pairs
    *                        in a hierarchical manner
-   * @param array $ids      (reference) the array that holds all the db ids
+   *
+   * @internal param array $ids (reference) the array that holds all the db ids
    *
    * @return object CRM_Member_BAO_Membership object
    * @access public
@@ -693,12 +710,17 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
   /**
    * Function to build Membership  Block in Contribution Pages
    *
-   * @param object  $form                      form object
-   * @param int     $pageId                    contribution page id
+   * @param object $form form object
+   * @param $pageID
+   * @param $cid
    * @param boolean $formItems
-   * @param int     $selectedMembershipTypeID  selected membership id
-   * @param boolean $thankPage                 thank you page
-   * @param boolean $memContactId              contact who is to be
+   * @param int $selectedMembershipTypeID selected membership id
+   * @param boolean $thankPage thank you page
+   * @param null $isTest
+   *
+   * @return bool|mixed
+   * @internal param int $pageId contribution page id
+   * @internal param bool $memContactId contact who is to be
    * checked for having a current membership for a particular membership
    *
    * @static
@@ -890,7 +912,10 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
   /**
    * Function to return Membership Block info in Contribution Pages
    *
-   * @param int $pageId contribution page id
+   * @param $pageID
+   *
+   * @return array|null
+   * @internal param int $pageId contribution page id
    *
    * @static
    */
@@ -927,11 +952,14 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    * Function to return a current membership of given contact
    *        NB: if more than one membership meets criteria, a randomly selected one is returned.
    *
-   * @param int $contactID  contact id
+   * @param int $contactID contact id
    * @param int $memType membership type, null to retrieve all types
    * @param int $isTest
-   * @param int $membershipID if provided, then determine if it is current
+   * @param null $membershipId
    * @param boolean $onlySameParentOrg true if only Memberships with same parent org as the $memType wanted, false otherwise
+   *
+   * @return array|bool
+   * @internal param int $membershipID if provided, then determine if it is current
    * @static
    */
   static function getContactMembership($contactID, $memType, $isTest, $membershipId = NULL, $onlySameParentOrg = FALSE) {
@@ -1098,11 +1126,11 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    * Signup" or "Membership Renewal" activity falls in the given date range.
    * Dates match the pattern "yyyy-mm-dd".
    *
-   * @param int    $membershipTypeId  membership type id
-   * @param int    $startDate         date on which to start counting
-   * @param int    $endDate           date on which to end counting
-   * @param bool   $isTest            if true, membership is for a test site
-   * @param bool   $isOwner           if true, only retrieve membership records for owners //LCD
+   * @param int $membershipTypeId membership type id
+   * @param int $startDate date on which to start counting
+   * @param int $endDate date on which to end counting
+   * @param bool|int $isTest if true, membership is for a test site
+   * @param bool|int $isOwner if true, only retrieve membership records for owners //LCD
    *
    * @return returns the number of members of type $membershipTypeId whose
    *         start_date is between $startDate and $endDate
@@ -1158,10 +1186,10 @@ INNER JOIN  civicrm_contact contact ON ( contact.id = membership.contact_id AND
    * The second condition takes care of records that have no end_date.  These
    * are assumed to be lifetime memberships.
    *
-   * @param int    $membershipTypeId   membership type id
-   * @param string $date               the date for which to retrieve the count
-   * @param bool   $isTest             if true, membership is for a test site
-   * @param bool   $isOwner           if true, only retrieve membership records for owners //LCD
+   * @param int $membershipTypeId membership type id
+   * @param string $date the date for which to retrieve the count
+   * @param bool|int $isTest if true, membership is for a test site
+   * @param bool|int $isOwner if true, only retrieve membership records for owners //LCD
    *
    * @return returns the number of members of type $membershipTypeId as of
    *         $date.
@@ -1196,7 +1224,7 @@ AND civicrm_membership.is_test = %2";
    *
    * @param int $contactId contact id
    *
-   * @return
+   * @return int
    */
   static function statusAvailabilty($contactId) {
     $membership = new CRM_Member_DAO_MembershipStatus();
@@ -1207,10 +1235,15 @@ AND civicrm_membership.is_test = %2";
   /**
    * Process the Memberships
    *
-   * @param array  $membershipParams array of membership fields
-   * @param int    $contactID        contact id
-   * @param object $form             form object
+   * @param array $membershipParams array of membership fields
+   * @param int $contactID contact id
+   * @param object $form form object
+   *
+   * @param $premiumParams
+   * @param null $customFieldsFormatted
+   * @param null $includeFieldTypes
    *
+   * @throws CRM_Core_Exception
    * @return void
    * @access public
    */
@@ -1291,19 +1324,15 @@ AND civicrm_membership.is_test = %2";
       $invoiceID = md5(uniqid(rand(), TRUE));
       $tempParams['invoiceID'] = $invoiceID;
 
-      //we don't allow recurring membership.CRM-3781.
-      if (!empty($tempParams['is_recur'])) {
-        $tempParams['is_recur'] = 0;
-      }
       $result = NULL;
       if ($form->_values['is_monetary'] && !$form->_params['is_pay_later'] && $minimumFee > 0.0) {
         $payment = CRM_Core_Payment::singleton($form->_mode, $form->_paymentProcessor, $form);
 
         if ($form->_contributeMode == 'express') {
-          $result = &$payment->doExpressCheckout($tempParams);
+          $result = $payment->doExpressCheckout($tempParams);
         }
         else {
-          $result = &$payment->doDirectPayment($tempParams);
+          $result = $payment->doDirectPayment($tempParams);
         }
       }
 
@@ -1367,6 +1396,9 @@ AND civicrm_membership.is_test = %2";
             $customFieldsFormatted, CRM_Utils_Array::value($memType, $typesTerms, 1)
           );
 
+          // update recurring id for membership record
+          self::updateRecurMembership($membership, $contribution[$index]);
+
           $createdMemberships[$memType] = $membership;
           if (isset($contribution[$index])) {
             //insert payment record
@@ -1405,6 +1437,10 @@ AND civicrm_membership.is_test = %2";
           CRM_Utils_Array::value('cms_contactID', $membershipParams),
           $customFieldsFormatted, CRM_Utils_Array::value('types_terms', $membershipParams, 1)
         );
+
+        // update recurring id for membership record
+        self::updateRecurMembership($membership, $contribution[$index]);
+
         if (isset($contribution[$index])) {
           //insert payment record
           $dao = new CRM_Member_DAO_MembershipPayment();
@@ -1474,6 +1510,32 @@ AND civicrm_membership.is_test = %2";
     );
   }
 
+  /**
+   * Function for updating a membership record's contribution_recur_id
+   *
+   * @param object CRM_Member_DAO_Membership $membership
+   * @param object CRM_Contribute_BAO_Contribution $contribution
+   *
+   * @return void
+   * @static
+   * @access public
+   */
+  static public function updateRecurMembership(CRM_Member_DAO_Membership &$membership,
+    CRM_Contribute_BAO_Contribution &$contribution) {
+
+    if (empty($contribution->contribution_recur_id)) {
+      return;
+    }
+
+    $params = array(
+      1 => array($contribution->contribution_recur_id, 'Integer'),
+      2 => array($membership->id, 'Integer'),
+    );
+
+    $sql = "UPDATE civicrm_membership SET contribution_recur_id = %1 WHERE id = %2";
+    CRM_Core_DAO::executeQuery($sql, $params);
+  }
+
   /**
    * @todo - this form method needs to have the interaction with the form layer removed from it
    * as a BAO function. Note that the api now supports membership renewals & it is not clear this function does anything
@@ -1483,20 +1545,22 @@ AND civicrm_membership.is_test = %2";
    * whether the given contact has a membership or not. And will add
    * the modified dates for membership and in the log table.
    *
-   * @param int     $contactID           id of the contact
-   * @param int     $membershipTypeID    id of the new membership type
-   * @param boolean $is_test             if this is test contribution or live contribution
-   * @param object  $form                form object
-   * @param array   $ipnParams           array of name value pairs, to be used (for e.g source) when $form not present
-   * @param int     $modifiedID          individual contact id in case of On Behalf signup (CRM-4027 )
-   * @param int     $numRenewTerms       how many membership terms are being added to end date (default is 1)
+   * @param int $contactID id of the contact
+   * @param int $membershipTypeID id of the new membership type
+   * @param boolean $is_test if this is test contribution or live contribution
+   * @param object $form form object
+   * @param null $changeToday
+   * @param int $modifiedID individual contact id in case of On Behalf signup (CRM-4027 )
+   * @param null $customFieldsFormatted
+   * @param int $numRenewTerms how many membership terms are being added to end date (default is 1)
    *
+   * @internal param array $ipnParams array of name value pairs, to be used (for e.g source) when $form not present
    * @return object $membership          object of membership
    *
    * @static
    * @access public
    *
-   **/
+   */
   static function renewMembership(
     $contactID,
     $membershipTypeID,
@@ -1937,6 +2001,8 @@ SELECT c.contribution_page_id as pageID
    * For now we only allow custom membership fields to be in
    * profile
    *
+   * @param null $mode
+   *
    * @return return the list of membership fields
    * @static
    * @access public
@@ -1981,8 +2047,10 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
    * function to create memberships for related contacts
    * takes into account the maximum related memberships
    *
-   * @param  array      $params       array of key - value pairs
-   * @param  object     $membership   membership object
+   * @param  array $params array of key - value pairs
+   * @param $dao
+   *
+   * @internal param object $membership membership object
    *
    * @return null|relatedMembership     array of memberships if created
    * @static
@@ -2115,7 +2183,8 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
         if (($params['status_id'] == $deceasedStatusId) || ($params['status_id'] == $expiredStatusId)) {
           // related membership is not active so does not count towards maximum
           CRM_Member_BAO_Membership::create($params, $relMemIds);
-        } else {
+        }
+        else {
           // related membership already exists, so this is just an update
           if (isset($params['id'])) {
             if ($available > 0) {
@@ -2162,6 +2231,12 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
     return $membesrshipPayment;
   }
 
+  /**
+   * @param $form
+   * @param null $membershipTypeID
+   *
+   * @return array
+   */
   static function &buildMembershipTypeValues(&$form, $membershipTypeID = NULL) {
     $whereClause = " WHERE domain_id = ". CRM_Core_Config::domainID();
 
@@ -2212,9 +2287,10 @@ FROM   civicrm_membership_type
   /**
    * Function to get membership record count for a Contact
    *
-   * @param int $contactId Contact ID
+   * @param $contactID
    * @param boolean $activeOnly
    *
+   * @internal param int $contactId Contact ID
    * @return int count of membership records
    * @access public
    * @static
@@ -2239,6 +2315,8 @@ FROM   civicrm_membership_type
    *
    * @param int $mid membership id
    *
+   * @param bool $isNotCancelled
+   *
    * @return boolean
    * @access public
    * @static
@@ -2295,10 +2373,10 @@ LEFT JOIN civicrm_membership mem ON ( cr.id = mem.contribution_recur_id )
    * join_date and start_date are within a specified date range.  Dates match
    * the pattern "yyyy-mm-dd".
    *
-   * @param int    $membershipTypeId  membership type id
-   * @param int    $startDate         date on which to start counting
-   * @param int    $endDate           date on which to end counting
-   * @param bool   $isTest            if true, membership is for a test site
+   * @param int $membershipTypeId membership type id
+   * @param int $startDate date on which to start counting
+   * @param int $endDate date on which to end counting
+   * @param bool|int $isTest if true, membership is for a test site
    *
    * @return returns the number of members of type $membershipTypeId
    *         whose join_date is between $startDate and $endDate and
@@ -2343,10 +2421,10 @@ INNER JOIN  civicrm_contact contact ON ( contact.id = membership.contact_id AND
    * whose join_date is before and start_date is within a specified date
    * range.  Dates match the pattern "yyyy-mm-dd".
    *
-   * @param int    $membershipTypeId  membership type id
-   * @param int    $startDate         date on which to start counting
-   * @param int    $endDate           date on which to end counting
-   * @param bool   $isTest            if true, membership is for a test site
+   * @param int $membershipTypeId membership type id
+   * @param int $startDate date on which to start counting
+   * @param int $endDate date on which to end counting
+   * @param bool|int $isTest if true, membership is for a test site
    *
    * @return returns the number of members of type $membershipTypeId
    *         whose join_date is before $startDate and
@@ -2389,6 +2467,9 @@ INNER JOIN  civicrm_contact contact ON ( contact.id = membership.contact_id AND
    *
    * @access public
    *
+   * @param $membershipId
+   * @param $lineItem
+   *
    * @return void
    */
   function processPriceSet($membershipId, $lineItem) {
@@ -2595,11 +2676,16 @@ WHERE      civicrm_membership.is_test = 0";
     return $result;
   }
 
-  /** The function returns the membershiptypes for a particular contact
+  /**
+   * The function returns the membershiptypes for a particular contact
    * who has lifetime membership without end date.
    *
-   * @param  array      $contactMembershipType       array of allMembershipTypes Key - value pairs
+   * @param $contactID
+   * @param bool $isTest
+   * @param bool $onlyLifeTime
    *
+   * @return array
+   * @internal param array $contactMembershipType array of allMembershipTypes Key - value pairs
    */
 
   static function getAllContactMembership($contactID, $isTest = FALSE, $onlyLifeTime = FALSE) {
@@ -2649,12 +2735,13 @@ WHERE      civicrm_membership.is_test = 0";
     $contributionParams['currency'] = $config->defaultCurrency;
     $contributionParams['receipt_date'] = (CRM_Utils_Array::value('receipt_date', $params)) ? $params['receipt_date'] : 'null';
     $contributionParams['source'] = CRM_Utils_Array::value('contribution_source', $params);
-    $contributionParams['soft_credit'] = CRM_Utils_Array::value('soft_credit', $params);
     $contributionParams['non_deductible_amount'] = 'null';
+    $contributionSoftParams = CRM_Utils_Array::value('soft_credit', $params);
     $recordContribution = array(
       'contact_id', 'total_amount', 'receive_date', 'financial_type_id',
       'payment_instrument_id', 'trxn_id', 'invoice_id', 'is_test',
       'contribution_status_id', 'check_number', 'campaign_id', 'is_pay_later',
+      'membership_id',
     );
     foreach ($recordContribution as $f) {
       $contributionParams[$f] = CRM_Utils_Array::value($f, $params);
@@ -2678,6 +2765,14 @@ WHERE      civicrm_membership.is_test = 0";
 
     $contribution = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids);
 
+    //CRM-13981, create new soft-credit record as to record payment from differnt person for this membership
+    if (!empty($contributionSoftParams)) {
+      $contributionSoftParams['contribution_id'] = $contribution->id;
+      $contributionSoftParams['currency'] = $contribution->currency;
+      $contributionSoftParams['amount'] = $contribution->total_amount;
+      CRM_Contribute_BAO_ContributionSoft::add($contributionSoftParams);
+    }
+
     // store contribution id
     $params['contribution_id'] = $contribution->id;
 
@@ -2705,7 +2800,9 @@ WHERE      civicrm_membership.is_test = 0";
    *
    * @param $membershipType array with membership type and organization
    *
-   * @param$ priceSetId priceset id
+   * @param $priceSetId
+   *
+   * @internal param $ $ priceSetId priceset id
    * @access public
    * @static
    */
@@ -2742,6 +2839,9 @@ WHERE      civicrm_membership.is_test = 0";
     $qf->_params['price_' . $fieldID] = CRM_Utils_Array::value('id', $editedResults);
   }
 
+  /**
+   * @todo document me - I seem a bit out of date....
+   */
   static function _getActTypes() {
     $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
     self::$_renewalActType = CRM_Utils_Array::key('Membership Renewal', $activityTypes);