From 0e5c9b5b6c39eb5676abdb75650173b9ae56b519 Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Fri, 20 Sep 2013 19:12:01 +0400 Subject: [PATCH] fixes due to upload event & upload agent --- .../classes/general/ICrmOrderActions.php | 235 +++++++++++++----- .../classes/general/events/ICrmOrderEvent.php | 46 +++- intaro.intarocrm/install/index.php | 5 +- intaro.intarocrm/options.php | 18 +- 4 files changed, 211 insertions(+), 93 deletions(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 6f237ecc..ce126a01 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -13,12 +13,14 @@ class ICrmOrderActions protected static $CRM_ORDER_LAST_ID = 'order_last_id'; protected static $CRM_ORDER_SITES = 'sites_ids'; protected static $CRM_ORDER_PROPS = 'order_props'; + protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids'; /** * Mass order uploading, without repeating; always returns true, but writes error log + * @param $failed -- flag to export failed orders * @return boolean */ - public static function uploadOrders($pSize = 50) { + public static function uploadOrders($pSize = 50, $failed = false) { //COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test @@ -42,11 +44,16 @@ class ICrmOrderActions $resOrders = array(); $resCustomers = array(); - + $lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); $lastOrderId = 0; + $failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0)); + if (!$failedIds) + $failedIds = array(); + $dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId)); + $dbFailedOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('ID' => $failedIds)); $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); @@ -63,50 +70,80 @@ class ICrmOrderActions $api = new IntaroCrm\RestApi($api_host, $api_key); $arParams = array( - 'optionsOrderTypes' => $optionsOrderTypes, - 'optionsDelivTypes' => $optionsDelivTypes, - 'optionsPayTypes' => $optionsPayTypes, + 'optionsOrderTypes' => $optionsOrderTypes, + 'optionsDelivTypes' => $optionsDelivTypes, + 'optionsPayTypes' => $optionsPayTypes, 'optionsPayStatuses' => $optionsPayStatuses, - 'optionsPayment' => $optionsPayment, - 'optionSites' => $optionsSites, - 'optionsOrderProps' => $optionsOrderProps + 'optionsPayment' => $optionsPayment, + 'optionSites' => $optionsSites, + 'optionsOrderProps' => $optionsOrderProps ); - //packmode - - $orderCount = 0; + if (!$failed) { - while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc - - if(is_array($optionsSites)) - if(!empty($optionsSites)) - if(!in_array($arOrder['LID'], $optionsSites)) - continue; - - $result = self::orderCreate($arOrder, $api, $arParams); + //packmode - if (!$result['order'] || !$result['customer']) - continue; - - $orderCount++; - - $resOrders[] = $result['order']; - $resCustomers[] = $result['customer']; + $orderCount = 0; - $lastOrderId = $arOrder['ID']; + while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc + if (is_array($optionsSites)) + if (!empty($optionsSites)) + if (!in_array($arOrder['LID'], $optionsSites)) + continue; - if($orderCount >= $pSize) { - $customers = $api->customerUpload($resCustomers); - - // error pushing customers - if ($api->getStatusCode() != 201) { - //handle err - //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); - - if($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors + $result = self::orderCreate($arOrder, $api, $arParams); + + if (!$result['order'] || !$result['customer']) + continue; + + $orderCount++; + + $resOrders[] = $result['order']; + $resCustomers[] = $result['customer']; + + $lastOrderId = $arOrder['ID']; + + if ($orderCount >= $pSize) { + $customers = $api->customerUpload($resCustomers); + + // error pushing customers + if ($api->getStatusCode() != 201) { + //handle err + //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); + + if ($api->getStatusCode() != 460) // some orders were sent + return false; // in pack mode return errors + } + + $orders = $api->orderUpload($resOrders); + + // error pushing orders + if ($api->getStatusCode() != 201) { + //handle err + self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + + if ($api->getStatusCode() != 460) // some orders were sent + return false; // in pack mode return errors + } + + if ($lastOrderId) + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId); + + return true; // end of pack } - + } + if (!empty($resOrders)) { + $customers = $api->customerUpload($resCustomers); + + // error pushing customers + if ($api->getStatusCode() != 201) { + //handle err + //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); + + if ($api->getStatusCode() != 460) // some orders were sent + return false; // in pack mode return errors + } + $orders = $api->orderUpload($resOrders); // error pushing orders @@ -114,42 +151,98 @@ class ICrmOrderActions //handle err self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); - if($api->getStatusCode() != 460) // some orders were sent + if ($api->getStatusCode() != 460) // some orders were sent return false; // in pack mode return errors } - - if($lastOrderId) - COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId); + } - return true; // end of pack + if ($lastOrderId) + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId); + + } else { + + // failed orders upload + $orderCount = 0; + $recOrders = array(); + + while ($arOrder = $dbFailedOrder->GetNext()) { // here orders by id asc + if (is_array($optionsSites)) + if (!empty($optionsSites)) + if (!in_array($arOrder['LID'], $optionsSites)) + continue; + + $result = self::orderCreate($arOrder, $api, $arParams); + + if (!$result['order'] || !$result['customer']) + continue; + + $orderCount++; + + $resOrders[] = $result['order']; + $resCustomers[] = $result['customer']; + + $recOrders[] = $arOrder['ID']; + + if ($orderCount >= $pSize) { + $customers = $api->customerUpload($resCustomers); + + // error pushing customers + if ($api->getStatusCode() != 201) { + //handle err + //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); + + if ($api->getStatusCode() != 460) // some orders were sent + return false; // in pack mode return errors + } + + $orders = $api->orderUpload($resOrders); + + // error pushing orders + if ($api->getStatusCode() != 201) { + //handle err + self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + + if ($api->getStatusCode() != 460) // some orders were sent + return false; // in pack mode return errors + } + + if (!empty($recOrders)) { + $failedIds = array_merge(array_diff($failedIds, $recOrders)); // clear success ids + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds)); + } + + return true; // end of pack + } + } + if (!empty($resOrders)) { + $customers = $api->customerUpload($resCustomers); + + // error pushing customers + if ($api->getStatusCode() != 201) { + //handle err + //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); + + if ($api->getStatusCode() != 460) // some orders were sent + return false; // in pack mode return errors + } + + $orders = $api->orderUpload($resOrders); + + // error pushing orders + if ($api->getStatusCode() != 201) { + //handle err + self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + + if ($api->getStatusCode() != 460) // some orders were sent + return false; // in pack mode return errors + } + } + + if (!empty($recOrders)) { + $failedIds = array_merge(array_diff($failedIds, $recOrders)); // clear success ids + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds)); } } - if (!empty($resOrders)) { - $customers = $api->customerUpload($resCustomers); - - // error pushing customers - if ($api->getStatusCode() != 201) { - //handle err - //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); - - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors - } - - $orders = $api->orderUpload($resOrders); - - // error pushing orders - if ($api->getStatusCode() != 201) { - //handle err - self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); - - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors - } - } - - if($lastOrderId) - COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId); return true; //all ok! } @@ -456,6 +549,10 @@ class ICrmOrderActions public static function uploadOrdersAgent() { self::uploadOrders(); + $failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0)); + if(is_array($failedIds) && !empty($failedIds)) + self::uploadOrders(50, true); // upload failed orders + return 'ICrmOrderActions::uploadOrdersAgent();'; } diff --git a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php index dca02903..cf12c209 100644 --- a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php +++ b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php @@ -14,6 +14,7 @@ class ICrmOrderEvent { protected static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N protected static $CRM_ORDER_LAST_ID = 'order_last_id'; protected static $CRM_ORDER_PROPS = 'order_props'; + protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids'; /** * onBeforeOrderAdd @@ -31,14 +32,18 @@ class ICrmOrderEvent { * @param mixed $ID - Order id * @param mixed $arFields - Order arFields */ - function onUpdateOrder($ID, $arFields = array()) { + function onBeforeUpdateOrder($ID, $arFields) { + if(isset($GLOBALS['INTARO_CRM_ORDER_ADD']) && $GLOBALS['INTARO_CRM_ORDER_ADD']) return; if(isset($GLOBALS['INTARO_CRM_FROM_HISTORY']) && $GLOBALS['INTARO_CRM_FROM_HISTORY']) return; - self::writeDataOnOrderCreate($ID); + if(isset($arFields['LOCKED_BY']) && $arFields['LOCKED_BY']) + return; + + self::writeDataOnOrderCreate($ID, $arFields); } @@ -51,8 +56,17 @@ class ICrmOrderEvent { * @param mixed $arFields - Order arFields for sending template */ function onSendOrderMail($ID, &$eventName, &$arFields) { - self::writeDataOnOrderCreate($ID); - COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $ID); + if(self::writeDataOnOrderCreate($ID)) + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $ID); + else { + $failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0)); + if(!$failedIds) + $failedIds = array(); + + $failedIds[] = $ID; + + COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds)); + } } /** @@ -60,7 +74,7 @@ class ICrmOrderEvent { * * @param integer $ID - Order Id */ - function writeDataOnOrderCreate($ID) { + function writeDataOnOrderCreate($ID, $arFields) { if (!CModule::IncludeModule('iblock')) { //handle err @@ -96,21 +110,37 @@ class ICrmOrderEvent { $api = new IntaroCrm\RestApi($api_host, $api_key); - $arParams = array( + $arParams = ICrmOrderActions::clearArr(array( 'optionsOrderTypes' => $optionsOrderTypes, 'optionsDelivTypes' => $optionsDelivTypes, 'optionsPayTypes' => $optionsPayTypes, 'optionsPayStatuses' => $optionsPayStatuses, 'optionsPayment' => $optionsPayment, 'optionsOrderProps' => $optionsOrderProps - ); + )); $arOrder = CSaleOrder::GetById($ID); + + if (is_array($arFields) && !empty($arFields)) { + $arFieldsNew = array( + 'USER_ID' => $arOrder['USER_ID'], + 'ID' => $ID, + 'PERSON_TYPE_ID' => $arOrder['PERSON_TYPE_ID'], + 'CANCELED' => $arOrder['CANCELED'], + 'STATUS_ID' => $arOrder['STATUS_ID'], + 'DATE_INSERT' => $arOrder['DATE_INSERT'], + 'LID' => $arOrder['LID'] + ); + + $arFieldsNew = array_merge($arFieldsNew, $arFields); + $arOrder = $arFieldsNew; + } + $result = ICrmOrderActions::orderCreate($arOrder, $api, $arParams, true); if(!$result) { ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'ICrmOrderActions::orderCreate', 'error during creating order'); - return true; + return false; } return true; diff --git a/intaro.intarocrm/install/index.php b/intaro.intarocrm/install/index.php index 7063e9ca..6587d9ce 100755 --- a/intaro.intarocrm/install/index.php +++ b/intaro.intarocrm/install/index.php @@ -33,6 +33,7 @@ class intaro_intarocrm extends CModule { var $CRM_ORDER_SITES = 'sites_ids'; var $CRM_ORDER_PROPS = 'order_props'; var $CRM_ORDER_DISCHARGE = 'order_discharge'; + var $CRM_ORDER_FAILED_IDS = 'order_failed_ids'; var $INSTALL_PATH; function intaro_intarocrm() { @@ -700,6 +701,7 @@ class intaro_intarocrm extends CModule { COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT, serialize($paymentArr)); COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0); COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE, 0); + COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS, serialize(array())); $APPLICATION->IncludeAdminFile( GetMessage('MODULE_INSTALL_TITLE'), @@ -1032,11 +1034,12 @@ class intaro_intarocrm extends CModule { COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_SITES); COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_PROPS); COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE); + COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS); UnRegisterModuleDependences("sale", "OnSalePayOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSalePayOrder"); UnRegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder"); UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $this->MODULE_ID, "ICrmOrderEvent", "onSendOrderMail"); - UnRegisterModuleDependences("sale", "OnOrderUpdate", $this->MODULE_ID, "ICrmOrderEvent", "onUpdateOrder"); + UnRegisterModuleDependences("sale", "OnBeforeOrderUpdate", $this->MODULE_ID, "ICrmOrderEvent", "onBeforeUpdateOrder"); UnRegisterModuleDependences("sale", "OnBeforeOrderAdd", $this->MODULE_ID, "ICrmOrderEvent", "onBeforeOrderAdd"); if (CModule::IncludeModule("catalog")) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/' . $this->INTARO_CRM_EXPORT . '_run.php')) { diff --git a/intaro.intarocrm/options.php b/intaro.intarocrm/options.php index aca82213..bebd3615 100755 --- a/intaro.intarocrm/options.php +++ b/intaro.intarocrm/options.php @@ -218,25 +218,13 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { if (($orderDischarge != $previousDischarge) && ($orderDischarge == 0)) { // remove depenedencies UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail"); - UnRegisterModuleDependences("sale", "OnOrderUpdate", $mid, "ICrmOrderEvent", "onUpdateOrder"); + UnRegisterModuleDependences("sale", "OnBeforeOrderUpdate", $mid, "ICrmOrderEvent", "onBeforeUpdateOrder"); UnRegisterModuleDependences("sale", "OnBeforeOrderAdd", $mid, "ICrmOrderEvent", "onBeforeOrderAdd"); - // 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"); + RegisterModuleDependences("sale", "OnBeforeOrderUpdate", $mid, "ICrmOrderEvent", "onBeforeUpdateOrder"); RegisterModuleDependences("sale", "OnBeforeOrderAdd", $mid, "ICrmOrderEvent", "onBeforeOrderAdd"); }