--CRM-16188, added function to check the total amount and sum of line total
authorPradeep Nayak <pradpnayak@gmail.com>
Tue, 26 Jan 2016 20:02:14 +0000 (01:32 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Tue, 26 Jan 2016 20:02:14 +0000 (01:32 +0530)
CRM/Contribute/BAO/Contribution.php
tests/phpunit/CRM/Contribute/BAO/ContributionTest.php

index abe514e7603b8d316c11dd34d722861eef208fc3..0f30b667d3658f040e4215f9918d938ee78c72ab 100644 (file)
@@ -4818,4 +4818,32 @@ LIMIT 1;";
     }
   }
 
+  /**
+   * Function use to check check line items
+   *
+   * @param array $params
+   *  array of order params.
+   *
+   * @return string
+   */
+  public static function checkLineItems(&$params) {
+    $totalAmount = CRM_Utils_Array::value('total_amount', $params);
+    $lineItemAmount = 0;
+    foreach ($params['line_items'] as &$lineItems) {
+      foreach ($lineItems['line_item'] as &$item) {
+        if (empty($item['financial_type_id'])) {
+          $item['financial_type_id'] = $params['financial_type_id'];
+        }
+        $lineItemAmount += $item['line_total'];
+      }
+    }
+    if (!isset($totalAmount)) {
+      $params['total_amount'] = $lineItemAmount;
+    }
+    elseif ($totalAmount != $lineItemAmount) {
+      return "Line item total doesn't match with total amount.";
+    }
+    return NULL;
+  }
+
 }
index 3399bfea8e7a1dad0a59fdd142dcf1d2a95a9dc4..726dc495619fdebfd5246715e2ab7e7cd7653f61 100644 (file)
@@ -783,4 +783,48 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
     return array($lineItems, $contributions);
   }
 
+  /**
+   * checkLineItems() check if total amount matches the sum of line total 
+   */
+  public function testcheckLineItems() {
+    $params = array(
+      'contact_id' => 202,
+      'receive_date' => '2010-01-20',
+      'total_amount' => 100,
+      'financial_type_id' => 3,
+      'line_items' => array(
+        array(
+          'line_item' => array(
+            array(
+              'entity_table' => 'civicrm_contribution',
+              'price_field_id' => 8,
+              'price_field_value_id' => 16,
+              'label' => 'test 1',
+              'qty' => 1,
+              'unit_price' => 100,
+              'line_total' => 100,
+            ),
+            array(
+              'entity_table' => 'civicrm_contribution',
+              'price_field_id' => 8,
+              'price_field_value_id' => 17,
+              'label' => 'Test 2',
+              'qty' => 1,
+              'unit_price' => 200,
+              'line_total' => 200,
+              'financial_type_id' => 1,
+            ),
+          ),
+          'params'=> array(),
+        ),
+      )
+    );
+    $error = CRM_Contribute_BAO_Contribution::checkLineItems($params);
+    $this->assertEquals("Line item total doesn't match with total amount.", $error);
+    $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
+    $params['total_amount'] = 300;
+    $error = CRM_Contribute_BAO_Contribution::checkLineItems($params);
+    $this->assertEquals(NULL, $error);
+  }
+
 }