1
0
mirror of synced 2024-11-22 05:16:09 +03:00
This commit is contained in:
МЫ 2013-08-14 10:44:27 +04:00
commit 29bf585826
4 changed files with 513 additions and 501 deletions

View File

@ -11,41 +11,41 @@ class ICrmOrderActions
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';
/**
* Mass order uploading, without repeating; always returns true, but writes error log
* @return boolean
*/
public static function uploadOrders($steps = false, $pSize = 50) {
//COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test
if (!CModule::IncludeModule("iblock")) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'iblock', 'module not found');
return true;
}
if (!CModule::IncludeModule("sale")) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'sale', 'module not found');
return true;
}
if (!CModule::IncludeModule("catalog")) {
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'catalog', 'module not found');
return true;
}
$resOrders = array();
$resCustomers = array();
$lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
$lastOrderId = 0;
$dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId));
$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);
@ -57,7 +57,7 @@ class ICrmOrderActions
$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,
@ -65,7 +65,7 @@ class ICrmOrderActions
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment
);
// pack mode enable / disable
// can send data evry 500 rows
if (!$steps) {
@ -94,8 +94,14 @@ class ICrmOrderActions
return true;
}
$resOrders[] = $order;
$lastOrderId = $arOrder['ID'];
}
if (!empty($resOrders)) {
$orders = $api->orderUpload($resOrders);
// error pushing orders
if ($api->getStatusCode() != 201) {
//handle err
@ -105,24 +111,24 @@ class ICrmOrderActions
return true;
}
}
} else { // package mode (by default runs after install)
$orderCount = 0;
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
$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);
@ -136,19 +142,19 @@ class ICrmOrderActions
}
$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
}
}
@ -174,9 +180,9 @@ class ICrmOrderActions
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);
@ -184,11 +190,12 @@ 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,
@ -197,19 +204,19 @@ class ICrmOrderActions
"DESCRIPTION" => $description,
));
}
/**
*
*
* Agent function
*
*
* @return self name
*/
public static function uploadOrdersAgent() {
if(self::uploadOrders())
return 'ICrmOrderActions::uploadOrdersAgent();';
else return;
}
@ -228,7 +235,7 @@ class ICrmOrderActions
if(!$api || empty($arParams)) { // add cond to check $arParams
return false;
}
if (empty($arFields)) {
//handle err
self::eventLog('ICrmOrderActions::orderCreate', 'empty($arFields)', 'incorrect order');
@ -238,7 +245,7 @@ class ICrmOrderActions
$rsUser = CUser::GetByID($arFields['USER_ID']);
$arUser = $rsUser->Fetch();
$createdAt = new \DateTime($arUser['DATE_REGISTER']);
$createdAt = $createdAt->format('Y-m-d H:i:s');
@ -314,7 +321,7 @@ class ICrmOrderActions
break;
}
}
$items = array();
$rsOrderBasket = CSaleBasket::GetList(array('PRODUCT_ID' => 'ASC'), array('ORDER_ID' => $arFields['ID']));
@ -324,10 +331,10 @@ class ICrmOrderActions
$pr = $pr['PURCHASING_PRICE'];
else
$pr = '';
if($p['DISCOUNT_VALUE'])
$p['DISCOUNT_PRICE'] = null;
$items[] = array(
'initialPrice' => (double) $p['PRICE'] + (double) $p['DISCOUNT_PRICE'],
'purchasePrice' => $pr,
@ -338,16 +345,16 @@ class ICrmOrderActions
'productName' => self::toJSON($p['NAME'])
);
}
if($arFields['CANCELED'] == 'Y')
$arFields['STATUS_ID'] = $arFields['CANCELED'];
$createdAt = new \DateTime($arFields['DATE_INSERT']);
$createdAt = $createdAt->format('Y-m-d H:i:s');
$resOrder = self::clearArr(array(
'firstName' => $resOrder['contactName'][0],
'lastName' => $resOrder['contactName'][1],
'lastName' => $resOrder['contactName'][0],
'firstName' => $resOrder['contactName'][1],
'patronymic' => $resOrder['contactName'][2],
'phone' => $resOrder['phone'],
'email' => $resOrder['email'],
@ -366,7 +373,7 @@ class ICrmOrderActions
'deliveryAddress' => $resOrderDeliveryAddress,
'items' => $items
));
if($send)
return $api->createOrder($resOrder);
@ -374,52 +381,51 @@ class ICrmOrderActions
'order' => $resOrder,
'customer' => $customer
);
}
/**
* removes all empty fields from arrays
* working with nested arrs
*
*
* @param type $arr
* @return boolean
*/
public static function clearArr($arr) {
if(!$arr || !is_array($arr))
return false;
foreach($arr as $key => $value) {
if(!$value || (is_array($value) && empty($value)))
unset($arr[$key]);
if(is_array($value) && !empty($value))
$arr[$key] = self::clearArr($value);
}
return $arr;
}
/**
*
*
* @global type $APPLICATION
* @param type $str in SITE_CHARSET
* @return type $str in utf-8
*/
public static function toJSON($str) {
global $APPLICATION;
return $APPLICATION->ConvertCharset($str, SITE_CHARSET, 'utf-8');
}
/**
*
*
* @global type $APPLICATION
* @param type $str in utf-8
* @return type $str in SITE_CHARSET
*/
public static function fromJSON($str) {
global $APPLICATION;
return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET);
}
}

