Resolve CRM-17182 by filtering on civicrm_line_item.price_field_value_id instead...
authorSeamus Lee <seamuslee001@gmail.com>
Sun, 18 Aug 2019 02:11:09 +0000 (12:11 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 22 Aug 2019 06:49:48 +0000 (16:49 +1000)
Pass id in the params array rather than in the ids array

CRM/Event/BAO/Query.php
CRM/Price/BAO/PriceFieldValue.php
CRM/Price/Form/Option.php
tests/phpunit/api/v3/ParticipantTest.php

index beb5f96e33c590d96514c465dcb7b018b709dfc6..03d83044cb10a789dc7d595ae84291588fb7d344 100644 (file)
@@ -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;
   }
index c01a61b7f1191c66686eeccb32e581c9afe4f2af..b8c304499eb6081f2385af081453b2bbd869516f 100644 (file)
@@ -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);
-      }
-    }
-  }
-
 }
index 1ad15e8ddcfd4afdef248182fcbf053435c9b3d7..3e52ab48c9144c2a1b64389e1f369991e2610041 100644 (file)
@@ -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);
 
index b3d76fbf38b34ab4f6f0816b7d73b56383b70867..59be0b1dca463abf052b4ca726b2dbc5e124df80 100644 (file)
@@ -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']]);