From dc2ead629fdbf08f9e09679f3ae1b342210bb3e3 Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Tue, 23 Jul 2013 17:02:37 +0400 Subject: [PATCH] v 0.3.5; improved ICrmOrderActions; + added step3 during install --- .../classes/general/ICrmOrderActions.php | 134 ++++++------ intaro.intarocrm/classes/general/RestApi.php | 6 +- intaro.intarocrm/install/index.php | 65 +++++- intaro.intarocrm/install/step2.php | 3 +- intaro.intarocrm/install/step3.php | 191 +++++++++++++++++- intaro.intarocrm/install/step4.php | 10 + intaro.intarocrm/install/version.php | 6 +- intaro.intarocrm/lang/ru/install/step3.php | 12 +- intaro.intarocrm/options.php | 8 +- 9 files changed, 354 insertions(+), 81 deletions(-) create mode 100644 intaro.intarocrm/install/step4.php diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index c2a737c8..0c7245b1 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -40,7 +40,7 @@ class ICrmOrderActions * Mass order uploading, without repeating; always returns true, but writes error log * @return boolean */ - public static function uploadOrders() { + public static function uploadOrders($steps = false, $pSize = 500) { //COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test @@ -61,13 +61,13 @@ class ICrmOrderActions self::eventLog('ICrmOrderActions::uploadOrders', 'catalog', 'module not found'); return true; } - + $resOrders = array(); - - $dbOrder = CSaleOrder::GetList(); $lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); - $lastUpOrderIdNew = 0; + $lastOrderId = 0; + + $dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId)); $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); @@ -89,38 +89,81 @@ class ICrmOrderActions 'optionsPayment' => $optionsPayment ); - while ($arOrder = $dbOrder->GetNext()) { - if ($arOrder['ID'] <= $lastUpOrderId) //old orders not to upload - break; - - if(!$lastUpOrderIdNew) - $lastUpOrderIdNew = $arOrder['ID']; - - $order = self::orderCreate($arOrder['ID'], $api, $arParams); - - if(!$order) - continue; - - $resOrders[] = $order; - } - - if(!empty($resOrders)) { - $orders = $api->orderUpload($resOrders); - - // error pushing orders - if (!$orders) { - //handle err - self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); - return true; + // pack mode enable / disable + // can send data evry 500 rows + if (!$steps) { + while ($arOrder = $dbOrder->GetNext()) { //here orders by id asc; with offset + + $order = self::orderCreate($arOrder['ID'], $api, $arParams); + + if (!$order) + continue; + + $resOrders[] = $order; + + $lastOrderId = $arOrder['ID']; } + + if (!empty($resOrders)) { + $orders = $api->orderUpload($resOrders); + + // error pushing orders + if (!$orders) { + //handle err + self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + return true; + } + } + } else { // package mode (by default runs after install) + $orderCount = 0; + + while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc + + $order = self::orderCreate($arOrder['ID'], $api, $arParams); + + if (!$order) + continue; + + $orderCount++; + + $resOrders[] = $order; + + $lastOrderId = $arOrder['ID']; + + if($orderCount >= $pSize) { + $orders = $api->orderUpload($resOrders); + + // error pushing orders + if (!$orders) { + //handle err + self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + return false; // in pack mode return errors + } + + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId); + + return true; // end of pack + } + } + + if (!empty($resOrders)) { + $orders = $api->orderUpload($resOrders); + + // error pushing orders + if (!$orders) { + //handle err + self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + return false; // in pack mode return errors + } + } } - if($lastUpOrderIdNew) - COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastUpOrderIdNew); + if($lastOrderId) + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId); return true; //all ok! } - + /** * * w+ event in bitrix log @@ -198,39 +241,12 @@ class ICrmOrderActions ); $phones[] = $phoneWork; - $addressPersonal = array( - 'index' => self::toJSON($arUser['PERSONAL_ZIP']), - 'country' => self::toJSON(GetCountryByID($arUser['PERSONAL_COUNTRY'])), - 'city' => self::toJSON($arUser['PERSONAL_CITY']), - 'street' => self::toJSON($arUser['PERSONAL_STREET']), - 'building' => self::toJSON($arUser['UF_PERSONAL_BUILDING']), - 'flat' => self::toJSON($arUser['UF_PERSONAL_FLAT']), - 'notes' => self::toJSON($arUser['PERSONAL_NOTES']), - 'text' => self::toJSON($arUser['UF_PERSONAL_TEXT']), - 'type' => 'home' - ); - $addresses[] = $addressPersonal; - - $addressWork = array( - 'index' => self::toJSON($arUser['WORK_ZIP']), - 'country' => self::toJSON(GetCountryByID($arUser['WORK_COUNTRY'])), - 'city' => self::toJSON($arUser['WORK_CITY']), - 'street' => self::toJSON($arUser['WORK_STREET']), - 'building' => self::toJSON($arUser['UF_WORK_BUILDING']), // -- - 'flat' => self::toJSON($arUser['UF_WORK_FLAT']), - 'notes' => self::toJSON($arUser['PERSONAL_NOTES']), - 'text' => self::toJSON($arUser['UF_WORK_TEXT']), - 'type' => 'work' - ); - $addresses[] = $addressWork; - $result = array( 'externalId' => $arFields['USER_ID'], 'lastName' => $lastName, 'firstName' => $firstName, 'patronymic' => $patronymic, - 'phones' => $phones, - 'addresses' => $addresses + 'phones' => $phones ); $customer = $api->customerEdit($result); diff --git a/intaro.intarocrm/classes/general/RestApi.php b/intaro.intarocrm/classes/general/RestApi.php index 3d5d1023..7e2711cb 100644 --- a/intaro.intarocrm/classes/general/RestApi.php +++ b/intaro.intarocrm/classes/general/RestApi.php @@ -154,8 +154,10 @@ class RestApi public function orderHistory($startDate = null, $endDate = null, $limit = 100, $offset = 0) { $url = $this->apiUrl.'orders/history'; - $this->parameters['startDate'] = $startDate; - $this->parameters['endDate'] = $endDate; + if($startDate) + $this->parameters['startDate'] = $startDate; + if($endDate) + $this->parameters['endDate'] = $endDate; $this->parameters['limit'] = $limit; $this->parameters['offset'] = $offset; diff --git a/intaro.intarocrm/install/index.php b/intaro.intarocrm/install/index.php index b049cf2d..c09bfc81 100755 --- a/intaro.intarocrm/install/index.php +++ b/intaro.intarocrm/install/index.php @@ -164,7 +164,7 @@ class intaro_intarocrm extends CModule "ACTIVE" => "Y" ) ); - + if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) { do { $arResult['bitrixPaymentTypesList'][] = $arPaymentTypesList; @@ -189,6 +189,11 @@ class intaro_intarocrm extends CModule } while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()); } + $arResult['bitrixPaymentStatusesList'][] = array( + 'ID' => 'Y', + 'NAME' => 'Отменен' + ); + $APPLICATION->IncludeAdminFile( GetMessage('MODULE_INSTALL_TITLE'), $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step2.php' @@ -198,6 +203,31 @@ class intaro_intarocrm extends CModule if(!CModule::IncludeModule("sale")) { //handler } + + if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') + && isset($_POST['ajax']) && ($_POST['ajax'] == 1)) { + include($this->INSTALL_PATH . '/../classes/general/ICrmOrderActions.php'); + + ICrmOrderActions::uploadOrders(true); // each 500 + + $lastUpOrderId = COption::GetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0); + $countLeft = (int) CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId), array()); + $countAll = (int) CSaleOrder::GetList(array("ID" => "ASC"), array(), array()); + + if(!isset($_POST['finish'])) + $finish = 0; + else + $finish = (int) $_POST['finish']; + + $percent = 100 - $countLeft * 100 / $countAll; + + if(!$countLeft) + $finish = 1; + + $APPLICATION->RestartBuffer(); + header('Content-Type: application/x-javascript; charset='.LANG_CHARSET); + die(json_encode(array("finish" => $finish, "percent" => $percent))); + } if (isset($_POST['back']) && $_POST['back']) { $APPLICATION->IncludeAdminFile( @@ -282,7 +312,8 @@ class intaro_intarocrm extends CModule ); //form payment statuses ids arr - $paymentStatusesArr = array(); + $paymentStatusesArr['Y'] = htmlspecialchars(trim($_POST['payment-status-Y'])); + if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) { do { $paymentStatusesArr[$arPaymentStatusesList['ID']] = htmlspecialchars(trim($_POST['payment-status-' . $arPaymentStatusesList['ID']])); @@ -300,11 +331,37 @@ class intaro_intarocrm extends CModule COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES, serialize($paymentStatusesArr)); COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT, serialize($paymentArr)); COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0); + /*RegisterModule($this->MODULE_ID); + + //agent + $dateAgent = new DateTime(); + $intAgent = new DateInterval('PT600S'); // PT60S - 60 sec; 600 - 600 sec + $dateAgent->add($intAgent); + + CAgent::AddAgent( + "ICrmOrderActions::uploadOrdersAgent();", + $this->MODULE_ID, + "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 + ); + + $this->CopyFiles(); */ + + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php' + ); + } else if ($step == 4) { + RegisterModule($this->MODULE_ID); //agent $dateAgent = new DateTime(); - $intAgent = new DateInterval('PT60S'); + $intAgent = new DateInterval('PT60S'); // PT60S - 60 sec; $dateAgent->add($intAgent); CAgent::AddAgent( @@ -322,7 +379,7 @@ class intaro_intarocrm extends CModule $APPLICATION->IncludeAdminFile( GetMessage('MODULE_INSTALL_TITLE'), - $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php' + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step4.php' ); } } diff --git a/intaro.intarocrm/install/step2.php b/intaro.intarocrm/install/step2.php index 77f99dd0..9fc1efd6 100755 --- a/intaro.intarocrm/install/step2.php +++ b/intaro.intarocrm/install/step2.php @@ -26,7 +26,8 @@ $defaultPayTypes = array ( $defaultPayStatuses = array ( 'N' => 'in-processing', 'P' => 'approval', - 'F' => 'complete' + 'F' => 'complete', + 'Y' => 'cancel-other' ); $defaultPayment = array( diff --git a/intaro.intarocrm/install/step3.php b/intaro.intarocrm/install/step3.php index 931fdfcc..78bcafe1 100755 --- a/intaro.intarocrm/install/step3.php +++ b/intaro.intarocrm/install/step3.php @@ -1,10 +1,185 @@ +if (!check_bitrix_sessid()) + return; +IncludeModuleLangFile(__FILE__); +?> -
- - - - "> - + + + + + + + + + + + + +
+ + + + + + +
+
+
+
+
+
+
0%
+
+ 0% +
+
+
+
+
+
+ " class="adm-btn-save"> +
+
+ " class="adm-btn-save"> +
+
+
+
\ No newline at end of file diff --git a/intaro.intarocrm/install/step4.php b/intaro.intarocrm/install/step4.php new file mode 100644 index 00000000..931fdfcc --- /dev/null +++ b/intaro.intarocrm/install/step4.php @@ -0,0 +1,10 @@ + + +
+ + + + "> + diff --git a/intaro.intarocrm/install/version.php b/intaro.intarocrm/install/version.php index 9323a3bd..167ad49c 100755 --- a/intaro.intarocrm/install/version.php +++ b/intaro.intarocrm/install/version.php @@ -1,5 +1,5 @@ '0.4.4', - 'VERSION_DATE' => '2013-07-16 14:41:00', -); \ No newline at end of file + 'VERSION' => '0.3.5', + 'VERSION_DATE' => '2013-07-23 17:01:00', +); diff --git a/intaro.intarocrm/lang/ru/install/step3.php b/intaro.intarocrm/lang/ru/install/step3.php index ae657628..5273f2c9 100755 --- a/intaro.intarocrm/lang/ru/install/step3.php +++ b/intaro.intarocrm/lang/ru/install/step3.php @@ -1,4 +1,12 @@ \ No newline at end of file diff --git a/intaro.intarocrm/options.php b/intaro.intarocrm/options.php index 965260cf..1cf55881 100755 --- a/intaro.intarocrm/options.php +++ b/intaro.intarocrm/options.php @@ -119,13 +119,13 @@ if (isset($_POST['Update']) && $_POST['Update']=='Y') { ); //form payment statuses ids arr - $paymentStatusesArr = array(); + $paymentStatusesArr['Y'] = htmlspecialchars(trim($_POST['payment-status-Y'])); if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) { do { $paymentStatusesArr[$arPaymentStatusesList['ID']] = htmlspecialchars(trim($_POST['payment-status-' . $arPaymentStatusesList['ID']])); } while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()); } - + //form payment ids arr $paymentArr = array(); $paymentArr['Y'] = htmlspecialchars(trim($_POST['payment-Y'])); @@ -226,6 +226,10 @@ if (isset($_POST['Update']) && $_POST['Update']=='Y') { $arResult['bitrixPaymentStatusesList'][] = $arPaymentStatusesList; } while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()); } + $arResult['bitrixPaymentStatusesList'][] = array( + 'ID' => 'Y', + 'NAME' => 'Отменен' + ); //bitrix pyament Y/N $arResult['bitrixPaymentList'][0]['NAME'] = GetMessage('PAYMENT_Y');