CRM-14197 make membership_payment create action take responsibility for setting entit...
authorEileen McNaughton <eileen@fuzion.co.nz>
Tue, 20 May 2014 07:48:23 +0000 (19:48 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Wed, 28 May 2014 07:44:26 +0000 (19:44 +1200)
CRM/Member/BAO/Membership.php
CRM/Member/BAO/MembershipPayment.php

index e08b8d6e20fbd1b5a700b4be3eae70a1806d5c85..8d9d430291584d9671581ad16f5d35bd31abe910 100644 (file)
@@ -2168,16 +2168,7 @@ INNER JOIN  civicrm_contact contact ON ( contact.id = membership.contact_id AND
    * @param $membershipContribution
    */
   public static function linkMembershipPayment($membership, $membershipContribution) {
-    $dao = new CRM_Member_DAO_MembershipPayment();
-    $dao->membership_id = $membership->id;
-    $dao->contribution_id = $membershipContribution->id;
-    //Fixed for avoiding duplicate entry error when user goes
-    //back and forward during payment mode is notify
-    if (!$dao->find(TRUE)) {
-      CRM_Utils_Hook::pre('create', 'MembershipPayment', NULL, $dao);
-      $dao->save();
-      CRM_Utils_Hook::post('create', 'MembershipPayment', $dao->id, $dao);
-    }
+    CRM_Member_BAO_MembershipPayment::create(array('membership_id' => $membership->id, 'contribution_id' => $membershipContribution->id));
   }
 
   /**
index 14c0541d90b551db68f1df384491af5032f1aca1..f83dbc1a59392f6d0d82493730f40b5388703e72 100644 (file)
@@ -51,14 +51,30 @@ class CRM_Member_BAO_MembershipPayment extends CRM_Member_DAO_MembershipPayment
    *
    * @return object
    */
-  static function create(&$params) {
+  static function create($params) {
     $hook = empty($params['id']) ? 'create' : 'edit';
     CRM_Utils_Hook::pre($hook, 'MembershipPayment', CRM_Utils_Array::value('id', $params), $params);
     $dao = new CRM_Member_DAO_MembershipPayment();
     $dao->copyValues($params);
     $dao->id = CRM_Utils_Array::value('id', $params);
-    $dao->save();
+    //Fixed for avoiding duplicate entry error when user goes
+    //back and forward during payment mode is notify
+    if (!$dao->find(TRUE)) {
+      $dao->save();
+    }
     CRM_Utils_Hook::post($hook, 'MembershipPayment', $dao->id, $dao);
+    // CRM-14197 we are in the process on phasing out membershipPayment in favour of storing both contribution_id & entity_id (membership_id) on the line items
+    // table. However, at this stage we have both - there is still quite a bit of refactoring to do to set the line_iten entity_id right the first time
+    // however, we can assume at this stage that any contribution id will have only one line item with that membership type in the line item table
+    // OR the caller will have taken responsibility for updating the line items themselves so we will update using SQL here
+    $membershipTypeID = civicrm_api3('membership', 'getvalue', array('id' => $dao->membership_id, 'return' => 'membership_type_id'));
+    $sql = "UPDATE civicrm_line_item li
+      LEFT JOIN civicrm_price_field_value pv ON pv.id = li.price_field_value_id
+      SET entity_table = 'civicrm_membership', entity_id = %1
+      WHERE pv.membership_type_id = %2
+      AND entity_table = 'civicrm_contribution' AND entity_id = contribution_id
+      AND contribution_id = %3";
+    CRM_Core_DAO::executeQuery($sql, array(1 => array($dao->membership_id, 'Integer'), 2 => array($membershipTypeID, 'Integer'), 3 => array($dao->contribution_id, 'Integer')));
     return $dao;
   }