Skip to content

Commit

Permalink
external source id support
Browse files Browse the repository at this point in the history
  • Loading branch information
bessudnov committed Jan 21, 2022
1 parent 8115e57 commit 5092f78
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
38 changes: 38 additions & 0 deletions examples/leads_actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,41 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {

var_dump($syncedElement);
}

// Рассмотрим кейс создания источника и создания сделки с этим источником
// ID источника на вашей стороне
$sourceExternalId = 'my-integration-super-id';
// Создадим источник
$source = new \AmoCRM\Models\SourceModel();
$source->setName('My super source')
->setExternalId($sourceExternalId)
->setPipelineId(4913583);

$sourcesService = $apiClient->sources();

try {
$source = $sourcesService->addOne($source);
} catch (AmoCRMApiException $e) {
printError($e);
die;
}
echo 'Added source: ';
var_dump($source->toArray());
echo PHP_EOL;


$lead = new LeadModel();
$lead->setName('Название сделки')
->setPrice(54321)
->setSourceExternalId($sourceExternalId);

try {
$lead = $leadsService->addOne($lead);
} catch (AmoCRMApiException $e) {
printError($e);
die;
}

echo 'Added lead: ';
var_dump($lead->toArray());
echo PHP_EOL;
40 changes: 40 additions & 0 deletions src/AmoCRM/Models/LeadModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ class LeadModel extends BaseApiModel implements
*/
protected $sourceId;

/**
* @var string|null
*/
protected $sourceExternalId = null;

/**
* @var CustomFieldsValuesCollection|null
*/
Expand Down Expand Up @@ -955,6 +960,14 @@ public function toApi(?string $requestId = "0"): array
];
}

// Источник можно передать только при создании
if (is_null($this->getId()) && !is_null($this->getSourceExternalId())) {
$result[AmoCRMApiRequest::EMBEDDED]['source'] = [
'type' => 'widget',
'external_id' => $this->getSourceExternalId(),
];
}

$result['request_id'] = $this->getRequestId();

return $result;
Expand Down Expand Up @@ -983,6 +996,13 @@ public function toComplexApi(?string $requestId = "0"): array
$result[AmoCRMApiRequest::EMBEDDED]['metadata'] = $this->getMetadata()->toComplexApi();
}

if (is_null($this->getId()) && !is_null($this->getSourceExternalId())) {
$result[AmoCRMApiRequest::EMBEDDED]['source'] = [
'type' => 'widget',
'external_id' => $this->getSourceExternalId(),
];
}

$result['request_id'] = $this->getRequestId();

return $result;
Expand Down Expand Up @@ -1115,4 +1135,24 @@ public function setIsMerged(bool $isMerged): LeadModel

return $this;
}

/**
* @return string|null
*/
public function getSourceExternalId(): ?string
{
return $this->sourceExternalId;
}

/**
* @param string|null $sourceExternalId
*
* @return LeadModel
*/
public function setSourceExternalId(?string $sourceExternalId): LeadModel
{
$this->sourceExternalId = $sourceExternalId;

return $this;
}
}

0 comments on commit 5092f78

Please sign in to comment.