diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 2f499d19..5d0a9294 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -120,9 +120,10 @@ class ICrmOrderActions if($api->getStatusCode() != 460) // some orders were sent return false; // in pack mode return errors - } - COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId); + if($lastOrderId) + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId); + } return true; // end of pack } @@ -147,7 +148,7 @@ class ICrmOrderActions return true; //all ok! } - + /** * * w+ event in bitrix log @@ -193,6 +194,9 @@ class ICrmOrderActions $rsUser = CUser::GetByID($arFields['USER_ID']); $arUser = $rsUser->Fetch(); + + $createdAt = new \DateTime($arUser['DATE_REGISTER']); + $createdAt = $createdAt->format('Y-m-d H:i:s'); // push customer (for crm) $firstName = self::toJSON($arUser['NAME']); @@ -211,16 +215,13 @@ class ICrmOrderActions ); $phones[] = $phoneWork; - $createdAt = new \DateTime($arUser['DATE_REGISTER']); - $createdAt = $createdAt->format('Y-m-d H:i:s'); - $result = self::clearArr(array( 'externalId' => $arFields['USER_ID'], 'lastName' => $lastName, 'firstName' => $firstName, 'patronymic' => $patronymic, 'phones' => $phones, - 'createdAt' => $createdAt, + 'createdAt' => $createdAt )); $customer = $api->customerEdit($result); @@ -255,7 +256,7 @@ class ICrmOrderActions break; case 'ADDRESS': $resOrderDeliveryAddress['text'] = self::toJSON($ar['VALUE']); break; - case 'FIO': $resOrder['contactName'] = self::toJSON($ar['VALUE']); + case 'FIO': $resOrder['contactName'] = explode(" ", self::toJSON($ar['VALUE'])); break; case 'PHONE': $resOrder['phone'] = $ar['VALUE']; break; @@ -291,8 +292,13 @@ class ICrmOrderActions if($arFields['CANCELED'] == 'Y') $arFields['STATUS_ID'] = $arFields['CANCELED']; + $createdAt = new \DateTime($arFields['DATE_INSERT']); + $createdAt = $createdAt->format('Y-m-d H:i:s'); + $resOrder = self::clearArr(array( - 'contactName' => $resOrder['contactName'], + 'firstName' => $resOrder['contactName'][0], + 'lastName' => $resOrder['contactName'][1], + 'patronymic' => $resOrder['contactName'][2], 'phone' => $resOrder['phone'], 'email' => $resOrder['email'], 'deliveryCost' => $arFields['PRICE_DELIVERY'], @@ -306,7 +312,7 @@ class ICrmOrderActions 'deliveryType' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId], 'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']], 'statusComment' => $arFields['REASON_CANCELED'], - 'createdAt' => $arFields['DATE_INSERT'], + 'createdAt' => $createdAt, 'deliveryAddress' => $resOrderDeliveryAddress, 'items' => $items )); diff --git a/intaro.intarocrm/classes/general/RestApi.php b/intaro.intarocrm/classes/general/RestApi.php index 7e2711cb..c8f85181 100644 --- a/intaro.intarocrm/classes/general/RestApi.php +++ b/intaro.intarocrm/classes/general/RestApi.php @@ -6,7 +6,8 @@ class RestApi protected $apiUrl; protected $apiKey; protected $apiVersion = '1'; - protected $lastError; + + protected $response; protected $statusCode; protected $parameters; @@ -30,16 +31,23 @@ class RestApi /* Получение кода статуса и сообщения об ошибке */ public function getLastError() { - if (!is_null($this->lastError)) - return $this->statusCode . ' ' . $this->lastError; + if (isset($this->response['errorMsg']) && isset($this->response['errors'])) + { + $result = $this->statusCode . ' ' . $this->response['errorMsg']; + foreach ($this->response['errors'] as $error) + $result .= ' ' . $error; + } + elseif (isset($this->response['errorMsg'])) + $result = $this->statusCode . ' ' . $this->response['errorMsg']; else - return null; + $result = null; + return $result; } /* Псообщения об ошибке */ public function getLastErrorMessage() { - return $this->lastError; + return $this->response['errorMsg']; } @@ -94,7 +102,7 @@ class RestApi } /** - * Загрузка нескольких заказов + * Пакетная загрузка заказов * * @param array $orders - массив заказов * @return array @@ -106,6 +114,8 @@ class RestApi $url = $this->apiUrl.'orders/upload'; $result = $this->curlRequest($url, 'POST'); + if (is_null($result) && isset($result['uploadedOrders'])) + return $result['uploadedOrders']; return $result; } @@ -154,10 +164,8 @@ class RestApi public function orderHistory($startDate = null, $endDate = null, $limit = 100, $offset = 0) { $url = $this->apiUrl.'orders/history'; - if($startDate) - $this->parameters['startDate'] = $startDate; - if($endDate) - $this->parameters['endDate'] = $endDate; + $this->parameters['startDate'] = $startDate; + $this->parameters['endDate'] = $endDate; $this->parameters['limit'] = $limit; $this->parameters['offset'] = $offset; @@ -215,6 +223,26 @@ class RestApi return $result; } + /** + * Пакетная загрузка клиентов + * + * @param array $customers - массив клиентов + * @return array + */ + public function customerUpload($customers) + { + $dataJson = json_encode($customers); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $this->parameters['customers'] = $dataJson; + + $url = $this->apiUrl.'customers/upload'; + $result = $this->curlRequest($url, 'POST'); + if (is_null($result) && isset($result['uploaded'])) + return $result['uploaded']; + return $result; + } + /** * Удаление клиента * @@ -414,9 +442,18 @@ class RestApi $result = $this->curlRequest($url); return $result; } - - - + + /** + * Обновление статистики + * + * @return array - статус вып обновления + */ + public function statisticUpdate() + { + $url = $this->apiUrl.'statistic/update'; + $result = $this->curlRequest($url); + return $result; + } protected function curlRequest($url, $method = 'GET', $format = 'json') { @@ -444,22 +481,21 @@ class RestApi if (curl_errno($ch)) { - $this->lastError = 'Curl error: ' . curl_error($ch); + $this->response = array('errorMsg' => 'Curl error: ' . curl_error($ch)); return null; } curl_close($ch); $result = (array)json_decode($response, true); + $this->response = $result; if ($result['success'] == false) - { - $this->lastError = $result['errorMsg']; return null; - } - $this->lastError = null; unset($result['success']); if (count($result) == 0) return true; return reset($result); } -} \ No newline at end of file +} + +?> \ No newline at end of file diff --git a/intaro.intarocrm/install/export/intarocrm_setup.php b/intaro.intarocrm/install/export/intarocrm_setup.php index 10919622..6679189a 100755 --- a/intaro.intarocrm/install/export/intarocrm_setup.php +++ b/intaro.intarocrm/install/export/intarocrm_setup.php @@ -150,8 +150,8 @@ if ($STEP==1)