|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Daaner\NovaPoshta\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Daaner\NovaPoshta\NovaPoshta; |
|
6
|
|
|
|
|
7
|
|
|
class TrackingDocument extends NovaPoshta |
|
8
|
|
|
{ |
|
9
|
|
|
protected $model = 'TrackingDocument'; |
|
10
|
|
|
protected $calledMethod; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Получение полной информации по ТТН/ТТНкам. |
|
14
|
|
|
* @see https://developers.novaposhta.ua/view/model/a99d2f28-8512-11ec-8ced-005056b2dbe1/method/a9ae7bc9-8512-11ec-8ced-005056b2dbe1 |
|
15
|
|
|
* |
|
16
|
|
|
* @param string|array $documents |
|
17
|
|
|
* @return array |
|
18
|
|
|
*/ |
|
19
|
|
|
public function getStatusDocuments($documents): array |
|
20
|
|
|
{ |
|
21
|
|
|
$this->calledMethod = 'getStatusDocuments'; |
|
22
|
|
|
|
|
23
|
|
|
if (is_array($documents) === false) { |
|
24
|
|
|
$documents = explode(', ', /** @scrutinizer ignore-type */ $documents); |
|
25
|
|
|
} |
|
26
|
|
|
$methodProperties = [ |
|
27
|
|
|
'Documents' => $documents, |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
return $this->getResponse($this->model, $this->calledMethod, $methodProperties, false); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Проверка единичной ТТН или массива. Телефон, если указан, подставляется один для всех. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string|array $ttns Номер ТТН либо массив ТТНок |
|
37
|
|
|
* @param string|null $phone Номер телефона получателя или отправителя |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
public function checkTTN($ttns, ?string $phone = null): array |
|
41
|
|
|
{ |
|
42
|
|
|
$documents = []; |
|
43
|
|
|
|
|
44
|
|
|
if (is_array($ttns)) { |
|
45
|
|
|
$docs = array_values($ttns); |
|
46
|
|
|
foreach ($docs as $key => $ttn) { |
|
47
|
|
|
$documents[$key]['DocumentNumber'] = $this->clearNumber($ttn); |
|
48
|
|
|
$documents[$key]['Phone'] = $phone; |
|
49
|
|
|
} |
|
50
|
|
|
} else { |
|
51
|
|
|
$documents[0]['DocumentNumber'] = $this->clearNumber($ttns); |
|
52
|
|
|
$documents[0]['Phone'] = $phone; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->getStatusDocuments($documents); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Получение статусов одной ТТН или массива. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string|array $ttns |
|
62
|
|
|
* @param string|int|null $phone |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getStatusTTN($ttns, $phone = null): array |
|
66
|
|
|
{ |
|
67
|
|
|
$answer = $this->checkTTN($ttns, $phone); |
|
68
|
|
|
$statuses = []; |
|
69
|
|
|
|
|
70
|
|
|
if ($answer['success'] && ! empty($answer['result'])) { |
|
71
|
|
|
foreach ($answer['result'] as $key => $status) { |
|
72
|
|
|
$statuses[$key]['Number'] = $status['Number']; |
|
73
|
|
|
$statuses[$key]['StatusCode'] = $status['StatusCode']; |
|
74
|
|
|
$statuses[$key]['Status'] = $status['Status']; |
|
75
|
|
|
$statuses[$key]['StatusLocale'] = trans('novaposhta::novaposhta.statusCode.'.$status['StatusCode']); |
|
76
|
|
|
$statuses[$key]['ActualDeliveryDate'] = $status['ActualDeliveryDate'] ?? null; |
|
77
|
|
|
|
|
78
|
|
|
$statuses[$key]['NewTTN'] = ''; |
|
79
|
|
|
$statuses[$key]['NewMoneyTTN'] = ''; |
|
80
|
|
|
|
|
81
|
|
|
// Проверка на существование поля обратной доставки и получения номера накладной |
|
82
|
|
|
// если присутствует тире в номере - значит это отправка денег назад |
|
83
|
|
|
// если длина значения > 11 - это номер возврата денег. Поэтому проверка четко на 11 символов |
|
84
|
|
|
if (isset($status['LastCreatedOnTheBasisNumber']) && $status['LastCreatedOnTheBasisNumber']) { |
|
85
|
|
|
if (strripos($status['LastCreatedOnTheBasisNumber'], '-')) { |
|
86
|
|
|
$statuses[$key]['NewMoneyTTN'] = $status['LastCreatedOnTheBasisNumber']; |
|
87
|
|
|
} else { |
|
88
|
|
|
$statuses[$key]['NewTTN'] = $status['LastCreatedOnTheBasisNumber']; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// $statuses[$key]['NewTTN'] = isset($status['LastCreatedOnTheBasisNumber']) |
|
93
|
|
|
// && $status['LastCreatedOnTheBasisNumber'] |
|
94
|
|
|
// && strlen($status['LastCreatedOnTheBasisNumber']) > 10 ? $status['LastCreatedOnTheBasisNumber'] : null; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$return = [ |
|
99
|
|
|
'success' => $answer['success'], |
|
100
|
|
|
'result' => $statuses, |
|
101
|
|
|
'info' => $answer['info'], |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
if (isset($answer['dev'])) { |
|
105
|
|
|
$return['dev'] = $answer['dev']; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Очистка пробелов и букв в ТТН |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $ttn |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function clearNumber(string $ttn): string |
|
118
|
|
|
{ |
|
119
|
|
|
return preg_replace('/[^0-9,]/', '', $ttn); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|