From 1a674a91befa2e107f92d80be415d2dcd98a5bbf Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Mon, 5 Aug 2013 14:46:13 +0400 Subject: [PATCH 1/5] added onCancelOrderEvent --- .../classes/general/ICrmOrderActions.php | 4 +- .../classes/general/events/ICrmOrderEvent.php | 156 ++++++++++++++++++ intaro.intarocrm/install/index.php | 6 +- intaro.intarocrm/install/version.php~ | 5 + 4 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 intaro.intarocrm/classes/general/events/ICrmOrderEvent.php create mode 100755 intaro.intarocrm/install/version.php~ diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 9d0542c4..8019a41b 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -154,7 +154,7 @@ class ICrmOrderActions * * w+ event in bitrix log */ - private static function eventLog($auditType, $itemId, $description) { + public static function eventLog($auditType, $itemId, $description) { CEventLog::Add(array( "SEVERITY" => "SECURITY", "AUDIT_TYPE_ID" => $auditType, @@ -358,7 +358,7 @@ class ICrmOrderActions * @param type $str in SITE_CHARSET * @return type $str in utf-8 */ - protected static function toJSON($str) { + public static function toJSON($str) { global $APPLICATION; return $APPLICATION->ConvertCharset($str, SITE_CHARSET, 'utf-8'); diff --git a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php new file mode 100644 index 00000000..9d27758e --- /dev/null +++ b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php @@ -0,0 +1,156 @@ + $optionsOrderTypes, + 'optionsDelivTypes' => $optionsDelivTypes, + 'optionsPayTypes' => $optionsPayTypes, + 'optionsPayStatuses' => $optionsPayStatuses, + 'optionsPayment' => $optionsPayment + ); + + $result = ICrmOrderActions::orderCreate($ID, $api, $arParams, true); + + if(!$result) { + ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'ICrmOrderActions::orderCreate', 'error during creating order'); + return true; + } + + 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; + } + + $arFields = CSaleOrder::GetById($ID); + + if (empty($arFields)) { + //handle err + self::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'empty($arFields)', 'incorrect order'); + + 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 + $optionsOrderTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_TYPES_ARR, 0)); + $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, + 'customerId' => (int) $arFields['USER_ID'], + 'orderType' => $optionsOrderTypes[$arFields['PERSON_TYPE_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; + } +} \ No newline at end of file diff --git a/intaro.intarocrm/install/index.php b/intaro.intarocrm/install/index.php index 6250d562..d470f618 100755 --- a/intaro.intarocrm/install/index.php +++ b/intaro.intarocrm/install/index.php @@ -378,6 +378,7 @@ class intaro_intarocrm extends CModule } else if ($step == 4) { RegisterModule($this->MODULE_ID); + RegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder"); //agent $dateAgent = new DateTime(); @@ -416,9 +417,10 @@ class intaro_intarocrm extends CModule COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES); COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT); COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_LAST_ID); - + UnRegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder"); + $this->DeleteFiles(); - + UnRegisterModule($this->MODULE_ID); $APPLICATION->IncludeAdminFile( diff --git a/intaro.intarocrm/install/version.php~ b/intaro.intarocrm/install/version.php~ new file mode 100755 index 00000000..b0b13837 --- /dev/null +++ b/intaro.intarocrm/install/version.php~ @@ -0,0 +1,5 @@ + '0.4.6', + 'VERSION_DATE' => '2013-07-25 15:35:00', +); From 47452c4f255928397e0261860e4d78054fa79a80 Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Mon, 5 Aug 2013 14:49:51 +0400 Subject: [PATCH 2/5] upd orderEdit method --- .../classes/general/events/ICrmOrderEvent.php | 12 ------------ intaro.intarocrm/install/version.php~ | 5 ----- 2 files changed, 17 deletions(-) delete mode 100755 intaro.intarocrm/install/version.php~ diff --git a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php index 9d27758e..bbdee5d5 100644 --- a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php +++ b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php @@ -118,29 +118,17 @@ class ICrmOrderEvent { ICrmOrderActions::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'catalog', 'module not found'); return true; } - - $arFields = CSaleOrder::GetById($ID); - - if (empty($arFields)) { - //handle err - self::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'empty($arFields)', 'incorrect order'); - - 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 - $optionsOrderTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_TYPES_ARR, 0)); $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, - 'customerId' => (int) $arFields['USER_ID'], - 'orderType' => $optionsOrderTypes[$arFields['PERSON_TYPE_ID']], 'status' => $optionsPayStatuses[$cancel], 'statusComment' => ICrmOrderActions::toJSON($reason) ); diff --git a/intaro.intarocrm/install/version.php~ b/intaro.intarocrm/install/version.php~ deleted file mode 100755 index b0b13837..00000000 --- a/intaro.intarocrm/install/version.php~ +++ /dev/null @@ -1,5 +0,0 @@ - '0.4.6', - 'VERSION_DATE' => '2013-07-25 15:35:00', -); From d2aa99fdad142619e12430b294b22c01d296989b Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Mon, 5 Aug 2013 15:15:15 +0400 Subject: [PATCH 3/5] orderCreate upgrade --- .../classes/general/ICrmOrderActions.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index 8019a41b..d038c82d 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -71,7 +71,7 @@ class ICrmOrderActions while ($arOrder = $dbOrder->GetNext()) { //here orders by id asc; with offset - $order = self::orderCreate($arOrder['ID'], $api, $arParams); + $order = self::orderCreate($arOrder, $api, $arParams); if (!$order) continue; @@ -99,7 +99,7 @@ class ICrmOrderActions while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc - $order = self::orderCreate($arOrder['ID'], $api, $arParams); + $order = self::orderCreate($arOrder, $api, $arParams); if (!$order) continue; @@ -179,18 +179,16 @@ class ICrmOrderActions else return; } - public static function orderCreate($orderId, $api, $arParams, $send = false) { - if(!$api || empty($arParams) || !$orderId) { // add cond to check $arParams + public static function orderCreate($arFields, $api, $arParams, $send = false) { + if(!$api || empty($arParams)) { // add cond to check $arParams return false; } - - $arFields = CSaleOrder::GetById($orderId); - + if (empty($arFields)) { //handle err self::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect order'); - return false; + return true; } $rsUser = CUser::GetByID($arFields['USER_ID']); From fb582a1752e250d663f3050ac01be88568aa9580 Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Mon, 5 Aug 2013 15:16:52 +0400 Subject: [PATCH 4/5] func response fix --- intaro.intarocrm/classes/general/ICrmOrderActions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index d038c82d..bc45cfdc 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -188,7 +188,7 @@ class ICrmOrderActions //handle err self::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect order'); - return true; + return false; } $rsUser = CUser::GetByID($arFields['USER_ID']); From 7fdddd5edf71aac1956346b266981cc3e6c164a7 Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Mon, 5 Aug 2013 15:39:37 +0400 Subject: [PATCH 5/5] orderCreate fix --- intaro.intarocrm/classes/general/events/ICrmOrderEvent.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php index bbdee5d5..01ee9dcf 100644 --- a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php +++ b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php @@ -80,7 +80,8 @@ class ICrmOrderEvent { 'optionsPayment' => $optionsPayment ); - $result = ICrmOrderActions::orderCreate($ID, $api, $arParams, true); + $arOrder = CSaleOrder::GetById($ID); + $result = ICrmOrderActions::orderCreate($arOrder, $api, $arParams, true); if(!$result) { ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'ICrmOrderActions::orderCreate', 'error during creating order');