Merge pull request #1 from gwinn/master
Комментарии клиента и менеджера, запись даты последнего заказа в лог
This commit is contained in:
commit
f46e80c88d
@ -48,67 +48,89 @@ class ApiHelper {
|
||||
|
||||
$orders = $this->filterOrders($orders);
|
||||
|
||||
if ($this->uploadOrders($orders)) {
|
||||
$a = sizeof($orders);
|
||||
echo "uploaded $a orders" . PHP_EOL;
|
||||
return true;
|
||||
} else {
|
||||
echo "upload failed" . PHP_EOL;
|
||||
return false;
|
||||
}
|
||||
$this->uploadOrders($orders);
|
||||
}
|
||||
|
||||
protected function uploadOrders($orders) {
|
||||
if ($orders === false)
|
||||
return false;
|
||||
if (sizeof($orders)) {
|
||||
$orders = array_chunk($orders, $this->config['retailcrm_order_chunk_size']);
|
||||
foreach ($orders as $chunk) {
|
||||
try {
|
||||
$result = $this->crmClient->ordersUpload($chunk);
|
||||
} catch (\RetailCrm\Exception\CurlException $e) {
|
||||
$this->writeLog(
|
||||
'\Retailcrm\ApiClient::ordersUpload: ' . $e,
|
||||
'error'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
if (!$result->isSuccessful()) {
|
||||
$this->writeLog(
|
||||
'\Retailcrm\ApiClient::ordersUpload: ' . $result['errorMsg'] . (isset($result['errors']) ? ': ' . json_encode($result['errors']) : '')
|
||||
);
|
||||
$this->writeLog('ordersUpload: ' . $result['errorMsg'] . (isset($result['errors']) ? ': ' . json_encode($result['errors']) : ''));
|
||||
}
|
||||
time_nanosleep(0, 200000000);
|
||||
} catch (\RetailCrm\Exception\CurlException $e) {
|
||||
$this->writeLog('ordersUpload: ' . $e, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function checkCustomers($order) {
|
||||
$customerId = false;
|
||||
if ($order['email'] != '') $filter['email']= $order['email'];
|
||||
if ($order['phone'] != '') $filter['name'] = $order['phone'];
|
||||
$filter = array();
|
||||
|
||||
if (isset($filter)) {
|
||||
if (!empty($order['email'])) $filter['email']= $order['email'];
|
||||
if (!empty($order['phone'])) $filter['name'] = $order['phone'];
|
||||
|
||||
if (!empty($filter)) {
|
||||
try {
|
||||
$customers = $this->crmClient->customersList($filter);
|
||||
} catch (\RetailCrm\Exception\CurlException $e) {
|
||||
$this->writeLog(
|
||||
'\Retailcrm\ApiClient::customersList: ' . $e,
|
||||
'error'
|
||||
);
|
||||
return false;
|
||||
$this->writeLog('customersList: ' . $e, 'error');
|
||||
}
|
||||
|
||||
if (!empty($customers['customers'])) {
|
||||
foreach ($customers as $_customer) {
|
||||
foreach ($customers['customers'] as $_customer) {
|
||||
if (!empty($_customer['externalId'])) {
|
||||
$customerId = $_customer['externalId'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($customerId === false) {
|
||||
try {
|
||||
$request = $this->crmClient->customersFixExternalId(
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $customerId;
|
||||
@ -129,6 +151,7 @@ class ApiHelper {
|
||||
|
||||
protected function filterOrders($toUpload) {
|
||||
$numbers = array_keys($toUpload);
|
||||
|
||||
if (date_create_from_format('Y-m-d H:i:s', $this->config['date_from'])) {
|
||||
foreach ($toUpload as $i => $order) {
|
||||
if ($order['createdAt'] < $this->config['date_from']) {
|
||||
|
@ -17,6 +17,7 @@ class Parser {
|
||||
case 'id':
|
||||
$val = (string)$val;
|
||||
$order['number'] = $this->config['order_prefix'] . $val;
|
||||
$order['externalId'] = $val;
|
||||
break;
|
||||
case 'state':
|
||||
$orderStatuses = array_flip($this->config['order_statuses']);
|
||||
@ -32,22 +33,42 @@ class Parser {
|
||||
$order = array_merge($order, array(
|
||||
'email' => (string)$xml->email,
|
||||
'phone' => (string)$xml->phone,
|
||||
'orderMethod' => $this->config['order_method'],
|
||||
'createdAt' => $createdAt,
|
||||
'paymentType' => $this->config['payment'][(string)$xml->paymentType],
|
||||
'delivery' => array(
|
||||
'address' => array(
|
||||
'text' => (string)$xml->address
|
||||
'text' => trim((string)$xml->address)
|
||||
),
|
||||
'code' => $this->config['delivery'][(string)$xml->deliveryType]
|
||||
)
|
||||
));
|
||||
|
||||
if (empty($xml->name) && empty((string) $xml->email)) {
|
||||
$order['orderMethod'] = 'callback';
|
||||
} else {
|
||||
$order['orderMethod'] = $this->config['order_method'];
|
||||
}
|
||||
|
||||
if (!empty($xml->deliveryType) && isset($this->config['delivery'][(string)$xml->deliveryType])) {
|
||||
$order['delivery']['code'] = $this->config['delivery'][(string)$xml->deliveryType];
|
||||
}
|
||||
|
||||
if (!empty($xml->paymentType) && isset($this->config['payment'][(string)$xml->paymentType])) {
|
||||
$order['paymentType'] = $this->config['payment'][(string)$xml->paymentType];
|
||||
}
|
||||
|
||||
if (!empty($xml->salescomment)) {
|
||||
$order['managerComment'] = (string)$xml->salescomment;
|
||||
}
|
||||
|
||||
if (!empty($xml->payercomment)) {
|
||||
$order['customerComment'] = (string)$xml->payercomment;
|
||||
}
|
||||
|
||||
$items = array();
|
||||
$xmlItems = $xml->items;
|
||||
foreach($xmlItems as $xmlItem) {
|
||||
$xmlItem = $xmlItem->item;
|
||||
$items[] = array(
|
||||
'productId' => (string)$xmlItem->external_id,
|
||||
'productId' => (string)$xmlItem['id'],
|
||||
'productName' => (string)$xmlItem->name,
|
||||
'quantity' => (string)$xmlItem->quantity,
|
||||
'initialPrice' => (string)$xmlItem->price
|
||||
|
10
run.php
10
run.php
@ -2,11 +2,13 @@
|
||||
require(__DIR__ . "/classes/ApiHelper.php");
|
||||
require(__DIR__ . "/config/config.php");
|
||||
|
||||
if (! file_exists(__DIR__ . "/logs"))
|
||||
if (! file_exists(__DIR__ . "/logs")) {
|
||||
mkdir(__DIR__ . "/logs");
|
||||
}
|
||||
|
||||
if (file_exists(__DIR__ . "/logs/cookie.txt"))
|
||||
if (file_exists(__DIR__ . "/logs/cookie.txt")) {
|
||||
unlink(__DIR__ . "/logs/cookie.txt");
|
||||
}
|
||||
|
||||
$lockFile = __DIR__ . "/run.lock";
|
||||
|
||||
@ -25,5 +27,7 @@ if (file_exists(__DIR__ . "/logs/sync.log")) {
|
||||
|
||||
$apiHelper = new ApiHelper($сonfig);
|
||||
|
||||
if ($apiHelper->processXMLOrders())
|
||||
if ($apiHelper->processXMLOrders()) {
|
||||
unlink($lockFile);
|
||||
file_put_contents(__DIR__ . "/logs/sync.log", date('Y-m-d H:i:s'));
|
||||
}
|
Loading…
Reference in New Issue
Block a user