|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmoCRM\Models; |
|
4
|
|
|
|
|
5
|
|
|
use AmoCRM\Models\Traits\SetTags; |
|
6
|
|
|
use AmoCRM\Models\Traits\SetNextDate; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Customer |
|
10
|
|
|
* |
|
11
|
|
|
* Класс модель для работы с Покупателями |
|
12
|
|
|
* |
|
13
|
|
|
* @package AmoCRM\Models |
|
14
|
|
|
* @author dotzero <[email protected]> |
|
15
|
|
|
* @link http://www.dotzero.ru/ |
|
16
|
|
|
* @link https://github.com/dotzero/amocrm-php |
|
17
|
|
|
* |
|
18
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
19
|
|
|
* file that was distributed with this source code. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Customer extends AbstractModel |
|
22
|
|
|
{ |
|
23
|
|
|
use SetTags, SetNextDate; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array Список доступный полей для модели (исключая кастомные поля) |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $fields = [ |
|
29
|
|
|
'name', |
|
30
|
|
|
'main_user_id', |
|
31
|
|
|
'created_by', |
|
32
|
|
|
'next_price', |
|
33
|
|
|
'periodicity', |
|
34
|
|
|
'tags', |
|
35
|
|
|
'next_date', |
|
36
|
|
|
'request_id', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Список покупателей |
|
41
|
|
|
* |
|
42
|
|
|
* Метод для получения покупателей аккаунта |
|
43
|
|
|
* |
|
44
|
|
|
* @link https://developers.amocrm.ru/rest_api/customers/list.php |
|
45
|
|
|
* @param array $parameters Массив параметров к amoCRM API |
|
46
|
|
|
* @return array Ответ amoCRM API |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function apiList($parameters) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$response = $this->getRequest('/private/api/v2/json/customers/list', $parameters); |
|
51
|
|
|
|
|
52
|
1 |
|
return isset($response['customers']) ? $response['customers'] : []; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Добавление покупателей |
|
57
|
|
|
* |
|
58
|
|
|
* Метод позволяет добавлять покупателей по одному или пакетно |
|
59
|
|
|
* |
|
60
|
|
|
* @link https://developers.amocrm.ru/rest_api/customers/set.php |
|
61
|
|
|
* @param array $customers Массив покупателей для пакетного добавления |
|
62
|
|
|
* @return int|array Уникальный идентификатор покупателя или массив при пакетном добавлении |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function apiAdd($customers = []) |
|
65
|
|
|
{ |
|
66
|
1 |
|
if (empty($customers)) { |
|
67
|
1 |
|
$customers = [$this]; |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
$parameters = [ |
|
71
|
|
|
'customers' => [ |
|
72
|
1 |
|
'add' => [], |
|
73
|
1 |
|
], |
|
74
|
1 |
|
]; |
|
75
|
|
|
|
|
76
|
1 |
|
foreach ($customers AS $customer) { |
|
77
|
1 |
|
$parameters['customers']['add'][] = $customer->getValues(); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
$response = $this->postRequest('/private/api/v2/json/customers/set', $parameters); |
|
81
|
|
|
|
|
82
|
1 |
|
if (isset($response['customers']['add'])) { |
|
83
|
1 |
|
$result = array_map(function ($item) { |
|
84
|
1 |
|
if(!empty($item['id'])) |
|
85
|
1 |
|
return $item['id']; |
|
86
|
|
|
elseif(!empty($item['error'])) |
|
87
|
|
|
throw new Exception($item['error'],filter_var(mb_strstr($item['error'],".",true), FILTER_SANITIZE_NUMBER_INT)); |
|
88
|
|
|
else |
|
89
|
|
|
return []; |
|
90
|
1 |
|
}, $response['customers']['add']['customers']); |
|
91
|
1 |
|
} else { |
|
92
|
|
|
return []; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return count($customers) == 1 ? array_shift($result) : $result; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Обновление покупателей |
|
100
|
|
|
* |
|
101
|
|
|
* Метод позволяет обновлять данные по уже существующим покупателям |
|
102
|
|
|
* |
|
103
|
|
|
* @link https://developers.amocrm.ru/rest_api/customers/set.php |
|
104
|
|
|
* @param int $id Уникальный идентификатор покупателя |
|
105
|
|
|
* @return bool Флаг успешности выполнения запроса |
|
106
|
|
|
* @throws \AmoCRM\Exception |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function apiUpdate($id) |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->checkId($id); |
|
111
|
|
|
|
|
112
|
|
|
$parameters = [ |
|
113
|
|
|
'customers' => [ |
|
114
|
1 |
|
'update' => [], |
|
115
|
1 |
|
], |
|
116
|
1 |
|
]; |
|
117
|
|
|
|
|
118
|
1 |
|
$customer = $this->getValues(); |
|
119
|
1 |
|
$customer['id'] = $id; |
|
120
|
|
|
|
|
121
|
1 |
|
$parameters['customers']['update'][] = $customer; |
|
122
|
|
|
|
|
123
|
1 |
|
$response = $this->postRequest('/private/api/v2/json/customers/set', $parameters); |
|
124
|
|
|
|
|
125
|
1 |
|
return isset($response['customers']) ? true : false; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|