1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: DrillCoder |
5
|
|
|
* Date: 09.10.2017 |
6
|
|
|
* Time: 12:51 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace DrillCoder\AmoCRM_Wrap; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Unsorted |
14
|
|
|
* @package DrillCoder\AmoCRM_Wrap |
15
|
|
|
*/ |
16
|
|
|
class Unsorted extends Base |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $id; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $formName; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $pipelineId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Contact[]|array |
35
|
|
|
*/ |
36
|
|
|
private $contacts = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Lead|array |
40
|
|
|
*/ |
41
|
|
|
private $lead = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Company[]|array |
45
|
|
|
*/ |
46
|
|
|
private $companies = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Note[] |
50
|
|
|
*/ |
51
|
|
|
private $notes; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $formName |
55
|
|
|
* @param Contact[] $contacts |
56
|
|
|
* @param Lead $lead |
57
|
|
|
* @param int|string|null $pipeline |
58
|
|
|
* @param Company[] $companies |
59
|
|
|
* |
60
|
|
|
* @throws AmoWrapException |
61
|
|
|
*/ |
62
|
|
|
public function __construct($formName, $lead, $contacts = array(), $pipeline = null, $companies = array()) |
63
|
|
|
{ |
64
|
|
|
if (!AmoCRM::isAuthorization()) { |
65
|
|
|
throw new AmoWrapException('Требуется авторизация'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->contacts = $contacts; |
69
|
|
|
$this->lead = $lead; |
70
|
|
|
$this->companies = $companies; |
71
|
|
|
$this->formName = $formName; |
72
|
|
|
if ($pipeline !== null) { |
73
|
|
|
$this->pipelineId = AmoCRM::searchPipelineId($pipeline); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Unsorted |
79
|
|
|
* |
80
|
|
|
* @throws AmoWrapException |
81
|
|
|
*/ |
82
|
|
|
public function save() |
83
|
|
|
{ |
84
|
|
|
if (!empty($this->lead) || !empty($this->contacts)) { |
85
|
|
|
$lead = null; |
86
|
|
|
if (!empty($this->lead)) { |
87
|
|
|
$lead = $this->lead->getRaw(); |
88
|
|
|
if (!empty($this->notes)) { |
89
|
|
|
foreach ($this->notes as $note) { |
90
|
|
|
$lead['notes'][] = $note->getRaw(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
$contacts = array(); |
95
|
|
|
foreach ($this->contacts as $contact) { |
96
|
|
|
$contacts[] = $contact->getRaw(); |
97
|
|
|
} |
98
|
|
|
$companies = array(); |
99
|
|
|
foreach ($this->companies as $company) { |
100
|
|
|
$companies[] = $company->getRaw(); |
101
|
|
|
} |
102
|
|
|
$request['add'] = array( |
|
|
|
|
103
|
|
|
array( |
104
|
|
|
'source_name' => 'DrillCoder AmoCRM Wrap', |
105
|
|
|
'created_at' => date('U'), |
106
|
|
|
'pipeline_id' => $this->pipelineId, |
107
|
|
|
'incoming_entities' => array( |
108
|
|
|
'leads' => array($lead), |
109
|
|
|
'contacts' => $contacts, |
110
|
|
|
'companies' => $companies, |
111
|
|
|
), |
112
|
|
|
'incoming_lead_info' => array( |
113
|
|
|
'form_id' => 25, |
114
|
|
|
'form_page' => $this->formName, |
115
|
|
|
), |
116
|
|
|
), |
117
|
|
|
); |
118
|
|
|
$response = AmoCRM::cUrl('api/v2/incoming_leads/form', $request); |
119
|
|
|
if ($response !== null && $response->status === 'success') { |
120
|
|
|
$this->id = $response->data[0]; |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
throw new AmoWrapException('Не удалось сохранить заявку в неразобранное'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $text |
129
|
|
|
* @param int $type |
130
|
|
|
* |
131
|
|
|
* @return Unsorted |
132
|
|
|
* |
133
|
|
|
* @throws AmoWrapException |
134
|
|
|
*/ |
135
|
|
|
public function addNote($text, $type = 4) |
136
|
|
|
{ |
137
|
|
|
$note = new Note(); |
138
|
|
|
$note->setText($text) |
139
|
|
|
->setType($type) |
140
|
|
|
->setElementType('lead'); |
|
|
|
|
141
|
|
|
$this->notes[] = $note; |
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
} |