1
0
mirror of synced 2024-12-05 03:36:05 +03:00
bitrix-module/intaro.retailcrm/classes/general/history/RetailCrmHistory_v4.php

1277 lines
64 KiB
PHP
Raw Normal View History

2016-09-15 16:42:10 +03:00
<?php
IncludeModuleLangFile(__FILE__);
class RetailCrmHistory
{
public static $MODULE_ID = 'intaro.retailcrm';
public static $CRM_API_HOST_OPTION = 'api_host';
public static $CRM_API_KEY_OPTION = 'api_key';
public static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
public static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
public static $CRM_PAYMENT_TYPES = 'pay_types_arr';
public static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
public static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
public static $CRM_ORDER_LAST_ID = 'order_last_id';
public static $CRM_SITES_LIST = 'sites_list';
public static $CRM_ORDER_PROPS = 'order_props';
public static $CRM_LEGAL_DETAILS = 'legal_details';
public static $CRM_CUSTOM_FIELDS = 'custom_fields';
public static $CRM_CONTRAGENT_TYPE = 'contragent_type';
public static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
public static $CRM_ORDER_HISTORY = 'order_history';
public static $CRM_CUSTOMER_HISTORY = 'customer_history';
public static $CRM_CATALOG_BASE_PRICE = 'catalog_base_price';
public static $CRM_ORDER_NUMBERS = 'order_numbers';
public static $CRM_CANSEL_ORDER = 'cansel_order';
const CANCEL_PROPERTY_CODE = 'INTAROCRM_IS_CANCELED';
2016-10-04 17:57:39 +03:00
public static function customerHistory()
{
2016-09-15 16:42:10 +03:00
if (!CModule::IncludeModule("iblock")) {
2017-09-07 13:27:28 +03:00
RCrmActions::eventLog('RetailCrmHistory::customerHistory', 'iblock', 'module not found');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return false;
}
if (!CModule::IncludeModule("sale")) {
2017-09-07 13:27:28 +03:00
RCrmActions::eventLog('RetailCrmHistory::customerHistory', 'sale', 'module not found');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return false;
}
if (!CModule::IncludeModule("catalog")) {
2017-09-07 13:27:28 +03:00
RCrmActions::eventLog('RetailCrmHistory::customerHistory', 'catalog', 'module not found');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return 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);
$api = new RetailCrm\ApiClient($api_host, $api_key);
$historyFilter = array();
$historyStart = COption::GetOptionString(self::$MODULE_ID, self::$CRM_CUSTOMER_HISTORY);
2017-09-04 11:36:04 +03:00
2016-10-04 17:57:39 +03:00
if ($historyStart && $historyStart > 0) {
2016-09-15 16:42:10 +03:00
$historyFilter['sinceId'] = $historyStart;
}
2017-09-04 11:36:04 +03:00
while (true) {
2017-09-07 13:27:28 +03:00
$customerHistory = RCrmActions::apiMethod($api, 'customersHistory', __METHOD__, $historyFilter);
2016-09-15 16:42:10 +03:00
$customerH = isset($customerHistory['history']) ? $customerHistory['history'] : array();
$log = new Logger();
$log->write($customerH, 'customerHistory');
2017-09-04 11:36:04 +03:00
if (count($customerH) == 0) {
if ($customerHistory['history']['totalPageCount'] > $customerHistory['history']['currentPage']) {
$historyFilter['page'] = $customerHistory['history']['currentPage'] + 1;
continue;
}
2016-09-15 16:42:10 +03:00
return true;
}
$customers = self::assemblyCustomer($customerH);
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
$newUser = new CUser;
2016-10-04 17:57:39 +03:00
foreach ($customers as $customer) {
2016-09-15 16:42:10 +03:00
if (function_exists('retailCrmBeforeCustomerSave')) {
$newResCustomer = retailCrmBeforeCustomerSave($customer);
if (is_array($newResCustomer) && !empty($newResCustomer)) {
$customer = $newResCustomer;
2017-09-04 11:36:04 +03:00
} elseif ($newResCustomer === false) {
RCrmActions::eventLog('RetailCrmHistory::customerHistory', 'retailCrmBeforeCustomerSave()', 'UserCrmId = ' . $customer['id'] . '. Sending canceled after retailCrmBeforeCustomerSave');
continue;
2016-09-15 16:42:10 +03:00
}
}
2016-10-04 17:57:39 +03:00
if (isset($customer['deleted'])) {
2016-09-15 16:42:10 +03:00
continue;
}
2019-01-22 11:39:23 +03:00
if (isset($customer['externalId']) && !is_numeric($customer['externalId'])) {
unset($customer['externalId']);
}
2016-09-15 16:42:10 +03:00
if (!isset($customer['externalId'])) {
if (!isset($customer['id'])) {
continue;
}
$registerNewUser = true;
if (!isset($customer['email']) || $customer['email'] == '') {
$login = $customer['email'] = uniqid('user_' . time()) . '@crm.com';
} else {
$dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $customer['email']));
switch ($dbUser->SelectedRowsCount()) {
case 0:
$login = $customer['email'];
break;
case 1:
$arUser = $dbUser->Fetch();
$registeredUserID = $arUser['ID'];
$registerNewUser = false;
break;
default:
$login = uniqid('user_' . time()) . '@crm.com';
break;
}
}
if ($registerNewUser === true) {
$userPassword = uniqid();
$arFields = array(
"EMAIL" => $customer['email'],
"LOGIN" => $login,
"ACTIVE" => "Y",
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword
);
$registeredUserID = $newUser->Add($arFields);
if ($registeredUserID === false) {
2017-09-07 13:27:28 +03:00
RCrmActions::eventLog('RetailCrmHistory::customerHistory', 'CUser::Register', 'Error register user');
2016-09-15 16:42:10 +03:00
continue;
}
2017-09-04 11:36:04 +03:00
if(RCrmActions::apiMethod($api, 'customersFixExternalIds', __METHOD__, array(array('id' => $customer['id'], 'externalId' => $registeredUserID))) == false) {
continue;
2016-09-15 16:42:10 +03:00
}
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
}
$customer['externalId'] = $registeredUserID;
}
2016-10-04 17:57:39 +03:00
if (isset($customer['externalId'])) {
2016-09-15 16:42:10 +03:00
$arUser = array();
2016-10-04 17:57:39 +03:00
if (array_key_exists('firstName', $customer)) {
2016-09-15 16:42:10 +03:00
$arUser["NAME"] = $customer['firstName'] ? RCrmActions::fromJSON($customer['firstName']) : '';
}
2016-10-04 17:57:39 +03:00
if (array_key_exists('lastName', $customer)) {
2016-09-15 16:42:10 +03:00
$arUser["LAST_NAME"] = $customer['lastName'] ? RCrmActions::fromJSON($customer['lastName']) : '';
}
2016-10-04 17:57:39 +03:00
if (array_key_exists('patronymic', $customer)) {
2016-09-15 16:42:10 +03:00
$arUser["SECOND_NAME"] = $customer['patronymic'] ? RCrmActions::fromJSON($customer['patronymic']) : '';
}
2017-09-04 11:36:04 +03:00
// if (array_key_exists('email', $customer)) {
// $arUser["EMAIL"] = $customer['email'] ? RCrmActions::fromJSON($customer['email']) : '';
// }
2016-09-15 16:42:10 +03:00
2016-10-04 17:57:39 +03:00
if (isset($customer['phones'])) {
2016-09-15 16:42:10 +03:00
$user = CUser::GetList(($by="ID"), ($order="desc"), array('ID' => $customer['externalId']), array('FIELDS' => array('PERSONAL_PHONE', 'PERSONAL_MOBILE')))->fetch();
2016-10-04 17:57:39 +03:00
foreach ($customer['phones'] as $phone) {
if (isset($phone['old_number']) && in_array($phone['old_number'], $user)) {
2016-09-15 16:42:10 +03:00
$key = array_search($phone['old_number'], $user);
2016-10-04 17:57:39 +03:00
if (isset($phone['number'])) {
2016-09-15 16:42:10 +03:00
$arUser[$key] = $phone['number'];
$user[$key] = $phone['number'];
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
$arUser[$key] = '';
$user[$key] = '';
}
}
2016-10-04 17:57:39 +03:00
if (isset($phone['number'])) {
if ((!isset($user['PERSONAL_PHONE']) || strlen($user['PERSONAL_PHONE']) == 0) && $user['PERSONAL_MOBILE'] != $phone['number']) {
2016-09-15 16:42:10 +03:00
$arUser['PERSONAL_PHONE'] = $phone['number'];
$user['PERSONAL_PHONE'] = $phone['number'];
continue;
}
2016-10-04 17:57:39 +03:00
if ((!isset($user['PERSONAL_MOBILE']) || strlen($user['PERSONAL_MOBILE']) == 0) && $user['PERSONAL_PHONE'] != $phone['number']) {
2016-09-15 16:42:10 +03:00
$arUser['PERSONAL_MOBILE'] = $phone['number'];
$user['PERSONAL_MOBILE'] = $phone['number'];
continue;
}
}
}
}
2016-10-31 17:56:11 +03:00
if (array_key_exists('index', $customer['address'])) {
$arUser["PERSONAL_ZIP"] = $customer['address']['index'] ? RCrmActions::fromJSON($customer['address']['index']) : '';
}
if (array_key_exists('city', $customer['address'])) {
$arUser["PERSONAL_CITY"] = $customer['address']['city'] ? RCrmActions::fromJSON($customer['address']['city']) : '';
}
2016-09-15 16:42:10 +03:00
$u = $newUser->Update($customer['externalId'], $arUser);
2016-10-04 17:57:39 +03:00
if (!$u) {
2016-09-15 16:42:10 +03:00
RCrmActions::eventLog('RetailCrmHistory::customerHistory', 'Error update user', $newUser->LAST_ERROR);
}
if (function_exists('retailCrmAfterCustomerSave')) {
retailCrmAfterCustomerSave($customer);
}
}
}
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
2017-09-04 11:36:04 +03:00
//last id
2016-09-15 16:42:10 +03:00
$end = array_pop($customerH);
COption::SetOptionString(self::$MODULE_ID, self::$CRM_CUSTOMER_HISTORY, $end['id']);
2016-10-04 17:57:39 +03:00
if ($customerHistory['pagination']['totalPageCount'] == 1) {
2016-09-15 16:42:10 +03:00
return true;
}
2017-09-04 11:36:04 +03:00
//new filter
2016-09-15 16:42:10 +03:00
$historyFilter['sinceId'] = $end['id'];
}
}
2016-10-04 17:57:39 +03:00
public static function orderHistory()
{
2016-09-15 16:42:10 +03:00
global $USER;
if (is_object($USER) == false) {
$USER = new RetailUser;
}
if (!CModule::IncludeModule("iblock")) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'iblock', 'module not found');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return false;
}
if (!CModule::IncludeModule("sale")) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'sale', 'module not found');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return false;
}
if (!CModule::IncludeModule("catalog")) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'catalog', 'module not found');
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
return 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);
2016-12-13 14:01:48 +03:00
$optionsOrderTypes = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_TYPES_ARR, 0));
2016-09-15 16:42:10 +03:00
$optionsDelivTypes = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_DELIVERY_TYPES_ARR, 0)));
$optionsPayTypes = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_TYPES, 0)));
$optionsPayStatuses = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_STATUSES, 0))); // --statuses
$optionsPayment = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0)));
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
$optionsLegalDetails = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_LEGAL_DETAILS, 0));
$optionsContragentType = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CONTRAGENT_TYPE, 0));
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
$optionsCustomFields = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CUSTOM_FIELDS, 0));
$optionsOrderNumbers = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_NUMBERS, 0);
$optionsCanselOrder = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CANSEL_ORDER, 0));
$api = new RetailCrm\ApiClient($api_host, $api_key);
$historyFilter = array();
$historyStart = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY);
2016-10-04 17:57:39 +03:00
if ($historyStart && $historyStart > 0) {
2016-09-15 16:42:10 +03:00
$historyFilter['sinceId'] = $historyStart;
}
2016-10-04 17:57:39 +03:00
while (true) {
2017-09-04 11:36:04 +03:00
$orderHistory = RCrmActions::apiMethod($api, 'ordersHistory', __METHOD__, $historyFilter);
2016-09-15 16:42:10 +03:00
$orderH = isset($orderHistory['history']) ? $orderHistory['history'] : array();
$log = new Logger();
$log->write($orderH, 'orderHistory');
2017-09-04 11:36:04 +03:00
2016-10-04 17:57:39 +03:00
if (count($orderH) == 0) {
2017-09-04 11:36:04 +03:00
if ($orderHistory['history']['totalPageCount'] > $orderHistory['history']['currentPage']) {
$historyFilter['page'] = $orderHistory['history']['currentPage'] + 1;
continue;
}
2016-09-15 16:42:10 +03:00
return true;
}
$orders = self::assemblyOrder($orderH);
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
2017-09-04 11:36:04 +03:00
//orders with changes
2016-09-15 16:42:10 +03:00
foreach ($orders as $order) {
if (function_exists('retailCrmBeforeOrderSave')) {
$newResOrder = retailCrmBeforeOrderSave($order);
if (is_array($newResOrder) && !empty($newResOrder)) {
$order = $newResOrder;
2017-09-04 11:36:04 +03:00
} elseif ($newResOrder === false) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'retailCrmBeforeOrderSave()', 'OrderCrmId = ' . $order['id'] . '. Sending canceled after retailCrmBeforeOrderSave');
continue;
2016-09-15 16:42:10 +03:00
}
}
2016-10-14 15:25:02 +03:00
$log->write($order, 'assemblyOrderHistory');
2016-10-20 17:41:07 +03:00
if ($order['deleted']) {
2016-09-15 16:42:10 +03:00
continue;
}
2019-01-22 11:39:23 +03:00
if (isset($order['customer']['externalId']) && !is_numeric($order['customer']['externalId'])) {
unset($order['customer']['externalId']);
}
2016-09-15 16:42:10 +03:00
if (!isset($order['externalId'])) {
if (!isset($order['customer']['externalId'])) {
if (!isset($order['customer']['id'])) {
continue;
}
$registerNewUser = true;
if (!isset($order['customer']['email']) || $order['customer']['email'] == '') {
$login = $order['customer']['email'] = uniqid('user_' . time()) . '@crm.com';
} else {
$dbUser = CUser::GetList(($by = 'ID'), ($sort = 'ASC'), array('=EMAIL' => $order['email']));
switch ($dbUser->SelectedRowsCount()) {
case 0:
$login = $order['customer']['email'];
break;
case 1:
$arUser = $dbUser->Fetch();
$registeredUserID = $arUser['ID'];
$registerNewUser = false;
break;
default:
$login = uniqid('user_' . time()) . '@crm.com';
break;
}
}
if ($registerNewUser === true) {
$userPassword = uniqid();
$newUser = new CUser;
$arFields = array(
"NAME" => RCrmActions::fromJSON($order['customer']['firstName']),
"LAST_NAME" => RCrmActions::fromJSON($order['customer']['lastName']),
"SECOND_NAME" => RCrmActions::fromJSON($order['customer']['patronymic']),
"EMAIL" => $order['customer']['email'],
"LOGIN" => $login,
"ACTIVE" => "Y",
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword
);
2016-10-04 17:57:39 +03:00
if ($order['customer']['phones'][0]) {
2016-09-15 16:42:10 +03:00
$arFields['PERSONAL_PHONE'] = $order['customer']['phones'][0];
}
2016-10-04 17:57:39 +03:00
if ($order['customer']['phones'][1]) {
2016-09-15 16:42:10 +03:00
$arFields['PERSONAL_MOBILE'] = $order['customer']['phones'][1];
}
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
$registeredUserID = $newUser->Add($arFields);
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
if ($registeredUserID === false) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'CUser::Register', 'Error register user');
continue;
2017-09-04 11:36:04 +03:00
}
2016-09-15 16:42:10 +03:00
2017-09-04 11:36:04 +03:00
if(RCrmActions::apiMethod($api, 'customersFixExternalIds', __METHOD__, array(array('id' => $order['customer']['id'], 'externalId' => $registeredUserID))) == false) {
continue;
2016-09-15 16:42:10 +03:00
}
}
$order['customer']['externalId'] = $registeredUserID;
}
if ($optionsSitesList) {
2016-09-15 16:42:10 +03:00
$site = array_search($order['site'], $optionsSitesList);
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
$site = CSite::GetDefSite();
}
2017-09-04 11:36:04 +03:00
if (empty($site)) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::create', 'Site = ' . $order['site'] . ' not found in setting. Order crm id=' . $order['id']);
continue;
}
2016-09-15 16:42:10 +03:00
$newOrder = Bitrix\Sale\Order::create($site, $order['customer']['externalId']);
2017-09-04 11:36:04 +03:00
$newOrder->save();
2016-09-15 16:42:10 +03:00
$externalId = $newOrder->getId();
if (isset($externalId)) {
2017-09-04 11:36:04 +03:00
if(RCrmActions::apiMethod($api, 'ordersFixExternalIds', __METHOD__, array(array('id' => $order['id'], 'externalId' => $externalId))) == false){
2016-09-15 16:42:10 +03:00
continue;
}
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::create', 'Error order create');
}
$order['externalId'] = $externalId;
}
if (isset($order['externalId']) && $order['externalId']) {
2016-10-06 17:34:35 +03:00
$itemUpdate = false;
2017-09-04 11:36:04 +03:00
try {
$newOrder = Bitrix\Sale\Order::load($order['externalId']);
} catch (Bitrix\Main\ArgumentNullException $e) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::load', $e->getMessage() . ': ' . $order['externalId']);
continue;
}
2016-10-03 16:56:59 +03:00
2016-10-04 17:57:39 +03:00
if (!$newOrder instanceof \Bitrix\Sale\Order) {
2016-10-20 17:41:07 +03:00
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::load', 'Error order load id=' . $order['externalId']);
2016-09-15 16:42:10 +03:00
continue;
}
2017-09-04 11:36:04 +03:00
2016-10-20 17:41:07 +03:00
if ($optionsSitesList) {
$site = array_search($order['site'], $optionsSitesList);
} else {
$site = CSite::GetDefSite();
}
2017-09-04 11:36:04 +03:00
if (empty($site)) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::edit', 'Site = ' . $order['site'] . ' not found in setting. Order id=' . $order['externalId']);
continue;
}
2016-09-15 16:42:10 +03:00
2016-12-13 14:01:48 +03:00
if ($optionsOrderNumbers == 'Y' && isset($order['number'])) {
2017-09-04 11:36:04 +03:00
$searchFilter = array(
'filter' => array('ACCOUNT_NUMBER' => $order['number']),
'select' => array('ID'),
);
$searchOrder = \Bitrix\Sale\OrderTable::GetList($searchFilter)->fetch();
2017-09-04 11:36:04 +03:00
if (!empty($searchOrder)) {
if ($searchOrder['ID'] != $order['externalId']) {
2017-09-04 11:36:04 +03:00
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'setField("ACCOUNT_NUMBER")', 'Error order load id=' . $order['externalId']) . '. Number ' . $order['number'] . ' already exists';
continue;
}
}
2016-09-15 16:42:10 +03:00
$newOrder->setField('ACCOUNT_NUMBER', $order['number']);
2016-12-13 14:01:48 +03:00
}
2016-10-04 17:57:39 +03:00
2016-10-03 16:56:59 +03:00
$personType = $newOrder->getField('PERSON_TYPE_ID');
if (isset($order['orderType']) && $order['orderType']) {
$nType = array();
$tList = RCrmActions::OrderTypesList(array(array('LID' => $site)));
foreach($tList as $type){
if (isset($optionsOrderTypes[$type['ID']])) {
$nType[$optionsOrderTypes[$type['ID']]] = $type['ID'];
}
2016-12-13 14:01:48 +03:00
}
$newOptionsOrderTypes = $nType;
if ($newOptionsOrderTypes[$order['orderType']]) {
if ($personType != $newOptionsOrderTypes[$order['orderType']] && $personType != 0) {
$propsRemove = true;
}
$personType = $newOptionsOrderTypes[$order['orderType']];
$newOrder->setField('PERSON_TYPE_ID', $personType);
} elseif ($personType == 0) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'orderType not found', 'PERSON_TYPE_ID = 0');
2016-10-03 16:56:59 +03:00
}
}
2016-09-15 16:42:10 +03:00
//status
2016-10-04 17:57:39 +03:00
if ($optionsPayStatuses[$order['status']]) {
2016-09-15 16:42:10 +03:00
$newOrder->setField('STATUS_ID', $optionsPayStatuses[$order['status']]);
2016-10-04 17:57:39 +03:00
if (in_array($optionsPayStatuses[$order['status']], $optionsCanselOrder)) {
2016-10-14 15:25:02 +03:00
$newOrder->setFieldNoDemand('CANCELED', 'Y');
2016-10-04 17:57:39 +03:00
} else {
2016-10-14 15:25:02 +03:00
$newOrder->setFieldNoDemand('CANCELED', 'N');
2016-09-15 16:42:10 +03:00
}
}
2017-09-04 11:36:04 +03:00
if (array_key_exists('statusComment', $order)) {
2016-10-12 11:40:18 +03:00
self::setProp($newOrder, RCrmActions::fromJSON($order['statusComment']), 'REASON_CANCELED');
2017-09-04 11:36:04 +03:00
}
2016-10-03 16:56:59 +03:00
2017-09-04 11:36:04 +03:00
//props
2016-09-15 16:42:10 +03:00
$propertyCollection = $newOrder->getPropertyCollection();
$propertyCollectionArr = $propertyCollection->getArray();
2016-10-03 16:56:59 +03:00
$nProps = array();
2016-10-04 17:57:39 +03:00
foreach ($propertyCollectionArr['properties'] as $orderProp) {
if ($orderProp['ID'][0] == 'n') {
2016-10-03 16:56:59 +03:00
$orderProp['ID'] = substr($orderProp['ID'], 1);
$orderProp['ID'] = $propertyCollection->getItemById($orderProp['ID'])->getField('ORDER_PROPS_ID');
}
$nProps[] = $orderProp;
}
$propertyCollectionArr['properties'] = $nProps;
2017-09-04 11:36:04 +03:00
if ($propsRemove) {//delete props
2016-10-04 17:57:39 +03:00
foreach ($propertyCollectionArr['properties'] as $orderProp) {
2016-10-03 16:56:59 +03:00
$somePropValue = $propertyCollection->getItemByOrderPropertyId($orderProp['ID']);
self::setProp($somePropValue);
}
2017-09-04 11:36:04 +03:00
$orderCrm = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
2016-10-03 16:56:59 +03:00
$orderDump = $order;
$order = $orderCrm['order'];
}
2016-09-15 16:42:10 +03:00
$propsKey = array();
2016-10-04 17:57:39 +03:00
foreach ($propertyCollectionArr['properties'] as $prop) {
2016-09-15 16:42:10 +03:00
$propsKey[$prop['CODE']]['ID'] = $prop['ID'];
2016-10-03 16:56:59 +03:00
$propsKey[$prop['CODE']]['TYPE'] = $prop['TYPE'];
2016-09-15 16:42:10 +03:00
}
//fio
2016-10-04 17:57:39 +03:00
if ($order['firstName'] || $order['lastName'] || $order['patronymic']) {
2016-09-15 16:42:10 +03:00
$fio = '';
2016-10-04 17:57:39 +03:00
foreach ($propertyCollectionArr['properties'] as $prop) {
if (in_array($optionsOrderProps[$personType]['fio'], $prop)) {
2016-10-14 15:25:02 +03:00
$getFio = $newOrder->getPropertyCollection()->getItemByOrderPropertyId($prop['ID']);
if (method_exists($getFio, 'getValue')) {
$fio = $getFio->getValue();
}
2016-09-15 16:42:10 +03:00
}
}
$fio = RCrmActions::explodeFIO($fio);
2016-10-12 11:40:18 +03:00
$newFio = array();
2016-10-04 17:57:39 +03:00
if ($fio) {
2016-10-12 11:40:18 +03:00
$newFio[] = isset($order['lastName']) ? RCrmActions::fromJSON($order['lastName']) : (isset($fio['lastName']) ? $fio['lastName'] : '');
$newFio[] = isset($order['firstName']) ? RCrmActions::fromJSON($order['firstName']) : (isset($fio['firstName']) ? $fio['firstName'] : '');
$newFio[] = isset($order['patronymic']) ? RCrmActions::fromJSON($order['patronymic']) : (isset($fio['patronymic']) ? $fio['patronymic'] : '');
$order['fio'] = trim(implode(' ', $newFio));
2016-10-04 17:57:39 +03:00
} else {
2016-10-12 11:40:18 +03:00
$newFio[] = isset($order['lastName']) ? RCrmActions::fromJSON($order['lastName']) : '';
$newFio[] = isset($order['firstName']) ? RCrmActions::fromJSON($order['firstName']) : '';
$newFio[] = isset($order['patronymic']) ? RCrmActions::fromJSON($order['patronymic']) : '';
$order['fio'] = trim(implode(' ', $newFio));
2016-09-15 16:42:10 +03:00
}
}
//optionsOrderProps
2016-10-04 17:57:39 +03:00
if ($optionsOrderProps[$personType]) {
foreach ($optionsOrderProps[$personType] as $key => $orderProp) {
if (array_key_exists($key, $order)) {
2016-09-15 16:42:10 +03:00
$somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
2016-10-25 17:56:50 +03:00
if ($key == 'fio') {
self::setProp($somePropValue, $order[$key]);
} else {
self::setProp($somePropValue, RCrmActions::fromJSON($order[$key]));
}
2016-10-04 17:57:39 +03:00
} elseif (array_key_exists($key, $order['delivery']['address'])) {
2016-10-25 17:56:50 +03:00
if ($propsKey[$orderProp]['TYPE'] == 'LOCATION') {
2017-09-04 11:36:04 +03:00
$order['delivery']['address'][$key] = trim($order['delivery']['address'][$key]);
2016-11-15 17:17:09 +03:00
if(!empty($order['delivery']['address'][$key])){
$parameters = array();
$loc = explode('.', $order['delivery']['address'][$key]);
if (count($loc) == 1) {
2017-09-04 11:36:04 +03:00
$parameters['filter']['PHRASE'] = RCrmActions::fromJSON(trim($loc[0]));
2016-11-15 17:17:09 +03:00
} elseif (count($loc) == 2) {
2017-09-04 11:36:04 +03:00
$parameters['filter']['PHRASE'] = RCrmActions::fromJSON(trim($loc[1]));
2016-11-15 17:17:09 +03:00
} else{
2017-09-04 11:36:04 +03:00
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'RetailCrmHistory::setProp', 'Error location. ' . $order['delivery']['address'][$key] . ' not found add in order id=' . $order['externalId']);
2016-11-15 17:17:09 +03:00
continue;
}
2017-09-04 11:36:04 +03:00
$parameters['filter']['NAME.LANGUAGE_ID'] = 'ru';
$location = \Bitrix\Sale\Location\Search\Finder::find($parameters, array('USE_INDEX' => false, 'USE_ORM' => false))->fetch();
$somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
self::setProp($somePropValue, $location['CODE']);
} else {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'RetailCrmHistory::setProp', 'Error location. ' . $order['delivery']['address'][$key] . ' is empty in order id=' . $order['externalId']);
continue;
2016-10-25 17:56:50 +03:00
}
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
$somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
2016-10-12 11:40:18 +03:00
self::setProp($somePropValue, RCrmActions::fromJSON($order['delivery']['address'][$key]));
2016-09-15 16:42:10 +03:00
}
}
}
}
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
//optionsLegalDetails
2016-10-04 17:57:39 +03:00
if ($optionsLegalDetails[$personType]) {
foreach ($optionsLegalDetails[$personType] as $key => $orderProp) {
if (array_key_exists($key, $order)) {
2016-10-03 16:56:59 +03:00
$somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
2016-09-15 16:42:10 +03:00
self::setProp($somePropValue, $order[$key]);
}
}
}
2016-10-04 17:57:39 +03:00
if ($propsRemove) {
2016-10-03 16:56:59 +03:00
$order = $orderDump;
}
2016-09-15 16:42:10 +03:00
//paymentStatus
2016-10-04 17:57:39 +03:00
if ($optionsPayment[$order['paymentStatus']]) {
2016-10-03 16:56:59 +03:00
$newOrder->setFieldNoDemand('PAYED', $optionsPayment[$order['paymentStatus']]);
2016-09-15 16:42:10 +03:00
}
//comments
2016-10-04 17:57:39 +03:00
if (array_key_exists('customerComment', $order)) {
2016-10-12 11:40:18 +03:00
self::setProp($newOrder, RCrmActions::fromJSON($order['customerComment']), 'USER_DESCRIPTION');
2016-09-15 16:42:10 +03:00
}
2016-10-04 17:57:39 +03:00
if (array_key_exists('managerComment', $order)) {
2016-10-12 11:40:18 +03:00
self::setProp($newOrder, RCrmActions::fromJSON($order['managerComment']), 'COMMENTS');
2016-09-15 16:42:10 +03:00
}
2016-10-03 16:56:59 +03:00
2016-09-15 16:42:10 +03:00
//items
$basket = $newOrder->getBasket();
2018-02-27 15:29:43 +03:00
$fUserId = $basket->getFUserId(true);
if ($fUserId === null) {
$fUserId = Bitrix\Sale\Fuser::getIdByUserId($order['customer']['externalId']);
$basket->setFUserId($fUserId);
}
2016-10-06 17:34:35 +03:00
if (isset($order['items'])) {
$itemUpdate = true;
foreach ($order['items'] as $product) {
$item = self::getExistsItem($basket, 'catalog', $product['offer']['externalId']);
if (!$item) {
if($product['delete']){
continue;
}
$item = $basket->createItem('catalog', $product['offer']['externalId']);
2016-10-20 13:16:59 +03:00
if ($item instanceof \Bitrix\Sale\BasketItem) {
2016-10-12 11:40:18 +03:00
$elem = self::getInfoElement($product['offer']['externalId']);
2016-10-06 17:34:35 +03:00
$item->setFields(array(
'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
2016-10-20 17:41:07 +03:00
'LID' => $site,
2016-10-06 17:34:35 +03:00
'BASE_PRICE' => $product['initialPrice'],
2017-11-13 11:05:11 +03:00
'NAME' => $product['offer']['name'] ? RCrmActions::fromJSON($product['offer']['name']) : $elem['NAME'],
2016-10-06 17:34:35 +03:00
'DETAIL_PAGE_URL' => $elem['URL']
));
} else {
2016-10-20 13:16:59 +03:00
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'createItem', 'Error item id=' . $product['offer']['externalId'] . ' add in order id=' . $order['externalId']);
2016-10-06 17:34:35 +03:00
continue;
}
}
if ($product['delete']) {
$item->delete();
2016-10-03 16:56:59 +03:00
continue;
}
2016-09-15 16:42:10 +03:00
2016-10-06 17:34:35 +03:00
if ($product['quantity']) {
$item->setFieldNoDemand('QUANTITY', $product['quantity']);
2016-09-15 16:42:10 +03:00
}
2016-10-06 17:34:35 +03:00
if (array_key_exists('discount', $product) || array_key_exists('discountPercent', $product)) {
if (!isset($orderCrm)) {
2017-09-04 11:36:04 +03:00
$orderCrm = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
2016-09-15 16:42:10 +03:00
}
2016-11-15 17:17:09 +03:00
if (isset($orderCrm['order']['items'])) {
foreach ($orderCrm['order']['items'] as $itemCrm) {
if ($itemCrm['offer']['externalId'] == $product['offer']['externalId']) {
$itemCost = $itemCrm['initialPrice'] - $itemCrm['discount'] - round(($itemCrm['initialPrice'] / 100 * $itemCrm['discountPercent']), 2);
break;
}
2016-10-06 17:34:35 +03:00
}
}
2017-09-04 11:36:04 +03:00
if (isset($itemCost) && $itemCost >= 0) {
2016-10-06 17:34:35 +03:00
$item->setField('CUSTOM_PRICE', 'Y');
$item->setField('PRICE', $itemCost);
$item->setField('DISCOUNT_NAME', '');
$item->setField('DISCOUNT_VALUE', '');
}
2016-09-15 16:42:10 +03:00
}
}
}
$orderSumm = 0;
2016-10-04 17:57:39 +03:00
foreach ($basket as $item) {
2016-09-15 16:42:10 +03:00
$orderSumm += $item->getFinalPrice();
}
2016-10-04 17:57:39 +03:00
if (array_key_exists('cost', $order['delivery'])) {
2016-09-15 16:42:10 +03:00
$deliverySumm = $order['delivery']['cost'];
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
$deliverySumm = $newOrder->getDeliveryPrice();
}
$orderSumm += $deliverySumm;
$newOrder->setField('PRICE', $orderSumm);
$order['summ'] = $orderSumm;
$newOrder->save();
//payment
2016-10-04 17:57:39 +03:00
if (array_key_exists('paymentType', $order)) {
2017-09-04 11:36:04 +03:00
self::paySystemUpdate($order, $optionsPayTypes, $newOrder->getField('ACCOUNT_NUMBER'));
2016-09-15 16:42:10 +03:00
}
//delivery
2016-10-06 17:34:35 +03:00
if (array_key_exists('code', $order['delivery'])) {
$itemUpdate = true;
2017-09-04 11:36:04 +03:00
//delete empty
2016-10-04 17:57:39 +03:00
if (!isset($orderCrm)) {
2017-09-04 11:36:04 +03:00
$orderCrm = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
}
if ($orderCrm) {
self::shipmentUpdate($orderCrm['order'], $optionsDelivTypes, $newOrder->getField('ACCOUNT_NUMBER'));
2016-09-15 16:42:10 +03:00
}
}
2016-10-04 17:57:39 +03:00
if (isset($orderCrm)) {
2017-09-04 11:36:04 +03:00
unset($orderCrm);
2016-09-15 16:42:10 +03:00
}
//delivery cost
2016-10-04 17:57:39 +03:00
if (array_key_exists('cost', $order['delivery'])) {
2016-10-20 13:16:59 +03:00
$shipment = Bitrix\Sale\Internals\ShipmentTable::getList(array(
2016-09-15 16:42:10 +03:00
'filter' => array('ORDER_ID' => $order['externalId'], 'SYSTEM' => 'N'),
'order' => array('ID')
))->fetch();
2016-10-04 17:57:39 +03:00
if ($shipment) {
2016-09-15 16:42:10 +03:00
Bitrix\Sale\Internals\ShipmentTable::update($shipment['ID'], array('BASE_PRICE_DELIVERY' => $order['delivery']['cost'], 'PRICE_DELIVERY' => $order['delivery']['cost'], 'CUSTOM_PRICE_DELIVERY' => 'Y'));
}
2017-09-04 11:36:04 +03:00
Bitrix\Sale\OrderTable::update($order['externalId'], array('PRICE_DELIVERY' => $order['delivery']['cost']));
2016-09-15 16:42:10 +03:00
}
Bitrix\Sale\OrderTable::update($order['externalId'], array('MARKED' => 'N', 'EMP_MARKED_ID' => '', 'REASON_MARKED' => ''));
2016-10-04 17:57:39 +03:00
2016-10-06 17:34:35 +03:00
if ($itemUpdate) {
self::updateShipmentItem($order['externalId']);
}
2016-10-04 17:57:39 +03:00
2016-09-15 16:42:10 +03:00
if (function_exists('retailCrmAfterOrderSave')) {
retailCrmAfterOrderSave($order);
}
}
}
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
2017-09-04 11:36:04 +03:00
//end id
2016-09-15 16:42:10 +03:00
$end = array_pop($orderH);
COption::SetOptionString(self::$MODULE_ID, self::$CRM_ORDER_HISTORY, $end['id']);
2016-10-04 17:57:39 +03:00
if ($orderHistory['pagination']['totalPageCount'] == 1) {
2016-09-15 16:42:10 +03:00
return true;
}
2017-09-04 11:36:04 +03:00
//new filter
2016-09-15 16:42:10 +03:00
$historyFilter['sinceId'] = $end['id'];
2017-09-04 11:36:04 +03:00
}
2016-09-15 16:42:10 +03:00
}
2016-10-04 17:57:39 +03:00
public static function assemblyCustomer($customerHistory)
{
2016-09-15 16:42:10 +03:00
$server = \Bitrix\Main\Context::getCurrent()->getServer()->getDocumentRoot();
$fields = array();
if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml')) {
$objects = simplexml_load_file($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml');
2016-10-04 17:57:39 +03:00
foreach ($objects->fields->field as $object) {
2016-09-15 16:42:10 +03:00
$fields[(string)$object["group"]][(string)$object["id"]] = (string)$object;
}
}
$customers = array();
foreach ($customerHistory as $change) {
$change['customer'] = self::removeEmpty($change['customer']);
2016-10-04 17:57:39 +03:00
if ($customers[$change['customer']['id']]) {
2016-09-15 16:42:10 +03:00
$customers[$change['customer']['id']] = array_merge($customers[$change['customer']['id']], $change['customer']);
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
$customers[$change['customer']['id']] = $change['customer'];
}
2016-10-04 17:57:39 +03:00
if ($change['customer']['contragent']['contragentType']) {
2016-09-15 16:42:10 +03:00
$change['customer']['contragentType'] = self::newValue($change['customer']['contragent']['contragentType']);
unset($change['customer']['contragent']);
}
2016-10-04 17:57:39 +03:00
if ($fields['customer'][$change['field']] == 'phones') {
2016-09-15 16:42:10 +03:00
$key = count($customers[$change['customer']['id']]['phones']);
2016-10-04 17:57:39 +03:00
if (isset($change['oldValue'])) {
2016-09-15 16:42:10 +03:00
$customers[$change['customer']['id']]['phones'][$key]['old_number'] = $change['oldValue'];
}
2016-10-04 17:57:39 +03:00
if (isset($change['newValue'])) {
2016-09-15 16:42:10 +03:00
$customers[$change['customer']['id']]['phones'][$key]['number'] = $change['newValue'];
}
2016-10-04 17:57:39 +03:00
} else {
if ($fields['customerAddress'][$change['field']]) {
2016-09-15 16:42:10 +03:00
$customers[$change['customer']['id']]['address'][$fields['customerAddress'][$change['field']]] = $change['newValue'];
2016-10-04 17:57:39 +03:00
} elseif ($fields['customerContragent'][$change['field']]) {
2016-09-15 16:42:10 +03:00
$customers[$change['customer']['id']]['contragent'][$fields['customerContragent'][$change['field']]] = $change['newValue'];
2016-10-04 17:57:39 +03:00
} elseif ($fields['customer'][$change['field']]) {
2016-09-15 16:42:10 +03:00
$customers[$change['customer']['id']][$fields['customer'][$change['field']]] = self::newValue($change['newValue']);
}
2016-10-04 17:57:39 +03:00
if (isset($change['created'])) {
2016-09-15 16:42:10 +03:00
$customers[$change['customer']['id']]['create'] = 1;
}
2016-10-04 17:57:39 +03:00
if (isset($change['deleted'])) {
2016-09-15 16:42:10 +03:00
$customers[$change['customer']['id']]['deleted'] = 1;
}
}
}
2016-10-04 17:57:39 +03:00
2016-09-15 16:42:10 +03:00
return $customers;
}
2016-10-04 17:57:39 +03:00
public static function assemblyOrder($orderHistory)
{
2016-09-15 16:42:10 +03:00
$server = \Bitrix\Main\Context::getCurrent()->getServer()->getDocumentRoot();
if (file_exists($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml')) {
$objects = simplexml_load_file($server . '/bitrix/modules/intaro.retailcrm/classes/general/config/objects.xml');
2016-10-04 17:57:39 +03:00
foreach ($objects->fields->field as $object) {
2016-09-15 16:42:10 +03:00
$fields[(string)$object["group"]][(string)$object["id"]] = (string)$object;
}
}
$orders = array();
foreach ($orderHistory as $change) {
$change['order'] = self::removeEmpty($change['order']);
2016-10-04 17:57:39 +03:00
if ($change['order']['items']) {
2016-09-15 16:42:10 +03:00
$items = array();
2016-10-04 17:57:39 +03:00
foreach ($change['order']['items'] as $item) {
if (isset($change['created'])) {
2016-09-15 16:42:10 +03:00
$item['create'] = 1;
}
$items[$item['id']] = $item;
}
$change['order']['items'] = $items;
}
if (isset($change['order']['contragent']) && count($change['order']['contragent']) > 0) {
foreach ($change['order']['contragent'] as $name => $value) {
$change['order'][$name] = self::newValue($value);
}
2016-09-15 16:42:10 +03:00
unset($change['order']['contragent']);
}
2016-10-04 17:57:39 +03:00
if ($orders[$change['order']['id']]) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']);
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']] = $change['order'];
}
2016-10-04 17:57:39 +03:00
if ($change['item']) {
if ($orders[$change['order']['id']]['items'][$change['item']['id']]) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['items'][$change['item']['id']] = array_merge($orders[$change['order']['id']]['items'][$change['item']['id']], $change['item']);
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['items'][$change['item']['id']] = $change['item'];
}
2016-10-04 17:57:39 +03:00
if (empty($change['oldValue']) && $change['field'] == 'order_product') {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = 1;
unset($orders[$change['order']['id']]['items'][$change['item']['id']]['delete']);
}
2016-10-04 17:57:39 +03:00
if (empty($change['newValue']) && $change['field'] == 'order_product') {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = 1;
}
if (/*!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && */$fields['item'][$change['field']]) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue'];
}
2016-10-04 17:57:39 +03:00
} else {
if ($fields['delivery'][$change['field']] == 'service') {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['delivery']['service']['code'] = self::newValue($change['newValue']);
2016-10-04 17:57:39 +03:00
} elseif ($fields['delivery'][$change['field']]) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['delivery'][$fields['delivery'][$change['field']]] = self::newValue($change['newValue']);
2016-10-04 17:57:39 +03:00
} elseif ($fields['orderAddress'][$change['field']]) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['delivery']['address'][$fields['orderAddress'][$change['field']]] = $change['newValue'];
2016-10-04 17:57:39 +03:00
} elseif ($fields['integrationDelivery'][$change['field']]) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['delivery']['service'][$fields['integrationDelivery'][$change['field']]] = self::newValue($change['newValue']);
2016-10-04 17:57:39 +03:00
} elseif ($fields['customerContragent'][$change['field']]) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']][$fields['customerContragent'][$change['field']]] = self::newValue($change['newValue']);
2016-10-04 17:57:39 +03:00
} elseif (strripos($change['field'], 'custom_') !== false) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['customFields'][str_replace('custom_', '', $change['field'])] = self::newValue($change['newValue']);
2016-10-04 17:57:39 +03:00
} elseif ($fields['order'][$change['field']]){
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']);
}
2016-10-04 17:57:39 +03:00
if (isset($change['created'])) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['create'] = 1;
}
2016-10-04 17:57:39 +03:00
if (isset($change['deleted'])) {
2016-09-15 16:42:10 +03:00
$orders[$change['order']['id']]['deleted'] = 1;
}
}
}
2016-10-04 17:57:39 +03:00
2016-09-15 16:42:10 +03:00
return $orders;
}
2017-09-04 11:36:04 +03:00
public static function shipmentUpdate($orderCrm, $optionsDelivTypes, $accountNumber = '')
2016-10-04 17:57:39 +03:00
{
2017-09-04 11:36:04 +03:00
if (strlen($accountNumber) < 1) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'shipmentUpdate', 'ACCOUNT_NUMBER not found');
return false;
}
2016-10-04 17:57:39 +03:00
if (isset($orderCrm['delivery']['code'])) {
2016-09-15 16:42:10 +03:00
$crmCode = $orderCrm['delivery']['code'];
2016-10-04 17:57:39 +03:00
if (isset($orderCrm['delivery']['data']['deliveryType'])) {
2016-09-15 16:42:10 +03:00
$crmService = $orderCrm['delivery']['data']['deliveryType'];
2016-10-04 17:57:39 +03:00
} elseif (isset($orderCrm['delivery']['service'])) {
2017-09-04 11:36:04 +03:00
$crmService = $orderCrm['delivery']['service']['code'];
2016-09-15 16:42:10 +03:00
}
2017-09-04 11:36:04 +03:00
//select bitrix service code
2016-09-15 16:42:10 +03:00
$arDeliveryServiceAll = \Bitrix\Sale\Delivery\Services\Manager::getActiveList();
2016-10-04 17:57:39 +03:00
foreach ($arDeliveryServiceAll as $arDeliveryService) {
2016-09-15 16:42:10 +03:00
$arDeliveryCode[$arDeliveryService['CODE']] = $arDeliveryService['ID'];
$arDeliveryID[$arDeliveryService['ID']] = $arDeliveryService;
2016-10-04 17:57:39 +03:00
if ($arDeliveryService['ID'] == $optionsDelivTypes[$crmCode]) {
2016-09-15 16:42:10 +03:00
$dCode = $arDeliveryService['CODE'] . ':' . $crmService;
}
}
2017-09-04 11:36:04 +03:00
//We will change delivery to this id
2016-10-04 17:57:39 +03:00
if ($crmService && $arDeliveryCode[$dCode]) {
2016-09-15 16:42:10 +03:00
$nowDelivery = $arDeliveryCode[$dCode];
2017-09-04 11:36:04 +03:00
} elseif (!empty($optionsDelivTypes[$crmCode])) {
2016-09-15 16:42:10 +03:00
$nowDelivery = $optionsDelivTypes[$crmCode];
2017-09-04 11:36:04 +03:00
} else {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'shipmentUpdate', 'Delivery ' . $crmCode . ' not found in options');
return false;
2016-09-15 16:42:10 +03:00
}
2017-09-04 11:36:04 +03:00
//Find the current delivery in the order
2016-09-15 16:42:10 +03:00
$cnt = Bitrix\Sale\Internals\ShipmentTable::getCount(array('ORDER_ID' => $orderCrm['externalId']));
2017-09-04 11:36:04 +03:00
if ($cnt > 0) {//update
2016-10-20 13:16:59 +03:00
$obDeliverys = \Bitrix\Sale\Internals\ShipmentTable::getList(array('filter' => array('ORDER_ID' => $orderCrm['externalId']),
2016-09-15 16:42:10 +03:00
'order' => array('ID')));
2016-10-04 17:57:39 +03:00
while ($arDelivery = $obDeliverys->fetch()) {
if ($arDelivery['DELIVERY_ID'] != $nowDelivery) {
2016-09-15 16:42:10 +03:00
\Bitrix\Sale\OrderTable::update($orderCrm['externalId'], array('DELIVERY_ID' => $nowDelivery));
\Bitrix\Sale\Internals\ShipmentTable::update($arDelivery['ID'], array('DELIVERY_ID' => $nowDelivery, 'DELIVERY_NAME' => $arDeliveryID[$nowDelivery]['NAME']));
}
}
2017-09-05 13:52:49 +03:00
if ($cnt == 1 && $arDelivery['DELIVERY_ID'] == 0) {
2017-09-04 11:36:04 +03:00
$shipment = Bitrix\Sale\Internals\ShipmentTable::add(array(
'ORDER_ID' => $orderCrm['externalId'],
'STATUS_ID' => 'DN',
'PRICE_DELIVERY' => 0,
'BASE_PRICE_DELIVERY' => 0,
'CUSTOM_PRICE_DELIVERY' => 'N',
'ALLOW_DELIVERY' => 'N',
'DEDUCTED' => 'N',
'RESERVED' => 'N',
'DELIVERY_ID' => $nowDelivery,
'DELIVERY_NAME' => $arDeliveryID[$nowDelivery]['NAME'],
'CANCELED' => 'N',
'MARKED' => 'N',
'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
'SYSTEM' => 'N',
'ACCOUNT_NUMBER' => $accountNumber . '/2',
'EXTERNAL_DELIVERY' => 'N',
'UPDATED_1C' => 'N',
'DATE_INSERT'=> new \Bitrix\Main\Type\DateTime()
));
}
} else {//create
2016-09-15 16:42:10 +03:00
\Bitrix\Sale\OrderTable::update($orderCrm['externalId'], array('DELIVERY_ID' => $nowDelivery));
$shipmentSystem = \Bitrix\Sale\Internals\ShipmentTable::add(array(
'ORDER_ID' => $orderCrm['externalId'],
'STATUS_ID' => 'DN',
'CUSTOM_PRICE_DELIVERY' => 'N',
'ALLOW_DELIVERY' => 'N',
'DEDUCTED' => 'N',
'RESERVED' => 'N',
'DELIVERY_ID' => $nowDelivery,
'DELIVERY_NAME' => $nowDelivery[$nowDelivery]['NAME'],
'CANCELED' => 'N',
'MARKED' => 'N',
'SYSTEM' => 'Y',
2017-09-04 11:36:04 +03:00
'ACCOUNT_NUMBER' => $accountNumber . '/1',
2016-09-15 16:42:10 +03:00
'EXTERNAL_DELIVERY' => 'N',
'UPDATED_1C' => 'N',
'DATE_INSERT'=> new \Bitrix\Main\Type\DateTime()
));
$shipment = Bitrix\Sale\Internals\ShipmentTable::add(array(
'ORDER_ID' => $orderCrm['externalId'],
'STATUS_ID' => 'DN',
'PRICE_DELIVERY' => 0,
'BASE_PRICE_DELIVERY' => 0,
'CUSTOM_PRICE_DELIVERY' => 'N',
'ALLOW_DELIVERY' => 'N',
'DEDUCTED' => 'N',
'RESERVED' => 'N',
'DELIVERY_ID' => $nowDelivery,
'DELIVERY_NAME' => $arDeliveryID[$nowDelivery]['NAME'],
'CANCELED' => 'N',
'MARKED' => 'N',
'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
'SYSTEM' => 'N',
2017-09-04 11:36:04 +03:00
'ACCOUNT_NUMBER' => $accountNumber . '/2',
2016-09-15 16:42:10 +03:00
'EXTERNAL_DELIVERY' => 'N',
'UPDATED_1C' => 'N',
'DATE_INSERT'=> new \Bitrix\Main\Type\DateTime()
));
}
2016-10-04 17:57:39 +03:00
} else {
2017-09-04 11:36:04 +03:00
//search for the order on the delivery site and delete / replace with no delivery
2016-09-15 16:42:10 +03:00
$noOrderId = \Bitrix\Sale\Delivery\Services\EmptyDeliveryService::getEmptyDeliveryServiceId();
\Bitrix\Sale\OrderTable::update($orderCrm['externalId'], array('DELIVERY_ID' => $noOrderId));
2016-10-20 13:16:59 +03:00
$obDeliverys = Bitrix\Sale\Internals\ShipmentTable::getList(array('filter' => array('ORDER_ID' => $orderCrm['externalId']),
2016-09-15 16:42:10 +03:00
'order' => array('ID')));
$create = true;
2016-10-04 17:57:39 +03:00
while ($arDelivery = $obDeliverys->fetch()) {
2017-09-04 11:36:04 +03:00
\Bitrix\Sale\Internals\ShipmentTable::update($arDelivery['ID'], array('DELIVERY_ID' => $noOrderId, 'DELIVERY_NAME' => GetMessage('NO_DELIVERY')));
2016-09-15 16:42:10 +03:00
$create = false;
}
2016-10-04 17:57:39 +03:00
if ($create) {
2016-09-15 16:42:10 +03:00
$shipmentSystem = \Bitrix\Sale\Internals\ShipmentTable::add(array(
'ORDER_ID' => $orderCrm['externalId'],
'STATUS_ID' => 'DN',
'CUSTOM_PRICE_DELIVERY' => 'N',
'ALLOW_DELIVERY' => 'N',
'DEDUCTED' => 'N',
'RESERVED' => 'N',
2017-09-04 11:36:04 +03:00
'DELIVERY_ID' => $noOrderId,
'DELIVERY_NAME' => GetMessage('NO_DELIVERY'),
2016-09-15 16:42:10 +03:00
'CANCELED' => 'N',
'MARKED' => 'N',
'SYSTEM' => 'Y',
2017-09-04 11:36:04 +03:00
'ACCOUNT_NUMBER' => $accountNumber . '/1',
2016-09-15 16:42:10 +03:00
'EXTERNAL_DELIVERY' => 'N',
'UPDATED_1C' => 'N',
'DATE_INSERT'=> new \Bitrix\Main\Type\DateTime()
));
$shipment = Bitrix\Sale\Internals\ShipmentTable::add(array(
'ORDER_ID' => $orderCrm['externalId'],
'STATUS_ID' => 'DN',
'PRICE_DELIVERY' => 0,
'BASE_PRICE_DELIVERY' => 0,
'CUSTOM_PRICE_DELIVERY' => 'N',
'ALLOW_DELIVERY' => 'N',
'DEDUCTED' => 'N',
'RESERVED' => 'N',
2017-09-04 11:36:04 +03:00
'DELIVERY_ID' => $noOrderId,
'DELIVERY_NAME' => GetMessage('NO_DELIVERY'),
2016-09-15 16:42:10 +03:00
'CANCELED' => 'N',
'MARKED' => 'N',
'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
'SYSTEM' => 'N',
2017-09-04 11:36:04 +03:00
'ACCOUNT_NUMBER' => $accountNumber . '/2',
2016-09-15 16:42:10 +03:00
'EXTERNAL_DELIVERY' => 'N',
'UPDATED_1C' => 'N',
'DATE_INSERT'=> new \Bitrix\Main\Type\DateTime()
));
}
}
return true;
}
2016-10-04 17:57:39 +03:00
public static function updateShipmentItem($orderId)
{
$orderBasket = \Bitrix\Sale\Internals\BasketTable::getList(array(
'filter' => array('ORDER_ID' => $orderId),
'select' => array('ID', 'QUANTITY')
));
2017-09-04 11:36:04 +03:00
2016-10-04 17:57:39 +03:00
$basketItems = array();
while ($basketItem = $orderBasket->fetch()) {
$basketItems[] = $basketItem;
$bItems[] = $basketItem['ID'];
}
$obShipments = \Bitrix\Sale\Internals\ShipmentTable::getList(array(
'filter' => array('ORDER_ID' => $orderId, 'SYSTEM' => 'N'),
2016-10-04 17:57:39 +03:00
'select' => array('ID')
));
2017-09-04 11:36:04 +03:00
2016-10-04 17:57:39 +03:00
$shipmentItems = array();
while ($arShipment = $obShipments->fetch()) {
$dlvBaslet = \Bitrix\Sale\Internals\ShipmentItemTable::getList(array(
'order' => array('ORDER_DELIVERY_ID'),
'filter' => array('ORDER_DELIVERY_ID' => $arShipment['ID'])
));
$shipmentItems[$arShipment['ID']] = array();
while ($item = $dlvBaslet->fetch()) {
$shipmentItems[$arShipment['ID']][] = $item;
}
}
foreach ($basketItems as $basketItem) {
foreach ($shipmentItems as $key => $arShipmentItems) {
$found = false;
foreach ($arShipmentItems as $elShipmentItem) {
if (!in_array($elShipmentItem['BASKET_ID'], $bItems)) {
2017-09-04 11:36:04 +03:00
//delete the element
2016-10-04 17:57:39 +03:00
\Bitrix\Sale\Internals\ShipmentItemTable::delete($elShipmentItem['ID']);
}
if ($elShipmentItem['BASKET_ID'] == $basketItem['ID']) {
2017-09-04 11:36:04 +03:00
//found
2016-10-04 17:57:39 +03:00
$found = true;
2017-09-04 11:36:04 +03:00
//update quantity
2016-10-04 17:57:39 +03:00
if ($elShipmentItem['QUANTITY'] != $basketItem['QUANTITY']) {
\Bitrix\Sale\Internals\ShipmentItemTable::update($elShipmentItem['ID'], array('QUANTITY' => $basketItem['QUANTITY']));
}
}
}
if (!$found) {
2017-09-04 11:36:04 +03:00
//create
2016-10-04 17:57:39 +03:00
\Bitrix\Sale\Internals\ShipmentItemTable::add(array(
'ORDER_DELIVERY_ID' => $key,
'BASKET_ID' => $basketItem['ID'],
'DATE_INSERT' => new \Bitrix\Main\Type\DateTime(),
'QUANTITY' => $basketItem['QUANTITY'],
'RESERVED_QUANTITY' => '0.00',
));
}
}
}
}
2017-09-04 11:36:04 +03:00
public static function paySystemUpdate($order, $optionsPayment, $accountNumber = '')
2016-10-04 17:57:39 +03:00
{
2017-09-04 11:36:04 +03:00
if (strlen($accountNumber) < 1) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'paySystemUpdate', 'ACCOUNT_NUMBER not found');
return false;
}
2016-10-04 17:57:39 +03:00
if (isset($order['paymentType'])) {
if ($optionsPayment[$order['paymentType']]) {
2016-09-15 16:42:10 +03:00
$paymentList = RCrmActions::PaymentList();
$arPayments = array();
$arPaymentsName = array();
2016-10-04 17:57:39 +03:00
foreach ($paymentList as $payment) {
2016-09-15 16:42:10 +03:00
$arPayments[] = $payment['ID'];
$arPaymentsName[$payment['ID']] = $payment['NAME'];
}
2016-10-04 17:57:39 +03:00
if (in_array($optionsPayment[$order['paymentType']], $arPayments)) {
2016-09-15 16:42:10 +03:00
\Bitrix\Sale\OrderTable::update($order['externalId'], array('PAY_SYSTEM_ID' => $optionsPayment[$order['paymentType']]));
2016-10-20 13:16:59 +03:00
$payment = \Bitrix\Sale\Internals\PaymentTable::getList(array(
2016-09-15 16:42:10 +03:00
'filter' => array('ORDER_ID' => $order['externalId']),
'order' => array('ID')
))->fetch();
2017-09-04 11:36:04 +03:00
if ($payment) {//update payment
2016-09-15 16:42:10 +03:00
\Bitrix\Sale\Internals\PaymentTable::update($payment['ID'], array('PAY_SYSTEM_ID' => $optionsPayment[$order['paymentType']], 'PAY_SYSTEM_NAME' => $arPaymentsName[$optionsPayment[$order['paymentType']]], 'SUM' => $order['summ']));
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
\Bitrix\Sale\Internals\PaymentTable::add(array(
'ORDER_ID' => $order['externalId'],
'PAID' => 'N',
'PAY_SYSTEM_ID' => $optionsPayment[$order['paymentType']],
'SUM' => $order['summ'],
'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
'PAY_SYSTEM_NAME' => $arPaymentsName[$optionsPayment[$order['paymentType']]],
'IS_RETURN' => 'N',
2017-09-04 11:36:04 +03:00
'ACCOUNT_NUMBER' => $accountNumber . '/1',
2016-09-15 16:42:10 +03:00
'PRICE_COD' => '0.00',
'EXTERNAL_PAYMENT' => 'N',
'UPDATED_1C' => 'N'
));
}
2017-09-04 11:36:04 +03:00
} else {
2016-10-20 17:41:07 +03:00
RCrmActions::eventLog('RetailCrmHistory::paySystemUpdate', 'RCrmActions::PaymentList()', 'Error paySystem not found in order id=' . $order['externalId']);
2016-09-15 16:42:10 +03:00
}
2017-09-04 11:36:04 +03:00
} else {
2016-10-20 17:41:07 +03:00
RCrmActions::eventLog('RetailCrmHistory::paySystemUpdate', 'RCrmActions::PaymentList()', 'Error paySystem not found in option in order id=' . $order['externalId']);;
2016-09-15 16:42:10 +03:00
}
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
\Bitrix\Sale\OrderTable::update($order['externalId'], array('PAY_SYSTEM_ID' => ''));
$payment = \Bitrix\Sale\Payment::getList(array(
'filter' => array('ORDER_ID' => $order['externalId']),
'order' => array('ID')
))->fetch();
2016-10-04 17:57:39 +03:00
if ($payment['ID']) {
2016-09-15 16:42:10 +03:00
\Bitrix\Sale\Internals\PaymentTable::delete($payment['ID']);
}
}
}
2016-10-04 17:57:39 +03:00
public static function newValue($value)
{
2016-10-12 11:40:18 +03:00
if (array_key_exists('code', $value)) {
2016-09-15 16:42:10 +03:00
return $value['code'];
2016-10-04 17:57:39 +03:00
} else {
2016-09-15 16:42:10 +03:00
return $value;
}
}
2016-10-04 17:57:39 +03:00
public static function removeEmpty($inputArray)
{
2016-09-15 16:42:10 +03:00
$outputArray = array();
2017-09-04 11:36:04 +03:00
2016-10-04 17:57:39 +03:00
if (!empty($inputArray)) {
foreach ($inputArray as $key => $element) {
if (!empty($element) || $element === 0 || $element === '0') {
if (is_array($element)) {
2016-09-15 16:42:10 +03:00
$element = self::removeEmpty($element);
}
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
$outputArray[$key] = $element;
}
}
}
return $outputArray;
}
2017-11-13 11:05:11 +03:00
public static function setProp($obj, $value = '', $prop = '')
2016-10-04 17:57:39 +03:00
{
2017-09-04 11:36:04 +03:00
if (!isset($obj)) {
2016-10-03 16:56:59 +03:00
return false;
}
2017-11-13 11:05:11 +03:00
if ($prop && $value) {
2016-09-15 16:42:10 +03:00
$obj->setField($prop, $value);
2017-11-13 11:05:11 +03:00
} elseif ($value && !$prop) {
2016-09-15 16:42:10 +03:00
$obj->setValue($value);
2017-11-13 11:05:11 +03:00
} elseif (!$value && !$prop) {
2016-09-15 16:42:10 +03:00
$obj->delete();
}
return true;
}
2016-10-04 17:57:39 +03:00
public static function getExistsItem($basket, $moduleId, $productId)
{
2017-09-04 11:36:04 +03:00
foreach ($basket as $basketItem) {
$itemExists = ($basketItem->getField('PRODUCT_ID') == $productId && $basketItem->getField('MODULE') == $moduleId);
2016-09-15 16:42:10 +03:00
2017-09-04 11:36:04 +03:00
if ($itemExists) {
2016-09-15 16:42:10 +03:00
return $basketItem;
2017-09-04 11:36:04 +03:00
}
}
2016-09-15 16:42:10 +03:00
2017-09-04 11:36:04 +03:00
return false;
}
2016-09-15 16:42:10 +03:00
2016-10-04 17:57:39 +03:00
public static function getInfoElement($offerId)
{
2016-10-03 16:56:59 +03:00
$elementInfo = CIBlockElement::GetByID($offerId)->fetch();
$url = CAllIBlock::ReplaceDetailUrl($elementInfo['DETAIL_PAGE_URL'], $elementInfo, false, 'E');
2017-09-04 11:36:04 +03:00
2016-10-03 16:56:59 +03:00
$info = array(
'NAME' => $elementInfo['NAME'],
'URL' => $url,
);
2016-10-04 17:57:39 +03:00
2016-10-03 16:56:59 +03:00
return $info;
2016-09-15 16:42:10 +03:00
}
}
class RetailUser extends CUser
{
public function GetID()
{
2017-09-04 11:36:04 +03:00
$rsUser = CUser::GetList(($by = 'ID'), ($order = 'DESC'), array('LOGIN' => 'retailcrm%'));
2016-09-15 16:42:10 +03:00
if ($arUser = $rsUser->Fetch()) {
return $arUser['ID'];
} else {
$retailUser = new CUser;
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
$userPassword = uniqid();
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
$arFields = array(
2017-09-04 11:36:04 +03:00
"NAME" => 'retailcrm',
"LAST_NAME" => 'retailcrm',
"EMAIL" => 'retailcrm@retailcrm.com',
"LOGIN" => 'retailcrm',
"LID" => "ru",
"ACTIVE" => "Y",
"GROUP_ID" => array(2),
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword
);
2016-09-15 16:42:10 +03:00
$id = $retailUser->Add($arFields);
2017-09-04 11:36:04 +03:00
2016-09-15 16:42:10 +03:00
if (!$id) {
return null;
} else {
return $id;
}
}
}
}