diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index c0370b2b..004d859f 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -20,6 +20,7 @@ class ICrmOrderActions /** * Mass order uploading, without repeating; always returns true, but writes error log + * @param $pSize * @param $failed -- flag to export failed orders * @return boolean */ @@ -362,19 +363,71 @@ class ICrmOrderActions $dateStart = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY_DATE, null); + if(!$dateStart) { + $dateStart = new \DateTime(); + $dateStart = $dateStart->format('Y-m-d H:i:s'); + } + $orderHistory = $api->orderHistory($dateStart); - if($dateStart) - $dateStart = new \DateTime($dateStart); + $dateStart = new \DateTime($dateStart); // pushing existing orders foreach ($orderHistory as $order) { - if(!isset($order['externalId']) && !$order['externalId']) { + if(!isset($order['externalId']) || !$order['externalId']) { // we dont need new orders without any customers (can check only for externalId) - if(!isset($order['customer']['externalId']) && !$order['customer']['externalId']) + if(!isset($order['customer']['externalId']) && !$order['customer']['externalId']) { + if (!$order['customer']['email']) { + $login = 'user_' . (microtime(true) * 100); + $server_name = 0 < strlen(SITE_SERVER_NAME)? + SITE_SERVER_NAME : 'server.com'; + $order['customer']['email'] = $login . '@' . $server_name; + $registerNewUser = true; + } else { + // if email already used + $dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $order['email'])); + if ($dbUser->SelectedRowsCount() == 0) { + $login = $order['customer']['email']; + $registerNewUser = true; + } elseif ($dbUser->SelectedRowsCount() == 1) { + $arUser = $dbUser->Fetch(); + $registeredUserID = $arUser['ID']; + } else { + $login = 'user_' . (microtime(true) * 100); + $registerNewUser = true; + } + } + if($registerNewUser) { + $useCaptcha = COption::GetOptionString('main', 'captcha_registration', 'N'); + if ($useCaptcha == 'Y') + COption::SetOptionString('main', 'captcha_registration', 'N'); + $userPassword = randString(10); + $newUser = $USER->Register($login, $order['customer']['firstName'], $order['customer']['lastName'], + $userPassword, $userPassword, $order['customer']['email']); + if ($useCaptcha == 'Y') + COption::SetOptionString('main', 'captcha_registration', 'Y'); + if ($newUser['TYPE'] == 'ERROR') { + self::eventLog('ICrmOrderActions::orderHistory', 'CUser::Register', $newUser['MESSAGE']); + continue; + } else { + $registeredUserID = $USER->GetID(); + $USER->Logout(); + } + } + + $order['customer']['externalId'] = $registeredUserID; + } + + $api->customerFixExternalIds(array(array('id' => $order['customer']['id'], 'externalId' => $order['customer']['externalId']))); + + if ($api->getStatusCode() != 200) { + //handle err - write log & continue + self::eventLog('ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds', $api->getLastError()); continue; + } + // new order $newOrderFields = array( @@ -446,7 +499,7 @@ class ICrmOrderActions break; } - if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']] > 4)) { + if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']]) > 4) { switch ($ar['CODE']) { /* case $optionsOrderProps[$arFields['PERSON_TYPE_ID']]['country']: $resOrderDeliveryAddress['country'] = self::toJSON($ar['VALUE']); break; @@ -515,7 +568,7 @@ class ICrmOrderActions if (isset($order['deliveryAddress']['text'])) self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['text'], self::fromJSON($order['deliveryAddress']['text']), $order['externalId']); - if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']] > 4)) { + if (count($optionsOrderProps[$arFields['PERSON_TYPE_ID']]) > 4) { if (isset($order['deliveryAddress']['street'])) self::addOrderProperty($optionsOrderProps[$arFields['PERSON_TYPE_ID']]['street'], self::fromJSON($order['deliveryAddress']['street']), $order['externalId']); @@ -710,6 +763,16 @@ class ICrmOrderActions if($arFields['CANCELED'] == 'Y') $wasCanaceled = true; + $resultDeliveryTypeId = $optionsDelivTypes[$order['deliveryType']]; + + if(isset($order['deliveryService']) && !empty($order['deliveryService'])) { + if (strpos($order['deliveryService']['code'], "-") !== false) + $deliveryServiceCode = explode("-", $order['deliveryService']['code'], 2); + + if ($deliveryServiceCode) + $resultDeliveryTypeId = $resultDeliveryTypeId . ':' . $deliveryServiceCode[1]; + } + // orderUpdate $arFields = self::clearArr(array( 'PRICE_DELIVERY' => $order['deliveryCost'], @@ -719,7 +782,7 @@ class ICrmOrderActions 'PAY_SYSTEM_ID' => $optionsPayTypes[$order['paymentType']], //'PAYED' => $optionsPayment[$order['paymentStatus']], //'PERSON_TYPE_ID' => $optionsOrderTypes[$order['orderType']], - 'DELIVERY_ID' => $optionsDelivTypes[$order['deliveryType']], + 'DELIVERY_ID' => $resultDeliveryTypeId, 'STATUS_ID' => $optionsPayStatuses[$order['status']], 'REASON_CANCELED' => $order['statusComment'], 'USER_DESCRIPTION' => $order['customerComment'], @@ -807,10 +870,10 @@ class ICrmOrderActions * * creates order or returns array of order and customer for mass upload * - * @param type $orderId - * @param type $api - * @param type $arParams - * @param type $send + * @param array $arFields + * @param $api + * @param $arParams + * @param $send * @return boolean * @return array - array('order' = $order, 'customer' => $customer) */ @@ -884,6 +947,22 @@ class ICrmOrderActions else $resultDeliveryTypeId = $arFields['DELIVERY_ID']; + // deliveryService + $deliveryService = array(); + if(count($arId) > 1) { + $dbDeliveryType = CSaleDeliveryHandler::GetBySID($arId[0]); + + if ($arDeliveryType = $dbDeliveryType->GetNext()) { + foreach($arDeliveryType['PROFILES'] as $id => $profile) { + if($id == $arId[1]) { + $deliveryService = array( + 'code' => $arId[0] . '-' . $id, + 'name' => $profile['TITLE'] + ); + } + } + } + } $resOrder = array(); $resOrderDeliveryAddress = array(); @@ -993,6 +1072,7 @@ class ICrmOrderActions 'paymentStatus' => $arParams['optionsPayment'][$arFields['PAYED']], 'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']], 'deliveryType' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId], + 'deliveryService' => $deliveryService, 'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']], 'statusComment' => $arFields['REASON_CANCELED'], 'customerComment' => $arFields['USER_DESCRIPTION'], @@ -1032,8 +1112,8 @@ class ICrmOrderActions * removes all empty fields from arrays * working with nested arrs * - * @param type $arr - * @return boolean + * @param array $arr + * @return array */ public static function clearArr($arr) { if(!$arr || !is_array($arr)) @@ -1052,9 +1132,9 @@ class ICrmOrderActions /** * - * @global type $APPLICATION - * @param type $str in SITE_CHARSET - * @return type $str in utf-8 + * @global $APPLICATION + * @param $str in SITE_CHARSET + * @return $str in utf-8 */ public static function toJSON($str) { global $APPLICATION; @@ -1064,9 +1144,9 @@ class ICrmOrderActions /** * - * @global type $APPLICATION - * @param type $str in utf-8 - * @return type $str in SITE_CHARSET + * @global $APPLICATION + * @param $str in utf-8 + * @return $str in SITE_CHARSET */ public static function fromJSON($str) { global $APPLICATION; diff --git a/intaro.intarocrm/classes/general/RestApi.php b/intaro.intarocrm/classes/general/RestApi.php index f4207900..3200b2db 100644 --- a/intaro.intarocrm/classes/general/RestApi.php +++ b/intaro.intarocrm/classes/general/RestApi.php @@ -122,12 +122,12 @@ class RestApi /** * Обновление externalId у заказов с переданными id * - * @param array $orders- массив, содержащий id и externalId заказа + * @param array $orders - массив, содержащий id и externalId заказа * @return array */ - public function orderFixExternalIds($order) + public function orderFixExternalIds($orders) { - $dataJson = json_encode($order); + $dataJson = json_encode($orders); $this->parameters['orders'] = $dataJson; $url = $this->apiUrl.'orders/fix-external-ids'; @@ -241,6 +241,22 @@ class RestApi return $result; } + /** + * Обновление externalId у клиентов с переданными id + * + * @param array $customers- массив, содержащий id и externalId заказа + * @return array + */ + public function customerFixExternalIds($customers) + { + $dataJson = json_encode($customers); + $this->parameters['customers'] = $dataJson; + + $url = $this->apiUrl.'customers/fix-external-ids'; + $result = $this->curlRequest($url, 'POST'); + return $result; + } + /** * Удаление клиента * @@ -313,6 +329,34 @@ class RestApi return $result; } + /** + * Получение списка служб доставки + * + * @return array - массив типов доставки + */ + public function deliveryServicesList() + { + $url = $this->apiUrl.'reference/delivery-services'; + $result = $this->curlRequest($url); + return $result; + } + + /** + * Редактирование службы доставки + * + * @param array $deliveryService - информация о типе доставки + * @return array + */ + public function deliveryServiceEdit($deliveryService) + { + $dataJson = json_encode($deliveryService); + $this->parameters['deliveryService'] = $dataJson; + + $url = $this->apiUrl.'reference/delivery-services/'.$deliveryService['code'].'/edit'; + $result = $this->curlRequest($url, 'POST'); + return $result; + } + /** * Получение списка типов оплаты diff --git a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php index 0ebb4e3b..d2bc424f 100644 --- a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php +++ b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php @@ -75,7 +75,9 @@ class ICrmOrderEvent { /** * writeDataOnOrderCreate via api * - * @param integer $ID - Order Id + * @param $ID - Order Id + * @param array $arFields + * @return boolean */ function writeDataOnOrderCreate($ID, $arFields) { diff --git a/intaro.intarocrm/install/index.php b/intaro.intarocrm/install/index.php index 150bd522..2163f70f 100755 --- a/intaro.intarocrm/install/index.php +++ b/intaro.intarocrm/install/index.php @@ -26,6 +26,7 @@ class intaro_intarocrm extends CModule { var $CRM_API_KEY_OPTION = 'api_key'; var $CRM_ORDER_TYPES_ARR = 'order_types_arr'; var $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr'; + var $CRM_DELIVERY_SERVICES_ARR = 'deliv_services_arr'; var $CRM_PAYMENT_TYPES = 'pay_types_arr'; var $CRM_PAYMENT_STATUSES = 'pay_statuses_arr'; var $CRM_PAYMENT = 'payment_arr'; //order payment Y/N @@ -204,6 +205,7 @@ class intaro_intarocrm extends CModule { } $arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList(); + $arResult['deliveryServicesList'] = $this->INTARO_CRM_API->deliveryServicesList(); $arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList(); $arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses $arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList(); @@ -247,6 +249,25 @@ class intaro_intarocrm extends CModule { } while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()); } + //bitrix deliveryServicesList + $dbDeliveryServicesList = CSaleDeliveryHandler::GetList( + array( + 'SORT' => 'ASC', + 'NAME' => 'ASC' + ), + array( + 'ACTIVE' => 'Y' + ) + ); + + //form delivery services ids arr + if ($arDeliveryServicesList = $dbDeliveryServicesList->Fetch()) { + do { + //auto delivery types + $deliveryTypesArr[$arDeliveryServicesList['SID']] = htmlspecialchars(trim($_POST['delivery-type-' . $arDeliveryServicesList['SID']])); + } while ($arDeliveryServicesList = $dbDeliveryServicesList->Fetch()); + } + //bitrix paymentTypesList $dbPaymentTypesList = CSalePaySystem::GetList( array( @@ -425,6 +446,8 @@ class intaro_intarocrm extends CModule { $input['order-type-' . $bitrixOrderType['ID']] .= ''; } + + $APPLICATION->RestartBuffer(); header('Content-Type: application/x-javascript; charset=' . LANG_CHARSET); die(json_encode(array("success" => true, "result" => $input))); @@ -476,10 +499,12 @@ class intaro_intarocrm extends CModule { //prepare crm lists $arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList(); $arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList(); + $arResult['deliveryServicesList'] = $this->INTARO_CRM_API->deliveryServicesList(); $arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList(); $arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses $arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList(); $arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups + //bitrix orderTypesList -- personTypes $dbOrderTypesList = CSalePersonType::GetList( array( @@ -512,6 +537,23 @@ class intaro_intarocrm extends CModule { } while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()); } + // bitrix deliveryServicesList + $dbDeliveryServicesList = CSaleDeliveryHandler::GetList( + array( + 'SORT' => 'ASC', + 'NAME' => 'ASC' + ), + array( + 'ACTIVE' => 'Y' + ) + ); + + if ($arDeliveryServicesList = $dbDeliveryServicesList->Fetch()) { + do { + $arResult['bitrixDeliveryTypesList'][] = array('ID' => $arDeliveryServicesList['SID'], 'NAME' => $arDeliveryServicesList['NAME']); + } while ($arDeliveryServicesList = $dbDeliveryServicesList->Fetch()); + } + //bitrix paymentTypesList $dbPaymentTypesList = CSalePaySystem::GetList( array( @@ -601,7 +643,18 @@ class intaro_intarocrm extends CModule { ), false, false, array() ); - //form delivery types ids arr + //bitrix deliveryServicesList + $dbDeliveryServicesList = CSaleDeliveryHandler::GetList( + array( + 'SORT' => 'ASC', + 'NAME' => 'ASC' + ), + array( + 'ACTIVE' => 'Y' + ) + ); + + //form delivery types / services ids arr $deliveryTypesArr = array(); if (htmlspecialchars(trim($_POST['delivery-types-export'])) == 'false') { @@ -610,8 +663,16 @@ class intaro_intarocrm extends CModule { $deliveryTypesArr[$arDeliveryTypesList['ID']] = htmlspecialchars(trim($_POST['delivery-type-' . $arDeliveryTypesList['ID']])); } while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()); } + + if ($arDeliveryServicesList = $dbDeliveryServicesList->Fetch()) { + do { + //auto delivery types + $deliveryTypesArr[$arDeliveryServicesList['SID']] = htmlspecialchars(trim($_POST['delivery-type-' . $arDeliveryServicesList['SID']])); + + } while ($arDeliveryServicesList = $dbDeliveryServicesList->Fetch()); + } } elseif (htmlspecialchars(trim($_POST['delivery-types-export'])) == 'true') { - // send to intaro crm and save + // send to intaro crm and save delivery types! if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) { do { // parse id @@ -636,7 +697,7 @@ class intaro_intarocrm extends CModule { 'paymentTypes' => '' ))); - // error pushing customer + // error pushing dt if ($this->INTARO_CRM_API->getStatusCode() != 200) { if ($this->INTARO_CRM_API->getStatusCode() != 201) { //handle err @@ -645,6 +706,49 @@ class intaro_intarocrm extends CModule { } } while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()); } + + if ($arDeliveryServicesList = $dbDeliveryServicesList->Fetch()) { + do { + + $deliveryTypesArr[$arDeliveryServicesList['SID']] = $arDeliveryServicesList['SID']; + + // send to crm + $this->INTARO_CRM_API->deliveryTypeEdit(ICrmOrderActions::clearArr(array( + 'code' => $arDeliveryServicesList['SID'], + 'name' => ICrmOrderActions::toJSON($arDeliveryServicesList['NAME']), + 'defaultCost' => 0, + 'description' => ICrmOrderActions::toJSON($arDeliveryTypesList['DESCRIPTION']), + 'paymentTypes' => '' + ))); + + // error pushing dt + if ($this->INTARO_CRM_API->getStatusCode() != 200) { + if ($this->INTARO_CRM_API->getStatusCode() != 201) { + //handle err + ICrmOrderActions::eventLog('install/index.php', 'IntaroCrm\RestApi::deliveryTypeEdit', $this->INTARO_CRM_API->getLastError()); + } + } + + foreach($arDeliveryServicesList['PROFILES'] as $id => $profile) { + + // send to crm + $this->INTARO_CRM_API->deliveryServiceEdit(ICrmOrderActions::clearArr(array( + 'code' => $arDeliveryServicesList['SID'] . '-' . $id, + 'name' => ICrmOrderActions::toJSON($profile['TITLE']), + 'deliveryType' => $arDeliveryServicesList['SID'] + ))); + + // error pushing dt + if ($this->INTARO_CRM_API->getStatusCode() != 200) { + if ($this->INTARO_CRM_API->getStatusCode() != 201) { + //handle err + ICrmOrderActions::eventLog('install/index.php', 'IntaroCrm\RestApi::deliveryServiceEdit', $this->INTARO_CRM_API->getLastError()); + } + } + } + + } while ($arDeliveryServicesList = $dbDeliveryServicesList->Fetch()); + } } //bitrix paymentTypesList @@ -704,7 +808,6 @@ class intaro_intarocrm extends CModule { COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0); COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE, 1); COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS, serialize(array())); - COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_HISTORY_DATE, date('Y-m-d H:i:s')); $APPLICATION->IncludeAdminFile( GetMessage('MODULE_INSTALL_TITLE'), @@ -715,6 +818,13 @@ class intaro_intarocrm extends CModule { //handler } + if (isset($_POST['back']) && $_POST['back']) { + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step2.php' + ); + } + if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') && isset($_POST['ajax']) && ($_POST['ajax'] == 1)) { ICrmOrderActions::uploadOrders(); // each 50 @@ -1049,6 +1159,9 @@ class intaro_intarocrm extends CModule { $this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key); $this->INTARO_CRM_API->statisticUpdate(); + // in fin order + COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_HISTORY_DATE, date('Y-m-d H:i:s')); + $APPLICATION->IncludeAdminFile( GetMessage('MODULE_INSTALL_TITLE'), $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step6.php' diff --git a/intaro.intarocrm/install/step2.php b/intaro.intarocrm/install/step2.php index 1e8e78a7..c3a83e72 100755 --- a/intaro.intarocrm/install/step2.php +++ b/intaro.intarocrm/install/step2.php @@ -91,14 +91,13 @@ $defaultPayment = array( }); return false; - }); - + }); + $('input[name="delivery-types-export"]').click(function() { if($(this).val() === 'true') $('tr.delivery-types').hide('slow'); else if($(this).val() === 'false') - $('tr.delivery-types').show('show'); - + $('tr.delivery-types').show('slow'); }); }); @@ -142,7 +141,7 @@ $defaultPayment = array(
+