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

Merge branch 'master'

Conflicts:
	intaro.intarocrm/classes/general/ICMLLoader.php
This commit is contained in:
m.korolev 2013-09-23 17:44:26 +04:00
commit 62bbcc0f48
11 changed files with 375 additions and 218 deletions

View File

@ -1,14 +1,17 @@
<?php
global $MESS;
IncludeModuleLangFile(__FILE__);
class ICMLLoader {
public $iblocks;
public $filename;
public $propertiesSKU;
public $propertiesProduct;
public $application;
public $encoding = 'utf-8';
protected $fp;
protected $mainSection = 1000000;
@ -17,7 +20,7 @@ class ICMLLoader {
global $USER;
if(!isset($USER))
$USER = new CUser;
if (count($this->iblocks) < count($this->articleProperties))
return false;
@ -26,11 +29,11 @@ class ICMLLoader {
$this->PrepareFile();
$this->PreWriteCatalog();
$categories = $this->GetCategories();
$this->WriteCategories($categories);
$this->PreWriteOffers();
$this->BuildOffers($categories);
$this->PostWriteOffers();
@ -39,7 +42,7 @@ class ICMLLoader {
$this->CloseFile();
return true;
}
protected function PrepareSettings()
@ -63,7 +66,7 @@ class ICMLLoader {
$newText = str_replace("&", "&#x26;", $newText);
return $newText;
}
protected function PrepareFile()
{
$fullFilename = $_SERVER["DOCUMENT_ROOT"] . $this->filename;
@ -74,7 +77,7 @@ class ICMLLoader {
else
return true;
}
protected function PreWriteCatalog()
{
@fwrite($this->fp, "<yml_catalog date=\"" . $this->PrepareValue(Date("Y-m-d H:i:s")) . "\">\n
@ -84,7 +87,7 @@ class ICMLLoader {
);
}
protected function WriteCategories($categories)
{
$stringCategories = "";
@ -99,33 +102,33 @@ class ICMLLoader {
{
@fwrite($this->fp, "<offers>\n");
}
protected function PostWriteOffers()
{
@fwrite($this->fp, "</offers>\n");
}
protected function WriteOffers($offers)
{
@fwrite($this->fp, $offers);
}
protected function PostWriteCatalog()
{
@fwrite($this->fp, "</shop>\n
</yml_catalog>\n");
}
protected function CloseFile()
{
@fclose($this->fp);
}
protected function GetCategories()
{
$categories = array();
foreach ($this->iblocks as $id)
foreach ($this->iblocks as $id)
{
$filter = Array(
"IBLOCK_ID" => $id,
@ -144,18 +147,18 @@ class ICMLLoader {
if (!$hasCategories)
{
$iblock = CIBlock::GetByID($id)->Fetch();
$arRes = Array();
$arRes['ID'] = $this->mainSection + $id;
$arRes['IBLOCK_SECTION_ID'] = 0;
$arRes['NAME'] = "Основной раздел каталога " . $iblock['NAME'];
$arRes['NAME'] = sprintf(GetMessage('ROOT_CATEGORY_FOR_CATALOG'), $iblock['NAME']);
$categories[$arRes['ID']] = $arRes;
}
}
return $categories;
}
protected function BuildCategory($arCategory)
{
return "
@ -166,12 +169,12 @@ class ICMLLoader {
. ">"
. $this->PrepareValue($arCategory["NAME"])
. "</category>\n";
}
protected function BuildOffers(&$allCategories)
{
foreach ($this->iblocks as $key => $id)
foreach ($this->iblocks as $key => $id)
{
$iblock['IBLOCK_DB'] = CIBlock::GetByID($id)->Fetch();
$iblockOffer = CCatalogSKU::GetInfoByProductIBlock($id);
@ -208,7 +211,7 @@ class ICMLLoader {
$dbResProducts = CIBlockElement::GetList(array(), $filter, false, false, $arSelect);
$stringOffers = "";
while ($product = $dbResProducts->GetNextElement()) {
$product = $product->GetFields();
// Get properties of product
@ -237,12 +240,12 @@ class ICMLLoader {
);
}
if (count($categories) == 0) {
$catId = $this->mainSection + $id;
$categories[$catId] = $allCategories[$catId];
}
$existOffer = false;
if (!empty($iblockOffer['IBLOCK_ID'])) {
@ -303,7 +306,7 @@ class ICMLLoader {
}
}
if (!$existOffer) {
$offer = CCatalogProduct::GetByID($product['ID']);
$product['QUANTITY'] = $offer["QUANTITY"];
@ -320,15 +323,15 @@ class ICMLLoader {
$stringOffers .= $this->BuildOffer($product, $categories, $iblock);
}
$count++;
if ($count == 1000) {
$this->WriteOffers($stringOffers);
$stringOffers = "";
}
}
if ($stringOffers != "") {
$this->WriteOffers($stringOffers);
$stringOffers = "";
@ -336,15 +339,15 @@ class ICMLLoader {
}
}
protected function BuildOffer($arOffer, $categories, $iblock)
{
$offer = "";
$offer .= "<offer id=\"" .$this->PrepareValue($arOffer["ID"]) . "\" ".
"productId=\"" . $this->PrepareValue($arOffer["PRODUCT_ID"]) . "\" ".
"quantity=\"" . $this->PrepareValue(DoubleVal($arOffer['QUANTITY'])) . "\">\n";
$offer .= "<url>http://" . $this->PrepareValue($iblock['IBLOCK_DB']['SERVER_NAME']) . $this->PrepareValue($arOffer['DETAIL_PAGE_URL']) . "</url>\n";
$offer .= "<price>" . $this->PrepareValue($arOffer['PRICE']) . "</price>\n";
@ -386,5 +389,5 @@ class ICMLLoader {
$offer.= "</offer>\n";
return $offer;
}
}

View File

@ -13,12 +13,14 @@ class ICrmOrderActions
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
protected static $CRM_ORDER_SITES = 'sites_ids';
protected static $CRM_ORDER_PROPS = 'order_props';
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
/**
* Mass order uploading, without repeating; always returns true, but writes error log
* @param $failed -- flag to export failed orders
* @return boolean
*/
public static function uploadOrders($pSize = 50) {
public static function uploadOrders($pSize = 50, $failed = false) {
//COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0); // -- for test
@ -42,11 +44,16 @@ class ICrmOrderActions
$resOrders = array();
$resCustomers = array();
$lastUpOrderId = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, 0);
$lastOrderId = 0;
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if (!$failedIds)
$failedIds = array();
$dbOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('>ID' => $lastUpOrderId));
$dbFailedOrder = CSaleOrder::GetList(array("ID" => "ASC"), array('ID' => $failedIds));
$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);
@ -63,50 +70,80 @@ class ICrmOrderActions
$api = new IntaroCrm\RestApi($api_host, $api_key);
$arParams = array(
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment,
'optionSites' => $optionsSites,
'optionsOrderProps' => $optionsOrderProps
'optionsPayment' => $optionsPayment,
'optionSites' => $optionsSites,
'optionsOrderProps' => $optionsOrderProps
);
//packmode
$orderCount = 0;
if (!$failed) {
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
if(is_array($optionsSites))
if(!empty($optionsSites))
if(!in_array($arOrder['LID'], $optionsSites))
continue;
$result = self::orderCreate($arOrder, $api, $arParams);
//packmode
if (!$result['order'] || !$result['customer'])
continue;
$orderCount++;
$resOrders[] = $result['order'];
$resCustomers[] = $result['customer'];
$orderCount = 0;
$lastOrderId = $arOrder['ID'];
while ($arOrder = $dbOrder->GetNext()) { // here orders by id asc
if (is_array($optionsSites))
if (!empty($optionsSites))
if (!in_array($arOrder['LID'], $optionsSites))
continue;
if($orderCount >= $pSize) {
$customers = $api->customerUpload($resCustomers);
// error pushing customers
if ($api->getStatusCode() != 201) {
//handle err
//self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError());
if($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
$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);
// error pushing customers
if ($api->getStatusCode() != 201) {
//handle err
//self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
$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
}
}
if (!empty($resOrders)) {
$customers = $api->customerUpload($resCustomers);
// error pushing customers
if ($api->getStatusCode() != 201) {
//handle err
//self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
$orders = $api->orderUpload($resOrders);
// error pushing orders
@ -114,42 +151,98 @@ class ICrmOrderActions
//handle err
self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError());
if($api->getStatusCode() != 460) // some orders were sent
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
if ($lastOrderId)
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $lastOrderId);
} else {
// failed orders upload
$orderCount = 0;
$recOrders = array();
while ($arOrder = $dbFailedOrder->GetNext()) { // here orders by id asc
if (is_array($optionsSites))
if (!empty($optionsSites))
if (!in_array($arOrder['LID'], $optionsSites))
continue;
$result = self::orderCreate($arOrder, $api, $arParams);
if (!$result['order'] || !$result['customer'])
continue;
$orderCount++;
$resOrders[] = $result['order'];
$resCustomers[] = $result['customer'];
$recOrders[] = $arOrder['ID'];
if ($orderCount >= $pSize) {
$customers = $api->customerUpload($resCustomers);
// error pushing customers
if ($api->getStatusCode() != 201) {
//handle err
//self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
$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 (!empty($recOrders)) {
$failedIds = array_merge(array_diff($failedIds, $recOrders)); // clear success ids
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds));
}
return true; // end of pack
}
}
if (!empty($resOrders)) {
$customers = $api->customerUpload($resCustomers);
// error pushing customers
if ($api->getStatusCode() != 201) {
//handle err
//self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
$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 (!empty($recOrders)) {
$failedIds = array_merge(array_diff($failedIds, $recOrders)); // clear success ids
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds));
}
}
if (!empty($resOrders)) {
$customers = $api->customerUpload($resCustomers);
// error pushing customers
if ($api->getStatusCode() != 201) {
//handle err
//self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError());
if ($api->getStatusCode() != 460) // some orders were sent
return false; // in pack mode return errors
}
$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; //all ok!
}
@ -200,7 +293,7 @@ class ICrmOrderActions
$api = new IntaroCrm\RestApi($api_host, $api_key);
$orderHistory = $api->orderHistory();
// pushing existing orders
foreach ($orderHistory as $order) {
@ -249,7 +342,9 @@ class ICrmOrderActions
if(isset($order['customer']) && $order['customer']) $userId = $order['customer'];
$LID = $arFields['LID'];
$rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID']));
while ($ar = $rsOrderProps->Fetch()) {
if (isset($order['deliveryAddress']) && $order['deliveryAddress']) {
switch ($ar['CODE']) {
@ -403,22 +498,26 @@ class ICrmOrderActions
CSaleBasket::Update($p['ID'], $arProduct);
}*/
// orderUpdate
$arFields = self::clearArr(array(
'PRICE_DELIVERY' => $order['deliveryCost'],
'PRICE' => $order['summ'],
'DATE_MARKED' => $order['markDatetime'],
'USER_ID' => $userId, //$order['customer']
'PAY_SYSTEM_ID' => $optionsPayTypes[$order['paymentType']],
'PAYED' => $optionsPayment[$order['paymentStatus']],
'PERSON_TYPE_ID' => $optionsOrderTypes[$order['orderType']],
'DELIVERY_ID' => $optionsDelivTypes[$order['deliveryType']],
'STATUS_ID' => $optionsPayStatuses[$order['status']]
'PRICE_DELIVERY' => $order['deliveryCost'],
'PRICE' => $order['summ'],
'DATE_MARKED' => $order['markDatetime'],
'USER_ID' => $userId, //$order['customer']
'PAY_SYSTEM_ID' => $optionsPayTypes[$order['paymentType']],
'PAYED' => $optionsPayment[$order['paymentStatus']],
//'PERSON_TYPE_ID' => $optionsOrderTypes[$order['orderType']],
'DELIVERY_ID' => $optionsDelivTypes[$order['deliveryType']],
'STATUS_ID' => $optionsPayStatuses[$order['status']],
'REASON_CANCELED' => $order['statusComment'],
'USER_DESCRIPTION' => $order['customerComment'],
'COMMENTS' => $order['managerComment']
));
$GLOBALS['INTARO_CRM_FROM_HISTORY'] = true;
CSaleOrder::Update($order['externalId'], $arFields);
}
}
@ -450,6 +549,10 @@ class ICrmOrderActions
public static function uploadOrdersAgent() {
self::uploadOrders();
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if(is_array($failedIds) && !empty($failedIds))
self::uploadOrders(50, true); // upload failed orders
return 'ICrmOrderActions::uploadOrdersAgent();';
}
@ -548,46 +651,46 @@ class ICrmOrderActions
$rsOrderProps = CSaleOrderPropsValue::GetList(array(), array('ORDER_ID' => $arFields['ID']));
while ($ar = $rsOrderProps->Fetch()) {
switch ($ar['CODE']) {
case $arParams['optionsOrderProps']['index']: $resOrderDeliveryAddress['index'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['index']: $resOrderDeliveryAddress['index'] = self::toJSON($ar['VALUE']);
break;
case 'CITY': $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['text']: $resOrderDeliveryAddress['text'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['text']: $resOrderDeliveryAddress['text'] = self::toJSON($ar['VALUE']);
break;
case 'LOCATION': if(!isset($resOrderDeliveryAddress['city']) && !$resOrderDeliveryAddress['city']) {
$resOrderDeliveryAddress['city'] = CSaleLocation::GetByID($ar['VALUE']);
$resOrderDeliveryAddress['city'] = self::toJSON($resOrderDeliveryAddress['city']['CITY_NAME_LANG']);
}
break;
case $arParams['optionsOrderProps']['fio']: $contactNameArr = self::explodeFIO($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['fio']: $contactNameArr = self::explodeFIO($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['phone']: $resOrder['phone'] = $ar['VALUE'];
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['phone']: $resOrder['phone'] = $ar['VALUE'];
break;
case $arParams['optionsOrderProps']['email']: $resOrder['email'] = $ar['VALUE'];
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['email']: $resOrder['email'] = $ar['VALUE'];
break;
}
if (count($arParams['optionsOrderProps'] > 5)) {
if (count($arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']] > 5)) {
switch ($ar['CODE']) {
/*case $arParams['optionsOrderProps']['country']: $resOrderDeliveryAddress['country'] = self::toJSON($ar['VALUE']);
/*case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['country']: $resOrderDeliveryAddress['country'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['region']: $resOrderDeliveryAddress['region'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['region']: $resOrderDeliveryAddress['region'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['city']: $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['city']: $resOrderDeliveryAddress['city'] = self::toJSON($ar['VALUE']);
break; */
case $arParams['optionsOrderProps']['street']: $resOrderDeliveryAddress['street'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['street']: $resOrderDeliveryAddress['street'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['building']: $resOrderDeliveryAddress['building'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['building']: $resOrderDeliveryAddress['building'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['flat']: $resOrderDeliveryAddress['flat'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['flat']: $resOrderDeliveryAddress['flat'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['inercomcode']: $resOrderDeliveryAddress['intercomcode'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['inercomcode']: $resOrderDeliveryAddress['intercomcode'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['floor']: $resOrderDeliveryAddress['floor'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['floor']: $resOrderDeliveryAddress['floor'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['block']: $resOrderDeliveryAddress['block'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['block']: $resOrderDeliveryAddress['block'] = self::toJSON($ar['VALUE']);
break;
case $arParams['optionsOrderProps']['house']: $resOrderDeliveryAddress['house'] = self::toJSON($ar['VALUE']);
case $arParams['optionsOrderProps'][$arFields['PERSON_TYPE_ID']]['house']: $resOrderDeliveryAddress['house'] = self::toJSON($ar['VALUE']);
break;
}
}
@ -624,6 +727,7 @@ class ICrmOrderActions
$createdAt = $createdAt->format('Y-m-d H:i:s');
$resOrder = array(
'number' => $arFields['ACCOUNT_NUMBER'],
'phone' => $resOrder['phone'],
'email' => $resOrder['email'],
'deliveryCost' => $arFields['PRICE_DELIVERY'],
@ -636,7 +740,9 @@ class ICrmOrderActions
'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']],
'deliveryType' => $arParams['optionsDelivTypes'][$resultDeliveryTypeId],
'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']],
'statusComment' => $arFields['USER_DESCRIPTION'],
'statusComment' => $arFields['REASON_CANCELED'],
'customerComment' => $arFields['USER_DESCRIPTION'],
'managerComment' => $arFields['COMMENTS'],
'createdAt' => $createdAt,
'deliveryAddress' => $resOrderDeliveryAddress,
'items' => $items
@ -657,8 +763,6 @@ class ICrmOrderActions
}
$resOrder = self::clearArr($resOrder);
self::eventLog('ICrmOrderActions::orderHistory', 'iblock', json_encode($resOrder));
if($send)
return $api->orderEdit($resOrder);
@ -749,4 +853,19 @@ class ICrmOrderActions
));
}
}
public static function getLocationCityId($cityName) {
if(!$cityName)
return;
$dbLocation = CSaleLocation::GetList(
array(
"SORT" => "ASC",
"CITY_NAME_LANG" => "ASC"
),
array("LID" => "ru", "CITY_NAME" => $cityName), false, false, array());
if($location = $dbLocation->Fetch())
return $location['ID'];
}
}

View File

@ -13,15 +13,37 @@ class ICrmOrderEvent {
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';
protected static $CRM_ORDER_PROPS = 'order_props';
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
/**
* onBeforeOrderAdd
*
* @param mixed $arFields - Order arFields
*/
function onBeforeOrderAdd($arFields = array()) {
$GLOBALS['INTARO_CRM_ORDER_ADD'] = true;
return;
}
/**
* onUpdateOrder
*
* @param mixed $ID - Order id
* @param mixed $arFields - Order arFields
*/
function onUpdateOrder($ID, $arFields = array()) {
self::writeDataOnOrderCreate($ID);
function onUpdateOrder($ID, $arFields) {
if(isset($GLOBALS['INTARO_CRM_ORDER_ADD']) && $GLOBALS['INTARO_CRM_ORDER_ADD'])
return;
if(isset($GLOBALS['INTARO_CRM_FROM_HISTORY']) && $GLOBALS['INTARO_CRM_FROM_HISTORY'])
return;
if(isset($arFields['LOCKED_BY']) && $arFields['LOCKED_BY'])
return;
self::writeDataOnOrderCreate($ID, $arFields);
}
@ -34,7 +56,17 @@ class ICrmOrderEvent {
* @param mixed $arFields - Order arFields for sending template
*/
function onSendOrderMail($ID, &$eventName, &$arFields) {
self::writeDataOnOrderCreate($ID);
if(self::writeDataOnOrderCreate($ID))
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $ID);
else {
$failedIds = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, 0));
if(!$failedIds)
$failedIds = array();
$failedIds[] = $ID;
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_FAILED_IDS, serialize($failedIds));
}
}
/**
@ -42,7 +74,8 @@ class ICrmOrderEvent {
*
* @param integer $ID - Order Id
*/
function writeDataOnOrderCreate($ID) {
function writeDataOnOrderCreate($ID, $arFields) {
if (!CModule::IncludeModule('iblock')) {
//handle err
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'iblock', 'module not found');
@ -60,6 +93,9 @@ class ICrmOrderEvent {
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'catalog', 'module not found');
return true;
}
$GLOBALS['INTARO_CRM_ORDER_ADD'] = false;
$GLOBALS['INTARO_CRM_FROM_HISTORY'] = false;
$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);
@ -70,27 +106,43 @@ class ICrmOrderEvent {
$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));
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
$api = new IntaroCrm\RestApi($api_host, $api_key);
$arParams = array(
$arParams = ICrmOrderActions::clearArr(array(
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment
);
'optionsPayment' => $optionsPayment,
'optionsOrderProps' => $optionsOrderProps
));
$arOrder = CSaleOrder::GetById($ID);
if (is_array($arFields) && !empty($arFields)) {
$arFieldsNew = array(
'USER_ID' => $arOrder['USER_ID'],
'ID' => $ID,
'PERSON_TYPE_ID' => $arOrder['PERSON_TYPE_ID'],
'CANCELED' => $arOrder['CANCELED'],
'STATUS_ID' => $arOrder['STATUS_ID'],
'DATE_INSERT' => $arOrder['DATE_INSERT'],
'LID' => $arOrder['LID']
);
$arFieldsNew = array_merge($arFieldsNew, $arFields);
$arOrder = $arFieldsNew;
}
$result = ICrmOrderActions::orderCreate($arOrder, $api, $arParams, true);
if(!$result) {
ICrmOrderActions::eventLog('ICrmOrderEvent::writeDataOnOrderCreate', 'ICrmOrderActions::orderCreate', 'error during creating order');
return true;
return false;
}
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_LAST_ID, $ID);
return true;
}
@ -188,6 +240,7 @@ class ICrmOrderEvent {
);
$api->orderEdit($order);
// error pushing order
if ($api->getStatusCode() != 201)

View File

@ -33,6 +33,7 @@ class intaro_intarocrm extends CModule {
var $CRM_ORDER_SITES = 'sites_ids';
var $CRM_ORDER_PROPS = 'order_props';
var $CRM_ORDER_DISCHARGE = 'order_discharge';
var $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
var $INSTALL_PATH;
function intaro_intarocrm() {
@ -64,7 +65,7 @@ class intaro_intarocrm extends CModule {
if (!date_default_timezone_get()) {
if (!ini_get('date.timezone')) {
$APPLICATION->ThrowException(GetMessage("DATE_TIMEZONE_ERR"));
return false;
return false;
}
}
@ -77,27 +78,19 @@ class intaro_intarocrm extends CModule {
$arResult['orderProps'] = array(
array(
'NAME' => GetMessage('FIO'),
'ID' => 'fio'
),
array(
'NAME' => GetMessage('ZIP'),
'ID' => 'index'
'ID' => 'fio'
),
array(
'NAME' => GetMessage('PHONE'),
'ID' => 'phone'
'ID' => 'phone'
),
array(
'NAME' => GetMessage('EMAIL'),
'ID' => 'email'
),
array(
'NAME' => GetMessage('ZIP'),
'ID' => 'index'
'ID' => 'email'
),
array(
'NAME' => GetMessage('ADDRESS'),
'ID' => 'text'
'ID' => 'text'
),
// address
/* array(
@ -114,35 +107,35 @@ class intaro_intarocrm extends CModule {
), */
array(
'NAME' => GetMessage('ZIP'),
'ID' => 'index'
'ID' => 'index'
),
array(
'NAME' => GetMessage('STREET'),
'ID' => 'street'
'ID' => 'street'
),
array(
'NAME' => GetMessage('BUILDING'),
'ID' => 'building'
'ID' => 'building'
),
array(
'NAME' => GetMessage('FLAT'),
'ID' => 'flat'
'ID' => 'flat'
),
array(
'NAME' => GetMessage('INTERCOMCODE'),
'ID' => 'intercomcode'
'ID' => 'intercomcode'
),
array(
'NAME' => GetMessage('FLOOR'),
'ID' => 'floor'
'ID' => 'floor'
),
array(
'NAME' => GetMessage('BLOCK'),
'ID' => 'block'
'ID' => 'block'
),
array(
'NAME' => GetMessage('HOUSE'),
'ID' => 'house'
'ID' => 'house'
)
);
@ -698,7 +691,7 @@ class intaro_intarocrm extends CModule {
//form orderProps
$dbProp = CSaleOrderProps::GetList(array(), array());
while ($arProp = $dbProp->GetNext()) {
$arResult['arProp'][] = $arProp;
$arResult['arProp'][$arProp['PERSON_TYPE_ID']][] = $arProp;
}
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_TYPES_ARR, serialize($orderTypesArr));
@ -708,6 +701,7 @@ class intaro_intarocrm extends CModule {
COption::SetOptionString($this->MODULE_ID, $this->CRM_PAYMENT, serialize($paymentArr));
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_LAST_ID, 0);
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE, 0);
COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS, serialize(array()));
$APPLICATION->IncludeAdminFile(
GetMessage('MODULE_INSTALL_TITLE'),
@ -772,7 +766,7 @@ class intaro_intarocrm extends CModule {
$propsCount = 0;
$_orderPropsArr = array();
foreach ($arResult['orderProps'] as $orderProp) {
if ((!(int) htmlspecialchars(trim($_POST['address-detail-' . $orderType['ID']]))) && $propsCount > 5)
if ((!(int) htmlspecialchars(trim($_POST['address-detail-' . $orderType['ID']]))) && $propsCount > 4)
break;
$_orderPropsArr[$orderProp['ID']] = htmlspecialchars(trim($_POST['order-prop-' . $orderProp['ID'] . '-' . $orderType['ID']]));
$propsCount++;
@ -879,6 +873,7 @@ class intaro_intarocrm extends CModule {
}
RegisterModule($this->MODULE_ID);
RegisterModuleDependences("sale", "OnSalePayOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSalePayOrder");
RegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
$this->CopyFiles();
if (isset($_POST['LOAD_NOW'])) {
@ -1043,9 +1038,13 @@ class intaro_intarocrm extends CModule {
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_SITES);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_PROPS);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_DISCHARGE);
COption::RemoveOption($this->MODULE_ID, $this->CRM_ORDER_FAILED_IDS);
UnRegisterModuleDependences("sale", "OnSalePayOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSalePayOrder");
UnRegisterModuleDependences("sale", "OnSaleCancelOrder", $this->MODULE_ID, "ICrmOrderEvent", "onSaleCancelOrder");
UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $this->MODULE_ID, "ICrmOrderEvent", "onSendOrderMail");
UnRegisterModuleDependences("sale", "OnOrderUpdate", $this->MODULE_ID, "ICrmOrderEvent", "onUpdateOrder");
UnRegisterModuleDependences("sale", "OnBeforeOrderAdd", $this->MODULE_ID, "ICrmOrderEvent", "onBeforeOrderAdd");
if (CModule::IncludeModule("catalog")) {
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/catalog_export/' . $this->INTARO_CRM_EXPORT . '_run.php')) {
$dbProfile = CCatalogExport::GetList(array(), array("FILE_NAME" => $this->INTARO_CRM_EXPORT));

View File

@ -29,9 +29,6 @@
<!--<tr align="center">
<td colspan="2"><b><?php echo GetMessage('INFO_3'); ?></b></td>
</tr>-->
<tr align="center">
<td colspan="2"><b><?php echo GetMessage('INFO_3'); ?></b></td>
</tr>
<tr align="center">
<td colspan="2">&nbsp;</td>
</tr>

View File

@ -58,13 +58,12 @@ $defaultOrderProps = array(
</tr>
<?php foreach($arResult['bitrixOrderTypesList'] as $bitrixOrderType): ?>
<tr class="heading">
<td colspan="2"><b><?php echo $bitrixOrderType['NAME']; ?></b></td>
<td colspan="2"><b><?php echo GetMessage('ORDER_TYPE_INFO') . ' ' . $bitrixOrderType['NAME']; ?></b></td>
</tr>
<?php $countProps = 0; foreach($arResult['orderProps'] as $orderProp): ?>
<?php if($orderProp['ID'] == 'text'): ?>
<tr class="heading">
<td colspan="2">
<td colspan="2" style="background-color: transparent;">
<b>
<label><input class="addr" type="radio" name="address-detail-<?php echo $bitrixOrderType['ID'];; ?>" value="0" <?php if(count($defaultOrderProps[$bitrixOrderType['ID']]) < 6) echo "checked"; ?>><?php echo GetMessage('ADDRESS_SHORT'); ?></label>
<label><input class="addr" type="radio" name="address-detail-<?php echo $bitrixOrderType['ID']; ?>" value="1" <?php if(count($defaultOrderProps[$bitrixOrderType['ID']]) > 5) echo "checked"; ?>><?php echo GetMessage('ADDRESS_FULL'); ?></label>
@ -72,16 +71,16 @@ $defaultOrderProps = array(
</td>
</tr>
<?php endif; ?>
<tr <?php if ($countProps > 5) echo 'class="address-detail-' . $bitrixOrderType['ID'] . '"'; if(($countProps > 5) && (count($defaultOrderProps[$bitrixOrderType['ID']]) < 6)) echo 'style="display:none;"';?>>
<tr <?php if ($countProps > 4) echo 'class="address-detail-' . $bitrixOrderType['ID'] . '"'; if(($countProps > 4) && (count($defaultOrderProps[$bitrixOrderType['ID']]) < 6)) echo 'style="display:none;"';?>>
<td width="50%" class="adm-detail-content-cell-l" name="<?php echo $orderProp['ID']; ?>">
<?php echo $orderProp['NAME']; ?>
<?php echo $APPLICATION->ConvertCharset($orderProp['NAME'], 'utf-8', SITE_CHARSET);; ?>
</td>
<td width="50%" class="adm-detail-content-cell-r">
<select name="order-prop-<?php echo $orderProp['ID'] . '-' . $bitrixOrderType['ID']; ?>" class="typeselect">
<option value=""></option>
<?php foreach ($arResult['arProp'] as $arProp): ?>
<?php foreach ($arResult['arProp'][$bitrixOrderType['ID']] as $arProp): ?>
<option value="<?php echo $arProp['CODE']; ?>" <?php if ($defaultOrderProps[$bitrixOrderType['ID']][$orderProp['ID']] == $arProp['CODE']) echo 'selected'; ?>>
<?php echo $arProp['NAME']; ?>
<?php echo $APPLICATION->ConvertCharset($arProp['NAME'], 'utf-8', SITE_CHARSET); ?>
</option>
<?php endforeach; ?>
</select>

View File

@ -0,0 +1,2 @@
<?php
$MESS["ROOT_CATEGORY_FOR_CATALOG"] = "Корневой раздел для каталога \"%s\"";

View File

@ -15,7 +15,7 @@ $MESS ['ERR_ARTICLE_IBLOCK'] = 'Не установлены артикулы';
$MESS ['DATE_TIMEZONE_ERR'] = 'Не указана временная зона в настройках php.';
$MESS ['ORDER_PROPS'] = 'Настройки соответствия полей заказа IntaroCRM свойствам заказа 1С-Битрикс';
$MESS ['FIO'] = 'ф.И.О.';
$MESS ['FIO'] = 'Ф.И.О.';
$MESS ['ZIP'] = 'Индекс';
$MESS ['ADDRESS'] = 'Адрес (строкой)';
$MESS ['PHONE'] = 'Телефон';

View File

@ -12,6 +12,7 @@ $MESS ['START_3'] = 'Возобновить выгрузку';
$MESS ['MOD_NEXT_STEP'] = 'Следующий шаг';
$MESS ['MOD_PREV_STEP'] = 'Предыдущий шаг';
$MESS ['INFO_2'] = ' Задайте соответствие между полями заказа 1C-Битрикс и IntaroCRM.';
$MESS ['ORDER_TYPE_INFO'] = 'Тип заказа:';
$MESS ['ORDER_PROPS'] = 'Настройки соответствия полей заказа IntaroCRM свойствам заказа 1С-Битрикс';
$MESS ['FIO'] = 'Ф.И.О.';

View File

@ -50,4 +50,6 @@ $MESS ['FLOOR'] = 'Этаж';
$MESS ['BLOCK'] = 'Подъезд';
$MESS ['HOUSE'] = 'Строение / корпус';
$MESS ['ADDRESS_SHORT'] = 'Краткий адрес';
$MESS ['ADDRESS_FULL'] = 'Детальный адрес';
$MESS ['ADDRESS_FULL'] = 'Детальный адрес';
$MESS ['ORDER_TYPE_INFO'] = 'Тип заказа:';

View File

@ -32,10 +32,6 @@ $arResult['orderProps'] = array(
'NAME' => GetMessage('FIO'),
'ID' => 'fio'
),
array(
'NAME' => GetMessage('ZIP'),
'ID' => 'index'
),
array(
'NAME' => GetMessage('PHONE'),
'ID' => 'phone'
@ -44,10 +40,6 @@ $arResult['orderProps'] = array(
'NAME' => GetMessage('EMAIL'),
'ID' => 'email'
),
array(
'NAME' => GetMessage('ZIP'),
'ID' => 'index'
),
array(
'NAME' => GetMessage('ADDRESS'),
'ID' => 'text'
@ -227,36 +219,26 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
// remove depenedencies
UnRegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail");
UnRegisterModuleDependences("sale", "OnOrderUpdate", $mid, "ICrmOrderEvent", "onUpdateOrder");
// new agent
$dateAgent = new DateTime();
$intAgent = new DateInterval('PT60S'); // PT60S - 60 sec;
$dateAgent->add($intAgent);
CAgent::AddAgent(
"ICrmOrderActions::uploadOrdersAgent();", $mid, "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
);
UnRegisterModuleDependences("sale", "OnBeforeOrderAdd", $mid, "ICrmOrderEvent", "onBeforeOrderAdd");
} else if (($orderDischarge != $previousDischarge) && ($orderDischarge == 1)) {
// remove agent
CAgent::RemoveAgent("ICrmOrderActions::uploadOrdersAgent();", $mid);
// event dependencies
RegisterModuleDependences("sale", "OnOrderNewSendEmail", $mid, "ICrmOrderEvent", "onSendOrderMail");
RegisterModuleDependences("sale", "OnOrderUpdate", $mid, "ICrmOrderEvent", "onUpdateOrder");
RegisterModuleDependences("sale", "OnBeforeOrderAdd", $mid, "ICrmOrderEvent", "onBeforeOrderAdd");
}
$orderPropsArr = array();
foreach ($orderTypesList as $orderType) {
$propsCount = 0;
$_orderPropsArr = array();
foreach ($arResult['orderProps'] as $orderProp) {
if ((!(int) htmlspecialchars(trim($_POST['address-detail-' . $orderType['ID']]))) && $propsCount > 5)
break;
$_orderPropsArr[$orderProp['ID']] = htmlspecialchars(trim($_POST['order-prop-' . $orderProp['ID'] . '-' . $orderType['ID']]));
$propsCount++;
}
$orderPropsArr[$orderType['ID']] = $_orderPropsArr;
$orderPropsArr = array();
foreach ($orderTypesList as $orderType) {
$propsCount = 0;
$_orderPropsArr = array();
foreach ($arResult['orderProps'] as $orderProp) {
if ((!(int) htmlspecialchars(trim($_POST['address-detail-' . $orderType['ID']]))) && $propsCount > 4)
break;
$_orderPropsArr[$orderProp['ID']] = htmlspecialchars(trim($_POST['order-prop-' . $orderProp['ID'] . '-' . $orderType['ID']]));
$propsCount++;
}
$orderPropsArr[$orderType['ID']] = $_orderPropsArr;
}
COption::SetOptionString($mid, $CRM_ORDER_TYPES_ARR, serialize($orderTypesArr));
@ -380,7 +362,7 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
$dbProp = CSaleOrderProps::GetList(array(), array());
while ($arProp = $dbProp->GetNext()) {
$arResult['arProp'][] = $arProp;
$arResult['arProp'][$arProp['PERSON_TYPE_ID']][] = $arProp;
}
//saved cat params
@ -583,13 +565,13 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
</tr>
<?php foreach($arResult['bitrixOrderTypesList'] as $bitrixOrderType): ?>
<tr class="heading">
<td colspan="2"><b><?php echo $bitrixOrderType['NAME']; ?></b></td>
<td colspan="2"><b><?php echo GetMessage('ORDER_TYPE_INFO') . ' ' . $bitrixOrderType['NAME']; ?></b></td>
</tr>
<?php $countProps = 0; foreach($arResult['orderProps'] as $orderProp): ?>
<?php if($orderProp['ID'] == 'text'): ?>
<tr class="heading">
<td colspan="2">
<td colspan="2" style="background-color: transparent;">
<b>
<label><input class="addr" type="radio" name="address-detail-<?php echo $bitrixOrderType['ID'];; ?>" value="0" <?php if(count($optionsOrderProps[$bitrixOrderType['ID']]) < 6) echo "checked"; ?>><?php echo GetMessage('ADDRESS_SHORT'); ?></label>
<label><input class="addr" type="radio" name="address-detail-<?php echo $bitrixOrderType['ID']; ?>" value="1" <?php if(count($optionsOrderProps[$bitrixOrderType['ID']]) > 5) echo "checked"; ?>><?php echo GetMessage('ADDRESS_FULL'); ?></label>
@ -597,16 +579,16 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) {
</td>
</tr>
<?php endif; ?>
<tr <?php if ($countProps > 5) echo 'class="address-detail-' . $bitrixOrderType['ID'] . '"'; if(($countProps > 5) && (count($optionsOrderProps[$bitrixOrderType['ID']]) < 6)) echo 'style="display:none;"';?>>
<tr <?php if ($countProps > 4) echo 'class="address-detail-' . $bitrixOrderType['ID'] . '"'; if(($countProps > 4) && (count($optionsOrderProps[$bitrixOrderType['ID']]) < 6)) echo 'style="display:none;"';?>>
<td width="50%" class="adm-detail-content-cell-l" name="<?php echo $orderProp['ID']; ?>">
<?php echo $orderProp['NAME']; ?>
</td>
<td width="50%" class="adm-detail-content-cell-r">
<select name="order-prop-<?php echo $orderProp['ID'] . '-' . $bitrixOrderType['ID']; ?>" class="typeselect">
<option value=""></option>
<?php foreach ($arResult['arProp'] as $arProp): ?>
<?php foreach ($arResult['arProp'][$bitrixOrderType['ID']] as $arProp): ?>
<option value="<?php echo $arProp['CODE']; ?>" <?php if ($optionsOrderProps[$bitrixOrderType['ID']][$orderProp['ID']] == $arProp['CODE']) echo 'selected'; ?>>
<?php echo $arProp['NAME']; ?>
<?php echo $APPLICATION->ConvertCharset($arProp['NAME'], 'utf-8', SITE_CHARSET); ?>
</option>
<?php endforeach; ?>
</select>