|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmoCRM\Models; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class CustomersPeriods |
|
7
|
|
|
* |
|
8
|
|
|
* Класс модель для работы со Списком периодов |
|
9
|
|
|
* |
|
10
|
|
|
* @package AmoCRM\Models |
|
11
|
|
|
* @author dotzero <[email protected]> |
|
12
|
|
|
* @link http://www.dotzero.ru/ |
|
13
|
|
|
* @link https://github.com/dotzero/amocrm-php |
|
14
|
|
|
* |
|
15
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
16
|
|
|
* file that was distributed with this source code. |
|
17
|
|
|
*/ |
|
18
|
|
|
class CustomersPeriods extends Base |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array Список доступный полей для модели (исключая кастомные поля) |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $fields = [ |
|
24
|
|
|
'id', |
|
25
|
|
|
'period', |
|
26
|
|
|
'sort', |
|
27
|
|
|
'color', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Список периодов |
|
32
|
|
|
* |
|
33
|
|
|
* Метод для получения списка периодов |
|
34
|
|
|
* |
|
35
|
|
|
* @link https://developers.amocrm.ru/rest_api/customers_periods/list.php |
|
36
|
|
|
* @return array Ответ amoCRM API |
|
37
|
|
|
*/ |
|
38
|
|
|
public function apiList() |
|
39
|
|
|
{ |
|
40
|
|
|
$response = $this->getRequest('/private/api/v2/json/customers_periods/list'); |
|
41
|
|
|
|
|
42
|
|
|
return isset($response['customers_periods']['list']) ? $response['customers_periods']['list'] : []; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Добавление, удаление и обновление периодов покупателей |
|
47
|
|
|
* |
|
48
|
|
|
* Метод позволяет изменять данные по периодам. |
|
49
|
|
|
* При изменении необходимо передать полный список периодов, включая уже существующие. |
|
50
|
|
|
* При удалении периода нужно исключить его из запроса. |
|
51
|
|
|
* |
|
52
|
|
|
* @link https://developers.amocrm.ru/rest_api/customers_periods/set.php |
|
53
|
|
|
* @param array periods Массив периодов |
|
54
|
|
|
* @return int|array Уникальный идентификатор периода или массив при пакетном добавлении |
|
55
|
|
|
*/ |
|
56
|
|
|
public function apiSet($periods = []) |
|
57
|
|
|
{ |
|
58
|
|
|
$parameters = [ |
|
59
|
|
|
'customers_periods' => [ |
|
60
|
|
|
'update' => [], |
|
61
|
|
|
], |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
foreach ($periods AS $period) { |
|
65
|
|
|
$parameters['customers_periods']['update'][] = $period->getValues(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$response = $this->postRequest('/private/api/v2/json/customers_periods/set', $parameters); |
|
69
|
|
|
|
|
70
|
|
|
if (isset($response['customers_periods']['set'])) { |
|
71
|
|
|
$result = array_map(function($item) { |
|
72
|
|
|
return $item['id']; |
|
73
|
|
|
}, $response['customers_periods']['set']); |
|
74
|
|
|
} else { |
|
75
|
|
|
return []; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return count($periods) == 1 ? array_shift($result) : $result; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|