From 0a6c409e11994f07a21ec6bb58ca0031b245c08f Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Fri, 1 Sep 2023 10:10:38 +0300 Subject: [PATCH] =?UTF-8?q?ref=20#91505=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B5=20=D1=81=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=D0=BC=D0=BE=D0=B9=20=D0=BB=D0=BE=D1=8F=D0=BB=D1=8C=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B8=20(#313)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 + .../classes/general/ApiClient_v5.php | 82 ++++++++----- .../general/order/RetailCrmOrder_v5.php | 11 +- intaro.retailcrm/description.ru | 2 +- intaro.retailcrm/install/version.php | 4 +- .../apiclient/traits/loyaltytrait.php | 112 +++++++++++------- .../lib/model/api/loyaltyaccount.php | 4 +- .../account/loyaltyaccountgetresponse.php | 33 ++++++ ...sponse.php => loyaltyaccountsresponse.php} | 7 +- .../lib/service/loyaltyaccountservice.php | 94 +++++++++++++++ 10 files changed, 262 insertions(+), 90 deletions(-) create mode 100644 intaro.retailcrm/lib/model/api/response/loyalty/account/loyaltyaccountgetresponse.php rename intaro.retailcrm/lib/model/api/response/loyalty/account/{loyaltyaccountresponse.php => loyaltyaccountsresponse.php} (86%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dbefbd0..467fbd13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2023-08-31 v.6.4.4 +- Исправлены ошибки при работе с программой лояльности + ## 2023-08-29 v.6.4.3 - Добавлена валидация валют при установке и настройке модуля diff --git a/intaro.retailcrm/classes/general/ApiClient_v5.php b/intaro.retailcrm/classes/general/ApiClient_v5.php index 036f7626..26c9985e 100644 --- a/intaro.retailcrm/classes/general/ApiClient_v5.php +++ b/intaro.retailcrm/classes/general/ApiClient_v5.php @@ -310,11 +310,11 @@ class ApiClient * @param string $by (default: 'externalId') * @param string $site (default: null) * + * @return ApiResponse * @throws \InvalidArgumentException * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * - * @return \RetailCrm\Response\ApiResponse */ public function customersCorporateCompanies( $id, @@ -371,7 +371,7 @@ class ApiClient * @param string $by (default: 'externalId') * @param string $site (default: null) * - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * @@ -403,11 +403,11 @@ class ApiClient * @param string $by (default: 'externalId') * @param string $site (default: null) * + * @return ApiResponse * @throws \InvalidArgumentException * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * - * @return \RetailCrm\Response\ApiResponse */ public function customersCorporateContacts( $id, @@ -443,11 +443,11 @@ class ApiClient * @param int $page (default: null) * @param int $limit (default: null) * - * @throws \InvalidArgumentException + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * - * @return \RetailCrm\Response\ApiResponse + * @throws \InvalidArgumentException */ public function customersCorporateList(array $filter = [], $page = null, $limit = null) { @@ -476,11 +476,11 @@ class ApiClient * @param int $page (default: null) * @param int $limit (default: null) * - * @throws \InvalidArgumentException + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * - * @return \RetailCrm\Response\ApiResponse + * @throws \InvalidArgumentException */ public function customersCorporateNotesList(array $filter = [], $page = null, $limit = null) { @@ -508,11 +508,11 @@ class ApiClient * @param array $note (default: array()) * @param string $site (default: null) * - * @throws \InvalidArgumentException + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * - * @return \RetailCrm\Response\ApiResponse + * @throws \InvalidArgumentException */ public function customersCorporateNotesCreate($note, $site = null) { @@ -534,11 +534,11 @@ class ApiClient * * @param integer $id * - * @throws \InvalidArgumentException + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * - * @return \RetailCrm\Response\ApiResponse + * @throws \InvalidArgumentException */ public function customersCorporateNotesDelete($id) { @@ -564,11 +564,11 @@ class ApiClient * @param string $by (default: 'externalId') * @param string $site (default: null) * + * @return ApiResponse * @throws \InvalidArgumentException * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * - * @return \RetailCrm\Response\ApiResponse */ public function customersCorporateAddresses( $id, @@ -605,7 +605,7 @@ class ApiClient * @param string $by (default: 'externalId') * @param null $site (default: null) * - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse */ public function customersCorporateAddressesCreate($id, array $address = [], $by = 'externalId', $site = null): ApiResponse { @@ -626,11 +626,11 @@ class ApiClient * @param string $addressBy (default: 'externalId') * @param string $site (default: null) * + * @return ApiResponse * @throws \InvalidArgumentException * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * - * @return \RetailCrm\Response\ApiResponse */ public function customersCorporateAddressesEdit( $customerId, @@ -670,11 +670,11 @@ class ApiClient * @param string $by (default: 'externalId') * @param string $site (default: null) * - * @throws \InvalidArgumentException + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException * - * @return \RetailCrm\Response\ApiResponse + * @throws \InvalidArgumentException */ public function customersCorporateCompaniesCreate($id, array $company = [], $by = 'externalId', $site = null) { @@ -696,7 +696,7 @@ class ApiClient * @param string $companyBy (default: 'externalId') * @param string $site (default: null) * - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse * @throws \InvalidArgumentException * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException @@ -732,7 +732,7 @@ class ApiClient * @param string $contactBy (default: 'externalId') * @param string $site (default: null) * - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse * @throws \InvalidArgumentException * @throws \RetailCrm\Exception\CurlException * @throws \RetailCrm\Exception\InvalidJsonException @@ -765,7 +765,7 @@ class ApiClient * @param string $by (default: 'externalId') * @param null $site (default: null) * - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse */ public function customersCorporateEdit( array $params, @@ -2883,7 +2883,8 @@ class ApiClient /** * @param array $request * @param int $checkId - * @return \RetailCrm\Response\ApiResponse + * + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function checkStatusPlVerification(array $request, int $checkId): ApiResponse @@ -2897,7 +2898,8 @@ class ApiClient /** * @param array $request - * @return \RetailCrm\Response\ApiResponse + * + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function loyaltyOrderApply(array $request): ApiResponse @@ -2915,7 +2917,8 @@ class ApiClient /** * @param array $request - * @return \RetailCrm\Response\ApiResponse + * + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function loyaltyOrderCalculate(array $request): ApiResponse @@ -2932,7 +2935,7 @@ class ApiClient } /** - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function getCredentials(): ApiResponse @@ -2945,7 +2948,8 @@ class ApiClient /** * @param array $request - * @return \RetailCrm\Response\ApiResponse + * + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function createLoyaltyAccount(array $request): ApiResponse @@ -2962,7 +2966,8 @@ class ApiClient /** * @param int $loyaltyId - * @return \RetailCrm\Response\ApiResponse + * + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function activateLoyaltyAccount(int $loyaltyId): ApiResponse @@ -2975,7 +2980,8 @@ class ApiClient /** * @param array $request - * @return \RetailCrm\Response\ApiResponse + * + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function sendVerificationCode(array $request): ApiResponse @@ -2991,7 +2997,8 @@ class ApiClient /** * @param array $request - * @return \RetailCrm\Response\ApiResponse + * + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function getLoyaltyAccounts(array $request): ApiResponse @@ -3005,7 +3012,8 @@ class ApiClient /** * @param int $loyaltyId - * @return \RetailCrm\Response\ApiResponse + * + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function getLoyaltyLoyalty(int $loyaltyId): ApiResponse @@ -3017,7 +3025,7 @@ class ApiClient } /** - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function getLoyaltyLoyalties(): ApiResponse @@ -3028,11 +3036,21 @@ class ApiClient ); } + /** + * @param int $accountId + * + * @return ApiResponse + */ + public function getLoyaltyAccount(int $accountId): ApiResponse + { + return $this->client->makeRequest('/loyalty/account/' . $accountId, Client::METHOD_GET); + } + /** * @param array $request * @param int $accountId * - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function editLoyaltyAccount(array $request, int $accountId): ApiResponse @@ -3049,7 +3067,7 @@ class ApiClient /** * @param array $request - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ protected function confirmLpVerificationBySMS(array $request): ApiResponse @@ -3063,7 +3081,7 @@ class ApiClient /** * @param array $request - * @return \RetailCrm\Response\ApiResponse + * @return ApiResponse * @throws \RetailCrm\Exception\CurlException */ public function sendSmsForLpVerification(array $request): ApiResponse diff --git a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php index 343214ea..cf2335fa 100644 --- a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php +++ b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php @@ -391,14 +391,6 @@ class RetailCrmOrder $order['payments'] = $payments; } - if (!empty($arParams['crmOrder']['privilegeType'])) { - $order['privilegeType'] = $arParams['crmOrder']['privilegeType']; - } elseif (ConfigProvider::getLoyaltyProgramStatus() === 'Y' && LoyaltyAccountService::getLoyaltyPersonalStatus()) { - $order['privilegeType'] = 'loyalty_level'; - } else { - $order['privilegeType'] = 'none'; - } - //send if (function_exists('retailCrmBeforeOrderSend')) { $newResOrder = retailCrmBeforeOrderSend($order, $arOrder); @@ -427,6 +419,9 @@ class RetailCrmOrder /** @var \Intaro\RetailCrm\Component\ApiClient\ClientAdapter $client */ $client = ClientFactory::createClientAdapter(); + // Check and set privilegeType + $order['privilegeType'] = LoyaltyAccountService::getPrivilegeType($client, $arParams); + if ($send) { if ($methodApi === 'ordersCreate') { if (isset($arParams['customerCorporate']) && !empty($order['contact']['externalId'])) { diff --git a/intaro.retailcrm/description.ru b/intaro.retailcrm/description.ru index 0e690930..b5ba1fab 100644 --- a/intaro.retailcrm/description.ru +++ b/intaro.retailcrm/description.ru @@ -1 +1 @@ -- Добавлена валидация валют при установке и настройке модуля \ No newline at end of file +- Исправлены ошибки при работе с программой лояльности \ No newline at end of file diff --git a/intaro.retailcrm/install/version.php b/intaro.retailcrm/install/version.php index 16b30a95..107fbc2b 100644 --- a/intaro.retailcrm/install/version.php +++ b/intaro.retailcrm/install/version.php @@ -1,6 +1,6 @@ '6.4.3', - 'VERSION_DATE' => '2023-08-29 16:00:00' + 'VERSION' => '6.4.4', + 'VERSION_DATE' => '2023-09-01 10:00:00' ]; diff --git a/intaro.retailcrm/lib/component/apiclient/traits/loyaltytrait.php b/intaro.retailcrm/lib/component/apiclient/traits/loyaltytrait.php index 16240e10..5f7a0ac4 100644 --- a/intaro.retailcrm/lib/component/apiclient/traits/loyaltytrait.php +++ b/intaro.retailcrm/lib/component/apiclient/traits/loyaltytrait.php @@ -20,7 +20,8 @@ use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountCreateReque use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountEditRequest; use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountRequest; use Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountEditResponse; -use Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountResponse; +use Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountGetResponse; +use Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountsResponse; use Intaro\RetailCrm\Model\Api\Request\Loyalty\LoyaltyCalculateRequest; use Intaro\RetailCrm\Model\Api\Request\Order\Loyalty\OrderLoyaltyApplyRequest; use Intaro\RetailCrm\Model\Api\Request\SmsVerification\SmsVerificationConfirmRequest; @@ -33,7 +34,7 @@ use Intaro\RetailCrm\Model\Api\Response\Order\Loyalty\OrderLoyaltyApplyResponse; use Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationConfirmResponse; use Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationStatusRequest; use Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationStatusResponse; -use RetailCrm\Response\ApiResponse; +use ReflectionException; /** * Trait LoyaltyTrait @@ -42,8 +43,10 @@ use RetailCrm\Response\ApiResponse; trait LoyaltyTrait { /** - * @param \Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationStatusRequest $request - * @return \Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationStatusResponse|null + * @param SmsVerificationStatusRequest $request + * + * @return SmsVerificationStatusResponse|null + * @throws ReflectionException */ public function checkStatusPlVerification(SmsVerificationStatusRequest $request): ?SmsVerificationStatusResponse { @@ -54,8 +57,10 @@ trait LoyaltyTrait } /** - * @param \Intaro\RetailCrm\Model\Api\Request\Order\Loyalty\OrderLoyaltyApplyRequest $request - * @return mixed + * @param OrderLoyaltyApplyRequest $request + * + * @return OrderLoyaltyApplyResponse|null + * @throws ReflectionException */ public function loyaltyOrderApply(OrderLoyaltyApplyRequest $request): ?OrderLoyaltyApplyResponse { @@ -66,8 +71,10 @@ trait LoyaltyTrait } /** - * @param \Intaro\RetailCrm\Model\Api\Request\Loyalty\LoyaltyCalculateRequest $request - * @return mixed + * @param LoyaltyCalculateRequest $request + * + * @return LoyaltyCalculateResponse|null + * @throws ReflectionException */ public function loyaltyCalculate(LoyaltyCalculateRequest $request): ?LoyaltyCalculateResponse { @@ -78,8 +85,36 @@ trait LoyaltyTrait } /** - * @param \Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountCreateRequest $request - * @return \Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountCreateResponse|null + * @param int $accountId + * + * @return LoyaltyAccountGetResponse|null + */ + public function getLoyaltyAccount(int $accountId): ?LoyaltyAccountGetResponse + { + $response = $this->client->getLoyaltyAccount($accountId); + + return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyAccountGetResponse::class); + } + + /** + * @param LoyaltyAccountRequest $request + * + * @return LoyaltyAccountsResponse|null + * @throws ReflectionException + */ + public function getLoyaltyAccounts(LoyaltyAccountRequest $request): ?LoyaltyAccountsResponse + { + $serialized = Serializer::serializeArray($request); + $response = $this->client->getLoyaltyAccounts($serialized); + + return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyAccountsResponse::class); + } + + /** + * @param LoyaltyAccountCreateRequest $request + * + * @return LoyaltyAccountCreateResponse|null + * @throws ReflectionException */ public function createLoyaltyAccount(LoyaltyAccountCreateRequest $request): ?LoyaltyAccountCreateResponse { @@ -90,8 +125,24 @@ trait LoyaltyTrait } /** - * @param \Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountActivateRequest $request - * @return \Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountActivateResponse|null + * @param LoyaltyAccountEditRequest $request + * + * @return LoyaltyAccountEditResponse|null + * @throws ReflectionException + */ + public function editLoyaltyAccount(LoyaltyAccountEditRequest $request): ?LoyaltyAccountEditResponse + { + $serialized = Serializer::serializeArray($request); + $response = $this->client->editLoyaltyAccount($serialized, $request->id); + + return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyAccountEditResponse::class); + } + + /** + * @param LoyaltyAccountActivateRequest $request + * + * @return LoyaltyAccountActivateResponse|null + * @throws ReflectionException */ public function activateLoyaltyAccount(LoyaltyAccountActivateRequest $request): ?LoyaltyAccountActivateResponse { @@ -101,9 +152,12 @@ trait LoyaltyTrait return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyAccountActivateResponse::class); } + /** - * @param \Intaro\RetailCrm\Model\Api\Request\SmsVerification\SmsVerificationConfirmRequest $request - * @return \Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountActivateResponse|null + * @param SmsVerificationConfirmRequest $request + * + * @return LoyaltyAccountActivateResponse|null + * @throws ReflectionException */ public function sendVerificationCode(SmsVerificationConfirmRequest $request): ?SmsVerificationConfirmResponse { @@ -113,22 +167,10 @@ trait LoyaltyTrait return Deserializer::deserializeArray($response->getResponseBody(), SmsVerificationConfirmResponse::class); } - /** - * @param \Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountRequest $request - * @return \Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountResponse|null - */ - public function getLoyaltyAccounts(LoyaltyAccountRequest $request): ?LoyaltyAccountResponse - { - $serialized = Serializer::serializeArray($request); - $response = $this->client->getLoyaltyAccounts($serialized); - - return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyAccountResponse::class); - } - /** * @param int $loyaltyId * - * @return \Intaro\RetailCrm\Model\Api\Response\Loyalty\LoyaltyLoyaltyResponse|null + * @return LoyaltyLoyaltyResponse|null */ public function getLoyaltyLoyalty(int $loyaltyId): ?LoyaltyLoyaltyResponse { @@ -138,7 +180,7 @@ trait LoyaltyTrait } /** - * @return \Intaro\RetailCrm\Model\Api\Response\Loyalty\LoyaltyLoyaltiesResponse|null + * @return LoyaltyLoyaltiesResponse|null */ public function getLoyaltyLoyalties(): ?LoyaltyLoyaltiesResponse { @@ -146,18 +188,4 @@ trait LoyaltyTrait return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyLoyaltiesResponse::class); } - - /** - * @param \Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountEditRequest $request - * - * @return \Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountEditResponse|null - * @throws \ReflectionException - */ - public function editLoyaltyAccount(LoyaltyAccountEditRequest $request): ?LoyaltyAccountEditResponse - { - $serialized = Serializer::serializeArray($request); - $response = $this->client->editLoyaltyAccount($serialized, $request->id); - - return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyAccountEditResponse::class); - } } \ No newline at end of file diff --git a/intaro.retailcrm/lib/model/api/loyaltyaccount.php b/intaro.retailcrm/lib/model/api/loyaltyaccount.php index 2e020521..289ea650 100644 --- a/intaro.retailcrm/lib/model/api/loyaltyaccount.php +++ b/intaro.retailcrm/lib/model/api/loyaltyaccount.php @@ -45,10 +45,10 @@ class LoyaltyAccount /** * Программа лояльности * - * @var \Intaro\RetailCrm\Model\Api\Loyalty + * @var Loyalty * * @Mapping\Type("\Intaro\RetailCrm\Model\Api\Loyalty") - * @Mapping\SerializedName("$loyalty") + * @Mapping\SerializedName("loyalty") */ public $loyalty; diff --git a/intaro.retailcrm/lib/model/api/response/loyalty/account/loyaltyaccountgetresponse.php b/intaro.retailcrm/lib/model/api/response/loyalty/account/loyaltyaccountgetresponse.php new file mode 100644 index 00000000..69d146f6 --- /dev/null +++ b/intaro.retailcrm/lib/model/api/response/loyalty/account/loyaltyaccountgetresponse.php @@ -0,0 +1,33 @@ +GetID())->Fetch(); return isset($userFields['UF_EXT_REG_PL_INTARO']) && $userFields['UF_EXT_REG_PL_INTARO'] === '1'; } + /** + * Возвращает ID аккаунта в программе лояльности + * + * @return int|null + */ + public static function getLoyaltyAccountId(): ?int + { + global $USER; + + $userFields = CUser::GetByID($USER->GetID())->Fetch(); + + return $userFields['UF_LP_ID_INTARO'] ?? null; + } + + /** + * Метод возвращает значение для поля 'privilegeType'. Валидация поля происходит по кейсам: + * 1. Программа лояльност не работает с корп. клиентами. + * 2. Участие клиента не найдено в CRM, соответственно либо нет участия, либо нет клиента. + * 3. Участие клиента найдено, но не активно (поле status = deactivated). + * 4. Программа лояльности деактивирована в CRM. + * + * @param $client + * @param array $arParams + * + * @return string + */ + public static function getPrivilegeType($client, array $arParams = []): string + { + $file = 'loyaltyStatus'; + $privilegeType = 'none'; + + if (!empty($arParams['customerCorporate']['privilegeType'])) { + $privilegeType = $arParams['crmOrder']['privilegeType']; + } elseif (ConfigProvider::getLoyaltyProgramStatus() === 'Y' && self::getLoyaltyPersonalStatus()) { + $privilegeType = 'loyalty_level'; + } + + if (!empty($arParams['customerCorporate']) || !empty($arParams['orderCompany'])) { + return 'none'; + } + + if ($privilegeType === 'none') { + return 'none'; + } + + $loyaltyAccountId = self::getLoyaltyAccountId(); + + if ($loyaltyAccountId === null) { + Logger::getInstance()->write('Участие клиента не найдено', $file); + + return 'none'; + } + + $loyaltyAccount = $client->getLoyaltyAccount($loyaltyAccountId)->loyaltyAccount ?? null; + + if ($loyaltyAccount === null) { + Logger::getInstance()->write('Участие клиента c ID: ' . $loyaltyAccountId . ' не найдено', $file); + + return 'none'; + } + + if ($loyaltyAccount->status === 'deactivated') { + Logger::getInstance()->write('Участие клиента c ID: ' . $loyaltyAccountId . ' деактивированно', $file); + + return 'none'; + } + + $loyaltyId = $loyaltyAccount->loyalty->id ?? null; + + if ($loyaltyId === null) { + Logger::getInstance()->write('Программа лояльности не найдена', $file); + + return 'none'; + } + + $loyalty = $client->getLoyaltyLoyalty($loyaltyId)->loyalty ?? null; + + if ($loyalty === null) { + Logger::getInstance()->write('Программа лояльности c ID: ' . $loyaltyId . ' не найдена', $file); + + return 'none'; + } + + if (!$loyalty->active) { + Logger::getInstance()->write('Программа лояльности c ID: ' . $loyaltyId . ' деактивированна', $file); + + return 'none'; + } + + return $privilegeType; + } + /** * @return array|null * @throws \ReflectionException