1
0
mirror of synced 2024-11-23 22:06:11 +03:00
bitrix-module/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php

1475 lines
68 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';
public static $CRM_CURRENCY = 'currency';
2020-02-18 10:54:42 +03:00
public static $CRM_DISCOUNT_ROUND = 'discount_round';
2016-09-15 16:42:10 +03:00
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);
2018-09-21 12:22:49 +03:00
2016-09-15 16:42:10 +03:00
$api = new RetailCrm\ApiClient($api_host, $api_key);
2018-09-21 12:22:49 +03:00
2016-09-15 16:42:10 +03:00
$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');
2018-09-21 12:22:49 +03:00
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;
2018-09-21 12:22:49 +03:00
2017-09-04 11:36:04 +03:00
continue;
}
2018-09-21 12:22:49 +03:00
2016-09-15 16:42:10 +03:00
return true;
}
2018-09-21 12:22:49 +03:00
2016-09-15 16:42:10 +03:00
$customers = self::assemblyCustomer($customerH);
2018-09-21 12:22:49 +03:00
2016-09-15 16:42:10 +03:00
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
$newUser = new CUser;
2018-09-21 12:22:49 +03:00
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
}
}
2018-09-21 12:22:49 +03:00
2016-10-04 17:57:39 +03:00
if (isset($customer['deleted'])) {
2016-09-15 16:42:10 +03:00
continue;
}
2018-09-21 12:22:49 +03:00
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) {
2019-09-17 18:10:30 +03:00
$userPassword = uniqid("R");
2016-09-15 16:42:10 +03:00
$arFields = array(
"EMAIL" => $customer['email'],
"LOGIN" => $login,
"ACTIVE" => "Y",
"PASSWORD" => $userPassword,
"CONFIRM_PASSWORD" => $userPassword
);
$registeredUserID = $newUser->Add($arFields);
if ($registeredUserID === false) {
2019-09-17 18:10:30 +03:00
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'CUser::Register', 'Error register user: ' . $newUser->LAST_ERROR);
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
}
}
$customer['externalId'] = $registeredUserID;
}
2018-09-21 12:22:49 +03:00
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']) : '';
}
2018-09-21 12:22:49 +03:00
2017-09-04 11:36:04 +03:00
// if (array_key_exists('email', $customer)) {
// $arUser["EMAIL"] = $customer['email'] ? RCrmActions::fromJSON($customer['email']) : '';
// }
2018-09-21 12:22:49 +03:00
2016-10-04 17:57:39 +03:00
if (isset($customer['phones'])) {
2017-09-04 11:36:04 +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);
}
2018-09-21 12:22:49 +03:00
2016-09-15 16:42:10 +03:00
if (function_exists('retailCrmAfterCustomerSave')) {
retailCrmAfterCustomerSave($customer);
}
}
}
2018-09-21 12:22:49 +03:00
2016-09-15 16:42:10 +03:00
$GLOBALS['RETAIL_CRM_HISTORY'] = false;
2018-09-21 12:22:49 +03:00
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']);
2018-09-21 12:22:49 +03:00
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'];
}
}
2018-09-21 12:22:49 +03:00
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)));
$optionsPayStatuses = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_STATUSES, 0))); // --statuses
$optionsOrderProps = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_PROPS, 0));
2018-09-21 12:22:49 +03:00
$optionsLegalDetails = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_LEGAL_DETAILS, 0));
2016-09-15 16:42:10 +03:00
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
$optionsOrderNumbers = COption::GetOptionString(self::$MODULE_ID, self::$CRM_ORDER_NUMBERS, 0);
$optionsCanselOrder = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_CANSEL_ORDER, 0));
$optionsCurrency = COption::GetOptionString(self::$MODULE_ID, self::$CRM_CURRENCY, 0);
$currency = $optionsCurrency ? $optionsCurrency : \Bitrix\Currency\CurrencyManager::getBaseCurrency();
2016-09-15 16:42:10 +03:00
$api = new RetailCrm\ApiClient($api_host, $api_key);
2016-09-15 16:42:10 +03:00
$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;
2018-09-21 12:22:49 +03:00
}
2016-09-15 16:42:10 +03:00
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');
2018-02-27 15:29:43 +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;
2018-09-21 12:22:49 +03:00
2017-09-04 11:36:04 +03:00
continue;
}
2018-02-27 15:29:43 +03:00
2016-09-15 16:42:10 +03:00
return true;
}
2018-02-27 15:29:43 +03:00
2016-09-15 16:42:10 +03:00
$orders = self::assemblyOrder($orderH);
2018-02-27 15:29:43 +03:00
2016-09-15 16:42:10 +03:00
$GLOBALS['RETAIL_CRM_HISTORY'] = true;
2018-02-27 15:29:43 +03:00
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) {
2018-06-13 13:05:58 +03:00
RCrmActions::eventLog('RetailCrmHistory::orderHistory',
'retailCrmBeforeOrderSave()',
'OrderCrmId = ' . $order['id'] . '. Sending canceled after retailCrmBeforeOrderSave'
);
2017-09-04 11:36:04 +03:00
continue;
2016-09-15 16:42:10 +03:00
}
}
2018-02-27 15:29:43 +03:00
2016-10-14 15:25:02 +03:00
$log->write($order, 'assemblyOrderHistory');
2018-02-27 15:29:43 +03:00
2017-09-04 11:36:04 +03:00
if (isset($order['deleted'])) {
if (isset($order['externalId'])) {
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;
}
if (!$newOrder instanceof \Bitrix\Sale\Order) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::load', 'Error order load: ' . $order['externalId']);
continue;
}
2018-02-27 15:29:43 +03:00
2017-09-04 11:36:04 +03:00
$newOrder->setField('CANCELED', 'Y');
$newOrder->save();
}
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) {
2019-09-17 18:10:30 +03:00
$userPassword = uniqid("R");
2016-09-15 16:42:10 +03:00
$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) {
2019-09-17 18:10:30 +03:00
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'CUser::Register', 'Error register user' . $newUser->LAST_ERROR);
2016-09-15 16:42:10 +03:00
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) {
2018-09-21 12:22:49 +03:00
continue;
2016-09-15 16:42:10 +03:00
}
}
$order['customer']['externalId'] = $registeredUserID;
}
2016-10-20 17:41:07 +03:00
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']);
2018-09-21 12:22:49 +03:00
2017-09-04 11:36:04 +03:00
continue;
}
2018-01-12 11:14:33 +03:00
$newOrder = Bitrix\Sale\Order::create($site, $order['customer']['externalId'], $currency);
if (!is_object($newOrder) || !$newOrder instanceof \Bitrix\Sale\Order) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::create', 'Error order create');
continue;
}
2016-09-15 16:42:10 +03:00
$externalId = $newOrder->getId();
$order['externalId'] = $externalId;
}
if (isset($order['externalId'])) {
2016-10-06 17:34:35 +03:00
$itemUpdate = false;
2019-09-17 18:10:30 +03:00
if ($order['externalId'] && is_numeric($order['externalId'])) {
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;
}
2017-09-04 11:36:04 +03:00
}
2016-10-03 16:56:59 +03:00
if (!isset($newOrder) || $newOrder === null) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'Bitrix\Sale\Order::load', 'Error order load number=' . $order['number']);
2018-02-27 15:29:43 +03:00
2016-09-15 16:42:10 +03:00
continue;
}
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 number=' . $order['number']);
2018-09-21 12:22:49 +03:00
2017-09-04 11:36:04 +03:00
continue;
}
2016-09-15 16:42:10 +03:00
2016-10-03 16:56:59 +03:00
$personType = $newOrder->getField('PERSON_TYPE_ID');
2018-09-21 12:22:49 +03:00
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;
2017-09-04 11:36:04 +03:00
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
}
2017-09-04 11:36:04 +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)) {
2018-02-27 15:29:43 +03:00
self::unreserveShipment($newOrder);
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);
2019-01-16 10:28:11 +03:00
$property = $propertyCollection->getItemById($orderProp['ID']);
if ($property) {
$orderProp['ID'] = $property->getField('ORDER_PROPS_ID');
} else {
continue;
}
2016-10-03 16:56:59 +03:00
}
$nProps[] = $orderProp;
}
$propertyCollectionArr['properties'] = $nProps;
2018-09-21 12:22:49 +03:00
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) {
2017-10-24 11:41:11 +03:00
if ($orderProp['PROPS_GROUP_ID'] == 0) {
$somePropValue = $propertyCollection->getItemByOrderPropertyId($orderProp['ID']);
self::setProp($somePropValue);
}
2016-10-03 16:56:59 +03:00
}
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'];
}
2018-09-21 12:22:49 +03:00
2016-09-15 16:42:10 +03:00
$propsKey = array();
2016-10-04 17:57:39 +03:00
foreach ($propertyCollectionArr['properties'] as $prop) {
2017-10-24 11:41:11 +03:00
if ($prop['PROPS_GROUP_ID'] != 0) {
$propsKey[$prop['CODE']]['ID'] = $prop['ID'];
$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
}
}
2018-09-21 12:22:49 +03:00
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') {
2019-09-17 18:10:30 +03:00
if( $order['delivery']['address']['index'] ) {
$location = CSaleLocation::GetByZIP($order['delivery']['address']['index']);
}
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]));
2018-05-23 12:19:59 +03:00
} else {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'RetailCrmHistory::setProp', 'Error location. ' . $order['delivery']['address'][$key] . ' not found add in order number=' . $order['number']);
2016-11-15 17:17:09 +03:00
continue;
}
2018-05-23 12:19:59 +03:00
2017-09-04 11:36:04 +03:00
$parameters['filter']['NAME.LANGUAGE_ID'] = 'ru';
2018-05-23 12:19:59 +03:00
try {
2019-09-17 18:10:30 +03:00
if ( !isset($location) ) {
$location = \Bitrix\Sale\Location\Search\Finder::find($parameters, array('USE_INDEX' => false, 'USE_ORM' => false))->fetch();
}
2018-05-23 12:19:59 +03:00
$somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
self::setProp($somePropValue, $location['CODE']);
} catch (\Bitrix\Main\ArgumentException $argumentException) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'RetailCrmHistory::setProp', 'Location parameter is incorrect in order number=' . $order['number']);
}
} else {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'RetailCrmHistory::setProp', 'Error location. ' . $order['delivery']['address'][$key] . ' is empty in order number=' . $order['number']);
2017-09-04 11:36:04 +03:00
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-10-24 11:41:11 +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) {
2019-03-28 14:35:26 +03:00
if (array_key_exists($key, $order)) {
2016-10-03 16:56:59 +03:00
$somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
2019-09-17 18:10:30 +03:00
self::setProp($somePropValue, RCrmActions::fromJSON($order[$key]));
} elseif(array_key_exists($key, $order['contragent'])) {
$somePropValue = $propertyCollection->getItemByOrderPropertyId($propsKey[$orderProp]['ID']);
self::setProp($somePropValue, RCrmActions::fromJSON($order['contragent'][$key]));
2016-09-15 16:42:10 +03:00
}
}
}
2017-10-24 11:41:11 +03:00
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
//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-01-12 11:14:33 +03:00
if (!$basket) {
$basket = Bitrix\Sale\Basket::create($site);
$newOrder->setBasket($basket);
}
2018-02-27 15:29:43 +03:00
$fUserId = $basket->getFUserId(true);
2018-07-16 16:01:19 +03:00
if (!$fUserId) {
2018-02-27 15:29:43 +03:00
$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;
$response = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
if (isset($response['order'])) {
$orderTemp = $response['order'];
2020-02-18 10:54:42 +03:00
$duplicateItems = [];
foreach ($orderTemp['items'] as $item) {
2020-02-18 10:54:42 +03:00
$duplicateItems[$item['id']]['externalId'] += $item['offer']['externalId'];
$duplicateItems[$item['id']]['quantity'] += $item['quantity'];
$duplicateItems[$item['id']]['discountTotal'] +=
$item['quantity'] * $item['discountTotal'];
$duplicateItems[$item['id']]['initialPrice'] = (float) $item['initialPrice'];
$duplicateItems[$item['id']]['price_sum'] = ($item['quantity'] * $item['initialPrice'])
- ($item['quantity'] * $item['discountTotal']);
}
unset($orderTemp);
} else {
continue;
}
2020-02-18 10:54:42 +03:00
$collectItems = [];
2020-02-18 10:54:42 +03:00
foreach ($duplicateItems as $it) {
$collectItems[$it['externalId']]['quantity'] += $it['quantity'];
$collectItems[$it['externalId']]['price_sum'] += $it['price_sum'];
$collectItems[$it['externalId']]['discountTotal_sum'] += $it['discountTotal'];
if (isset($collectItems[$it['externalId']]['initialPrice_max'])) {
if ($collectItems[$it['externalId']]['initialPrice_max'] < $it['initialPrice']) {
$collectItems[$it['externalId']]['initialPrice_max'] = $it['initialPrice'];
}
} else {
$collectItems[$it['externalId']]['initialPrice_max'] = $it['initialPrice'];
}
$collectItems[$it['externalId']]['initialPricesList'][] = $it['initialPrice'];
}
foreach ($collectItems as $key => $itemData) {
if (count($itemData['initialPricesList']) > 1) {
$discountDelta = 0;
foreach ($itemData['initialPrices'] as $initialPriceItem) {
$delta = $itemData['initialPrice_max'] - (float) $initialPriceItem;
if ($delta !== 0) {
$discountDelta += $delta;
}
}
$collectItems[$key]['discountTotal_sum'] += $discountDelta;
}
2020-02-18 10:54:42 +03:00
}
$log->write($duplicateItems, 'duplicateItemsOrderHistory');
$log->write($collectItems, 'collectItemsOrderHistory');
$optionDiscRound = COption::GetOptionString(self::$MODULE_ID, self::$CRM_DISCOUNT_ROUND, 0);
2016-10-06 17:34:35 +03:00
foreach ($order['items'] as $product) {
2020-02-18 10:54:42 +03:00
if ($collectItems[$product['offer']['externalId']]['quantity']) {
$product['quantity'] = $collectItems[$product['offer']['externalId']]['quantity'];
}
2016-10-06 17:34:35 +03:00
$item = self::getExistsItem($basket, 'catalog', $product['offer']['externalId']);
2018-01-12 11:14:33 +03:00
2016-10-06 17:34:35 +03:00
if (!$item) {
2018-02-27 15:29:43 +03:00
if ($product['delete']) {
2016-10-06 17:34:35 +03:00
continue;
}
2018-01-12 11:14:33 +03:00
2016-10-06 17:34:35 +03:00
$item = $basket->createItem('catalog', $product['offer']['externalId']);
2018-01-12 11:14:33 +03:00
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' => $newOrder->getCurrency(),
2016-10-20 17:41:07 +03:00
'LID' => $site,
'BASE_PRICE' => $collectItems[$product['offer']['externalId']]['initialPrice_max'],
2017-11-13 11:05:11 +03:00
'NAME' => $product['offer']['name'] ? RCrmActions::fromJSON($product['offer']['name']) : $elem['NAME'],
2017-09-04 11:36:04 +03:00
'DETAIL_PAGE_URL' => $elem['URL'],
'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider',
'DIMENSIONS' => $elem['DIMENSIONS'],
'WEIGHT' => $elem['WEIGHT'],
'NOTES' => GetMessage('PRICE_TYPE'),
'PRODUCT_XML_ID' => $elem["XML_ID"],
'CATALOG_XML_ID' => $elem["IBLOCK_XML_ID"]
2016-10-06 17:34:35 +03:00
));
} else {
2017-09-04 11:36:04 +03:00
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'createItem', 'Error item add');
2018-02-27 15:29:43 +03:00
2016-10-06 17:34:35 +03:00
continue;
}
}
2018-01-12 11:14:33 +03:00
2016-10-06 17:34:35 +03:00
if ($product['delete']) {
2020-02-18 10:54:42 +03:00
if ($collectItems[$product['offer']['externalId']]['quantity'] <= 0) {
$item->delete();
2018-09-21 12:22:49 +03:00
continue;
}
2016-10-03 16:56:59 +03:00
}
2016-09-15 16:42:10 +03:00
2016-10-06 17:34:35 +03:00
if ($product['quantity']) {
$item->setFieldNoDemand('QUANTITY', $product['quantity']);
}
if (array_key_exists('initialPrice_max', $collectItems[$product['offer']['externalId']])) {
$item->setField(
'BASE_PRICE',
$collectItems[$product['offer']['externalId']]['initialPrice_max']
);
2016-09-15 16:42:10 +03:00
}
2020-02-18 10:54:42 +03:00
if (array_key_exists('discountTotal_sum', $collectItems[$product['offer']['externalId']])) {
$item->setField('CUSTOM_PRICE', 'Y');
$item->setField('DISCOUNT_NAME', '');
$item->setField('DISCOUNT_VALUE', '');
// Полную цену позиции с учётом скидок делим на количество - получаем цену каждой единицы
// товара с учётом скидок.
$price = $collectItems[
$product['offer']['externalId']
]['price_sum'] / $collectItems[$product['offer']['externalId']]['quantity'];
2018-07-16 16:01:19 +03:00
2020-02-18 10:54:42 +03:00
if ('Y' == $optionDiscRound) {
$price = self::truncate($price, 2);
2016-10-06 17:34:35 +03:00
}
2020-02-18 10:54:42 +03:00
$item->setField('PRICE', $price);
2016-09-15 16:42:10 +03:00
}
}
}
2018-01-12 11:14:33 +03:00
2016-09-15 16:42:10 +03:00
$orderSumm = 0;
2016-10-04 17:57:39 +03:00
foreach ($basket as $item) {
2018-09-21 12:22:49 +03:00
$orderSumm += $item->getFinalPrice();
2016-09-15 16:42:10 +03:00
}
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;
$order['summ'] = $orderSumm;
2018-01-12 11:14:33 +03:00
2016-09-15 16:42:10 +03:00
//payment
2018-01-12 11:14:33 +03:00
$newHistoryPayments = array();
2017-09-04 11:36:04 +03:00
if (array_key_exists('payments', $order)) {
if (!isset($orderCrm)) {
$orderCrm = RCrmActions::apiMethod($api, 'orderGet', __METHOD__, $order['id']);
}
if ($orderCrm) {
2018-03-22 16:11:04 +03:00
self::paymentsUpdate($newOrder, $orderCrm['order'], $newHistoryPayments);
2017-09-04 11:36:04 +03:00
}
2016-09-15 16:42:10 +03:00
}
2016-09-15 16:42:10 +03:00
//delivery
if (array_key_exists('delivery', $order)) {
2016-10-06 17:34:35 +03:00
$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) {
2018-01-12 11:14:33 +03:00
self::deliveryUpdate($newOrder, $optionsDelivTypes, $orderCrm['order']);
2016-09-15 16:42:10 +03:00
}
}
2018-08-08 10:30:18 +03:00
if ($itemUpdate === true && $newOrder->getField('CANCELED') != 'Y') {
2018-01-12 11:14:33 +03:00
self::shipmentItemReset($newOrder);
}
2016-10-04 17:57:39 +03:00
if (isset($orderCrm)) {
2018-09-21 12:22:49 +03:00
unset($orderCrm);
2016-09-15 16:42:10 +03:00
}
2018-01-12 11:14:33 +03:00
$newOrder->setField('PRICE', $orderSumm);
2018-09-21 12:22:49 +03:00
self::orderSave($newOrder);
2018-01-23 17:24:32 +03:00
if ($optionsOrderNumbers == 'Y' && isset($order['number'])) {
$newOrder->setField('ACCOUNT_NUMBER', $order['number']);
2018-09-21 12:22:49 +03:00
self::orderSave($newOrder);
2018-01-23 17:24:32 +03:00
}
if (!empty($newHistoryPayments)) {
2018-01-12 11:14:33 +03:00
foreach ($newOrder->getPaymentCollection() as $orderPayment) {
if (array_key_exists($orderPayment->getField('XML_ID'), $newHistoryPayments)) {
$paymentId = $orderPayment->getId();
$paymentExternalId = RCrmActions::generatePaymentExternalId($paymentId);
2019-09-10 13:29:23 +03:00
if (is_null($paymentId)) {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'paymentsUpdate', 'Save payment error, order=' . $order['number']);
continue;
}
$paymentExternalId = $orderPayment->getId();
2018-01-12 11:14:33 +03:00
if ($paymentExternalId) {
$newHistoryPayments[$orderPayment->getField('XML_ID')]['externalId'] = $paymentExternalId;
RCrmActions::apiMethod($api, 'paymentEditById', __METHOD__, $newHistoryPayments[$orderPayment->getField('XML_ID')]);
if ($paymentId) {
\Bitrix\Sale\Internals\PaymentTable::update($paymentId, array('XML_ID' => ''));
}
2018-09-21 12:22:49 +03:00
}
2018-01-12 11:14:33 +03:00
}
}
}
if (!$order['externalId']) {
$order["externalId"] = $newOrder->getId();
2018-09-21 12:22:49 +03:00
if (RCrmActions::apiMethod($api, 'ordersFixExternalIds', __METHOD__, array(array('id' => $order['id'], 'externalId' => $newOrder->getId()))) == false){
continue;
2016-09-15 16:42:10 +03:00
}
2016-10-06 17:34:35 +03:00
}
2016-09-15 16:42:10 +03:00
if (function_exists('retailCrmAfterOrderSave')) {
retailCrmAfterOrderSave($order);
}
}
2018-07-16 16:01:19 +03:00
unset($newOrder);
2016-09-15 16:42:10 +03:00
}
2016-09-15 16:42:10 +03:00
$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'];
}
}
2018-09-21 12:22:49 +03:00
/**
* @param $array
* @param $value
*
* @return array
*/
public static function search_array_by_value($array, $value)
{
$results = array();
if (is_array($array)) {
$found = array_search($value,$array);
if ($found) {
$results[] = $found;
}
foreach ($array as $subarray)
$results = array_merge($results, static::search_array_by_value($subarray, $value));
}
return $results;
}
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')) {
2018-09-21 12:22:49 +03:00
$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']);
2018-09-21 12:22:49 +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'];
}
2018-09-21 12:22:49 +03:00
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']);
}
2018-09-21 12:22:49 +03:00
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']);
} elseif (strripos($change['field'], 'custom_') !== false) {
$customers[$change['customer']['id']]['customFields'][str_replace('custom_', '', $change['field'])] = self::newValue($change['newValue']);
2016-09-15 16:42:10 +03:00
}
2018-09-21 12:22:49 +03:00
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-09-15 16:42:10 +03:00
return $customers;
}
2018-09-21 12:22:49 +03:00
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')) {
2018-09-21 12:22:49 +03:00
$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'])) {
2018-09-21 12:22:49 +03:00
$item['create'] = 1;
2016-09-15 16:42:10 +03:00
}
$items[$item['id']] = $item;
}
$change['order']['items'] = $items;
}
2018-09-21 12:22:49 +03:00
2017-09-04 11:36:04 +03:00
if ($change['order']['payments']) {
$payments = array();
foreach ($change['order']['payments'] as $payment) {
$payments[$payment['id']] = $payment;
}
$change['order']['payments'] = $payments;
}
2018-09-21 12:22:49 +03:00
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']);
}
2018-09-21 12:22:49 +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'];
2018-09-21 12:22:49 +03:00
}
2016-09-15 16:42:10 +03:00
2019-12-20 14:22:53 +03:00
if ($change['field'] == 'number') {
$orders[$change['order']['id']]['number'] = $change['newValue'];
}
2016-10-04 17:57:39 +03:00
if ($change['item']) {
2018-09-21 12:22:49 +03:00
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'];
}
2017-09-04 11:36:04 +03:00
} elseif ($change['payment']) {
2018-09-21 12:22:49 +03:00
if ($orders[$change['order']['id']]['payments'][$change['payment']['id']]) {
2017-09-04 11:36:04 +03:00
$orders[$change['order']['id']]['payments'][$change['payment']['id']] = array_merge($orders[$change['order']['id']]['payments'][$change['payment']['id']], $change['payment']);
} else {
$orders[$change['order']['id']]['payments'][$change['payment']['id']] = $change['payment'];
}
if (empty($change['oldValue']) && $change['field'] == 'payments') {
$orders[$change['order']['id']]['payments'][$change['payment']['id']]['create'] = 1;
unset($orders[$change['order']['id']]['payments'][$change['payment']['id']]['delete']);
}
if (empty($change['newValue']) && $change['field'] == 'payments') {
$orders[$change['order']['id']]['payments'][$change['payment']['id']]['delete'] = 1;
}
if (!$orders[$change['order']['id']]['payments'][$change['payment']['id']]['create'] && $fields['payment'][$change['field']]) {
$orders[$change['order']['id']]['payments'][$change['payment']['id']][$fields['payment'][$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;
}
2018-09-21 12:22:49 +03:00
}
2016-09-15 16:42:10 +03:00
}
2018-09-21 12:22:49 +03:00
2016-09-15 16:42:10 +03:00
return $orders;
}
/**
* Update shipment in order
*
* @param order object
* @param options delivery types
* @param order from crm
2018-09-21 12:22:49 +03:00
*
2018-01-12 11:14:33 +03:00
* @return void
*/
2018-01-12 11:14:33 +03:00
public static function deliveryUpdate(Bitrix\Sale\Order $order, $optionsDelivTypes, $orderCrm)
2016-10-04 17:57:39 +03:00
{
if (!$order instanceof Bitrix\Sale\Order) {
2017-09-04 11:36:04 +03:00
return false;
}
2016-09-15 16:42:10 +03:00
if ($order->getId()) {
$update = true;
2016-10-04 17:57:39 +03:00
} else {
$update = false;
2016-10-04 17:57:39 +03:00
}
$crmCode = isset($orderCrm['delivery']['code']) ? $orderCrm['delivery']['code'] : false;
$noDeliveryId = \Bitrix\Sale\Delivery\Services\EmptyDeliveryService::getEmptyDeliveryServiceId();
if ($crmCode === false || !isset($optionsDelivTypes[$crmCode])) {
$deliveryId = $noDeliveryId;
} else {
$deliveryId = $optionsDelivTypes[$crmCode];
2016-10-04 17:57:39 +03:00
if (isset($orderCrm['delivery']['service']['code'])) {
$deliveryCode = \Bitrix\Sale\Delivery\Services\Manager::getCodeById($deliveryId);
2019-04-18 09:52:35 +03:00
$serviceCode = $orderCrm['delivery']['service']['code'];
2019-09-17 18:10:30 +03:00
2019-10-08 16:23:18 +03:00
$service = \Bitrix\Sale\Delivery\Services\Manager::getService($deliveryId);
if (is_object($service)) {
$services = $service->getProfilesList();
if (!array_key_exists($serviceCode, $services)) {
$serviceCode = strtoupper($serviceCode);
$serviceCode = str_replace(array('-'), "_", $serviceCode);
}
2019-04-18 09:52:35 +03:00
}
if ($deliveryCode) {
try {
2019-09-17 18:10:30 +03:00
$deliveryService = \Bitrix\Sale\Delivery\Services\Manager::getObjectByCode($deliveryCode . ':' . $serviceCode);
} catch (Bitrix\Main\SystemException $systemException) {
RCrmActions::eventLog('RetailCrmHistory::deliveryEdit', '\Bitrix\Sale\Delivery\Services\Manager::getObjectByCode', $systemException->getMessage());
2016-10-04 17:57:39 +03:00
}
if (isset($deliveryService)) {
$deliveryId = $deliveryService->getId();
2016-10-04 17:57:39 +03:00
}
}
}
}
$delivery = \Bitrix\Sale\Delivery\Services\Manager::getObjectById($deliveryId);
$shipmentColl = $order->getShipmentCollection();
if ($delivery) {
if (!$update) {
$shipment = $shipmentColl->createItem($delivery);
$shipment->setFields(array(
'BASE_PRICE_DELIVERY' => $orderCrm['delivery']['cost'],
'CURRENCY' => $order->getCurrency(),
'DELIVERY_NAME' => $delivery->getName(),
'CUSTOM_PRICE_DELIVERY' => 'Y'
));
} else {
foreach ($shipmentColl as $shipment) {
if (!$shipment->isSystem()) {
$shipment->setFields(array(
'BASE_PRICE_DELIVERY' => $orderCrm['delivery']['cost'],
'CURRENCY' => $order->getCurrency(),
'DELIVERY_ID' => $deliveryId,
2018-01-12 11:14:33 +03:00
'DELIVERY_NAME' => $delivery->getName(),
2018-02-27 15:29:43 +03:00
'CUSTOM_PRICE_DELIVERY' => 'Y'
));
}
2016-10-04 17:57:39 +03:00
}
}
}
2018-01-12 11:14:33 +03:00
}
2018-01-12 11:14:33 +03:00
/**
* Update shipment item colletion
2018-09-21 12:22:49 +03:00
*
2018-02-27 15:29:43 +03:00
* @param \Bitrix\Sale\Order $order
2018-09-21 12:22:49 +03:00
*
2018-01-12 11:14:33 +03:00
* @return void | boolean
*/
public static function shipmentItemReset($order)
{
$shipmentCollection = $order->getShipmentCollection();
$basket = $order->getBasket();
foreach ($shipmentCollection as $shipment) {
if (!$shipment->isSystem()) {
2018-02-27 15:29:43 +03:00
$reserved = false;
2018-03-22 16:11:04 +03:00
if ($shipment->needReservation()) {
2018-02-27 15:29:43 +03:00
$reserved = true;
}
2018-01-12 11:14:33 +03:00
$shipmentItemColl = $shipment->getShipmentItemCollection();
2018-02-27 15:29:43 +03:00
if ($reserved === true) {
$shipment->tryUnreserve();
}
2018-01-12 11:14:33 +03:00
try {
2018-02-27 15:29:43 +03:00
$shipmentItemColl->resetCollection($basket);
if ($reserved === true) {
$shipment->tryReserve();
}
2018-01-12 11:14:33 +03:00
} catch (\Bitrix\Main\NotSupportedException $NotSupportedException) {
RCrmActions::eventLog('RetailCrmHistory::shipmentItemReset', '\Bitrix\Sale\ShipmentItemCollection::resetCollection()', $NotSupportedException->getMessage());
return false;
}
}
}
2016-10-04 17:57:39 +03:00
}
2018-02-27 15:29:43 +03:00
/**
* Unreserve items if order canceled
2018-09-21 12:22:49 +03:00
*
2018-02-27 15:29:43 +03:00
* @param \Bitrix\Sale\Order $order
2018-09-21 12:22:49 +03:00
*
2018-02-27 15:29:43 +03:00
* @return void | boolean
*/
public static function unreserveShipment($order)
{
$shipmentCollection = $order->getShipmentCollection();
foreach ($shipmentCollection as $shipment) {
if (!$shipment->isSystem()) {
try {
$shipment->tryUnreserve();
} catch (Main\ArgumentOutOfRangeException $ArgumentOutOfRangeException) {
RCrmActions::eventLog('RetailCrmHistory::unreserveShipment', '\Bitrix\Sale\Shipment::tryUnreserve()', $ArgumentOutOfRangeException->getMessage());
return false;
} catch (Main\NotSupportedException $NotSupportedException) {
RCrmActions::eventLog('RetailCrmHistory::unreserveShipment', '\Bitrix\Sale\Shipment::tryUnreserve()', $NotSupportedException->getMessage());
return false;
}
}
}
}
2018-01-12 11:14:33 +03:00
/**
* Update payment in order
2018-09-21 12:22:49 +03:00
*
2018-01-12 11:14:33 +03:00
* @param object $order
* @param array $paymentsCrm
* @param array $newHistoryPayments
2018-09-21 12:22:49 +03:00
*
2018-01-12 11:14:33 +03:00
* @return void
*/
2018-03-22 16:11:04 +03:00
public static function paymentsUpdate($order, $paymentsCrm, &$newHistoryPayments = array())
2016-10-04 17:57:39 +03:00
{
2017-09-04 11:36:04 +03:00
$optionsPayTypes = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT_TYPES, 0)));
$optionsPayment = array_flip(unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_PAYMENT, 0)));
$allPaymentSystems = RCrmActions::PaymentList();
foreach ($allPaymentSystems as $allPaymentSystem) {
$arPaySysmems[$allPaymentSystem['ID']] = $allPaymentSystem['NAME'];
}
$paymentsList = array();
$paymentColl = $order->getPaymentCollection();
foreach ($paymentColl as $paymentData) {
$data = $paymentData->getFields()->getValues();
$paymentsList[$data['ID']] = $paymentData;
}
2018-09-21 12:22:49 +03:00
2017-09-04 11:36:04 +03:00
//data from crm
$paySumm = 0;
foreach ($paymentsCrm['payments'] as $paymentCrm) {
2017-11-13 11:05:11 +03:00
if (isset($paymentCrm['externalId']) && !empty($paymentCrm['externalId'])) {
2017-09-04 11:36:04 +03:00
//find the payment
2019-09-17 18:10:30 +03:00
$nowPaymentId = RCrmActions::getFromPaymentExternalId($paymentCrm['externalId']);
$nowPayment = $paymentsList[$nowPaymentId];
2017-09-04 11:36:04 +03:00
//update data
if ($nowPayment instanceof \Bitrix\Sale\Payment) {
$nowPayment->setField('SUM', $paymentCrm['amount']);
if ($optionsPayTypes[$paymentCrm['type']] != $nowPayment->getField('PAY_SYSTEM_ID')) {
$nowPayment->setField('PAY_SYSTEM_ID', $optionsPayTypes[$paymentCrm['type']]);
$nowPayment->setField('PAY_SYSTEM_NAME', $arPaySysmems[$optionsPayTypes[$paymentCrm['type']]]);
2016-09-15 16:42:10 +03:00
}
2017-09-04 11:36:04 +03:00
if (isset($optionsPayment[$paymentCrm['status']])) {
$nowPayment->setField('PAID', $optionsPayment[$paymentCrm['status']]);
}
2019-09-17 18:10:30 +03:00
unset($paymentsList[$nowPaymentId]);
2016-09-15 16:42:10 +03:00
}
2019-09-10 13:23:51 +03:00
} elseif (array_key_exists($paymentCrm['type'], $optionsPayTypes)) {
2018-01-12 11:14:33 +03:00
$newHistoryPayments[$paymentCrm['id']] = $paymentCrm;
2017-09-04 11:36:04 +03:00
$newPayment = $paymentColl->createItem();
$newPayment->setField('SUM', $paymentCrm['amount']);
$newPayment->setField('PAY_SYSTEM_ID', $optionsPayTypes[$paymentCrm['type']]);
$newPayment->setField('PAY_SYSTEM_NAME', $arPaySysmems[$optionsPayTypes[$paymentCrm['type']]]);
$newPayment->setField('PAID', $optionsPayment[$paymentCrm['status']] ? $optionsPayment[$paymentCrm['status']] : 'N');
$newPayment->setField('CURRENCY', $order->getCurrency());
2017-09-04 11:36:04 +03:00
$newPayment->setField('IS_RETURN', 'N');
$newPayment->setField('PRICE_COD', '0.00');
$newPayment->setField('EXTERNAL_PAYMENT', 'N');
$newPayment->setField('UPDATED_1C', 'N');
2018-01-12 11:14:33 +03:00
$newPayment->setField('XML_ID', $paymentCrm['id']);
2017-09-04 11:36:04 +03:00
$newPaymentId = $newPayment->getId();
unset($paymentsList[$newPaymentId]);
2019-09-10 13:29:23 +03:00
} else {
RCrmActions::eventLog('RetailCrmHistory::orderHistory', 'paymentsUpdate', 'Save payment error, incorrect type: ' . $paymentCrm['type']);
2016-09-15 16:42:10 +03:00
}
2017-11-13 11:05:11 +03:00
2017-09-04 11:36:04 +03:00
if ($optionsPayment[$paymentCrm['status']] == 'Y') {
$paySumm += $paymentCrm['amount'];
}
}
foreach ($paymentsList as $payment) {
if ($payment->isPaid()) {
$payment->setPaid("N");
2016-09-15 16:42:10 +03:00
}
2017-09-04 11:36:04 +03:00
$payment->delete();
2016-09-15 16:42:10 +03:00
}
2018-09-21 12:22:49 +03:00
2017-09-04 11:36:04 +03:00
if ($paymentsCrm['totalSumm'] == $paySumm) {
$order->setFieldNoDemand('PAYED', 'Y');
} else {
$order->setFieldNoDemand('PAYED', 'N');
}
2016-09-15 16:42:10 +03:00
}
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;
}
2018-09-21 12:22:49 +03:00
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;
}
2018-09-21 12:22:49 +03:00
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;
}
2018-09-21 12:22:49 +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
$catalog = CCatalogProduct::GetByID($offerId);
2016-10-03 16:56:59 +03:00
$info = array(
'NAME' => $elementInfo['NAME'],
'URL' => $url,
2018-02-27 15:29:43 +03:00
'DIMENSIONS' => serialize(array(
2017-09-04 11:36:04 +03:00
'WIDTH' => $catalog['WIDTH'],
'HEIGHT' => $catalog['HEIGHT'],
'LENGTH' => $catalog['LENGTH'],
2018-02-27 15:29:43 +03:00
)),
'WEIGHT' => $catalog['WEIGHT'],
'XML_ID' => $elementInfo["XML_ID"],
'IBLOCK_XML_ID' => $elementInfo["IBLOCK_EXTERNAL_ID"]
2016-10-03 16:56:59 +03:00
);
2018-09-21 12:22:49 +03:00
2016-10-03 16:56:59 +03:00
return $info;
2016-09-15 16:42:10 +03:00
}
2018-09-21 12:22:49 +03:00
/**
* @param $order
*
* @return boolean
*/
private static function orderSave($order)
{
try {
$order->save();
return true;
} catch (\Exception $exception) {
RCrmActions::eventLog(
'RetailCrmHistory::orderHistory',
'Order saving',
$exception->getMessage()
);
return false;
}
}
2020-02-18 10:54:42 +03:00
/**
* Truncate a float number
*
* @param float $val
* @param int f Number of precision
*
* @return float
*/
public static function truncate($val, $precision = "0")
{
if(($p = strpos($val, '.')) !== false
|| ($p = strpos($val, ',')) !== false
) {
$val = floatval(substr($val, 0, $p + 1 + $precision));
}
return $val;
}
2018-09-21 12:22:49 +03:00
}
2016-09-15 16:42:10 +03:00
class RetailUser extends CUser
{
public function GetID()
{
$rsUser = CUser::GetList(($by = 'ID'), ($order = 'DESC'), array('LOGIN' => 'retailcrm'));
2017-09-04 11:36:04 +03:00
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;
}
}
}
}