From b1b2d685dbb8fc8f8057172ee05171157adb363d Mon Sep 17 00:00:00 2001 From: Grisha Pomadchin Date: Wed, 14 Aug 2013 20:12:40 +0400 Subject: [PATCH] event reovery --- .../classes/general/events/ICrmOrderEvent.php | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 intaro.intarocrm/classes/general/events/ICrmOrderEvent.php diff --git a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php new file mode 100644 index 00000000..5e9470ff --- /dev/null +++ b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php @@ -0,0 +1,195 @@ + $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; + } + + 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