Update ApiHelper.php
This commit is contained in:
parent
ec353829f3
commit
131a1a12a8
@ -24,26 +24,30 @@ class ApiHelper {
|
|||||||
public function processXMLOrders() {
|
public function processXMLOrders() {
|
||||||
$url = $this->config['tiu_xml_url'];
|
$url = $this->config['tiu_xml_url'];
|
||||||
$xml = simplexml_load_file($url);
|
$xml = simplexml_load_file($url);
|
||||||
|
|
||||||
if (! $xml instanceof SimpleXMLElement) {
|
if (! $xml instanceof SimpleXMLElement) {
|
||||||
$this->writeLog("ApiHelper::processXmlOrders: cannot get XML from $url");
|
$this->writeLog("ApiHelper::processXmlOrders: cannot get XML from $url");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$orders = array();
|
$orders = array();
|
||||||
|
|
||||||
foreach($xml as $xmlOrder) {
|
foreach($xml as $xmlOrder) {
|
||||||
$order = $this->parser->parseXMLNewOrder($xmlOrder);
|
$order = $this->parser->parseXMLNewOrder($xmlOrder);
|
||||||
|
|
||||||
if ($order) {
|
if ($order) {
|
||||||
$customerId = $this->checkCustomers($order);
|
$customerId = $this->checkCustomers($order);
|
||||||
if ($customerId === false) {
|
|
||||||
echo "upload failed" . PHP_EOL;
|
if ($customerId !== false) {
|
||||||
return false;
|
|
||||||
} elseif ($customerId !== 0) {
|
|
||||||
$order['customerId'] = $customerId;
|
$order['customerId'] = $customerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
$orders[$order['number']] = $order;
|
$orders[$order['number']] = $order;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$orders = $this->filterOrders($orders);
|
$orders = $this->filterOrders($orders);
|
||||||
|
|
||||||
if ($this->uploadOrders($orders)) {
|
if ($this->uploadOrders($orders)) {
|
||||||
$a = sizeof($orders);
|
$a = sizeof($orders);
|
||||||
echo "uploaded $a orders" . PHP_EOL;
|
echo "uploaded $a orders" . PHP_EOL;
|
||||||
@ -81,11 +85,10 @@ class ApiHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function checkCustomers($order) {
|
protected function checkCustomers($order) {
|
||||||
$customerId = 0;
|
$customerId = false;
|
||||||
if ($order['email'] != '')
|
if ($order['email'] != '') $filter['email']= $order['email'];
|
||||||
$filter = array('email' => $order['email']);
|
if ($order['phone'] != '') $filter['name'] = $order['phone'];
|
||||||
if ($order['phone'] != '')
|
|
||||||
$filter = array('name' => $order['phone']);
|
|
||||||
if (isset($filter)) {
|
if (isset($filter)) {
|
||||||
try {
|
try {
|
||||||
$customers = $this->crmClient->customersList($filter);
|
$customers = $this->crmClient->customersList($filter);
|
||||||
@ -96,17 +99,23 @@ class ApiHelper {
|
|||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isset($customers['customers']) && sizeof($customers['customers'] && isset($customers['customers'][0]['externalId']))) {
|
|
||||||
$customerId = $customers['customers'][0]['externalId'];
|
if (!empty($customers['customers'])) {
|
||||||
|
foreach ($customers as $_customer) {
|
||||||
|
if (!empty($_customer['externalId'])) {
|
||||||
|
$customerId = $_customer['externalId'];
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
time_nanosleep(0, 200000000);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return $customerId;
|
return $customerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function writeLog($text, $type = null) {
|
protected function writeLog($text, $type = null) {
|
||||||
if (! file_exists(__DIR__ ."/../logs"))
|
if (! file_exists(__DIR__ ."/../logs")) mkdir(__DIR__ ."/../logs");
|
||||||
mkdir(__DIR__ ."/../logs");
|
|
||||||
$date = date('Y-m-d H:i:s');
|
$date = date('Y-m-d H:i:s');
|
||||||
file_put_contents(__DIR__ . "/../logs/error.log", "[$date]$text" . PHP_EOL, FILE_APPEND);
|
file_put_contents(__DIR__ . "/../logs/error.log", "[$date]$text" . PHP_EOL, FILE_APPEND);
|
||||||
if ($type == 'error') {
|
if ($type == 'error') {
|
||||||
@ -120,35 +129,14 @@ class ApiHelper {
|
|||||||
|
|
||||||
protected function filterOrders($toUpload) {
|
protected function filterOrders($toUpload) {
|
||||||
$numbers = array_keys($toUpload);
|
$numbers = array_keys($toUpload);
|
||||||
if (date_create_from_format('Y-m-d H:i:s', $this->config['filter_date'])) {
|
if (date_create_from_format('Y-m-d H:i:s', $this->config['date_from'])) {
|
||||||
foreach ($toUpload as $i => $order) {
|
foreach ($toUpload as $i => $order) {
|
||||||
if ($order['createdAt'] < $this->config['filter_date']) {
|
if ($order['createdAt'] < $this->config['date_from']) {
|
||||||
unset($toUpload[$i]);
|
unset($toUpload[$i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$ordersListPage = 0;
|
|
||||||
do {
|
|
||||||
$ordersListPage++;
|
|
||||||
try {
|
|
||||||
$orders = $this->crmClient->ordersList(array(
|
|
||||||
'numbers' => $numbers
|
|
||||||
), $ordersListPage, 100);
|
|
||||||
} catch (\RetailCrm\Exception\CurlException $e) {
|
|
||||||
$text = '\Retailcrm\ApiClient::ordersList: ' . $e;
|
|
||||||
$this->writeLog($text, 'error');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (isset($orders['orders']) && sizeof($orders['orders'])) {
|
|
||||||
foreach ($orders['orders'] as $order) {
|
|
||||||
if (isset($toUpload[$order['number']]))
|
|
||||||
unset($toUpload[$order['number']]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
time_nanosleep(0, 200000000);
|
|
||||||
} while ($ordersListPage < $orders['pagination']['totalPageCount']);
|
|
||||||
|
|
||||||
return $toUpload;
|
return $toUpload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user