Merge pull request #6220 from monishdeb/CRM-16828
authorcolemanw <coleman@civicrm.org>
Wed, 15 Jul 2015 14:01:57 +0000 (10:01 -0400)
committercolemanw <coleman@civicrm.org>
Wed, 15 Jul 2015 14:01:57 +0000 (10:01 -0400)
Crm 16828: Backport CRM-16575 changes to 4.6

CRM/Contribute/BAO/Contribution.php

index d46fb266a89240dbf42fdc0ab5c5a1da86e379d5..9062f5c7358a77de5356fa448f4dfca6c7dba2e0 100644 (file)
@@ -374,6 +374,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
 
     if ($pcp = CRM_Utils_Array::value('pcp', $params)) {
       $softParams = array();
+      $softParams['id'] = $pcpId ? $pcpId : NULL;
       $softParams['contribution_id'] = $contribution->id;
       $softParams['pcp_id'] = $pcp['pcp_made_through_id'];
       $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP',
@@ -398,17 +399,12 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
     }
     if (isset($params['soft_credit'])) {
       $softParams = $params['soft_credit'];
-
-      if (!empty($softIDs)) {
-        foreach ($softIDs as $softID) {
-          if (!in_array($softID, $params['soft_credit_ids'])) {
-            $deleteParams = array('id' => $softID);
-            CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
-          }
-        }
-      }
-
       foreach ($softParams as $softParam) {
+        if (!empty($softIDs)) {
+          $key = key($softIDs);
+          $softParam['id'] = $softIDs[$key];
+          unset($softIDs[$key]);
+        }
         $softParam['contribution_id'] = $contribution->id;
         $softParam['currency'] = $contribution->currency;
         //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default
@@ -417,6 +413,15 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
         }
         CRM_Contribute_BAO_ContributionSoft::add($softParam);
       }
+
+      if (!empty($softIDs)) {
+        foreach ($softIDs as $softID) {
+          if (!in_array($softID, $params['soft_credit_ids'])) {
+            $deleteParams = array('id' => $softID);
+            CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
+          }
+        }
+      }
     }
 
     $transaction->commit();