From 4720af1a09517b8178996cb3a554718e7ec25559 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 4 Apr 2020 14:24:35 +1300 Subject: [PATCH] [REF] Extract transferParticipantRegistration function This is the extraction portion of https://github.com/civicrm/civicrm-core/pull/16956/ On testing that I hit an error but getting the extraction merged first should make it easierr to review future iterations as it will reduce noise in the code. Note I made the function non-static since it is still on the form & passed in event ID since we have it but it's otherwise pretty much the same as the extraction part of that PR --- CRM/Event/Form/SelfSvcTransfer.php | 99 +++++++++++-------- .../CRM/Event/Form/ParticipantTest.php | 37 +++++++ 2 files changed, 94 insertions(+), 42 deletions(-) diff --git a/CRM/Event/Form/SelfSvcTransfer.php b/CRM/Event/Form/SelfSvcTransfer.php index d44012ff41..ca7aa0fd71 100644 --- a/CRM/Event/Form/SelfSvcTransfer.php +++ b/CRM/Event/Form/SelfSvcTransfer.php @@ -322,51 +322,11 @@ class CRM_Event_Form_SelfSvcTransfer extends CRM_Core_Form { } } - $query = 'select role_id, source, fee_level, is_test, is_pay_later, fee_amount, discount_id, fee_currency,campaign_id, discount_amount from civicrm_participant where id = ' . $this->_from_participant_id; - $dao = CRM_Core_DAO::executeQuery($query); - $value_to = []; - while ($dao->fetch()) { - $value_to['role_id'] = $dao->role_id; - $value_to['source'] = $dao->source; - $value_to['fee_level'] = $dao->fee_level; - $value_to['is_test'] = $dao->is_test; - $value_to['is_pay_later'] = $dao->is_pay_later; - $value_to['fee_amount'] = $dao->fee_amount; - } - $value_to['contact_id'] = $contact_id; - $value_to['event_id'] = $this->_event_id; - $value_to['status_id'] = CRM_Core_PseudoConstant::getKey( - 'CRM_Event_BAO_Participant', - 'status_id', - 'Registered' - ); - $value_to['register_date'] = date("Y-m-d"); - //first create the new participant row -don't set registered_by yet or email won't be sent - $participant = CRM_Event_BAO_Participant::create($value_to); - //send a confirmation email to the new participant - $this->participantTransfer($participant); - //now update registered_by_id - $query = "UPDATE civicrm_participant cp SET cp.registered_by_id = %1 WHERE cp.id = ({$participant->id})"; - $params = [1 => [$this->_from_participant_id, 'Integer']]; - $dao = CRM_Core_DAO::executeQuery($query, $params); - //copy line items to new participant - $line_items = CRM_Price_BAO_LineItem::getLineItems($this->_from_participant_id); - foreach ($line_items as $item) { - $item['entity_id'] = $participant->id; - $item['id'] = NULL; - $item['entity_table'] = "civicrm_participant"; - $new_item = CRM_Price_BAO_LineItem::create($item); - } - //now cancel the from participant record, leaving the original line-item(s) - $value_from = []; - $value_from['id'] = $this->_from_participant_id; - $tansferId = array_search('Transferred', CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'")); - $value_from['status_id'] = $tansferId; - $value_from['transferred_to_contact_id'] = $contact_id; + $this->transferParticipantRegistration($contact_id, $this->_from_participant_id, $this->_event_id); + $contact_details = CRM_Contact_BAO_Contact::getContactDetails($contact_id); $display_name = current($contact_details); $this->assign('to_participant', $display_name); - CRM_Event_BAO_Participant::create($value_from); $this->sendCancellation(); list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contact_id); $statusMsg = ts('Event registration information for %1 has been updated.', [1 => $displayName]); @@ -539,4 +499,59 @@ class CRM_Event_Form_SelfSvcTransfer extends CRM_Core_Form { CRM_Core_Session::setStatus($statusMsg, ts('Thanks'), 'success'); } + /** + * Move Participant registration to new contact. + * + * @param int $toContactID + * @param int $fromParticipantID + * @param int $eventID + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public function transferParticipantRegistration($toContactID, $fromParticipantID, $eventID) { + $query = 'select role_id, source, fee_level, is_test, is_pay_later, fee_amount, discount_id, fee_currency,campaign_id, discount_amount from civicrm_participant where id = ' . $fromParticipantID; + $dao = CRM_Core_DAO::executeQuery($query); + $value_to = []; + while ($dao->fetch()) { + $value_to['role_id'] = $dao->role_id; + $value_to['source'] = $dao->source; + $value_to['fee_level'] = $dao->fee_level; + $value_to['is_test'] = $dao->is_test; + $value_to['is_pay_later'] = $dao->is_pay_later; + $value_to['fee_amount'] = $dao->fee_amount; + } + $value_to['contact_id'] = $toContactID; + $value_to['event_id'] = $eventID; + $value_to['status_id'] = CRM_Core_PseudoConstant::getKey( + 'CRM_Event_BAO_Participant', + 'status_id', + 'Registered' + ); + $value_to['register_date'] = date("Y-m-d"); + //first create the new participant row -don't set registered_by yet or email won't be sent + $participant = CRM_Event_BAO_Participant::create($value_to); + //send a confirmation email to the new participant + $this->participantTransfer($participant); + //now update registered_by_id + $query = "UPDATE civicrm_participant cp SET cp.registered_by_id = %1 WHERE cp.id = ({$participant->id})"; + $params = [1 => [$fromParticipantID, 'Integer']]; + $dao = CRM_Core_DAO::executeQuery($query, $params); + //copy line items to new participant + $line_items = CRM_Price_BAO_LineItem::getLineItems($this->_from_participant_id); + foreach ($line_items as $item) { + $item['entity_id'] = $participant->id; + $item['id'] = NULL; + $item['entity_table'] = "civicrm_participant"; + $new_item = CRM_Price_BAO_LineItem::create($item); + } + //now cancel the from participant record, leaving the original line-item(s) + $value_from = []; + $value_from['id'] = $fromParticipantID; + $tansferId = array_search('Transferred', CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'")); + $value_from['status_id'] = $tansferId; + $value_from['transferred_to_contact_id'] = $toContactID; + CRM_Event_BAO_Participant::create($value_from); + } + } diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php index 79f7ee481c..372011dc1b 100644 --- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -816,6 +816,43 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { return $submitParams; } + /** + * Check if participant is transferred correctly. + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public function testTransferParticipantRegistration() { + //Register a contact to a sample event. + $this->createParticipantRecordsFromTwoFieldPriceSet(); + $contribution = $this->callAPISuccessGetSingle('Contribution', []); + //Check line item count of the contribution id before transfer. + $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']); + $this->assertEquals(count($lineItems), 2); + $participantId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $contribution['id'], 'participant_id', 'contribution_id'); + /* @var CRM_Event_Form_SelfSvcTransfer $form */ + $form = $this->getFormObject('CRM_Event_Form_SelfSvcTransfer'); + $toContactId = $this->individualCreate(); + $form->transferParticipantRegistration($toContactId, $participantId, CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $participantId, 'event_id')); + + //Assert participant is transferred to $toContactId. + $participant = $this->callAPISuccess('Participant', 'getsingle', [ + 'return' => ["transferred_to_contact_id"], + 'id' => $participantId, + ]); + $this->assertEquals($participant['transferred_to_contact_id'], $toContactId); + + //Assert $toContactId has a new registration. + $toParticipant = $this->callAPISuccess('Participant', 'getsingle', [ + 'contact_id' => $toContactId, + ]); + $this->assertEquals($toParticipant['participant_registered_by_id'], $participantId); + + //Check line item count of the contribution id remains the same. + $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']); + $this->assertEquals(count($lineItems), 2); + } + /** * Get the id of the created event. * -- 2.25.1