From 785f03e2de9e79452bc007a85b36b0a51442076f Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 19 Dec 2018 18:06:50 +1300 Subject: [PATCH] Fix order api to support a pseudoConstant for financial_type_id 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 | 27 ++++++++++++++++++++++++--- api/v3/utils.php | 5 +++++ tests/phpunit/api/v3/OrderTest.php | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/api/v3/Order.php b/api/v3/Order.php index 2347a7a33a..bc29312006 100644 --- a/api/v3/Order.php +++ b/api/v3/Order.php @@ -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', + ], + ]; } /** diff --git a/api/v3/utils.php b/api/v3/utils.php index ed8acaa8d8..e21de97898 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -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; diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php index beb2dc8a27..7f18041fdd 100644 --- a/tests/phpunit/api/v3/OrderTest.php +++ b/tests/phpunit/api/v3/OrderTest.php @@ -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(); -- 2.25.1