diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index d19090b2..8afcb964 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -255,6 +255,7 @@ class ICrmOrderActions $resOrder = array(); $resOrderDeliveryAddress = array(); + $contactNameArr = array(); $rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID'])); while ($ar = $rsOrderProps->Fetch()) { @@ -270,7 +271,7 @@ class ICrmOrderActions $resOrderDeliveryAddress['city'] = self::toJSON($resOrderDeliveryAddress['city']['CITY_NAME_LANG']); } break; - case 'FIO': $resOrder['contactName'] = explode(" ", self::toJSON($ar['VALUE'])); + case 'FIO': $contactNameArr = self::explodeFIO($ar['VALUE']); break; case 'PHONE': $resOrder['phone'] = $ar['VALUE']; break; @@ -309,10 +310,7 @@ class ICrmOrderActions $createdAt = new \DateTime($arFields['DATE_INSERT']); $createdAt = $createdAt->format('Y-m-d H:i:s'); - $resOrder = self::clearArr(array( - 'lastName' => $resOrder['contactName'][0], - 'firstName' => $resOrder['contactName'][1], - 'patronymic' => $resOrder['contactName'][2], + $resOrder = array( 'phone' => $resOrder['phone'], 'email' => $resOrder['email'], 'deliveryCost' => $arFields['PRICE_DELIVERY'], @@ -325,7 +323,7 @@ class ICrmOrderActions 'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']], 'deliveryType' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId], 'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']], - 'statusComment' => $arFields['REASON_CANCELED'], + 'statusComment' => $arFields['USER_DESCRIPTION'], 'createdAt' => $createdAt, 'deliveryAddress' => $resOrderDeliveryAddress, 'items' => $items @@ -335,8 +333,19 @@ class ICrmOrderActions && in_array($arFields['LID'], $arParams['optionsSites'])) $resOrder['site'] = $arFields['LID']; + // parse fio + if(count($contactNameArr) == 1) { + $resOrder['firstName'] = $contactNameArr[0]; + } else { + $resOrder['lastName'] = $contactNameArr[0]; + $resOrder['firstName'] = $contactNameArr[1]; + $resOrder['patronymic'] = $contactNameArr[2]; + } + + $resOrder = self::clearArr($resOrder); + if($send) - $api->createOrder($resOrder); + return $api->orderEdit($resOrder); return array( 'order' => $resOrder, @@ -389,4 +398,21 @@ class ICrmOrderActions return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET); } + + public static function explodeFIO($str) { + if(!$str) + return array(); + + $array = explode(" ", self::toJSON($str), 3); + $newArray = array(); + + foreach($array as $ar) { + if(!$ar) + continue; + + $newArray[] = $ar; + } + + return $newArray; + } } \ No newline at end of file diff --git a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php new file mode 100644 index 00000000..7b7e8178 --- /dev/null +++ b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php @@ -0,0 +1,198 @@ + $optionsOrderTypes, + 'optionsDelivTypes' => $optionsDelivTypes, + 'optionsPayTypes' => $optionsPayTypes, + 'optionsPayStatuses' => $optionsPayStatuses, + 'optionsPayment' => $optionsPayment + ); + + $arOrder = CSaleOrder::GetById($ID); + $result = ICrmOrderActions::orderCreate($arOrder, $api, $arParams, true); + + if(!$result) { + ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'ICrmOrderActions::orderCreate', 'error during creating order'); + return true; + } + + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $ID); + + return true; + } + + /** + * + * @param type $ID -- orderId + * @param type $cancel -- Y / N - cancel order status + * @param type $reason -- cancel reason + * @return boolean + */ + function onSaleCancelOrder($ID, $cancel, $reason) { + if(!$ID || !$cancel || ($cancel != 'Y')) + return true; + + if (!CModule::IncludeModule('iblock')) { + //handle err + ICrmOrderActions::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'iblock', 'module not found'); + return true; + } + + if (!CModule::IncludeModule("sale")) { + //handle err + ICrmOrderActions::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'sale', 'module not found'); + return true; + } + + if (!CModule::IncludeModule("catalog")) { + //handle err + ICrmOrderActions::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'catalog', 'module not found'); + return true; + } + + $api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0); + $api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0); + + //saved cat params + $optionsPayStatuses = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_STATUSES, 0)); // --statuses + + $api = new IntaroCrm\RestApi($api_host, $api_key); + + $order = array( + 'externalId' => (int) $ID, + 'status' => $optionsPayStatuses[$cancel], + 'statusComment' => ICrmOrderActions::toJSON($reason) + ); + + $api->orderEdit($order); + + // error pushing order + if ($api->getStatusCode() != 201) + ICrmOrderActions::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'IntaroCrm\RestApi::orderEdit', $api->getLastError()); + + return true; + } + + /** + * + * @param type $ID -- orderId + * @param type $payed -- Y / N - pay order status + * @return boolean + */ + function onSalePayOrder($ID, $payed) { + if(!$ID || !$payed || ($payed != 'Y')) + return true; + + if (!CModule::IncludeModule('iblock')) { + //handle err + ICrmOrderActions::eventLog('ICrmOrderEvent::onSalePayOrder', 'iblock', 'module not found'); + return true; + } + + if (!CModule::IncludeModule("sale")) { + //handle err + ICrmOrderActions::eventLog('ICrmOrderEvent::onSalePayOrder', 'sale', 'module not found'); + return true; + } + + if (!CModule::IncludeModule("catalog")) { + //handle err + ICrmOrderActions::eventLog('ICrmOrderEvent::onSalePayOrder', 'catalog', 'module not found'); + return true; + } + + $api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0); + $api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0); + + //saved cat params + $optionsPayment = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0)); + + $api = new IntaroCrm\RestApi($api_host, $api_key); + + $order = array( + 'externalId' => (int) $ID, + 'paymentStatus' => $optionsPayment[$payed] + ); + + $api->orderEdit($order); + + // error pushing order + if ($api->getStatusCode() != 201) + ICrmOrderActions::eventLog('ICrmOrderEvent::onSalePayOrder', 'IntaroCrm\RestApi::orderEdit', $api->getLastError()); + + return true; + } +} \ No newline at end of file diff --git a/intaro.intarocrm/export/export_setup.php b/intaro.intarocrm/export/export_setup.php new file mode 100644 index 00000000..1fafdeee --- /dev/null +++ b/intaro.intarocrm/export/export_setup.php @@ -0,0 +1,295 @@ + 1) +{ + + + if (count($IBLOCK_EXPORT) < count($IBLOCK_PROPERTY_ARTICLE)) + $arSetupErrors[] = GetMessage("ERROR_ARTICLE_NOT_SET"); + + if (strlen($SETUP_FILE_NAME)<=0) + { + $arSetupErrors[] = GetMessage("CET_ERROR_NO_FILENAME"); + } + elseif ($APPLICATION->GetFileAccessPermission($SETUP_FILE_NAME) < "W") + { + $arSetupErrors[] = str_replace("#FILE#", $SETUP_FILE_NAME, GetMessage('CET_YAND_RUN_ERR_SETUP_FILE_ACCESS_DENIED')); + } + + if (($ACTION=="EXPORT_SETUP" || $ACTION == 'EXPORT_EDIT' || $ACTION == 'EXPORT_COPY') && strlen($SETUP_PROFILE_NAME)<=0) + { + $arSetupErrors[] = GetMessage("CET_ERROR_NO_PROFILE_NAME"); + } + + if (!empty($arSetupErrors)) + { + $STEP = 1; + } +} + +if (!empty($arSetupErrors)) + echo ShowError(implode('
', $arSetupErrors)); + + +if ($STEP==1) +{ + + +?> +
+ +

