From 3123273f06e5747386339013763e8a2a7dbd5a47 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 24 Jan 2017 15:01:36 +1300 Subject: [PATCH] CRM-19830 declare address_id & contribution_recur_id, financial_type_id as exportable to reduce special handling --- CRM/Contact/BAO/Query.php | 4 +- CRM/Contribute/BAO/Contribution.php | 11 +---- CRM/Contribute/BAO/Query.php | 33 +++----------- CRM/Contribute/DAO/Contribution.php | 12 +++++- api/v3/Contribution.php | 52 ++++++++++++++++++++++- tests/phpunit/api/v3/ContributionTest.php | 5 +++ xml/schema/Contribute/Contribution.xml | 5 ++- 7 files changed, 79 insertions(+), 43 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 57470e38e8..7999d1e841 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -654,8 +654,6 @@ class CRM_Contact_BAO_Query { (substr($name, 0, 12) == 'participant_') || (substr($name, 0, 7) == 'pledge_') || (substr($name, 0, 5) == 'case_') || - (substr($name, 0, 13) == 'contribution_' && - (strpos($name, 'source') !== FALSE && strpos($name, 'recur') !== FALSE)) || (substr($name, 0, 8) == 'payment_') ) { continue; @@ -4340,7 +4338,7 @@ civicrm_relationship.is_permission_a_b = 0 $smartGroupCache = TRUE, $count = FALSE, $skipPermissions = TRUE, - $mode = 1, + $mode = CRM_Contact_BAO_Query::MODE_CONTACTS, $apiEntity = NULL ) { diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index e74a29f2f0..09f57773ec 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -825,15 +825,6 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { ), ); - $contributionRecurId = array( - 'contribution_recur_id' => array( - 'title' => ts('Recurring Contributions ID'), - 'name' => 'contribution_recur_id', - 'where' => 'civicrm_contribution.contribution_recur_id', - 'data_type' => CRM_Utils_Type::T_INT, - ), - ); - $extraFields = array( 'contribution_batch' => array( 'title' => ts('Batch Name'), @@ -893,7 +884,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { ); $fields = array_merge($impFields, $typeField, $contributionStatus, $contributionPage, $optionField, $expFieldProduct, - $expFieldsContrib, $contributionNote, $contributionRecurId, $extraFields, $softCreditFields, $financialAccount, $premiums, $campaignTitle, + $expFieldsContrib, $contributionNote, $extraFields, $softCreditFields, $financialAccount, $premiums, $campaignTitle, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution', FALSE, FALSE, FALSE, $checkPermission) ); diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index e09ddd64d1..943748b3c5 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -97,12 +97,6 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { $query->_tables['contribution_batch'] = 1; } - if (!empty($query->_returnProperties['contribution_source'])) { - $query->_select['contribution_source'] = "civicrm_contribution.source as contribution_source"; - $query->_element['contribution_source'] = 1; - $query->_tables['civicrm_contribution'] = 1; - } - // get contribution_status if (!empty($query->_returnProperties['contribution_status_id'])) { $query->_select['contribution_status_id'] = "contribution_status.value as contribution_status_id"; @@ -153,22 +147,6 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { $query->_element['contribution_campaign_title'] = $query->_tables['civicrm_campaign'] = 1; } - // Adding address_id in a way that is more easily extendable since the above is a bit ... wordy. - $supportedBasicReturnValues = array('address_id'); - foreach ($supportedBasicReturnValues as $fieldName) { - if (!empty($query->_returnProperties[$fieldName]) && empty($query->_select[$fieldName])) { - $query->_select[$fieldName] = "civicrm_contribution.{$fieldName} as $fieldName"; - $query->_element[$fieldName] = $query->_tables['civicrm_contribution'] = 1; - } - } - - //CRM-16116: get financial_type_id - if (!empty($query->_returnProperties['financial_type_id'])) { - $query->_select['financial_type_id'] = "civicrm_contribution.financial_type_id as financial_type_id"; - $query->_element['financial_type_id'] = $query->_tables['civicrm_contribution'] = 1; - } - // LCD 716 END - self::addSoftCreditFields($query); } @@ -230,6 +208,10 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { return; } } + + $qillName = $name; + $pseudoExtraParam = array(); + switch ($name) { case 'contribution_date': case 'contribution_date_low': @@ -304,8 +286,7 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { case (strpos($name, '_amount') !== FALSE): case (strpos($name, '_date') !== FALSE && $name != 'contribution_fulfilled_date'): case 'contribution_campaign_id': - $qillName = $name; - $pseudoExtraParam = array(); + // @todo including names using a switch statement & then using an 'if' to filter them out is ... odd! if ((strpos($name, '_amount') !== FALSE) || (strpos($name, '_date') !== FALSE) || in_array($name, array( @@ -320,10 +301,10 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query { ) ) ) { - $name = str_replace('contribution_', '', $name); - if (!in_array($name, array('source', 'id', 'contact_id', 'campaign_id'))) { + if (!isset($fields[$name])) { $qillName = str_replace('contribution_', '', $qillName); } + $name = str_replace('contribution_', '', $name); } if (in_array($name, array('contribution_currency', 'contribution_currency_type'))) { $qillName = $name = 'currency'; diff --git a/CRM/Contribute/DAO/Contribution.php b/CRM/Contribute/DAO/Contribution.php index f35c3c7707..380f69f5a9 100644 --- a/CRM/Contribute/DAO/Contribution.php +++ b/CRM/Contribute/DAO/Contribution.php @@ -291,7 +291,7 @@ class CRM_Contribute_DAO_Contribution extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_INT, 'title' => ts('Financial Type') , 'description' => 'FK to Financial Type for (total_amount - non_deductible_amount).', - 'export' => false, + 'export' => true, 'where' => 'civicrm_contribution.financial_type_id', 'headerPattern' => '', 'dataPattern' => '', @@ -624,6 +624,10 @@ class CRM_Contribute_DAO_Contribution extends CRM_Core_DAO { 'type' => CRM_Utils_Type::T_INT, 'title' => ts('Recurring Contribution ID') , 'description' => 'Conditional foreign key to civicrm_contribution_recur id. Each contribution made in connection with a recurring contribution carries a foreign key to the recurring contribution record. This assumes we can track these processor initiated events.', + 'export' => true, + 'where' => 'civicrm_contribution.contribution_recur_id', + 'headerPattern' => '', + 'dataPattern' => '', 'table_name' => 'civicrm_contribution', 'entity' => 'Contribution', 'bao' => 'CRM_Contribute_BAO_Contribution', @@ -682,11 +686,15 @@ class CRM_Contribute_DAO_Contribution extends CRM_Core_DAO { 'optionEditPath' => 'civicrm/admin/options/contribution_status', ) ) , - 'address_id' => array( + 'contribution_address_id' => array( 'name' => 'address_id', 'type' => CRM_Utils_Type::T_INT, 'title' => ts('Contribution Address') , 'description' => 'Conditional foreign key to civicrm_address.id. We insert an address record for each contribution when we have associated billing name and address data.', + 'export' => true, + 'where' => 'civicrm_contribution.address_id', + 'headerPattern' => '', + 'dataPattern' => '', 'table_name' => 'civicrm_contribution', 'entity' => 'Contribution', 'bao' => 'CRM_Contribute_BAO_Contribution', diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index 976ad8cc65..7d36472bf5 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -240,19 +240,69 @@ function _civicrm_api3_contribution_delete_spec(&$params) { function civicrm_api3_contribution_get($params) { $mode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE; + $additionalOptions = _civicrm_api3_contribution_get_support_nonunique_returns($params); $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties($mode); - $contributions = _civicrm_api3_get_using_query_object('Contribution', $params, array(), NULL, $mode, $returnProperties); + $contributions = _civicrm_api3_get_using_query_object('Contribution', $params, $additionalOptions, NULL, $mode, $returnProperties); foreach ($contributions as $id => $contribution) { $softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id, TRUE); $contributions[$id] = array_merge($contribution, $softContribution); // format soft credit for backward compatibility _civicrm_api3_format_soft_credit($contributions[$id]); + _civicrm_api3_contribution_add_supported_fields($contributions[$id]); + } return civicrm_api3_create_success($contributions, $params, 'Contribution', 'get'); } +/** + * Fix the return values to reflect cases where the schema has been changed. + * + * At the query object level using uniquenames dismbiguates between tables. + * + * However, adding uniquename can change inputs accepted by the api, so we need + * to ensure we are asking for the unique name return fields. + * + * @param array $params + * + * @return array + * @throws \API_Exception + */ +function _civicrm_api3_contribution_get_support_nonunique_returns($params) { + $additionalOptions = array(); + $options = _civicrm_api3_get_options_from_params($params, TRUE); + foreach (array('check_number', 'address_id') as $changedVariable) { + if (isset($options['return']) && !empty($options['return'][$changedVariable])) { + $additionalOptions['return']['contribution_' . $changedVariable] = 1; + } + } + return $additionalOptions; +} + +/** + * Support for supported output variables. + * + * @param $contribution + */ +function _civicrm_api3_contribution_add_supported_fields(&$contribution) { + // These are output fields that are supported in our test contract. + // Arguably we should also do the same with 'campaign_id' & + // 'source' - which are also fields being rendered with unique names. + // That seems more consistent with other api where we output the actual field names. + $outputAliases = array( + 'contribution_check_number' => 'check_number', + 'contribution_address_id' => 'address_id', + 'payment_instrument_id' => 'instrument_id', + ); + foreach ($outputAliases as $returnName => $copyTo) { + if (array_key_exists($returnName, $contribution)) { + $contribution[$copyTo] = $contribution[$returnName]; + } + } + +} + /** * Get number of contacts matching the supplied criteria. * diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 9aa3c634cb..7d7795f73d 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -252,6 +252,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->assertEquals('bouncer', $contribution['contribution_check_number']); $fields = CRM_Contribute_BAO_Contribution::fields(); + // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we + // need to specify them. + $fields['address_id'] = $fields['contribution_address_id']; + $fields['check_number'] = $fields['contribution_check_number']; + $fieldsLockedIn = array( 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id', 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount', diff --git a/xml/schema/Contribute/Contribution.xml b/xml/schema/Contribute/Contribution.xml index af8869f953..4efb239d57 100644 --- a/xml/schema/Contribute/Contribution.xml +++ b/xml/schema/Contribute/Contribution.xml @@ -78,7 +78,6 @@ financial_type_id Financial Type - false int unsigned FK to Financial Type for (total_amount - non_deductible_amount). @@ -86,6 +85,7 @@ id name + true Select @@ -361,6 +361,7 @@ Recurring Contribution ID int unsigned Conditional foreign key to civicrm_contribution_recur id. Each contribution made in connection with a recurring contribution carries a foreign key to the recurring contribution record. This assumes we can track these processor initiated events. + true 1.4 @@ -419,10 +420,12 @@ address_id + contribution_address_id Contribution Address int unsigned Conditional foreign key to civicrm_address.id. We insert an address record for each contribution when we have associated billing name and address data. 2.2 + true address_id -- 2.25.1