CRM-19126 CRM-19152 Attempt to resurect total amount from previous values rather...
authorSeamus Lee <seamuslee001@gmail.com>
Fri, 5 Aug 2016 22:21:25 +0000 (08:21 +1000)
committereileenmcnaugton <eileen@fuzion.co.nz>
Sun, 7 Aug 2016 22:44:48 +0000 (10:44 +1200)
CRM/Contribute/BAO/Contribution.php
tests/phpunit/api/v3/ContributionTest.php

index 6a4f2221f895cba44642b8eb53ef4efc65f95a4c..6360c6a2f788d904d16151e35386fbe719fbd5b1 100644 (file)
@@ -4112,6 +4112,12 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']})
 
     // Update contribution.
     if (!empty($params['id'])) {
+
+      $id = $params['id'];
+      $values = $ids = array();
+      $contrbutionParams = array('id' => $id);
+      $prevContributionValue = CRM_Contribute_BAO_Contribution::getValues($contrbutionParams, $values, $ids);
+
       // CRM-19126 and CRM-19152, civicrm_line_item.tax_amount incorrectly set when using online payment processor.
       // It looks like this method is meant to be called in multiple contexts: new & update
       // When creating, would expect total_amount to be set?  Prior to 4.7, when creating online membership
@@ -4121,14 +4127,27 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']})
       // Conceptually, if we're "updating", and total_amount is unknown.  We're dealing with an incomplete
       // view, why are we nulling out all line item taxes, or messing with any other tax amounts?
       if (!isset($params['total_amount'])) {
-        return $params;
+        if (!empty($prevContributionValue->tax_amount)) {
+          $params['total_amount'] = $prevContributionValue->total_amount - $prevContributionValue->tax_amount;
+          if (isset($params['fee_amount'])) {
+            $params['net_amount'] = $params['total_amount'] - $params['fee_amount'];
+          }
+          else {
+            $params['net_amount'] = $params['total_amount'] - $prevContributionValue->fee_amount;
+            $params['fee_amount'] = $prevContributionValue->fee_amount;
+          }
+        }
+        else {
+          if (isset($params['fee_amount'])) {
+            $params['net_amount'] = $prevContributionValue->total_amount - $params['fee_amount'];
+            $params['total_amount'] = $prevContributionValue->total_amount;
+          }
+          else {
+            $params['total_amount'] = $prevContributionValue->total_amount;
+            $params['fee_amount'] = $prevContributionValue->fee_amount;
+          }
+        }
       }
-
-      $id = $params['id'];
-      $values = $ids = array();
-      $contrbutionParams = array('id' => $id);
-      $prevContributionValue = CRM_Contribute_BAO_Contribution::getValues($contrbutionParams, $values, $ids);
-
       // To assign pervious finantial type on update of contribution
       if (!isset($params['financial_type_id'])) {
         $params['financial_type_id'] = $prevContributionValue->financial_type_id;
index f03f459106e5aa64e3cd5c165726ff5b35bbf693..6b3d9be309285fa1a156e8ad5e5aacb4be47494b 100644 (file)
@@ -1794,9 +1794,12 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $this->callAPISuccess('contribution', 'completetransaction', array(
        'id' => $contribution['id'],
        'trxn_id' => '777788888',
+       'fee_amount' => '6.00',
     ));
-    $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
+    $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
     $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
+    $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
+    $this->assertEquals('94.00', $contribution2['values'][0]['net_amount']);
   }
 
   /**