Skip to content

Instantly share code, notes, and snippets.

@marceloandrader
Created February 8, 2019 15:18
Show Gist options
  • Save marceloandrader/a771ebce568856a2594c5657f52ef6e8 to your computer and use it in GitHub Desktop.
Save marceloandrader/a771ebce568856a2594c5657f52ef6e8 to your computer and use it in GitHub Desktop.
<?php
$pw = $endpoint->edit($model->pw_id, $payload);
$pw->custom_fields['_payload'] = $payload;
/*
* if the opportunity has the dummy contact still related to it, remove the relation
*/
if ($resourceName == CRM::RES_OPPORTUNITY) {
$dummyContact = Clients::findFirst('user_id = 8165');
if (!empty($dummyContact) && ($dummyContact->pw_id === null || $dummyContact->pw_id == 0)) {
$dummyContact = Clients::findFirst('pw_id IS NOT NULL AND pw_id > 0');
}
$resource->related($model->pw_id)->delete($dummyContact->pw_id, strtolower(CRM::RES_PERSON));
}
/*
* Prospeworks fails to deliver a good api, on opportunities updates, it sets the
* main company id of the opp to the same as the main contact.
* Additionally, deleting company relations and recreating them allows to set an
* arbitrary main company, but only by sending it as the first relation after deleting
* all the current ones. It also requires to have a small delay between api calls to ensure
* the calls order as it seems that the calls may not reach the backend in the same order as they are
* sent to the endpoint if we do it too fast.
*/
if ($resourceName == CRM::RES_OPPORTUNITY) {
foreach ($relations as $key => $relation) {
if ($relation['type'] == strtolower(CRM::RES_PERSON)) {
$resource->related($model->pw_id)->create($relation['id'], $relation['type']);
unset($relations[$key]);
}
}
$currentRelations = $resource->related($model->pw_id)->all();
if (is_object($currentRelations)) $currentRelations = [$currentRelations];
foreach ($currentRelations as $relation) {
if ($relation->type == strtolower(CRM::RES_COMPANY))
$resource->related($model->pw_id)->delete($relation->id, $relation->type);
}
foreach ($relations as $key => $relation) {
sleep(1);
$resource->related($model->pw_id)->create($relation['id'], $relation['type']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment