CRM-19585, added function to calculate net amount if contribution has tax amount
authorPradeep Nayak <pradpnayak@gmail.com>
Fri, 23 Dec 2016 14:10:38 +0000 (19:40 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Thu, 29 Dec 2016 11:25:41 +0000 (16:55 +0530)
added test for the same

----------------------------------------
* CRM-19585: Sales tax issue
  https://issues.civicrm.org/jira/browse/CRM-19585

CRM-19585, removed space

----------------------------------------
* CRM-19585: Sales tax issue
  https://issues.civicrm.org/jira/browse/CRM-19585

CRM/Contribute/BAO/Contribution.php
CRM/Contribute/Form/Contribution.php
tests/phpunit/CRM/Contribute/BAO/ContributionTest.php

index 7e20ab4e5559a07d1dd24887062fff4cfb4bfbf4..301e98a39feaed557c4449e3a06c66c959d38fba 100644 (file)
@@ -5360,4 +5360,20 @@ LEFT JOIN  civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co
     return $amount;
   }
 
+  /**
+   * Calculate net amount.
+   *
+   * @param array $netAmount
+   *
+   * @param float $taxAmount
+   *
+   * @return array
+   */
+  public static function calculateNetAmount($netAmount, $taxAmount) {
+    if ($taxAmount) {
+      $netAmount -= $taxAmount;
+    }
+    return CRM_Utils_Money::format($netAmount, NULL, '%a');
+  }
+
 }
index 757073e6fec9d216644df7cc709859ab30ebf819..8bf943c5b89d549d53d37d6613d6a924d2532e42 100644 (file)
@@ -378,7 +378,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     }
 
     if (isset($defaults['net_amount'])) {
-      $defaults['net_amount'] = CRM_Utils_Money::format($defaults['net_amount'], NULL, '%a');
+      $defaults['net_amount'] = CRM_Contribute_BAO_Contribution::calculateNetAmount($defaults['net_amount'], $defaults['tax_amount']);
     }
 
     if ($this->_contributionType) {
@@ -970,16 +970,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
 
     if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) || !empty($fields['fee_amount']))) {
       $sum = CRM_Utils_Rule::cleanMoney($fields['net_amount']) + CRM_Utils_Rule::cleanMoney($fields['fee_amount']);
-      // For taxable contribution we need to deduct taxable amount from
-      // (net amount + fee amount) before comparing it with total amount
-      if (!empty($self->_values['tax_amount'])) {
-        $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($self->_id);
-        if (!(CRM_Utils_Array::value('membership', $componentDetails) ||
-            CRM_Utils_Array::value('participant', $componentDetails))
-        ) {
-          $sum = CRM_Utils_Money::format($sum - $self->_values['tax_amount'], NULL, '%a');
-        }
-      }
       if (CRM_Utils_Rule::cleanMoney($fields['total_amount']) != $sum) {
         $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount');
       }
index 0e9395417e329b42bb5062fbc47362513376afbd..6144348e8400db4ec612361d3bfbb6a67c59fcc7 100644 (file)
@@ -938,6 +938,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
   }
 
   /**
+<<<<<<< HEAD
    * Test calculateFinancialItemAmount().
    */
   public function testcalculateFinancialItemAmount() {
@@ -1005,4 +1006,37 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
     }
   }
 
+  /**
+   * Test calculateNetAmount.
+   */
+  public function testcalculateNetAmount() {
+    $testParams = array(
+      array(
+        'net_amount' => 100,
+        'tax_amount' => 10,
+        'expectedNetAmount' => 90,
+      ),
+      array(
+        'net_amount' => 200,
+        'tax_amount' => 0,
+        'expectedNetAmount' => 200,
+      ),
+      array(
+        'net_amount' => 300,
+        'tax_amount' => NULL,
+        'expectedNetAmount' => 300,
+      ),
+      array(
+        'net_amount' => -100,
+        'tax_amount' => 20,
+        'expectedNetAmount' => -120,
+      ),
+    );
+
+    foreach ($testParams as $params) {
+      $netAmount = CRM_Contribute_BAO_Contribution::calculateNetAmount($params['net_amount'], $params['tax_amount']);
+      $this->assertEquals($netAmount, $params['expectedNetAmount'], 'Invalid Net amount.');
+    }
+  }
+
 }