fix order upload & logger singleton
This commit is contained in:
parent
b4c8879ec9
commit
4060df418a
12 changed files with 109 additions and 90 deletions
|
@ -212,7 +212,7 @@ class ApiClient
|
|||
*
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function customersСorporateСreate(array $customer, $site = null)
|
||||
public function customersCorporateCreate(array $customer, $site = null)
|
||||
{
|
||||
if (! count($customer)) {
|
||||
throw new \InvalidArgumentException(
|
||||
|
|
|
@ -1,10 +1,50 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class Logger
|
||||
*
|
||||
* @author pavel
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class Logger
|
||||
{
|
||||
/** @var self $instance */
|
||||
private static $instance;
|
||||
|
||||
/** @var string $logPath */
|
||||
private $logPath;
|
||||
|
||||
/** @var int $files */
|
||||
private $files;
|
||||
|
||||
public function __construct($logPath = '/bitrix/modules/intaro.retailcrm/log', $files = 3)
|
||||
|
||||
/**
|
||||
* Get logger instance or re-initialize it with new parameters
|
||||
*
|
||||
* @param string $logPath
|
||||
* @param int $files
|
||||
*
|
||||
* @return \Logger
|
||||
*/
|
||||
public static function getInstance($logPath = '/bitrix/modules/intaro.retailcrm/log', $files = 3)
|
||||
{
|
||||
if (empty(self::$instance)
|
||||
|| self::$instance instanceof Logger
|
||||
&& (self::$instance->logPath != $logPath || self::$instance->files != $files)
|
||||
) {
|
||||
self::$instance = new Logger($logPath, $files);
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logger constructor.
|
||||
*
|
||||
* @param string $logPath
|
||||
* @param int $files
|
||||
*/
|
||||
private function __construct($logPath = '/bitrix/modules/intaro.retailcrm/log', $files = 3)
|
||||
{
|
||||
$this->logPath = $logPath;
|
||||
$this->files = $files;
|
||||
|
|
|
@ -467,7 +467,6 @@ class RCrmActions
|
|||
}
|
||||
|
||||
private static function proxy($api, $methodApi, $method, $params) {
|
||||
$log = new Logger();
|
||||
$version = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_VERSION, 0);
|
||||
try {
|
||||
$result = call_user_func_array(array($api, $methodApi), $params);
|
||||
|
@ -477,7 +476,7 @@ class RCrmActions
|
|||
|| $methodApi == 'customersGet'
|
||||
|| $methodApi == 'customersСorporateGet'
|
||||
) {
|
||||
$log->write(array(
|
||||
Logger::getInstance()->write(array(
|
||||
'api' => $version,
|
||||
'methodApi' => $methodApi,
|
||||
'errorMsg' => !empty($result['errorMsg']) ? $result['errorMsg'] : '',
|
||||
|
@ -485,7 +484,7 @@ class RCrmActions
|
|||
'params' => $params
|
||||
), 'apiErrors');
|
||||
} elseif ($methodApi == 'customersUpload' || $methodApi == 'ordersUpload') {
|
||||
$log->write(array(
|
||||
Logger::getInstance()->write(array(
|
||||
'api' => $version,
|
||||
'methodApi' => $methodApi,
|
||||
'errorMsg' => !empty($result['errorMsg']) ? $result['errorMsg'] : '',
|
||||
|
@ -498,7 +497,7 @@ class RCrmActions
|
|||
'RetailCrm\ApiClient::' . $methodApi,
|
||||
!empty($result['errorMsg']) ? $result['errorMsg'] : ''
|
||||
);
|
||||
$log->write(array(
|
||||
Logger::getInstance()->write(array(
|
||||
'api' => $version,
|
||||
'methodApi' => $methodApi,
|
||||
'errorMsg' => !empty($result['errorMsg']) ? $result['errorMsg'] : '',
|
||||
|
@ -519,7 +518,6 @@ class RCrmActions
|
|||
}
|
||||
} catch (\RetailCrm\Exception\CurlException $e) {
|
||||
static::logException(
|
||||
$log,
|
||||
$method,
|
||||
$methodApi,
|
||||
'CurlException',
|
||||
|
@ -532,7 +530,6 @@ class RCrmActions
|
|||
return false;
|
||||
} catch (InvalidArgumentException $e) {
|
||||
static::logException(
|
||||
$log,
|
||||
$method,
|
||||
$methodApi,
|
||||
'InvalidArgumentException',
|
||||
|
@ -545,7 +542,6 @@ class RCrmActions
|
|||
return false;
|
||||
} catch (\RetailCrm\Exception\InvalidJsonException $e) {
|
||||
static::logException(
|
||||
$log,
|
||||
$method,
|
||||
$methodApi,
|
||||
'InvalidJsonException',
|
||||
|
@ -566,7 +562,6 @@ class RCrmActions
|
|||
/**
|
||||
* Log exception into log file and event log
|
||||
*
|
||||
* @param \Logger $log
|
||||
* @param string $method
|
||||
* @param string $methodApi
|
||||
* @param string $exceptionName
|
||||
|
@ -576,7 +571,6 @@ class RCrmActions
|
|||
* @param array $params
|
||||
*/
|
||||
protected static function logException(
|
||||
$log,
|
||||
$method,
|
||||
$methodApi,
|
||||
$exceptionName,
|
||||
|
@ -590,7 +584,7 @@ class RCrmActions
|
|||
$exception->getCode() . ': ' . $exception->getMessage()
|
||||
);
|
||||
|
||||
$log->write(array(
|
||||
Logger::getInstance()->write(array(
|
||||
'api' => $version,
|
||||
'methodApi' => $methodApi,
|
||||
'errorMsg' => $exception->getMessage(),
|
||||
|
|
|
@ -141,7 +141,6 @@ class RetailCrmEvent
|
|||
return false;
|
||||
}
|
||||
|
||||
$log = new Logger();
|
||||
$arOrder = RetailCrmOrder::orderObjToArr($obOrder);
|
||||
|
||||
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
|
||||
|
@ -282,7 +281,7 @@ class RetailCrmEvent
|
|||
$site
|
||||
);
|
||||
|
||||
$log->write($resultUserCorp, 'resultUserCorp');
|
||||
Logger::getInstance()->write($resultUserCorp, 'resultUserCorp');
|
||||
|
||||
if (!$resultUserCorp) {
|
||||
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmCorporateClient::clientSend', 'error during creating client');
|
||||
|
@ -380,7 +379,7 @@ class RetailCrmEvent
|
|||
);
|
||||
|
||||
if (!$addressResult || ($addressResult && !$addressResult->isSuccessful())) {
|
||||
$log->write(sprintf(
|
||||
Logger::getInstance()->write(sprintf(
|
||||
'error while trying to append address to corporate customer%s%s',
|
||||
PHP_EOL,
|
||||
print_r(array(
|
||||
|
|
|
@ -60,8 +60,7 @@ class RetailCrmHistory
|
|||
|
||||
$customerH = isset($customerHistory['history']) ? $customerHistory['history'] : array();
|
||||
|
||||
$log = new Logger();
|
||||
$log->write($customerH, 'customerHistory');
|
||||
Logger::getInstance()->write($customerH, 'customerHistory');
|
||||
|
||||
if (count($customerH) == 0) {
|
||||
if ($customerHistory['history']['totalPageCount'] > $customerHistory['history']['currentPage']) {
|
||||
|
@ -275,8 +274,7 @@ class RetailCrmHistory
|
|||
|
||||
$orderH = isset($orderHistory['history']) ? $orderHistory['history'] : array();
|
||||
|
||||
$log = new Logger();
|
||||
$log->write($orderH, 'orderHistory');
|
||||
Logger::getInstance()->write($orderH, 'orderHistory');
|
||||
|
||||
if (count($orderH) == 0) {
|
||||
if ($orderHistory['history']['totalPageCount'] > $orderHistory['history']['currentPage']) {
|
||||
|
@ -305,7 +303,7 @@ class RetailCrmHistory
|
|||
}
|
||||
}
|
||||
|
||||
$log->write($order, 'assemblyOrderHistory');
|
||||
Logger::getInstance()->write($order, 'assemblyOrderHistory');
|
||||
|
||||
if ($order['deleted']) {
|
||||
continue;
|
||||
|
|
|
@ -63,8 +63,7 @@ class RetailCrmHistory
|
|||
|
||||
$customerH = isset($customerHistory['history']) ? $customerHistory['history'] : array();
|
||||
|
||||
$log = new Logger();
|
||||
$log->write($customerH, 'customerHistory');
|
||||
Logger::getInstance()->write($customerH, 'customerHistory');
|
||||
|
||||
if (count($customerH) == 0) {
|
||||
if ($customerHistory['history']['totalPageCount'] > $customerHistory['history']['currentPage']) {
|
||||
|
@ -275,8 +274,7 @@ class RetailCrmHistory
|
|||
|
||||
$orderH = isset($orderHistory['history']) ? $orderHistory['history'] : array();
|
||||
|
||||
$log = new Logger();
|
||||
$log->write($orderH, 'orderHistory');
|
||||
Logger::getInstance()->write($orderH, 'orderHistory');
|
||||
|
||||
if (count($orderH) == 0) {
|
||||
if ($orderHistory['history']['totalPageCount'] > $orderHistory['history']['currentPage']) {
|
||||
|
@ -308,7 +306,7 @@ class RetailCrmHistory
|
|||
}
|
||||
}
|
||||
|
||||
$log->write($order, 'assemblyOrderHistory');
|
||||
Logger::getInstance()->write($order, 'assemblyOrderHistory');
|
||||
|
||||
if (isset($order['deleted'])) {
|
||||
if (isset($order['externalId'])) {
|
||||
|
@ -384,7 +382,7 @@ class RetailCrmHistory
|
|||
}
|
||||
|
||||
if (!$contact || empty($contact['customer'])) {
|
||||
$log->write(sprintf(
|
||||
Logger::getInstance()->write(sprintf(
|
||||
'cannot sync order - no customer found. order: %s',
|
||||
print_r($order, true)
|
||||
), 'orderHistory');
|
||||
|
@ -785,8 +783,8 @@ class RetailCrmHistory
|
|||
}
|
||||
}
|
||||
|
||||
$log->write($duplicateItems, 'duplicateItemsOrderHistory');
|
||||
$log->write($collectItems, 'collectItemsOrderHistory');
|
||||
Logger::getInstance()->write($duplicateItems, 'duplicateItemsOrderHistory');
|
||||
Logger::getInstance()->write($collectItems, 'collectItemsOrderHistory');
|
||||
$optionDiscRound = COption::GetOptionString(self::$MODULE_ID, self::$CRM_DISCOUNT_ROUND, 0);
|
||||
|
||||
foreach ($order['items'] as $product) {
|
||||
|
|
|
@ -55,8 +55,6 @@ class RetailCrmInventories
|
|||
}
|
||||
|
||||
if (count($infoBlocks) > 0) {
|
||||
$log = new Logger();
|
||||
|
||||
foreach ($infoBlocks as $id) {
|
||||
$iblockOffer = CCatalogSKU::GetInfoByProductIBlock($id);
|
||||
|
||||
|
@ -118,7 +116,7 @@ class RetailCrmInventories
|
|||
//for log
|
||||
$splitedItems = array_chunk($invUpload, 200);
|
||||
foreach ($splitedItems as $chunk) {
|
||||
$log->write($chunk, 'inventoriesUpload');
|
||||
Logger::getInstance()->write($chunk, 'inventoriesUpload');
|
||||
|
||||
foreach ($shops as $shop) {
|
||||
RCrmActions::apiMethod($api, 'storeInventoriesUpload', __METHOD__, $chunk, $shop);
|
||||
|
|
|
@ -176,8 +176,7 @@ class RetailCrmOrder
|
|||
$normalizer = new RestNormalizer();
|
||||
$order = $normalizer->normalize($order, 'orders');
|
||||
|
||||
$log = new Logger();
|
||||
$log->write($order, 'orderSend');
|
||||
Logger::getInstance()->write($order, 'orderSend');
|
||||
|
||||
if($send) {
|
||||
if (!RCrmActions::apiMethod($api, $methodApi, __METHOD__, $order, $site)) {
|
||||
|
|
|
@ -2,30 +2,6 @@
|
|||
IncludeModuleLangFile(__FILE__);
|
||||
class RetailCrmOrder
|
||||
{
|
||||
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_DATE = 'order_history_date';
|
||||
public static $CRM_CATALOG_BASE_PRICE = 'catalog_base_price';
|
||||
public static $CRM_ORDER_NUMBERS = 'order_numbers';
|
||||
public static $CRM_ORDER_DIMENSIONS = 'order_dimensions';
|
||||
public static $CRM_CURRENCY = 'currency';
|
||||
public static $CRM_CC = 'cc';
|
||||
|
||||
const CANCEL_PROPERTY_CODE = 'INTAROCRM_IS_CANCELED';
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates order or returns order for mass upload
|
||||
|
@ -296,10 +272,9 @@ class RetailCrmOrder
|
|||
$normalizer = new RestNormalizer();
|
||||
$order = $normalizer->normalize($order, 'orders');
|
||||
|
||||
$log = new Logger();
|
||||
$log->write($order, 'orderSend');
|
||||
Logger::getInstance()->write($order, 'orderSend');
|
||||
|
||||
if($send) {
|
||||
if ($send) {
|
||||
if (!RCrmActions::apiMethod($api, $methodApi, __METHOD__, $order, $site)) {
|
||||
return false;
|
||||
}
|
||||
|
@ -329,6 +304,7 @@ class RetailCrmOrder
|
|||
|
||||
$resOrders = array();
|
||||
$resCustomers = array();
|
||||
$resCustomersAdded = array();
|
||||
$resCustomersCorporate = array();
|
||||
$orderIds = array();
|
||||
|
||||
|
@ -456,12 +432,17 @@ class RetailCrmOrder
|
|||
continue;
|
||||
}
|
||||
|
||||
if (!empty($arCustomerCorporate) && !empty($arCustomerCorporate['nickName'])
|
||||
) {
|
||||
if (!empty($arCustomerCorporate) && !empty($arCustomerCorporate['nickName'])) {
|
||||
$resCustomersCorporate[$arCustomerCorporate['nickName']] = $arCustomerCorporate;
|
||||
}
|
||||
|
||||
$resCustomers[$order['LID']][] = $arCustomer;
|
||||
$email = isset($arCustomer['email']) ? $arCustomer['email'] : '';
|
||||
|
||||
if (!in_array($email, $resCustomersAdded)) {
|
||||
$resCustomersAdded[] = $email;
|
||||
$resCustomers[$order['LID']][] = $arCustomer;
|
||||
}
|
||||
|
||||
$resOrders[$order['LID']][] = $arOrders;
|
||||
$recOrders[] = $orderId;
|
||||
}
|
||||
|
@ -513,36 +494,53 @@ class RetailCrmOrder
|
|||
}
|
||||
|
||||
if ("Y" == RetailcrmConfigProvider::getCorporateClientStatus()) {
|
||||
$cachedCorporateIds = array();
|
||||
|
||||
foreach ($resOrders as $packKey => $pack) {
|
||||
foreach ($pack as $key => $orderData) {
|
||||
if (isset($orderData['contragent']['contragentType'])
|
||||
&& $orderData['contragent']['contragentType'] == 'legal-entity'
|
||||
&& !empty($orderData['contragent']['legalName'])
|
||||
) {
|
||||
$corpData = $api->customersCorporateList(array(
|
||||
'nickName' => array($orderData['contragent']['legalName'])
|
||||
));
|
||||
if (isset($cachedCorporateIds[$orderData['contragent']['legalName']])) {
|
||||
$orderData['customer'] = array(
|
||||
'id' => $cachedCorporateIds[$orderData['contragent']['legalName']]
|
||||
);
|
||||
} else {
|
||||
$corpData = $api->customersCorporateList(array(
|
||||
'nickName' => array($orderData['contragent']['legalName'])
|
||||
));
|
||||
|
||||
if ($corpData
|
||||
&& $corpData->isSuccessful()
|
||||
&& $corpData->offsetExists('customersCorporate')
|
||||
) {
|
||||
$corpData = $corpData['customersCorporate'];
|
||||
$corpData = reset($corpData);
|
||||
if ($corpData
|
||||
&& $corpData->isSuccessful()
|
||||
&& $corpData->offsetExists('customersCorporate')
|
||||
&& !empty($corpData['customersCorporate'])
|
||||
) {
|
||||
$corpData = $corpData['customersCorporate'];
|
||||
$corpData = reset($corpData);
|
||||
|
||||
$orderData['customer'] = array('id' => $corpData['id']);
|
||||
} elseif (array_key_exists($orderData['contragent']['legalName'], $resCustomersCorporate)) {
|
||||
$createResponse = $api
|
||||
->customersCreate($resCustomersCorporate[$orderData['contragent']['legalName']]);
|
||||
$orderData['customer'] = array('id' => $corpData['id']);
|
||||
$cachedCorporateIds[$orderData['contragent']['legalName']] = $corpData['id'];
|
||||
} elseif (array_key_exists(
|
||||
$orderData['contragent']['legalName'],
|
||||
$resCustomersCorporate
|
||||
)) {
|
||||
$createResponse = $api
|
||||
->customersCorporateCreate(
|
||||
$resCustomersCorporate[$orderData['contragent']['legalName']]
|
||||
);
|
||||
|
||||
if ($createResponse && $createResponse->isSuccessful()) {
|
||||
$orderData['customer'] = array('id' => $createResponse['id']);
|
||||
if ($createResponse && $createResponse->isSuccessful()) {
|
||||
$orderData['customer'] = array('id' => $createResponse['id']);
|
||||
$cachedCorporateIds[$orderData['contragent']['legalName']]
|
||||
= $createResponse['id'];
|
||||
}
|
||||
}
|
||||
|
||||
time_nanosleep(0, 250000000);
|
||||
}
|
||||
|
||||
$pack[$key] = $orderData;
|
||||
|
||||
time_nanosleep(0, 250000000);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -50,8 +50,6 @@ class RetailCrmPrices
|
|||
}
|
||||
|
||||
if (count($infoBlocks) > 0) {
|
||||
$log = new Logger();
|
||||
|
||||
foreach ($infoBlocks as $id) {
|
||||
$iblockOffer = CCatalogSKU::GetInfoByProductIBlock($id);
|
||||
|
||||
|
@ -133,7 +131,7 @@ class RetailCrmPrices
|
|||
//for log
|
||||
$splitedItems = array_chunk($pricesUpload, 200);
|
||||
foreach ($splitedItems as $chunk) {
|
||||
$log->write($chunk, 'storePricesUpload');
|
||||
Logger::getInstance()->write($chunk, 'storePricesUpload');
|
||||
|
||||
foreach ($shops as $shop) {
|
||||
RCrmActions::apiMethod($api, 'storePricesUpload', __METHOD__, $chunk, $shop);
|
||||
|
|
|
@ -99,8 +99,7 @@ class RetailCrmCorporateClient
|
|||
$normalizer = new RestNormalizer();
|
||||
$customerCorporate = $normalizer->normalize($customerCorporate, 'customerCorporate');
|
||||
|
||||
$log = new Logger();
|
||||
$log->write($customerCorporate, 'clientCorporate');
|
||||
Logger::getInstance()->write($customerCorporate, 'clientCorporate');
|
||||
|
||||
if ($send) {
|
||||
$result = RCrmActions::apiMethod($api, 'customersСorporateСreate', __METHOD__, $customerCorporate, $site);
|
||||
|
|
|
@ -67,8 +67,7 @@ class RetailCrmUser
|
|||
$normalizer = new RestNormalizer();
|
||||
$customer = $normalizer->normalize($customer, 'customers');
|
||||
|
||||
$log = new Logger();
|
||||
$log->write($customer, 'customerSend');
|
||||
Logger::getInstance()->write($customer, 'customerSend');
|
||||
|
||||
if ($send) {
|
||||
if (!RCrmActions::apiMethod($api, 'customersCreate', __METHOD__, $customer, $site)) {
|
||||
|
@ -149,8 +148,7 @@ class RetailCrmUser
|
|||
}
|
||||
}
|
||||
|
||||
$log = new Logger();
|
||||
$log->write($customer, 'customerSend');
|
||||
Logger::getInstance()->write($customer, 'customerSend');
|
||||
|
||||
RCrmActions::apiMethod($api, 'customersEdit', __METHOD__, $customer, $site);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue