forked from amocrm/amocrm-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leads_complex_actions.php
149 lines (137 loc) · 5.45 KB
/
leads_complex_actions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
use AmoCRM\Collections\ContactsCollection;
use AmoCRM\Collections\CustomFieldsValuesCollection;
use AmoCRM\Collections\Leads\LeadsCollection;
use AmoCRM\Collections\TagsCollection;
use AmoCRM\Exceptions\AmoCRMApiException;
use AmoCRM\Models\CompanyModel;
use AmoCRM\Models\ContactModel;
use AmoCRM\Models\CustomFieldsValues\MultitextCustomFieldValuesModel;
use AmoCRM\Models\CustomFieldsValues\ValueCollections\MultitextCustomFieldValueCollection;
use AmoCRM\Models\CustomFieldsValues\ValueModels\MultitextCustomFieldValueModel;
use AmoCRM\Models\LeadModel;
use AmoCRM\Models\TagModel;
use AmoCRM\Models\Unsorted\FormsMetadata;
use League\OAuth2\Client\Token\AccessTokenInterface;
include_once __DIR__ . '/bootstrap.php';
$accessToken = getToken();
$apiClient->setAccessToken($accessToken)
->setAccountBaseDomain($accessToken->getValues()['baseDomain'])
->onAccessTokenRefresh(
function (AccessTokenInterface $accessToken, string $baseDomain) {
saveToken(
[
'accessToken' => $accessToken->getToken(),
'refreshToken' => $accessToken->getRefreshToken(),
'expires' => $accessToken->getExpires(),
'baseDomain' => $baseDomain,
]
);
}
);
//Представим, что у нас есть данные, полученные из сторонней системы
$externalData = [
[
'is_new' => true,
'price' => 54321,
'name' => 'Lead name',
'contact' => [
'first_name' => 'Ivan',
'last_name' => 'Zinoviev',
'phone' => '+79129876543',
],
'company' => [
'name' => 'Qwerty LLC',
],
'tag' => 'Новый клиент',
'external_id' => '0752a617-c834-4bde-b4a6-76ff0fe26871',
],
[
'is_new' => false,
'price' => 99212,
'name' => 'Lead 2 name',
'contact' => [
'first_name' => 'Masha',
'last_name' => 'Petrova',
'phone' => '+79123456789',
],
'company' => [
'name' => 'Asdfg LLC',
],
'tag' => 'Важный клиент',
'external_id' => '4268bc22-f568-4689-84ac-7d2df9599c08',
],
];
$leadsCollection = new LeadsCollection();
//Создадим модели и заполним ими коллекцию
foreach ($externalData as $externalLead) {
$lead = (new LeadModel())
->setName($externalLead['name'])
->setPrice($externalLead['price'])
->setTags(
(new TagsCollection())
->add(
(new TagModel())
->setName($externalLead['tag'])
)
)
->setContacts(
(new ContactsCollection())
->add(
(new ContactModel())
->setFirstName($externalLead['contact']['first_name'])
->setLastName($externalLead['contact']['last_name'])
->setCustomFieldsValues(
(new CustomFieldsValuesCollection())
->add(
(new MultitextCustomFieldValuesModel())
->setFieldCode('PHONE')
->setValues(
(new MultitextCustomFieldValueCollection())
->add(
(new MultitextCustomFieldValueModel())
->setValue($externalLead['contact']['phone'])
)
)
)
)
)
)
->setCompany(
(new CompanyModel())
->setName($externalLead['company']['name'])
)
->setRequestId($externalLead['external_id']);
if ($externalLead['is_new']) {
$lead->setMetadata(
(new FormsMetadata())
->setFormId('my_best_form')
->setFormName('Обратная связь')
->setFormPage('https://example.com/form')
->setFormSentAt(mktime(date('h'), date('i'), date('s'), date('m'), date('d'), date('Y')))
->setReferer('https://google.com/search')
->setIp('192.168.0.1')
);
}
$leadsCollection->add($lead);
}
//Создадим сделки
try {
$addedLeadsCollection = $apiClient->leads()->addComplex($leadsCollection);
} catch (AmoCRMApiException $e) {
printError($e);
die;
}
/** @var LeadModel $addedLead */
foreach ($addedLeadsCollection as $addedLead) {
//Пройдемся по добавленным сделкам и выведем результат
$leadId = $addedLead->getId();
$contactId = $addedLead->getContacts()->first()->getId();
$companyId = $addedLead->getCompany()->getId();
$externalRequestIds = $addedLead->getComplexRequestIds();
foreach ($externalRequestIds as $requestId) {
$action = $addedLead->isMerged() ? 'обновлены' : 'созданы';
$separator = PHP_SAPI === 'cli' ? PHP_EOL : "<br>";
echo "Для сущности с ID {$requestId} были {$action}: сделка ({$leadId}), контакт ({$contactId}), компания ({$companyId})" . $separator;
}
}