From 1a674a91befa2e107f92d80be415d2dcd98a5bbf Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Mon, 5 Aug 2013 14:46:13 +0400 Subject: [PATCH] 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', +);