1
0
mirror of synced 2024-11-22 04:56:05 +03:00

Merge pull request #1 from gwinn/master

Комментарии клиента и менеджера, запись даты последнего заказа в лог
This commit is contained in:
Alex Lushpai 2015-11-16 10:40:03 +03:00
commit f46e80c88d
3 changed files with 89 additions and 41 deletions

View File

@ -48,67 +48,89 @@ class ApiHelper {
$orders = $this->filterOrders($orders); $orders = $this->filterOrders($orders);
if ($this->uploadOrders($orders)) { $this->uploadOrders($orders);
$a = sizeof($orders);
echo "uploaded $a orders" . PHP_EOL;
return true;
} else {
echo "upload failed" . PHP_EOL;
return false;
}
} }
protected function uploadOrders($orders) { protected function uploadOrders($orders) {
if ($orders === false)
return false;
if (sizeof($orders)) { if (sizeof($orders)) {
$orders = array_chunk($orders, $this->config['retailcrm_order_chunk_size']); $orders = array_chunk($orders, $this->config['retailcrm_order_chunk_size']);
foreach ($orders as $chunk) { foreach ($orders as $chunk) {
try { try {
$result = $this->crmClient->ordersUpload($chunk); $result = $this->crmClient->ordersUpload($chunk);
if (!$result->isSuccessful()) {
$this->writeLog('ordersUpload: ' . $result['errorMsg'] . (isset($result['errors']) ? ': ' . json_encode($result['errors']) : ''));
}
time_nanosleep(0, 200000000);
} catch (\RetailCrm\Exception\CurlException $e) { } catch (\RetailCrm\Exception\CurlException $e) {
$this->writeLog( $this->writeLog('ordersUpload: ' . $e, 'error');
'\Retailcrm\ApiClient::ordersUpload: ' . $e,
'error'
);
return false;
} }
if (! $result->isSuccessful()) {
$this->writeLog(
'\Retailcrm\ApiClient::ordersUpload: ' . $result['errorMsg'] . (isset($result['errors']) ? ': ' . json_encode($result['errors']) : '')
);
}
time_nanosleep(0, 200000000);
} }
return true;
} else {
return false;
} }
return true;
} }
protected function checkCustomers($order) { protected function checkCustomers($order) {
$customerId = false; $customerId = false;
if ($order['email'] != '') $filter['email']= $order['email']; $filter = array();
if ($order['phone'] != '') $filter['name'] = $order['phone'];
if (isset($filter)) { if (!empty($order['email'])) $filter['email']= $order['email'];
if (!empty($order['phone'])) $filter['name'] = $order['phone'];
if (!empty($filter)) {
try { try {
$customers = $this->crmClient->customersList($filter); $customers = $this->crmClient->customersList($filter);
} catch (\RetailCrm\Exception\CurlException $e) { } catch (\RetailCrm\Exception\CurlException $e) {
$this->writeLog( $this->writeLog('customersList: ' . $e, 'error');
'\Retailcrm\ApiClient::customersList: ' . $e,
'error'
);
return false;
} }
if (!empty($customers['customers'])) { if (!empty($customers['customers'])) {
foreach ($customers as $_customer) { foreach ($customers['customers'] as $_customer) {
if (!empty($_customer['externalId'])) { if (!empty($_customer['externalId'])) {
$customerId = $_customer['externalId']; $customerId = $_customer['externalId'];
break; 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; return $customerId;
@ -129,6 +151,7 @@ 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['date_from'])) { 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['date_from']) { if ($order['createdAt'] < $this->config['date_from']) {

View File

@ -17,6 +17,7 @@ class Parser {
case 'id': case 'id':
$val = (string)$val; $val = (string)$val;
$order['number'] = $this->config['order_prefix'] . $val; $order['number'] = $this->config['order_prefix'] . $val;
$order['externalId'] = $val;
break; break;
case 'state': case 'state':
$orderStatuses = array_flip($this->config['order_statuses']); $orderStatuses = array_flip($this->config['order_statuses']);
@ -32,22 +33,42 @@ class Parser {
$order = array_merge($order, array( $order = array_merge($order, array(
'email' => (string)$xml->email, 'email' => (string)$xml->email,
'phone' => (string)$xml->phone, 'phone' => (string)$xml->phone,
'orderMethod' => $this->config['order_method'],
'createdAt' => $createdAt, 'createdAt' => $createdAt,
'paymentType' => $this->config['payment'][(string)$xml->paymentType],
'delivery' => array( 'delivery' => array(
'address' => 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(); $items = array();
$xmlItems = $xml->items; $xmlItems = $xml->items;
foreach($xmlItems as $xmlItem) { foreach($xmlItems as $xmlItem) {
$xmlItem = $xmlItem->item; $xmlItem = $xmlItem->item;
$items[] = array( $items[] = array(
'productId' => (string)$xmlItem->external_id, 'productId' => (string)$xmlItem['id'],
'productName' => (string)$xmlItem->name, 'productName' => (string)$xmlItem->name,
'quantity' => (string)$xmlItem->quantity, 'quantity' => (string)$xmlItem->quantity,
'initialPrice' => (string)$xmlItem->price 'initialPrice' => (string)$xmlItem->price

10
run.php
View File

@ -2,11 +2,13 @@
require(__DIR__ . "/classes/ApiHelper.php"); require(__DIR__ . "/classes/ApiHelper.php");
require(__DIR__ . "/config/config.php"); require(__DIR__ . "/config/config.php");
if (! file_exists(__DIR__ . "/logs")) if (! file_exists(__DIR__ . "/logs")) {
mkdir(__DIR__ . "/logs"); mkdir(__DIR__ . "/logs");
}
if (file_exists(__DIR__ . "/logs/cookie.txt")) if (file_exists(__DIR__ . "/logs/cookie.txt")) {
unlink(__DIR__ . "/logs/cookie.txt"); unlink(__DIR__ . "/logs/cookie.txt");
}
$lockFile = __DIR__ . "/run.lock"; $lockFile = __DIR__ . "/run.lock";
@ -25,5 +27,7 @@ if (file_exists(__DIR__ . "/logs/sync.log")) {
$apiHelper = new ApiHelper($сonfig); $apiHelper = new ApiHelper($сonfig);
if ($apiHelper->processXMLOrders()) if ($apiHelper->processXMLOrders()) {
unlink($lockFile); unlink($lockFile);
file_put_contents(__DIR__ . "/logs/sync.log", date('Y-m-d H:i:s'));
}