+ "ASC", "NAME"=>"ASC"),array('CHECK_PERMISSIONS' => 'Y','MIN_PERMISSION' => 'W')); + while ($res = $db_res->Fetch()) + { + if ($arCatalog = CCatalog::GetByIDExt($res["ID"])) + { + if($arCatalog['CATALOG_TYPE'] == "D" || $arCatalog['CATALOG_TYPE'] == "X" || $arCatalog['CATALOG_TYPE'] == "P") + { + $arSiteList = array(); + $rsSites = CIBlock::GetSite($res["ID"]); + while ($arSite = $rsSites->Fetch()) + { + $arSiteList[] = $arSite["SITE_ID"]; + } + $db_properties = CIBlock::GetProperties($res['ID'], Array()); + + $properties = Array(); + while($prop = $db_properties->Fetch()) + $properties[] = $prop; + + if (count($IBLOCK_EXPORT) != 0) + $boolExport = (in_array($res['ID'], $IBLOCK_EXPORT)); + else + $boolExport = true; + + $arIBlockList[] = array( + 'ID' => $res['ID'], + 'NAME' => $res['NAME'], + 'IBLOCK_TYPE_ID' => $res['IBLOCK_TYPE_ID'], + 'IBLOCK_EXPORT' => $boolExport, + 'PROPERTIES' => $properties, + 'OLD_PROPERTY_SELECT' => $IBLOCK_PROPERTY_ARTICLE[$res['ID']] != "" ? $IBLOCK_PROPERTY_ARTICLE[$res['ID']] : null, + 'SITE_LIST' => '('.implode(' ',$arSiteList).')', + ); + + if ($boolExport) + $intCountChecked++; + $intCountAvailIBlock++; + } + } + } + if (count($IBLOCK_EXPORT) != 0) { + if ($intCountChecked == $intCountAvailIBlock) + $boolAll = true; + } else { + $intCountChecked = $intCountAvailIBlock; + $boolAll = true; + } + + + ?> + + + + + + + + + + + + + + + + + + $arIBlock) + { + ?> + + + + + + + +
+
+
+
+   +
+
+
+
+ + + > + + +   +
+ + + + ]" + id="IBLOCK_EXPORT" + value="" + + onclick="checkOne(this,);" + > + + + +
+ +
+
+
+ +

