Skip to content

Commit

Permalink
Merge pull request #18076 from MegaphoneJon/better-on-behalf-of-2
Browse files Browse the repository at this point in the history
[REF] change deprecated function to API4 call
  • Loading branch information
eileenmcnaughton authored Sep 9, 2020
2 parents 21dc285 + e4addeb commit 2d4074a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
28 changes: 15 additions & 13 deletions CRM/Contribute/Form/Contribution/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1145,19 +1145,9 @@ public static function processOnBehalfOrganization(&$behalfOrganization, &$conta
// create employer relationship with $contactID only when new organization is there
// else retain the existing relationship
else {
// get the Employee relationship type id
$relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');

// keep relationship params ready
$relParams['relationship_type_id'] = $relTypeId . '_a_b';
$relParams['is_permission_a_b'] = 1;
$relParams['is_active'] = 1;
$isNotCurrentEmployer = TRUE;
}

// formalities for creating / editing organization.
$behalfOrganization['contact_type'] = 'Organization';

if (!$orgID) {
// check if matching organization contact exists
$dupeIDs = CRM_Contact_BAO_Contact::getDuplicateContacts($behalfOrganization, 'Organization', 'Unsupervised', [], FALSE);
Expand All @@ -1182,14 +1172,26 @@ public static function processOnBehalfOrganization(&$behalfOrganization, &$conta
}

// create organization, add location
$behalfOrganization['contact_type'] = 'Organization';
$orgID = CRM_Contact_BAO_Contact::createProfileContact($behalfOrganization, $fields, $orgID,
NULL, NULL, 'Organization'
);
// create relationship
if ($isNotCurrentEmployer) {
$relParams['contact_check'][$orgID] = 1;
$cid = ['contact' => $contactID];
CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid);
try {
\Civi\Api4\Relationship::create(FALSE)
->addValue('contact_id_a', $contactID)
->addValue('contact_id_b', $orgID)
->addValue('relationship_type_id', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b'))
->addValue('is_permission_a_b:name', 'View and update')
->execute();
}
catch (CRM_Core_Exception $e) {
// Ignore if duplicate relationship.
if ($e->getMessage() !== 'Duplicate Relationship') {
throw $e;
}
}
}

// if multiple match - send a duplicate alert
Expand Down
11 changes: 6 additions & 5 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ public function testProcessOnBehalfOrganization() {
'postal_code' => '222222',
'country' => 'United States',
];
$contactID = $this->individualCreate();
$originalContactId = $contactID = $this->individualCreate();
$orgId = $this->organizationCreate(['organization_name' => 'testorg1']);
$orgCount = $this->callAPISuccessGetCount('Contact', [
'contact_type' => "Organization",
Expand All @@ -1347,7 +1347,7 @@ public function testProcessOnBehalfOrganization() {
$this->assertEquals($orgCount, 1);

$values = $params = [];
$behalfOrganization = [
$originalBehalfOrganization = $behalfOrganization = [
'organization_name' => 'testorg1',
'phone' => [
1 => [
Expand Down Expand Up @@ -1383,9 +1383,10 @@ public function testProcessOnBehalfOrganization() {
'country-3' => 1,
'state_province-3' => 1,
];
CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
$empty = [];
CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $empty, $empty, $empty);

//Check whether new organisation is not created.
//Check whether new organisation is created.
$result = $this->callAPISuccess('Contact', 'get', [
'contact_type' => "Organization",
'organization_name' => "testorg1",
Expand All @@ -1399,7 +1400,7 @@ public function testProcessOnBehalfOrganization() {

//Check if alert is assigned to params if more than 1 dupe exists.
$orgId = $this->organizationCreate(['organization_name' => 'testorg1', 'email' => '[email protected]']);
CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($originalBehalfOrganization, $originalContactId, $values, $params, $fields);
$this->assertEquals($params['onbehalf_dupe_alert'], 1);
}

Expand Down

0 comments on commit 2d4074a

Please sign in to comment.