2015-09-07 18:09:12 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once(__DIR__ . "/../vendor/autoload.php");
|
|
|
|
require(__DIR__ . "/Parser.php");
|
|
|
|
|
|
|
|
class ApiHelper {
|
|
|
|
|
|
|
|
protected $config;
|
|
|
|
protected $crmClient;
|
|
|
|
protected $parser;
|
|
|
|
|
|
|
|
public function __construct($config) {
|
|
|
|
|
|
|
|
$this->config = $config;
|
|
|
|
|
|
|
|
$this->crmClient = new \RetailCrm\ApiClient(
|
|
|
|
$this->config['retailcrm_url'],
|
|
|
|
$this->config['retailcrm_apikey']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->parser = new Parser($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processXMLOrders() {
|
|
|
|
$url = $this->config['tiu_xml_url'];
|
|
|
|
$xml = simplexml_load_file($url);
|
2015-11-05 16:43:26 +03:00
|
|
|
|
2015-09-07 18:09:12 +03:00
|
|
|
if (! $xml instanceof SimpleXMLElement) {
|
|
|
|
$this->writeLog("ApiHelper::processXmlOrders: cannot get XML from $url");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$orders = array();
|
2015-11-05 16:43:26 +03:00
|
|
|
|
2015-09-07 18:09:12 +03:00
|
|
|
foreach($xml as $xmlOrder) {
|
|
|
|
$order = $this->parser->parseXMLNewOrder($xmlOrder);
|
2015-11-05 16:43:26 +03:00
|
|
|
|
2015-09-07 18:09:12 +03:00
|
|
|
if ($order) {
|
|
|
|
$customerId = $this->checkCustomers($order);
|
2015-11-05 16:43:26 +03:00
|
|
|
|
|
|
|
if ($customerId !== false) {
|
2015-09-07 18:09:12 +03:00
|
|
|
$order['customerId'] = $customerId;
|
|
|
|
}
|
2015-11-05 16:43:26 +03:00
|
|
|
|
2015-09-07 18:09:12 +03:00
|
|
|
$orders[$order['number']] = $order;
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 16:43:26 +03:00
|
|
|
|
2015-09-07 18:09:12 +03:00
|
|
|
$orders = $this->filterOrders($orders);
|
2015-11-05 16:43:26 +03:00
|
|
|
|
2015-11-16 10:38:47 +03:00
|
|
|
$this->uploadOrders($orders);
|
2015-09-07 18:09:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function uploadOrders($orders) {
|
|
|
|
if (sizeof($orders)) {
|
|
|
|
$orders = array_chunk($orders, $this->config['retailcrm_order_chunk_size']);
|
|
|
|
foreach ($orders as $chunk) {
|
|
|
|
try {
|
|
|
|
$result = $this->crmClient->ordersUpload($chunk);
|
2015-11-16 10:38:47 +03:00
|
|
|
if (!$result->isSuccessful()) {
|
|
|
|
$this->writeLog('ordersUpload: ' . $result['errorMsg'] . (isset($result['errors']) ? ': ' . json_encode($result['errors']) : ''));
|
|
|
|
}
|
|
|
|
time_nanosleep(0, 200000000);
|
2015-09-07 18:09:12 +03:00
|
|
|
} catch (\RetailCrm\Exception\CurlException $e) {
|
2015-11-16 10:38:47 +03:00
|
|
|
$this->writeLog('ordersUpload: ' . $e, 'error');
|
2015-09-07 18:09:12 +03:00
|
|
|
}
|
|
|
|
}
|
2015-11-16 10:38:47 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2015-09-07 18:09:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function checkCustomers($order) {
|
2015-11-05 16:43:26 +03:00
|
|
|
$customerId = false;
|
2015-11-16 10:38:47 +03:00
|
|
|
$filter = array();
|
2015-11-05 16:43:26 +03:00
|
|
|
|
2015-11-16 10:38:47 +03:00
|
|
|
if (!empty($order['email'])) $filter['email']= $order['email'];
|
|
|
|
if (!empty($order['phone'])) $filter['name'] = $order['phone'];
|
|
|
|
|
|
|
|
if (!empty($filter)) {
|
2015-09-07 18:09:12 +03:00
|
|
|
try {
|
|
|
|
$customers = $this->crmClient->customersList($filter);
|
|
|
|
} catch (\RetailCrm\Exception\CurlException $e) {
|
2015-11-16 10:38:47 +03:00
|
|
|
$this->writeLog('customersList: ' . $e, 'error');
|
2015-09-07 18:09:12 +03:00
|
|
|
}
|
2015-11-05 16:43:26 +03:00
|
|
|
|
|
|
|
if (!empty($customers['customers'])) {
|
2015-11-16 10:38:47 +03:00
|
|
|
foreach ($customers['customers'] as $_customer) {
|
2015-11-05 16:43:26 +03:00
|
|
|
if (!empty($_customer['externalId'])) {
|
|
|
|
$customerId = $_customer['externalId'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 10:38:47 +03:00
|
|
|
if ($customerId === false) {
|
|
|
|
try {
|
2016-02-24 14:24:59 +03:00
|
|
|
$request = $this->crmClient->customersFixExternalIds(
|
2015-11-16 10:38:47 +03:00
|
|
|
array(
|
|
|
|
'id' => $customers['customers'][0]['id'],
|
|
|
|
'externalId' => $order['externalId']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($request->isSuccessful()) {
|
|
|
|
$customerId = $order['externalId'];
|
|
|
|
}
|
|
|
|
} catch (\RetailCrm\Exception\CurlException $e) {
|
|
|
|
$this->writeLog('customersFixExternalIds: ' . $e, 'error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$request = $this->crmClient->customersCreate(
|
|
|
|
array(
|
|
|
|
'externalId' => $order['externalId'],
|
|
|
|
'firstName' => $order['firstName'],
|
|
|
|
'lastName' => !empty($order['lastName']) ? $order['lastName'] : '',
|
|
|
|
'email' => !empty($order['email']) ? $order['email'] : '',
|
|
|
|
'phones' => !empty($order['phone'])
|
|
|
|
? array(array('number' => $order['phone']))
|
|
|
|
: array()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($request->isSuccessful()) {
|
|
|
|
$customerId = $order['externalId'];
|
|
|
|
}
|
|
|
|
} catch (\RetailCrm\Exception\CurlException $e) {
|
|
|
|
$this->writeLog('customersCreate: ' . $e, 'error');
|
|
|
|
}
|
|
|
|
}
|
2015-09-07 18:09:12 +03:00
|
|
|
}
|
2015-11-05 16:43:26 +03:00
|
|
|
|
2015-09-07 18:09:12 +03:00
|
|
|
return $customerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function writeLog($text, $type = null) {
|
2015-11-05 16:43:26 +03:00
|
|
|
if (! file_exists(__DIR__ ."/../logs")) mkdir(__DIR__ ."/../logs");
|
2015-09-07 18:09:12 +03:00
|
|
|
$date = date('Y-m-d H:i:s');
|
|
|
|
file_put_contents(__DIR__ . "/../logs/error.log", "[$date]$text" . PHP_EOL, FILE_APPEND);
|
|
|
|
if ($type == 'error') {
|
|
|
|
$this->sendAlertEmail($text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function sendAlertEmail($text) {
|
|
|
|
mail($this->config['support_email'], $this->config['support_email_subject'], $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function filterOrders($toUpload) {
|
|
|
|
$numbers = array_keys($toUpload);
|
2015-11-16 10:38:47 +03:00
|
|
|
|
2015-11-05 16:43:26 +03:00
|
|
|
if (date_create_from_format('Y-m-d H:i:s', $this->config['date_from'])) {
|
2015-09-07 18:09:12 +03:00
|
|
|
foreach ($toUpload as $i => $order) {
|
2015-11-05 16:43:26 +03:00
|
|
|
if ($order['createdAt'] < $this->config['date_from']) {
|
2015-09-07 18:09:12 +03:00
|
|
|
unset($toUpload[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $toUpload;
|
|
|
|
}
|
|
|
|
|
2016-02-24 14:24:59 +03:00
|
|
|
}
|