View File

@ -1,443 +1,449 @@
<?php
/**
* Module Install/Uninstall script
* Module name: intaro.intarocrm
* Class name: intaro_intarocrm
*/
global $MESS;
IncludeModuleLangFile(__FILE__);
if (class_exists('intaro_intarocrm'))
return;
class intaro_intarocrm extends CModule
{
var $MODULE_ID = 'intaro.intarocrm';
var $MODULE_VERSION;
var $MODULE_VERSION_DATE;
var $MODULE_NAME;
var $MODULE_DESCRIPTION;
var $MODULE_GROUP_RIGHTS = 'N';
var $PARTNER_NAME;
var $PARTNER_URI;
var $INTARO_CRM_API;
var $CRM_API_HOST_OPTION = 'api_host';
var $CRM_API_KEY_OPTION = 'api_key';
var $CRM_ORDER_TYPES_ARR = 'order_types_arr';
var $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
var $CRM_PAYMENT_TYPES = 'pay_types_arr';
var $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
var $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
var $CRM_ORDER_LAST_ID = 'order_last_id';
var $INSTALL_PATH;
function intaro_intarocrm()
{
$arModuleVersion = array();
$path = str_replace("\\", "/", __FILE__);
$path = substr($path, 0, strlen($path) - strlen("/index.php"));
$this->INSTALL_PATH = $path;
include($path."/version.php");
$this->MODULE_VERSION = $arModuleVersion["VERSION"];
$this->MODULE_VERSION_DATE = $arModuleVersion["VERSION_DATE"];
$this->MODULE_NAME = GetMessage('MODULE_NAME');
$this->MODULE_DESCRIPTION = GetMessage('MODULE_DESCRIPTION');
$this->PARTNER_NAME = GetMessage('MODULE_PARTNER_NAME');
$this->PARTNER_URI = GetMessage('MODULE_PARTNER_URI');
}
/**
* Functions DoInstall and DoUninstall are
* All other functions are optional
*/
function DoInstall()
{
global $APPLICATION, $step, $arResult;
if (!in_array('curl', get_loaded_extensions())) {
$APPLICATION->ThrowException( GetMessage("INTAROCRM_CURL_ERR") );
return false;
}
include($this->INSTALL_PATH . '/../classes/general/RestApi.php');
include($this->INSTALL_PATH . '/../classes/general/ICrmOrderActions.php');
$step = intval($_REQUEST['step']);
if ($step <= 1) {
if(!CModule::IncludeModule("sale")) {
$arResult['errCode'] = 'ERR_SALE';
}
if(!CModule::IncludeModule("iblock")) {
$arResult['errCode'] = 'ERR_IBLOCK';
}
if(!CModule::IncludeModule("catalog")) {
$arResult['errCode'] = 'ERR_CATALOG';
}
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
} else if ($step == 2) {
if(!CModule::IncludeModule("sale")) {
$arResult['errCode'] = 'ERR_SALE';
}
if(!CModule::IncludeModule("iblock")) {
$arResult['errCode'] = 'ERR_IBLOCK';
}
if(!CModule::IncludeModule("catalog")) {
$arResult['errCode'] = 'ERR_CATALOG';
}
if(isset($arResult['errCode']) && $arResult['errCode']) {
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
return;
}
$api_host = htmlspecialchars(trim($_POST[$this->CRM_API_HOST_OPTION]));
$api_key = htmlspecialchars(trim($_POST[$this->CRM_API_KEY_OPTION]));
// form correct url
$api_host = parse_url($api_host);
$api_host = $api_host['scheme'] . '://' . $api_host['host'];
if(!$api_host || !$api_key) {
$arResult['errCode'] = 'ERR_FIELDS_API_HOST';
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
return;
}
$this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
$this->INTARO_CRM_API->paymentStatusesList();
//check connection & apiKey valid
if((int) $this->INTARO_CRM_API->getStatusCode() != 200) {
$arResult['errCode'] = 'ERR_' . $this->INTARO_CRM_API->getStatusCode();
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
return;
}
COption::SetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, $api_host);
COption::SetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, $api_key);
//prepare crm lists
$arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList();
$arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList();
$arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList();
$arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses
$arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList();
$arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
$arResult['bitrixOrderTypesList'][] = $arOrderTypesList;
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
//bitrix deliveryTypesList
$dbDeliveryTypesList = CSaleDelivery::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
$arResult['bitrixDeliveryTypesList'][] = $arDeliveryTypesList;
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
//bitrix paymentTypesList
$dbPaymentTypesList = CSalePaySystem::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y"
)
);
if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) {
do {
$arResult['bitrixPaymentTypesList'][] = $arPaymentTypesList;
} while ($arPaymentTypesList = $dbPaymentTypesList->Fetch());
}
//bitrix paymentStatusesList --statuses
$dbPaymentStatusesList = CSaleStatus::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"LID" => "ru", //ru
"ACTIVE" => "Y"
)
);
if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) {
do {
$arResult['bitrixPaymentStatusesList'][] = $arPaymentStatusesList;
} while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch());
}
$arResult['bitrixPaymentStatusesList'][] = array(
'ID' => 'Y',
'NAME' => GetMessage('CANCELED')
);
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step2.php'
);
} else if ($step == 3) {
if(!CModule::IncludeModule("sale")) {
//handler
}
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
&& isset($_POST['ajax']) && ($_POST['ajax'] == 1)) {
ICrmOrderActions::uploadOrders(true); // each 50
$lastUpOrderId = COption::GetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0);
$countLeft = (int) CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId), array());
$countAll = (int) CSaleOrder::GetList(array("ID" => "ASC"), array(), array());
if(!isset($_POST['finish']))
$finish = 0;
else
$finish = (int) $_POST['finish'];
$percent = 100 - round(($countLeft * 100 / $countAll), 1);
if(!$countLeft) {
$api_host = COption::GetOptionString($mid, $this->CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString($mid, $this->CRM_API_KEY_OPTION, 0);
$this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
$this->INTARO_CRM_API->statisticUpdate();
$finish = 1;
}
$APPLICATION->RestartBuffer();
header('Content-Type: application/x-javascript; charset='.LANG_CHARSET);
die(json_encode(array("finish" => $finish, "percent" => $percent)));
}
if (isset($_POST['back']) && $_POST['back']) {
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
}
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
//form order types ids arr
$orderTypesArr = array();
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
$orderTypesArr[$arOrderTypesList['ID']] = htmlspecialchars(trim($_POST['order-type-' . $arOrderTypesList['ID']]));
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
//bitrix deliveryTypesList
$dbDeliveryTypesList = CSaleDelivery::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
//form delivery types ids arr
$deliveryTypesArr = array();
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
$deliveryTypesArr[$arDeliveryTypesList['ID']] = htmlspecialchars(trim($_POST['delivery-type-' . $arDeliveryTypesList['ID']]));
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
//bitrix paymentTypesList
$dbPaymentTypesList = CSalePaySystem::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y"
)
);
//form payment types ids arr
$paymentTypesArr = array();
if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) {
do {
$paymentTypesArr[$arPaymentTypesList['ID']] = htmlspecialchars(trim($_POST['payment-type-' . $arPaymentTypesList['ID']]));
} while ($arPaymentTypesList = $dbPaymentTypesList->Fetch());
}
//bitrix paymentStatusesList
$dbPaymentStatusesList = CSaleStatus::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"LID" => "ru", //ru
"ACTIVE" => "Y"
)
);
//form payment statuses ids arr
$paymentStatusesArr['Y'] = htmlspecialchars(trim($_POST['payment-status-Y']));
if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) {
do {
$paymentStatusesArr[$arPaymentStatusesList['ID']] = htmlspecialchars(trim($_POST['payment-status-' . $arPaymentStatusesList['ID']]));
} while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch());
}
//form payment ids arr
$paymentArr = array();
$paymentArr['Y'] = htmlspecialchars(trim($_POST['payment-Y']));
$paymentArr['N'] = htmlspecialchars(trim($_POST['payment-N']));
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_TYPES_ARR, serialize($orderTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_DELIVERY_TYPES_ARR, serialize($deliveryTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_TYPES, serialize($paymentTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES, serialize($paymentStatusesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT, serialize($paymentArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0);
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
);
} else if ($step == 4) {
RegisterModule($this->MODULE_ID);
//agent
$dateAgent = new DateTime();
$intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
$dateAgent->add($intAgent);
CAgent::AddAgent(
"ICrmOrderActions::uploadOrdersAgent();",
$this->MODULE_ID,
"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
);
$this->CopyFiles();
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step4.php'
);
}
}
function DoUninstall() {
global $APPLICATION;
CAgent::RemoveAgent("ICrmOrderActions::uploadOrdersAgent();", $this->MODULE_ID);
COption::RemoveOption($this->MODULE_ID, $this->CRM_API_HOST_OPTION);
COption::RemoveOption($this->MODULE_ID, $this->CRM_API_KEY_OPTION);
COption::RemoveOption($this->MODULE_ID, $this->CRM_DELIVERY_TYPES_ARR);
COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT_TYPES);
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);
$this->DeleteFiles();
UnRegisterModule($this->MODULE_ID);
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_UNINSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/unstep1.php'
);
}
function CopyFiles() {
CopyDirFiles(
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/export/',
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/',
true, true
);
}
function DeleteFiles() {
unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/intarocrm_run.php');
unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/intarocrm_setup.php');
}
<?php
/**
* Module Install/Uninstall script
* Module name: intaro.intarocrm
* Class name: intaro_intarocrm
*/
global $MESS;
IncludeModuleLangFile(__FILE__);
if (class_exists('intaro_intarocrm'))
return;
class intaro_intarocrm extends CModule
{
var $MODULE_ID = 'intaro.intarocrm';
var $MODULE_VERSION;
var $MODULE_VERSION_DATE;
var $MODULE_NAME;
var $MODULE_DESCRIPTION;
var $MODULE_GROUP_RIGHTS = 'N';
var $PARTNER_NAME;
var $PARTNER_URI;
var $INTARO_CRM_API;
var $CRM_API_HOST_OPTION = 'api_host';
var $CRM_API_KEY_OPTION = 'api_key';
var $CRM_ORDER_TYPES_ARR = 'order_types_arr';
var $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
var $CRM_PAYMENT_TYPES = 'pay_types_arr';
var $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
var $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
var $CRM_ORDER_LAST_ID = 'order_last_id';
var $INSTALL_PATH;
function intaro_intarocrm()
{
$arModuleVersion = array();
$path = str_replace("\\", "/", __FILE__);
$path = substr($path, 0, strlen($path) - strlen("/index.php"));
$this->INSTALL_PATH = $path;
include($path."/version.php");
$this->MODULE_VERSION = $arModuleVersion["VERSION"];
$this->MODULE_VERSION_DATE = $arModuleVersion["VERSION_DATE"];
$this->MODULE_NAME = GetMessage('MODULE_NAME');
$this->MODULE_DESCRIPTION = GetMessage('MODULE_DESCRIPTION');
$this->PARTNER_NAME = GetMessage('MODULE_PARTNER_NAME');
$this->PARTNER_URI = GetMessage('MODULE_PARTNER_URI');
}
/**
* Functions DoInstall and DoUninstall are
* All other functions are optional
*/
function DoInstall()
{
global $APPLICATION, $step, $arResult;
if (!in_array('curl', get_loaded_extensions())) {
$APPLICATION->ThrowException( GetMessage("INTAROCRM_CURL_ERR") );
return false;
}
include($this->INSTALL_PATH . '/../classes/general/RestApi.php');
include($this->INSTALL_PATH . '/../classes/general/ICrmOrderActions.php');
$step = intval($_REQUEST['step']);
if ($step <= 1) {
if(!CModule::IncludeModule("sale")) {
$arResult['errCode'] = 'ERR_SALE';
}
if(!CModule::IncludeModule("iblock")) {
$arResult['errCode'] = 'ERR_IBLOCK';
}
if(!CModule::IncludeModule("catalog")) {
$arResult['errCode'] = 'ERR_CATALOG';
}
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
} else if ($step == 2) {
if(!CModule::IncludeModule("sale")) {
$arResult['errCode'] = 'ERR_SALE';
}
if(!CModule::IncludeModule("iblock")) {
$arResult['errCode'] = 'ERR_IBLOCK';
}
if(!CModule::IncludeModule("catalog")) {
$arResult['errCode'] = 'ERR_CATALOG';
}
if(isset($arResult['errCode']) && $arResult['errCode']) {
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
return;
}
$api_host = htmlspecialchars(trim($_POST[$this->CRM_API_HOST_OPTION]));
$api_key = htmlspecialchars(trim($_POST[$this->CRM_API_KEY_OPTION]));
// form correct url
$api_host = parse_url($api_host);
$api_host = $api_host['scheme'] . '://' . $api_host['host'];
if(!$api_host || !$api_key) {
$arResult['errCode'] = 'ERR_FIELDS_API_HOST';
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
return;
}
$this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
$this->INTARO_CRM_API->paymentStatusesList();
//check connection & apiKey valid
if((int) $this->INTARO_CRM_API->getStatusCode() != 200) {
$arResult['errCode'] = 'ERR_' . $this->INTARO_CRM_API->getStatusCode();
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
return;
}
COption::SetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, $api_host);
COption::SetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, $api_key);
//prepare crm lists
$arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList();
$arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList();
$arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList();
$arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses
$arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList();
$arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
$arResult['bitrixOrderTypesList'][] = $arOrderTypesList;
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
//bitrix deliveryTypesList
$dbDeliveryTypesList = CSaleDelivery::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
$arResult['bitrixDeliveryTypesList'][] = $arDeliveryTypesList;
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
//bitrix paymentTypesList
$dbPaymentTypesList = CSalePaySystem::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y"
)
);
if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) {
do {
$arResult['bitrixPaymentTypesList'][] = $arPaymentTypesList;
} while ($arPaymentTypesList = $dbPaymentTypesList->Fetch());
}
//bitrix paymentStatusesList --statuses
$dbPaymentStatusesList = CSaleStatus::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"LID" => "ru", //ru
"ACTIVE" => "Y"
)
);
if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) {
do {
$arResult['bitrixPaymentStatusesList'][] = $arPaymentStatusesList;
} while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch());
}
$arResult['bitrixPaymentStatusesList'][] = array(
'ID' => 'Y',
'NAME' => GetMessage('CANCELED')
);
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step2.php'
);
} else if ($step == 3) {
if(!CModule::IncludeModule("sale")) {
//handler
}
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
&& isset($_POST['ajax']) && ($_POST['ajax'] == 1)) {
ICrmOrderActions::uploadOrders(true); // each 50
$lastUpOrderId = COption::GetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0);
$countLeft = (int) CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId), array());
$countAll = (int) CSaleOrder::GetList(array("ID" => "ASC"), array(), array());
if(!isset($_POST['finish']))
$finish = 0;
else
$finish = (int) $_POST['finish'];
$percent = 100 - round(($countLeft * 100 / $countAll), 1);
if(!$countLeft)
$finish = 1;
$APPLICATION->RestartBuffer();
header('Content-Type: application/x-javascript; charset='.LANG_CHARSET);
die(json_encode(array("finish" => $finish, "percent" => $percent)));
}
if (isset($_POST['back']) && $_POST['back']) {
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php'
);
}
//bitrix orderTypesList -- personTypes
$dbOrderTypesList = CSalePersonType::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
//form order types ids arr
$orderTypesArr = array();
if ($arOrderTypesList = $dbOrderTypesList->Fetch()) {
do {
$orderTypesArr[$arOrderTypesList['ID']] = htmlspecialchars(trim($_POST['order-type-' . $arOrderTypesList['ID']]));
} while ($arOrderTypesList = $dbOrderTypesList->Fetch());
}
//bitrix deliveryTypesList
$dbDeliveryTypesList = CSaleDelivery::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y",
),
false,
false,
array()
);
//form delivery types ids arr
$deliveryTypesArr = array();
if ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()) {
do {
$deliveryTypesArr[$arDeliveryTypesList['ID']] = htmlspecialchars(trim($_POST['delivery-type-' . $arDeliveryTypesList['ID']]));
} while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch());
}
//bitrix paymentTypesList
$dbPaymentTypesList = CSalePaySystem::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"ACTIVE" => "Y"
)
);
//form payment types ids arr
$paymentTypesArr = array();
if ($arPaymentTypesList = $dbPaymentTypesList->Fetch()) {
do {
$paymentTypesArr[$arPaymentTypesList['ID']] = htmlspecialchars(trim($_POST['payment-type-' . $arPaymentTypesList['ID']]));
} while ($arPaymentTypesList = $dbPaymentTypesList->Fetch());
}
//bitrix paymentStatusesList
$dbPaymentStatusesList = CSaleStatus::GetList(
array(
"SORT" => "ASC",
"NAME" => "ASC"
),
array(
"LID" => "ru", //ru
"ACTIVE" => "Y"
)
);
//form payment statuses ids arr
$paymentStatusesArr['Y'] = htmlspecialchars(trim($_POST['payment-status-Y']));
if ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch()) {
do {
$paymentStatusesArr[$arPaymentStatusesList['ID']] = htmlspecialchars(trim($_POST['payment-status-' . $arPaymentStatusesList['ID']]));
} while ($arPaymentStatusesList = $dbPaymentStatusesList->Fetch());
}
//form payment ids arr
$paymentArr = array();
$paymentArr['Y'] = htmlspecialchars(trim($_POST['payment-Y']));
$paymentArr['N'] = htmlspecialchars(trim($_POST['payment-N']));
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_TYPES_ARR, serialize($orderTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_DELIVERY_TYPES_ARR, serialize($deliveryTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_TYPES, serialize($paymentTypesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT_STATUSES, serialize($paymentStatusesArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT, serialize($paymentArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0);
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step3.php'
);
} else if ($step == 4) {
RegisterModule($this->MODULE_ID);
RegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
RegisterModuleDependences("sale", "OnSalePayOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSalePayOrder");
//agent
$dateAgent = new DateTime();
$intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
$dateAgent->add($intAgent);
CAgent::AddAgent(
"ICrmOrderActions::uploadOrdersAgent();",
$this->MODULE_ID,
"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
);
$this->CopyFiles();
// statistic update
$api_host = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, 0);
$this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key);
$this->INTARO_CRM_API->statisticUpdate();
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step4.php'
);
}
}
function DoUninstall() {
global $APPLICATION;
CAgent::RemoveAgent("ICrmOrderActions::uploadOrdersAgent();", $this->MODULE_ID);
COption::RemoveOption($this->MODULE_ID, $this->CRM_API_HOST_OPTION);
COption::RemoveOption($this->MODULE_ID, $this->CRM_API_KEY_OPTION);
COption::RemoveOption($this->MODULE_ID, $this->CRM_DELIVERY_TYPES_ARR);
COption::RemoveOption($this->MODULE_ID, $this->CRM_PAYMENT_TYPES);
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", "OnSalePayOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSalePayOrder");
UnRegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
$this->DeleteFiles();
UnRegisterModule($this->MODULE_ID);
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_UNINSTALL_TITLE'),
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/unstep1.php'
);
}
function CopyFiles() {
CopyDirFiles(
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/export/',
$_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/',
true, true
);
}
function DeleteFiles() {
unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/intarocrm_run.php');
unlink($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/intarocrm_setup.php');
}
}

View File

@ -19,7 +19,7 @@ $defaultDelivTypes = array (
$defaultPayTypes = array (
1 => 'cash',
5 => 'bank-card',
5 => 'bank-transfer',
6 => 'bank-transfer'
);

View File

@ -1,5 +1,5 @@
<?
$arModuleVersion = array(
'VERSION' => '0.3.6',
'VERSION_DATE' => '2013-07-29 18:40:00',
);
<?
$arModuleVersion = array(
'VERSION' => '0.3.7',
'VERSION_DATE' => '2013-08-06 15:07:00',
);