Fix order api to support a pseudoConstant for financial_type_id
authoreileen <emcnaughton@wikimedia.org>
Wed, 19 Dec 2018 05:06:50 +0000 (18:06 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 16 Jan 2019 22:34:41 +0000 (11:34 +1300)
Adds functionality & test to support 'Event Fee' instead of '4' for Order.create financial_type_id.

This improves consistency with Contribution.create api and others

api/v3/Order.php
api/v3/utils.php
tests/phpunit/api/v3/OrderTest.php

index 2347a7a33a3f8ee4ee084e2066846ed2389e0808..bc29312006939eed479dfb8cddf5bfeff6ed8964 100644 (file)
@@ -61,6 +61,19 @@ function civicrm_api3_order_get($params) {
   return civicrm_api3_create_success($contributions, $params, 'Order', 'get');
 }
 
+/**
+ * Adjust Metadata for Get action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
+ *
+ * @param array $params
+ *   Array of parameters determined by getfields.
+ */
+function _civicrm_api3_order_get_spec(&$params) {
+  $params['id']['api.aliases'] = ['order_id'];
+  $params['id']['title'] = ts('Contribution / Order ID');
+}
+
 /**
  * Add or update a Order.
  *
@@ -72,7 +85,7 @@ function civicrm_api3_order_get($params) {
  *   Api result array
  */
 function civicrm_api3_order_create(&$params) {
-  $contribution = array();
+
   $entity = NULL;
   $entityIds = array();
   if (CRM_Utils_Array::value('line_items', $params) && is_array($params['line_items'])) {
@@ -202,12 +215,20 @@ function _civicrm_api3_order_create_spec(&$params) {
     'title' => 'Total Amount',
     'api.required' => TRUE,
   );
-  $params['financial_type_id'] = array(
+  $params['financial_type_id'] = [
     'name' => 'financial_type_id',
     'title' => 'Financial Type',
     'type' => CRM_Utils_Type::T_INT,
     'api.required' => TRUE,
-  );
+    'table_name' => 'civicrm_contribution',
+    'entity' => 'Contribution',
+    'bao' => 'CRM_Contribute_BAO_Contribution',
+    'pseudoconstant' => [
+      'table' => 'civicrm_financial_type',
+      'keyColumn' => 'id',
+      'labelColumn' => 'name',
+    ],
+  ];
 }
 
 /**
index ed8acaa8d8e64ad17ca5c77fc397f7e6fb34b8a1..e21de97898020be22690b90a58ebdd98e38e0cc7 100644 (file)
@@ -376,6 +376,11 @@ function _civicrm_api3_get_BAO($name) {
   if ($name == 'PrintLabel') {
     return 'CRM_Badge_BAO_Layout';
   }
+  if ($name === 'Order') {
+    // Order basically maps to contribution at the top level but
+    // has enhanced access to other entities.
+    $name = 'Contribution';
+  }
   $dao = _civicrm_api3_get_DAO($name);
   if (!$dao) {
     return NULL;
index beb2dc8a27249b8454d7c059c25b8b4a70b0541c..7f18041fdddcaf153e2e4508e27137c3d17b3221 100644 (file)
@@ -211,7 +211,7 @@ class api_v3_OrderTest extends CiviUnitTestCase {
       'contact_id' => $this->_individualId,
       'receive_date' => '2010-01-20',
       'total_amount' => 200,
-      'financial_type_id' => $this->_financialTypeId,
+      'financial_type_id' => 'Event Fee',
       'contribution_status_id' => 1,
     );
     $priceFields = $this->createPriceSet();