final clean up(CRM-12463)
authoryashodha <yashodha.chaku@webaccess.co.in>
Tue, 7 May 2013 23:37:31 +0000 (05:07 +0530)
committeryashodha <yashodha.chaku@webaccess.co.in>
Tue, 7 May 2013 23:37:31 +0000 (05:07 +0530)
CRM/Contribute/BAO/Contribution.php
CRM/Contribute/BAO/ContributionSoft.php
CRM/Contribute/Form/Contribution.php
CRM/Contribute/Form/SoftCredit.php

index 639b15ab9c6f7b6bf309ee7531906c169d980114..9accdf2d5021d4ab12d15f8abf221f0b0593ccef 100644 (file)
@@ -284,7 +284,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
         }
       }
       if ($retrieveRequired == 1) {
-        $contribution->find(true);
+        $contribution->find(TRUE);
       }
     }
 
@@ -299,34 +299,46 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
     if (!$activity->find()) {
       CRM_Activity_BAO_Activity::addActivity($contribution, 'Offline');
     }
+
     // Handle soft credit and / or link to personal campaign page
-    if (CRM_Utils_Array::value('deleteSoftCredit', $params, TRUE)) {
-        // first delete soft credits if any                                                                                                                                                            
-        //CRM_Contribute_BAO_ContributionSoft::del($contribution->id);
-
-      if ($pcp = CRM_Utils_Array::value('pcp', $params)) {
-        $softParams = array();
-        $softParams['contribution_id'] = $contribution->id;
-        $softParams['pcp_id'] = $pcp['pcp_made_through_id'];
-        $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP',
-          $pcp['pcp_made_through_id'], 'contact_id'
-        );
-        $softParams['currency'] = $contribution->currency;
-        $softParams['amount'] = $contribution->total_amount;
-        $softParams['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcp);
-        $softParams['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcp);
-        $softParams['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcp);
-        CRM_Contribute_BAO_ContributionSoft::add($softParams);
-      }
-      elseif (CRM_Utils_Array::value('soft_credit', $params)) {
-        $softParams = $params['soft_credit'];
-        foreach ($softParams as $softParam) {
-          $softParam['contribution_id'] = $contribution->id;
-          $softParam['currency'] = $contribution->currency;
-          CRM_Contribute_BAO_ContributionSoft::add($softParam);
+    list($type, $softIDs) = CRM_Contribute_BAO_ContributionSoft::getSoftCreditType($contribution->id);
+    if ($pcp = CRM_Utils_Array::value('pcp', $params)) {
+      if ($type == 'soft') {
+        $deleteParams = array('contribution_id' => $contribution->id);
+        CRM_Contribute_BAO_ContributionSoft::del($deleteParams);
+      }
+      $softParams = array();
+      $softParams['contribution_id'] = $contribution->id;
+      $softParams['pcp_id'] = $pcp['pcp_made_through_id'];
+      $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP',
+        $pcp['pcp_made_through_id'], 'contact_id'
+      );
+      $softParams['currency'] = $contribution->currency;
+      $softParams['amount'] = $contribution->total_amount;
+      $softParams['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcp);
+      $softParams['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcp);
+      $softParams['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcp);
+      CRM_Contribute_BAO_ContributionSoft::add($softParams);
+    }
+    elseif (CRM_Utils_Array::value('soft_credit', $params)) {
+      $softParams = $params['soft_credit'];
+      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) {
+        $softParam['contribution_id'] = $contribution->id;
+        $softParam['currency'] = $contribution->currency;
+        $softParam['pcp_display_in_roll'] = NULL;
+        $softParam['pcp_roll_nickname'] = NULL;
+        $softParam['pcp_personal_note'] = NULL;
+        CRM_Contribute_BAO_ContributionSoft::add($softParam);
+      }
     }
+
     $transaction->commit();
 
     // do not add to recent items for import, CRM-4399
@@ -349,7 +361,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
         }
       }
       if($retrieveRequired == 1){
-        $contribution->find(true);
+        $contribution->find(TRUE);
       }
       $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
       $title = CRM_Contact_BAO_Contact::displayName($contribution->contact_id) . ' - (' . CRM_Utils_Money::format($contribution->total_amount, $contribution->currency) . ' ' . ' - ' . $contributionTypes[$contribution->financial_type_id] . ')';
index 1039a0bd12c6fd4085d579753079630a1d30add7..f65d06612550351caf378814b7fa300595e4948e 100644 (file)
@@ -92,10 +92,12 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio
    * @param int $contributionTypeId
    * @static
    */
-  static function del($contributionID) {
+  static function del($params) {
     //delete from contribution soft table
     $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
-    $contributionSoft->contribution_id = $contributionID;
+    foreach($params as $column => $value) {
+      $contributionSoft->$column = $value;
+    }
     $contributionSoft->delete();
   }
 
@@ -206,6 +208,27 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio
     return $softContribution;
   }
 
+  static function getSoftCreditType($contributionID) {
+  $query = "
+  SELECT id, pcp_id
+  FROM  civicrm_contribution_soft
+  WHERE contribution_id = %1
+  ";
+    $params = array(1 => array($contributionID, 'Integer'));
+
+    $dao = CRM_Core_DAO::executeQuery($query, $params);
+    $id = array();
+    while ($dao->fetch()) {
+      if ($dao->pcp_id) {
+        $type = 'pcp';
+      }
+      else {
+        $type = 'soft';
+      }
+      $id[] = $dao->id;
+    }
+    return array($type, $id);
+  }
   /**
    *  Function to retrieve the list of soft contributons for given contact.
    *  @param int $contact_id contact id
index c88de3c73d0d3370ef80e498409eb44ebc39926d..dcddbc572b267849d2107316ac2b1a136fd764b6 100644 (file)
@@ -1094,7 +1094,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
             $softParams[$key]['contact_id'] = $val;
             $softParams[$key]['amount'] = $submittedValues['soft_credit_amount'][$key];
             if (!empty($submittedValues['soft_credit_id'][$key])) {
-              $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key];
+              $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key];
             }
           }
         }
@@ -1141,6 +1141,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       }
       if (!empty($softParams)) {
         $params['soft_credit'] = $softParams;
+        $params['soft_credit_ids'] = $softIDs;
       }
 
       //if priceset is used, no need to cleanup money
index efa6b757306494f77773d227ba0ad12ef91349c9..ea22b5e93f4e53ad21a6649d5a65589348ddee30 100644 (file)
@@ -108,7 +108,7 @@ class CRM_Contribute_Form_SoftCredit {
       }
     }
 
-    if (CRM_Utils_Array::value('pcp_id', $form->_softCreditInfo)) {
+    elseif (CRM_Utils_Array::value('pcp_id', $form->_softCreditInfo)) {
       $pcpInfo = $form->_softCreditInfo;
       $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo);
       $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');