1
0
mirror of synced 2024-11-22 05:16:09 +03:00

added onCancelOrderEvent

This commit is contained in:
Grisha Pomadchin 2013-08-05 14:46:13 +04:00
parent e4176b2856
commit 1a674a91be
4 changed files with 167 additions and 4 deletions

View File

@ -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');

View File

@ -0,0 +1,156 @@
<?php
/**
* OrderEvent
*/
class ICrmOrderEvent {
protected static $MODULE_ID = 'intaro.intarocrm';
protected static $CRM_API_HOST_OPTION = 'api_host';
protected static $CRM_API_KEY_OPTION = 'api_key';
protected static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
protected static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
protected static $CRM_PAYMENT_TYPES = 'pay_types_arr';
protected static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
protected static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
/**
* onUpdateOrder
*
* @param mixed $ID - Order id
* @param mixed $arFields - Order arFields
*/
function onUpdateOrder($ID, $arFields = array()) {
//self::writeDataOnOrderCreate($ID);
}
/**
* onSendOrderMail
* in: sale.order.ajax, sale.order.full
*
* @param mixed $ID - Order id
* @param mixed $eventName - Event type
* @param mixed $arFields - Order arFields for sending template
*/
function onSendOrderMail($ID, &$eventName, &$arFields) {
self::writeDataOnOrderCreate($ID);
}
/**
* writeDataOnOrderCreate via api
*
* @param integer $ID - Order Id
*/
function writeDataOnOrderCreate($ID) {
if (!CModule::IncludeModule('iblock')) {
//handle err
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'iblock', 'module not found');
return true;
}
if (!CModule::IncludeModule("sale")) {
//handle err
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'sale', 'module not found');
return true;
}
if (!CModule::IncludeModule("catalog")) {
//handle err
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'catalog', 'module not found');
return true;
}
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString(Iself::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
//saved cat params
$optionsOrderTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_TYPES_ARR, 0));
$optionsDelivTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_DELIVERY_TYPES_ARR, 0));
$optionsPayTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_TYPES, 0));
$optionsPayStatuses = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_STATUSES, 0)); // --statuses
$optionsPayment = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0));
$api = new IntaroCrm\RestApi($api_host, $api_key);
$arParams = array(
'optionsOrderTypes' => $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;
}
}

View File

@ -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(

View File

@ -0,0 +1,5 @@
<?
$arModuleVersion = array(
'VERSION' => '0.4.6',
'VERSION_DATE' => '2013-07-25 15:35:00',
);