From 72b84deda54a9a04a0d62483cd87ed08faec200c Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 18 Aug 2019 12:11:09 +1000 Subject: [PATCH] Resolve CRM-17182 by filtering on civicrm_line_item.price_field_value_id instead of th fee_amount column Pass id in the params array rather than in the ids array --- CRM/Event/BAO/Query.php | 19 ++++++----- CRM/Price/BAO/PriceFieldValue.php | 42 ------------------------ CRM/Price/Form/Option.php | 2 +- tests/phpunit/api/v3/ParticipantTest.php | 24 ++++++++++++++ 4 files changed, 35 insertions(+), 52 deletions(-) diff --git a/CRM/Event/BAO/Query.php b/CRM/Event/BAO/Query.php index beb5f96e33..03d83044cb 100644 --- a/CRM/Event/BAO/Query.php +++ b/CRM/Event/BAO/Query.php @@ -339,16 +339,13 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query { return; case 'participant_fee_id': - $val_regexp = []; - foreach ($value as $k => &$val) { - $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label'); - $val_regexp[$k] = CRM_Core_DAO::escapeString(preg_quote(trim($val))); - $val = CRM_Core_DAO::escapeString(trim($val)); + $labels = []; + foreach ($value as $val) { + $labels[] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label'); } - $feeLabel = implode('|', $val_regexp); - $query->_where[$grouping][] = "civicrm_participant.fee_level REGEXP '{$feeLabel}'"; - $query->_qill[$grouping][] = ts("Fee level") . " IN " . implode(', ', $value); - $query->_tables['civicrm_participant'] = $query->_whereTables['civicrm_participant'] = 1; + $query->_where[$grouping][] = "civicrm_line_item.price_field_value_id IN (" . implode(', ', $value) . ")"; + $query->_qill[$grouping][] = ts("Fee level") . " IN " . implode(', ', $labels); + $query->_tables['civicrm_participant'] = $query->_tables['civicrm_line_item'] = $query->_whereTables['civicrm_line_item'] = 1; return; case 'participant_fee_amount_high': @@ -516,6 +513,10 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query { $from = " $side JOIN civicrm_discount discount ON ( civicrm_participant.discount_id = discount.id )"; $from .= " $side JOIN civicrm_option_group discount_name ON ( discount_name.id = discount.price_set_id ) "; break; + + case 'civicrm_line_item': + $from .= " $side JOIN civicrm_line_item ON civicrm_line_item.entity_id = civicrm_participant.id AND civicrm_line_item.entity_table = 'civicrm_participant'"; + break; } return $from; } diff --git a/CRM/Price/BAO/PriceFieldValue.php b/CRM/Price/BAO/PriceFieldValue.php index c01a61b7f1..b8c304499e 100644 --- a/CRM/Price/BAO/PriceFieldValue.php +++ b/CRM/Price/BAO/PriceFieldValue.php @@ -54,13 +54,6 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { $fieldValueBAO = new CRM_Price_BAO_PriceFieldValue(); $fieldValueBAO->copyValues($params); - if ($id = CRM_Utils_Array::value('id', $ids)) { - $fieldValueBAO->id = $id; - $prevLabel = self::getOptionLabel($id); - if (!empty($params['label']) && $prevLabel != $params['label']) { - self::updateAmountAndFeeLevel($id, $prevLabel, $params['label']); - } - } // CRM-16189 $priceFieldID = CRM_Utils_Array::value('price_field_id', $params); if (!$priceFieldID) { @@ -316,39 +309,4 @@ WHERE cpse.id IS NOT NULL {$where}"; CRM_Core_DAO::executeQuery($sql, $params); } - /** - * Update price option label in line_item, civicrm_contribution and civicrm_participant. - * - * @param int $id - id of the price_field_value - * @param string $prevLabel - * @param string $newLabel - * - */ - public static function updateAmountAndFeeLevel($id, $prevLabel, $newLabel) { - // update price field label in line item. - $lineItem = new CRM_Price_DAO_LineItem(); - $lineItem->price_field_value_id = $id; - $lineItem->label = $prevLabel; - $lineItem->find(); - while ($lineItem->fetch()) { - $lineItemParams['id'] = $lineItem->id; - $lineItemParams['label'] = $newLabel; - CRM_Price_BAO_LineItem::create($lineItemParams); - - // update amount and fee level in civicrm_contribution and civicrm_participant - $params = [ - 1 => [CRM_Core_DAO::VALUE_SEPARATOR . $prevLabel . ' -', 'String'], - 2 => [CRM_Core_DAO::VALUE_SEPARATOR . $newLabel . ' -', 'String'], - ]; - // Update contribution - if (!empty($lineItem->contribution_id)) { - CRM_Core_DAO::executeQuery("UPDATE `civicrm_contribution` SET `amount_level` = REPLACE(amount_level, %1, %2) WHERE id = {$lineItem->contribution_id}", $params); - } - // Update participant - if ($lineItem->entity_table == 'civicrm_participant') { - CRM_Core_DAO::executeQuery("UPDATE `civicrm_participant` SET `fee_level` = REPLACE(fee_level, %1, %2) WHERE id = {$lineItem->entity_id}", $params); - } - } - } - } diff --git a/CRM/Price/Form/Option.php b/CRM/Price/Form/Option.php index 1ad15e8ddc..3e52ab48c9 100644 --- a/CRM/Price/Form/Option.php +++ b/CRM/Price/Form/Option.php @@ -357,7 +357,7 @@ class CRM_Price_Form_Option extends CRM_Core_Form { $params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE); $ids = []; if ($this->_oid) { - $ids['id'] = $this->_oid; + $params['id'] = $this->_oid; } $optionValue = CRM_Price_BAO_PriceFieldValue::create($params, $ids); diff --git a/tests/phpunit/api/v3/ParticipantTest.php b/tests/phpunit/api/v3/ParticipantTest.php index b3d76fbf38..59be0b1dca 100644 --- a/tests/phpunit/api/v3/ParticipantTest.php +++ b/tests/phpunit/api/v3/ParticipantTest.php @@ -576,6 +576,30 @@ class api_v3_ParticipantTest extends CiviUnitTestCase { $this->callAPISuccess('PriceFieldValue', 'create', ['id' => $pfv2['id'], 'label' => 'Price FIeld Value 2 Label']); $participantGet = $this->callAPISuccess('Participant', 'get', ['id' => $participant['id']]); $this->assertEquals(["pricefieldvalue1 - 2", "pricefieldvalue2 - 2"], $participantGet['values'][$participant['id']]['participant_fee_level']); + $conatactID4 = $this->individualCreate(); + $myParams['contact_id'] = $conatactID4; + $myParams['participant_fee_level'] = CRM_Core_DAO::VALUE_SEPARATOR . "pricefieldvalue1 - 2" . CRM_Core_DAO::VALUE_SEPARATOR . "Price FIeld Value 2 Label - 2" . CRM_Core_DAO::VALUE_SEPARATOR; + $AdditionalParticipant = $this->callAPISuccess('Participant', 'create', $myParams); + $this->assertEquals(["pricefieldvalue1 - 2", "Price FIeld Value 2 Label - 2"], $AdditionalParticipant['values'][$AdditionalParticipant['id']]['fee_level']); + $lineItems = $this->callAPISuccess('LineItem', 'get', [ + 'entity_id' => $AdditionalParticipant['id'], + 'entity_table' => 'civicrm_participant', + ]); + $this->assertEquals(2, $lineItems['count']); + + // Check quantity, label and unit price of lines. + // TODO: These assertions depend on the order of the line items, which is + // technically incorrect. + + $lineItem = array_pop($lineItems['values']); + $this->assertEquals(2, $lineItem['qty']); + $this->assertEquals(5, $lineItem['unit_price']); + $this->assertEquals('Price FIeld Value 2 Label', $lineItem['label']); + + $lineItem = array_pop($lineItems['values']); + $this->assertEquals(2, $lineItem['qty']); + $this->assertEquals(20, $lineItem['unit_price']); + $this->assertEquals('pricefieldvalue1', $lineItem['label']); // Cleanup $this->callAPISuccess('participant', 'delete', ['id' => $participant['id']]); -- 2.25.1