From bdc0f3baa90c7e9ff6b10afb924622d8980a451a Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 25 Nov 2023 14:14:35 +1300 Subject: [PATCH] Move getAmountLevel() to Order class --- CRM/Financial/BAO/Order.php | 28 +++++++++++ CRM/Price/BAO/PriceSet.php | 49 ++----------------- .../CRM/Event/Form/ParticipantTest.php | 1 + 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/CRM/Financial/BAO/Order.php b/CRM/Financial/BAO/Order.php index 5be8addb19..fc418700a8 100644 --- a/CRM/Financial/BAO/Order.php +++ b/CRM/Financial/BAO/Order.php @@ -986,6 +986,34 @@ class CRM_Financial_BAO_Order { return $amount; } + /** + * Get Amount Level text. + * + * @return string + * @throws \CRM_Core_Exception + */ + public function getAmountLevel() : string { + $amount_level = []; + $totalParticipant = 0; + foreach ($this->getLineItems() as $lineItem) { + if ($lineItem['label'] !== ts('Contribution Amount')) { + $amount_level[] = $lineItem['label'] . ' - ' . (float) $lineItem['qty']; + } + $totalParticipant += (float) ($lineItem['participant_count'] ?? 0); + } + $displayParticipantCount = ''; + if ($totalParticipant > 0) { + $displayParticipantCount = ' Participant Count -' . $totalParticipant; + } + if (!empty($amount_level)) { + $amountString = CRM_Utils_Array::implodePadded($amount_level); + if (!empty($displayParticipantCount)) { + $amountString = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amount_level) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR; + } + } + return $amountString ?? ''; + } + /** * Get the total amount relating to memberships for the order. * diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index 566ef3287a..9176c152a0 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -651,7 +651,6 @@ WHERE id = %1"; */ public static function processAmount($fields, &$params, &$lineItem, $priceSetID = NULL) { // using price set - $totalPrice = $totalTax = 0; foreach ($fields as $id => $field) { if (empty($params["price_{$id}"]) || (empty($params["price_{$id}"]) && $params["price_{$id}"] == NULL) @@ -662,49 +661,11 @@ WHERE id = %1"; [$params, $lineItem] = self::getLine($params, $lineItem, $priceSetID, $field, $id); } - - $amount_level = []; - $totalParticipant = 0; - if (is_array($lineItem)) { - foreach ($lineItem as $values) { - $totalPrice += $values['line_total'] + $values['tax_amount']; - $totalTax += $values['tax_amount']; - $totalParticipant += $values['participant_count']; - // This is a bit nasty. The logic of 'quick config' was because price set configuration was - // (and still is) too difficult to replace the 'quick config' price set configuration on the contribution - // page. - // - // However, because the quick config concept existed all sorts of logic was hung off it - // and function behaviour sometimes depends on whether 'price set' is set - although actually it - // is always set at the functional level. In this case we are dealing with the default 'quick config' - // price set having a label of 'Contribution Amount' which could wind up creating a 'funny looking' label. - // The correct answer is probably for it to have an empty label in the DB - the label is never shown so it is a - // place holder. - // - // But, in the interests of being careful when capacity is low - avoiding the known default value - // will get us by. - // Crucially a test has been added so a better solution can be implemented later with some comfort. - // @todo - stop setting amount level in this function & call the getAmountLevel function to retrieve it. - if ($values['label'] !== ts('Contribution Amount')) { - $amount_level[] = $values['label'] . ' - ' . (float) $values['qty']; - } - } - } - - $displayParticipantCount = ''; - if ($totalParticipant > 0) { - $displayParticipantCount = ' Participant Count -' . $totalParticipant; - } - // @todo - stop setting amount level in this function & call the getAmountLevel function to retrieve it. - if (!empty($amount_level)) { - $params['amount_level'] = CRM_Utils_Array::implodePadded($amount_level); - if (!empty($displayParticipantCount)) { - $params['amount_level'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amount_level) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR; - } - } - - $params['amount'] = $totalPrice; - $params['tax_amount'] = $totalTax; + $order = new CRM_Financial_BAO_Order(); + $order->setLineItems($lineItem); + $params['amount_level'] = $order->getAmountLevel(); + $params['amount'] = $order->getTotalAmount(); + $params['tax_amount'] = $order->getTotalTaxAmount(); } /** diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php index 0ed334fd0c..0ee3867184 100644 --- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -229,6 +229,7 @@ United States
', $this->assertEquals('Offline Registration for Event: Annual CiviCRM meet by: ', $participant['participant_source']); $contribution = $this->callAPISuccessGetSingle('Contribution', []); $this->assertEquals(20, $contribution['total_amount']); + $this->assertEquals(['Family Deal - 1'], $contribution['amount_level']); $this->assertEquals('Debit Card', $contribution['payment_instrument']); $this->assertNotEmpty($contribution['receipt_date']); // Just check it's not something weird like 1970 without getting into flakey-precise. -- 2.25.1