+ + +
+
+
+ + + +

+ +
+
+
+ + + + + + + + + + + + "> + + + + "> + + +
+ + \ No newline at end of file diff --git a/intaro.intarocrm/install/index.php b/intaro.intarocrm/install/index.php index 57f8633f..f1da9c13 100755 --- a/intaro.intarocrm/install/index.php +++ b/intaro.intarocrm/install/index.php @@ -72,6 +72,13 @@ class intaro_intarocrm extends CModule } } + if (!date_default_timezone_get()) { + if (!ini_get('date.timezone')) { + $APPLICATION->ThrowException(GetMessage("DATE_TIMEZONE_ERR")); + return false; + } + } + include($this->INSTALL_PATH . '/../classes/general/RestApi.php'); include($this->INSTALL_PATH . '/../classes/general/ICrmOrderActions.php'); include($this->INSTALL_PATH . '/../classes/general/ICMLLoader.php'); diff --git a/intaro.intarocrm/install/version.php b/intaro.intarocrm/install/version.php index 8dc479e8..2d43932f 100755 --- a/intaro.intarocrm/install/version.php +++ b/intaro.intarocrm/install/version.php @@ -1,5 +1,6 @@ "0.4.0", - "VERSION_DATE" => "2013-09-02 15:46:00", -); \ No newline at end of file + "VERSION_DATE" => "2013-09-12 17:00:00", +); + diff --git a/intaro.intarocrm/lang/ru/options.php b/intaro.intarocrm/lang/ru/options.php index 0fa2a864..d3b623ff 100755 --- a/intaro.intarocrm/lang/ru/options.php +++ b/intaro.intarocrm/lang/ru/options.php @@ -23,4 +23,9 @@ $MESS ['ERR_403'] = 'Неверный apiKey.'; $MESS ['ERR_0'] = 'Превышено время ожидания ответа от сервера.'; $MESS ['ICRM_OPTIONS_OK'] = 'Изменения успешно сохранены.'; $MESS ['CANCELED'] = 'Флаг «Отменен»'; -$MESS ['INFO_1'] = ' Задайте соответствие между справочниками 1C-Битрикс и справочниками IntaroCRM.'; \ No newline at end of file +$MESS ['INFO_1'] = ' Задайте соответствие между справочниками 1C-Битрикс и справочниками IntaroCRM.'; + +$MESS ['ICRM_OPTIONS_ORDER_DISCHARGE_TAB'] = 'Режим выгрузки заказов'; +$MESS ['ORDER_DISCH'] = 'Режим выгрузки заказов'; +$MESS ['DISCHARGE_AGENT'] = 'Выгрузка заказов с помощью агента'; +$MESS ['DISCHARGE_EVENTS'] = 'Выгрузка заказов по событию'; diff --git a/intaro.intarocrm/options.php b/intaro.intarocrm/options.php index b95bb9ae..c5966dde 100755 --- a/intaro.intarocrm/options.php +++ b/intaro.intarocrm/options.php @@ -12,6 +12,7 @@ $CRM_PAYMENT_STATUSES = 'pay_statuses_arr'; $CRM_PAYMENT = 'payment_arr'; //order payment Y/N $CRM_ORDER_LAST_ID = 'order_last_id'; $CRM_ORDER_SITES = 'sites_ids'; +$CRM_ORDER_DISCHARGE = 'order_discharge'; if(!CModule::IncludeModule('intaro.intarocrm') || !CModule::IncludeModule('sale')) @@ -139,12 +140,43 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { $paymentArr['Y'] = htmlspecialchars(trim($_POST['payment-Y'])); $paymentArr['N'] = htmlspecialchars(trim($_POST['payment-N'])); + $previousDischarge = COption::GetOptionString($mid, $CRM_ORDER_DISCHARGE, 0); + //order discharge mode + // 0 - agent + // 1 - event + $orderDischarge = 0; + $orderDischarge = (int) htmlspecialchars(trim($_POST['order-discharge'])); + + if (($orderDischarge != $previousDischarge) && ($orderDischarge == 0)) { + // remove depenedencies + UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail"); + UnRegisterModuleDependences("sale", "OnOrderUpdate", $mid, "ICrmOrderEvent", "onUpdateOrder"); + // new agent + $dateAgent = new DateTime(); + $intAgent = new DateInterval('PT60S'); // PT60S - 60 sec; + $dateAgent->add($intAgent); + CAgent::AddAgent( + "ICrmOrderActions::uploadOrdersAgent();", $mid, "N", 600, // interval - 10 mins + $dateAgent->format('d.m.Y H:i:s'), // date of first check + "Y", // агент активен + $dateAgent->format('d.m.Y H:i:s'), // date of first start + 30 + ); + } else if (($orderDischarge != $previousDischarge) && ($orderDischarge == 1)) { + // remove agent + CAgent::RemoveAgent("ICrmOrderActions::uploadOrdersAgent();", $mid); + // event dependencies + RegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail"); + RegisterModuleDependences("sale", "OnOrderUpdate", $mid, "ICrmOrderEvent", "onUpdateOrder"); + } + COption::SetOptionString($mid, $CRM_ORDER_TYPES_ARR, serialize($orderTypesArr)); COption::SetOptionString($mid, $CRM_DELIVERY_TYPES_ARR, serialize($deliveryTypesArr)); COption::SetOptionString($mid, $CRM_PAYMENT_TYPES, serialize($paymentTypesArr)); COption::SetOptionString($mid, $CRM_PAYMENT_STATUSES, serialize($paymentStatusesArr)); COption::SetOptionString($mid, $CRM_PAYMENT, serialize($paymentArr)); COption::SetOptionString($mid, $CRM_ORDER_SITES, serialize($orderSites)); + COption::SetOptionString($mid, $CRM_ORDER_DISCHARGE, $orderDischarge); $uri .= '&ok=Y'; LocalRedirect($uri); @@ -263,6 +295,7 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { $optionsPayStatuses = unserialize(COption::GetOptionString($mid, $CRM_PAYMENT_STATUSES, 0)); // --statuses $optionsPayment = unserialize(COption::GetOptionString($mid, $CRM_PAYMENT, 0)); $optionsSites = unserialize(COption::GetOptionString($mid, $CRM_ORDER_SITES, 0)); + $optionsDischarge = COption::GetOptionString($mid, $CRM_ORDER_DISCHARGE, 0); $aTabs = array( array( @@ -277,6 +310,12 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { "ICON" => '', "TITLE" => GetMessage('ICRM_OPTIONS_CATALOG_CAPTION') ), + array( + "DIV" => "edit4", + "TAB" => GetMessage('ICRM_OPTIONS_ORDER_DISCHARGE_TAB'), + "ICON" => '', + "TITLE" => GetMessage('ICRM_OPTIONS_ORDER_DISCHARGE_CAPTION') + ) ); $tabControl = new CAdminTabControl("tabControl", $aTabs); $tabControl->Begin(); @@ -298,7 +337,7 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { - + BeginNextTab(); ?> @@ -418,6 +457,18 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { BeginNextTab(); ?> + + + + + + + + + + + + Buttons(); ?>