diff --git a/intaro.crm/classes/general/ICrmApi.php b/intaro.crm/classes/general/ICrmApi.php new file mode 100644 index 00000000..c6d67794 --- /dev/null +++ b/intaro.crm/classes/general/ICrmApi.php @@ -0,0 +1,435 @@ +apiUrl = $crmUrl.'/api/v'.$this->apiVersion.'/'; + $this->apiKey = $apiKey; + } + + + public function getStatusCode() + { + return $this->statusCode; + } + + /* Получение кода статуса и сообщения об ошибке */ + public function getLastError() + { + if (!is_null($this->lastError)) + return $this->statusCode . ' ' . $this->lastError; + else + return null; + } + + /* Псообщения об ошибке */ + public function getLastErrorMessage() + { + return $this->lastError; + } + + + /* Методы для работы с заказами */ + /** + * Получение заказа по id + * + * @param string $id - идентификатор заказа + * @return array - информация о заказе + */ + public function orderGet($id) + { + $url = $this->apiUrl.'orders/'.$id; + $result = $this->curlRequest($url); + return $result; + } + + /** + * Создание заказа + * + * @param array $order- информация о заказе + * @return array + */ + public function orderCreate($order) + { + $dataJson = json_encode($order); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $parameters = array(); + $parameters['order'] = $dataJson; + + $url = $this->apiUrl.'orders/create'; + $result = $this->curlRequest($url, $parameters, 'POST'); + return $result; + } + + /** + * Изменение заказа + * + * @param array $order- информация о заказе + * @return array + */ + public function orderEdit($order) + { + $dataJson = json_encode($order); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $parameters = array(); + $parameters['order'] = $dataJson; + + $url = $this->apiUrl.'orders/'.$order['id'].'/edit'; + $result = $this->curlRequest($url, $parameters, 'POST'); + return $result; + } + + /** + * Загрузка нескольких заказов + * + * @param array $orders - массив заказов + * @return array + */ + public function orderUpload($orders) + { + $dataJson = json_encode($orders); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $parameters = array(); + $parameters['orders'] = $dataJson; + + $url = $this->apiUrl.'orders/upload'; + $result = $this->curlRequest($url, $parameters, 'POST'); + return $result; + } + + /** + * Удаление заказа + * + * @param string $id - идентификатор заказа + * @return array + */ + public function orderDelete($id) + { + $url = $this->apiUrl.'orders/'.$id.'/delete'; + $result = $this->curlRequest($url, array(), 'POST'); + return $result; + } + + /** + * Получение последних измененных заказов + * + * @param DateTime $startDate - начальная дата выборки + * @param DateTime $endDate - конечная дата + * @param int $limit - ограничение на размер выборки + * @param int $offset - сдвиг + * @return array - массив заказов + */ + public function orderHistory($startDate = null, $endDate = null, $limit = 100, $offset = 0) + { + $url = $this->apiUrl.'orders/history'; + $parameters = array(); + $parameters['startDate'] = $startDate; + $parameters['endDate'] = $endDate; + $parameters['limit'] = $limit; + $parameters['offset'] = $offset; + + $result = $this->curlRequest($url, $parameters); + return $result; + } + + + /* Методы для работы с клиентами */ + /** + * Получение клиента по id + * + * @param string $id - идентификатор + * @return array - информация о клиенте + */ + public function customerGet($id) + { + $url = $this->apiUrl.'customers/'.$id; + $result = $this->curlRequest($url); + return $result; + } + + /** + * Создание клиента + * + * @param array $customer - информация о клиенте + * @return array + */ + public function customerCreate($customer) + { + $dataJson = json_encode($customer); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $parameters = array(); + $parameters['customer'] = $dataJson; + + $url = $this->apiUrl.'customers/create'; + $result = $this->curlRequest($url, $parameters, 'POST'); + return $result; + } + + /** + * Редактирование клиента + * + * @param array $customer - информация о клиенте + * @return array + */ + public function customerEdit($customer) + { + $dataJson = json_encode($customer); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $parameters = array(); + $parameters['customer'] = $dataJson; + + $url = $this->apiUrl.'customers/'.$customer['id'].'/edit'; + $result = $this->curlRequest($url, $parameters, 'POST'); + return $result; + } + + /** + * Удаление клиента + * + * @param string $id - идентификатор + * @return array + */ + public function customerDelete($id) + { + $url = $this->apiUrl.'customers/'.$id.'/delete'; + $result = $this->curlRequest($url, array(), 'POST'); + return $result; + } + + /** + * Получение списка заказов клиента + * + * @param string $id - идентификатор клиента + * @param DateTime $startDate - начальная дата выборки + * @param DateTime $endDate - конечная дата + * @param int $limit - ограничение на размер выборки + * @param int $offset - сдвиг + * @return array - массив заказов + */ + public function customerOrdersList($id, $startDate = null, $endDate = null, + $limit = 100, $offset = 0) + { + $url = $this->apiUrl.'customers/'.$id.'/orders'; + $parameters = array(); + $parameters['startDate'] = $startDate; + $parameters['endDate'] = $endDate; + $parameters['limit'] = $limit; + $parameters['offset'] = $offset; + + $result = $this->curlRequest($url, $parameters); + return $result; + } + + /* Методы для работы со справочниками */ + /** + * Получение списка типов доставки + * + * @return array - массив типов доставки + */ + public function deliveryTypesList() + { + $url = $this->apiUrl.'reference/delivery-types'; + $result = $this->curlRequest($url); + return $result; + } + + /** + * Редактирование типа доставки + * + * @param array $deliveryType - информация о типе доставки + * @return array + */ + public function deliveryTypeEdit($deliveryType) + { + $dataJson = json_encode($deliveryType); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $parameters = array(); + $parameters['deliveryType'] = $dataJson; + + $url = $this->apiUrl.'delivery-types/'.$deliveryType['code'].'/edit'; + $result = $this->curlRequest($url, $parameters, 'POST'); + return $result; + } + + /** + * Получение списка типов оплаты + * + * @return array - массив типов оплаты + */ + public function paymentTypesList() + { + $url = $this->apiUrl.'reference/payment-types'; + $result = $this->curlRequest($url); + return $result; + } + + /** + * Редактирование типа оплаты + * + * @param array $paymentType - информация о типе оплаты + * @return array + */ + public function paymentTypesEdit($paymentType) + { + $dataJson = json_encode($paymentType); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $parameters = array(); + $parameters['paymentType'] = $dataJson; + + $url = $this->apiUrl.'payment-types/'.$paymentType['code'].'/edit'; + $result = $this->curlRequest($url, $parameters, 'POST'); + return $result; + } + + + /** + * Получение списка статусов оплаты + * + * @return array - массив статусов оплаты + */ + public function paymentStatusesList() + { + $url = $this->apiUrl.'reference/payment-statuses'; + $result = $this->curlRequest($url); + return $result; + } + + /** + * Редактирование статуса оплаты + * + * @param array $paymentStatus - информация о статусе оплаты + * @return array + */ + public function paymentStatusesEdit($paymentStatus) + { + $dataJson = json_encode($paymentStatus); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $parameters = array(); + $parameters['paymentStatus'] = $dataJson; + + $url = $this->apiUrl.'payment-statuses/'.$paymentStatus['code'].'/edit'; + $result = $this->curlRequest($url, $parameters, 'POST'); + return $result; + } + + /** + * Получение списка типов заказа + * + * @return array - массив типов заказа + */ + public function orderTypesList() + { + $url = $this->apiUrl.'reference/order-types'; + $result = $this->curlRequest($url); + return $result; + } + + /** + * Редактирование типа заказа + * + * @param array $paymentType - информация о типе заказа + * @return array + */ + public function orderTypesEdit($orderType) + { + $dataJson = json_encode($orderType); + $dataJson = str_replace(self::$jsonReplaceSource, self::$jsonReplaceTarget, + $dataJson); + $parameters = array(); + $parameters['orderType'] = $dataJson; + + $url = $this->apiUrl.'order-types/'.$paymentType['code'].'/edit'; + $result = $this->curlRequest($url, $parameters, 'POST'); + return $result; + } + + protected function curlRequest($url, $parameters = null, $method = 'GET', $format = 'json') + { + $parameters['apiKey'] = $this->apiKey; + + if ($method == 'GET' && !is_null($parameters)) + $url .= '?'.http_build_query($parameters); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_FAILONERROR, FALSE); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable + curl_setopt($ch, CURLOPT_TIMEOUT, 6); // times out after 6s + + if ($method == 'POST') + { + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); + } + + $response = curl_exec($ch); + $this->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if (curl_errno($ch)) + { + $this->lastError = 'Curl error: ' . curl_error($ch); + return null; + } + curl_close($ch); + + $result = (array)json_decode($response, true); + 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); + } +} + +?> diff --git a/intaro.crm/include.php b/intaro.crm/include.php new file mode 100644 index 00000000..03117c82 --- /dev/null +++ b/intaro.crm/include.php @@ -0,0 +1,15 @@ + 'classes/general/IntaroCrmRestApi.php', + 'ICrmApi' => 'classes/general/ICrmApi.php' + ) +); + +?> diff --git a/intaro.crm/install/index.php b/intaro.crm/install/index.php new file mode 100644 index 00000000..e0f8eec8 --- /dev/null +++ b/intaro.crm/install/index.php @@ -0,0 +1,269 @@ +INSTALL_PATH = $path; + include($path."/version.php"); + $this->MODULE_VERSION = $arModuleVersion["VERSION"]; + $this->MODULE_VERSION_DATE = $arModuleVersion["VERSION_DATE"]; + $this->MODULE_NAME = GetMessage('MODULE_NAME'); + $this->MODULE_DESCRIPTION = GetMessage('MODULE_DESCRIPTION'); + $this->PARTNER_NAME = GetMessage('MODULE_PARTNER_NAME'); + $this->PARTNER_URI = GetMessage('MODULE_PARTNER_URI'); + } + + /** + * Functions DoInstall and DoUninstall are + * All other functions are optional + */ + + function DoInstall() + { + global $APPLICATION, $step, $arResult; + + include($this->INSTALL_PATH . '/../classes/general/IntaroCrmRestApi.php'); + + $step = intval($_REQUEST['step']); + + if ($step <= 1) { + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php' + ); + } else if ($step == 2) { + if(!CModule::IncludeModule("sale")) { + //handler + } + + $api_host = htmlspecialchars(trim($_POST[$this->CRM_API_HOST_OPTION])); + $api_key = htmlspecialchars(trim($_POST[$this->CRM_API_KEY_OPTION])); + + if(!$api_host || !$api_key) { + $arResult['errCode'] = 'ERR_FIELDS_API_HOST'; + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php' + ); + return; + } + + $this->INTARO_CRM_API = new IntaroCrmRestApi($api_host, $api_key); + + $this->INTARO_CRM_API->paymentStatusesList(); + + //check connection & apiKey valid + if((int) $this->INTARO_CRM_API->getStatusCode() != 200) { + $arResult['errCode'] = 'ERR_' . $this->INTARO_CRM_API->getStatusCode(); + + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php' + ); + + return; + } + + COption::SetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, $api_host); + COption::SetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, $api_key); + + //prepare crm lists + //$orderTypes = $this->INTARO_CRM_API->orderTypesList(); -- no such method + $arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList(); + $arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList(); + $arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); + + //bitrix orderTypesList + /* + * ...some code here... + */ + + //bitrix deliveryTypesList + $dbDeliveryTypesList = CSaleDelivery::GetList( + array( + "SORT" => "ASC", + "NAME" => "ASC" + ), + array( + "ACTIVE" => "Y", + ), + false, + false, + array() + ); + + if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) { + do { + $arResult['bitrixDeliveryTypesList'][] = $arDeliveryTypesList; + } while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()); + } + + //bitrix paymentTypesList + $dbPaymentTypesList = CSalePaySystem::GetList( + array( + "SORT" => "ASC", + "NAME" => "ASC" + ), + array( + "ACTIVE" => "Y" + ) + ); + + if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) { + do { + $arResult['bitrixPaymentTypesList'][] = $arPaymentTypesList; + } while ($arPaymentTypesList = $dbPaymentTypesList->Fetch()); + } + + //bitrix paymentStatusesList + $dbPaymentStatusesList = CSaleStatus::GetList( + array( + "SORT" => "ASC", + "NAME" => "ASC" + ), + array( + "LID" => "ru", //ru + "ACTIVE" => "Y" + ) + ); + + if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) { + do { + $arResult['bitrixPaymentStatusesList'][] = $arPaymentStatusesList; + } while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()); + } + + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step2.php' + ); + + } else if ($step == 3) { + if(!CModule::IncludeModule("sale")) { + //handler + } + + //bitrix deliveryTypesList + $dbDeliveryTypesList = CSaleDelivery::GetList( + array( + "SORT" => "ASC", + "NAME" => "ASC" + ), + array( + "ACTIVE" => "Y", + ), + false, + false, + array() + ); + + //form delivery types ids arr + $deliveryTypesArr = array(); + if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) { + do { + $deliveryTypesArr[$arDeliveryTypesList['ID']] = $_POST['delivery-type-' . $arDeliveryTypesList['ID']]; + } while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()); + } + + //bitrix paymentTypesList + $dbPaymentTypesList = CSalePaySystem::GetList( + array( + "SORT" => "ASC", + "NAME" => "ASC" + ), + array( + "ACTIVE" => "Y" + ) + ); + + //form payment types ids arr + $paymentTypesArr = array(); + if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) { + do { + $paymentTypesArr[$arPaymentTypesList['ID']] = $_POST['payment-type-' . $arPaymentTypesList['ID']]; + } while ($arPaymentTypesList = $dbPaymentTypesList->Fetch()); + } + + //bitrix paymentStatusesList + $dbPaymentStatusesList = CSaleStatus::GetList( + array( + "SORT" => "ASC", + "NAME" => "ASC" + ), + array( + "LID" => "ru", //ru + "ACTIVE" => "Y" + ) + ); + + //form payment statuses ids arr + $paymentStatusesArr = array(); + if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) { + do { + $paymentStatusesArr[$arPaymentStatusesList['ID']] = $_POST['payment-status-' . $arPaymentStatusesList['ID']]; + } while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()); + } + + COption::SetOptionString($this->MODULE_ID, $this->CRM_DELIVERY_TYPES_ARR, serialize($deliveryTypesArr)); + COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_TYPES, serialize($paymentTypesArr)); + COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES, serialize($paymentStatusesArr)); + RegisterModule($this->MODULE_ID); + + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php' + ); + + } + } + + function DoUninstall() { + global $APPLICATION; + + UnRegisterModule($this->MODULE_ID); + + COption::RemoveOption($this->MODULE_ID, $this->CRM_API_HOST_OPTION); + COption::RemoveOption($this->MODULE_ID, $this->CRM_API_KEY_OPTION); + COption::RemoveOption($this->MODULE_ID, $this->CRM_DELIVERY_TYPES_ARR); + COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT_TYPES); + COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES); + + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_UNINSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/unstep1.php' + ); + } +} +?> diff --git a/intaro.crm/install/step1.php b/intaro.crm/install/step1.php new file mode 100644 index 00000000..0282d2f8 --- /dev/null +++ b/intaro.crm/install/step1.php @@ -0,0 +1,31 @@ + + + + +