From 0623f992262f74542a207c76b7e5490156b090ef Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 31 Jan 2020 20:55:16 +1300 Subject: [PATCH] [REF] Interim code cleanup - make the usage of addPayments clearer This brings the condition back to the calling function which clarifies what is going on with it --- CRM/Contribute/BAO/Contribution.php | 37 ++++++++----------- CRM/Core/BAO/CustomField.php | 2 + CRM/Event/Form/Participant.php | 6 ++- .../CRM/Contribute/BAO/ContributionTest.php | 2 +- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 31f5418ff917..de829faeeb74 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4834,38 +4834,31 @@ protected static function getRecurringContributionDescription($contribution, $ev * * Replace with Order.create->Payment.create flow. * - * @param array $contributions + * @param array $contribution * * @throws \CiviCRM_API3_Exception */ - public static function addPayments($contributions) { + public static function addPayments($contribution) { // get financial trxn which is a payment $ftSql = "SELECT ft.id, ft.total_amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution' WHERE eft.entity_id = %1 AND ft.is_payment = 1 ORDER BY ft.id DESC LIMIT 1"; - $contributionStatus = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id', [ - 'labelColumn' => 'name', + + $ftDao = CRM_Core_DAO::executeQuery($ftSql, [ + 1 => [ + $contribution->id, + 'Integer', + ], ]); - foreach ($contributions as $contribution) { - if ($contributionStatus[$contribution->contribution_status_id] !== 'Partially paid') { - continue; - } - $ftDao = CRM_Core_DAO::executeQuery($ftSql, [ - 1 => [ - $contribution->id, - 'Integer', - ], - ]); - $ftDao->fetch(); + $ftDao->fetch(); - // store financial item Proportionaly. - $trxnParams = [ - 'total_amount' => $ftDao->total_amount, - 'contribution_id' => $contribution->id, - ]; - self::assignProportionalLineItems($trxnParams, $ftDao->id, $contribution->total_amount); - } + // store financial item Proportionaly. + $trxnParams = [ + 'total_amount' => $ftDao->total_amount, + 'contribution_id' => $contribution->id, + ]; + self::assignProportionalLineItems($trxnParams, $ftDao->id, $contribution->total_amount); } /** diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 25bd3c1e9ab5..1f21c3e80edc 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -161,6 +161,8 @@ public static function create($params) { * Array of arrays as would be passed into create * @param array $defaults * Default parameters to be be merged into each of the params. + * + * @throws \CiviCRM_API3_Exception */ public static function bulkSave($bulkParams, $defaults = []) { $addedColumns = $sql = $tables = $customFields = []; diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index d12d0a9d6e79..f44a8320aa9e 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -1410,10 +1410,14 @@ public function submit($params) { $lineItem[$this->_priceSetId][$lineKey] = $line; } CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant'); - CRM_Contribute_BAO_Contribution::addPayments($contributions); } } } + foreach ($contributions as $contribution) { + if ('Partially paid' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution->contribution_status_id)) { + CRM_Contribute_BAO_Contribution::addPayments($contribution); + } + } } $updateStatusMsg = NULL; diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index 5e2ca3972c93..5c6d66f1a3e7 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -683,7 +683,7 @@ public function testAddPayments() { // want to deprecate & remove & the test relies on bad data asa starting point. // End goal is the Order.create->Payment.create flow. CRM_Core_DAO::executeQuery('DELETE FROM civicrm_entity_financial_trxn WHERE entity_table = "civicrm_financial_item"'); - CRM_Contribute_BAO_Contribution::addPayments([$contribution]); + CRM_Contribute_BAO_Contribution::addPayments($contribution); $this->checkItemValues($contribution); }