From 5c750e73977b30d05aaabd44ea5208bd4d8b13a6 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Thu, 13 Jul 2023 17:24:05 +0300 Subject: [PATCH] =?UTF-8?q?ref=20#91040=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=D0=B5=D0=BD=D0=BE=20=D1=81=D0=BE=D0=B1=D1=8B=D1=82?= =?UTF-8?q?=D0=B8=D0=B5=20OnAfterUserRegister=20=D0=98=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D0=9F?= =?UTF-8?q?=D0=9B=20=D0=B8=20=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B2=20=D0=BE=D0=B4=D0=B8=D0=BD=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB.=20=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=20up?= =?UTF-8?q?dater.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../classes/general/events/RetailCrmEvent.php | 51 ++++ intaro.retailcrm/install/index.php | 18 +- intaro.retailcrm/lib/component/constants.php | 6 +- .../component/installer/installertrait.php | 257 ++++++++++++++++++ intaro.retailcrm/updater.php | 91 +++++++ 5 files changed, 414 insertions(+), 9 deletions(-) create mode 100644 intaro.retailcrm/lib/component/installer/installertrait.php diff --git a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php index 23e0d230..297a68ad 100644 --- a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php +++ b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php @@ -1,6 +1,10 @@ 0) { + $user = UserRepository::getById($arFields['USER_ID']); + + if (isset($_POST['REGISTER']['PERSONAL_PHONE'])) { + $phone = htmlspecialchars($_POST['REGISTER']['PERSONAL_PHONE']); + + if ($user !== null) { + $user->setPersonalPhone($phone); + $user->save(); + } + + $arFields['PERSONAL_PHONE'] = $phone; + } + + /* @var CustomerService $customerService */ + $customerService = ServiceLocator::get(CustomerService::class); + $customer = $customerService->createModel($arFields['USER_ID']); + + $customerService->createOrUpdateCustomer($customer); + + //Если пользователь выразил желание зарегистрироваться в ПЛ и согласился со всеми правилами + if ((int) $arFields['UF_REG_IN_PL_INTARO'] === 1 + && (int) $arFields['UF_AGREE_PL_INTARO'] === 1 + && (int) $arFields['UF_PD_PROC_PL_INTARO'] === 1 + ) { + $phone = $arFields['PERSONAL_PHONE'] ?? ''; + $card = $arFields['UF_CARD_NUM_INTARO'] ?? ''; + $customerId = (string) $arFields['USER_ID']; + + /** @var LoyaltyAccountService $service */ + $service = ServiceLocator::get(LoyaltyAccountService::class); + $createResponse = $service->createLoyaltyAccount($phone, $card, $customerId); + + $service->activateLpUserInBitrix($createResponse, $arFields['USER_ID']); + } + } + } + /** * @return bool */ diff --git a/intaro.retailcrm/install/index.php b/intaro.retailcrm/install/index.php index be5db0fb..07908158 100644 --- a/intaro.retailcrm/install/index.php +++ b/intaro.retailcrm/install/index.php @@ -16,6 +16,7 @@ use Bitrix\Sale\Internals\OrderTable; use Intaro\RetailCrm\Component\ConfigProvider; use Intaro\RetailCrm\Component\Installer\LoyaltyInstallerTrait; use Intaro\RetailCrm\Component\Installer\SubscriberInstallerTrait; +use Intaro\RetailCrm\Component\Installer\InstallerTrait; use Intaro\RetailCrm\Service\OrderLoyaltyDataService; use Intaro\RetailCrm\Vendor\Symfony\Component\Process\PhpExecutableFinder; use RetailCrm\ApiClient; @@ -32,11 +33,13 @@ if (class_exists('intaro_retailcrm')) { include(__DIR__ . '/../lib/component/installer/loyaltyinstallertrait.php'); include (__DIR__ . '/../lib/component/installer/subscriberinstallertrait.php'); +include (__DIR__ . '/../lib/component/installer/installertrait.php'); class intaro_retailcrm extends CModule { - use LoyaltyInstallerTrait; - use SubscriberInstallerTrait; + //use LoyaltyInstallerTrait; + //use SubscriberInstallerTrait; + use InstallerTrait; public const V5 = 'v5'; public $MODULE_ID = 'intaro.retailcrm'; @@ -250,13 +253,17 @@ class intaro_retailcrm extends CModule include($this->INSTALL_PATH . '/../lib/component/apiclient/clientadapter.php'); $this->CopyFiles(); - $this->addLPUserFields(); + $this->addEvents(); + $this->addAgreement(); + $this->addUserFields(); + + /* $this->addLPUserFields(); $this->addLPEvents(); $this->addAgreement(); $this->CopyFilesSubscribe(); $this->addSubscribeUserFields(); - $this->addSubscriberEvents(); + $this->addSubscriberEvents();*/ OrderLoyaltyDataService::createLoyaltyHlBlock(); @@ -1077,6 +1084,8 @@ class intaro_retailcrm extends CModule RegisterModuleDependences('sale', 'OnSaleOrderDeleted', $this->MODULE_ID, 'RetailCrmEvent', 'orderDelete'); RegisterModuleDependences('sale', 'OnSalePaymentEntitySaved', $this->MODULE_ID, 'RetailCrmEvent', 'paymentSave'); RegisterModuleDependences('sale', 'OnSalePaymentEntityDeleted', $this->MODULE_ID, 'RetailCrmEvent', 'paymentDelete'); + RegisterModuleDependences('main', 'OnAfterUserRegister', $this->MODULE_ID, 'RetailCrmEvent', 'OnAfterUserRegister'); + COption::SetOptionString($this->MODULE_ID, $this->CRM_CATALOG_BASE_PRICE, htmlspecialchars(trim($_POST['price-types']))); COption::SetOptionString($this->MODULE_ID, $this->CRM_INVENTORIES_UPLOAD, 'N'); @@ -1276,6 +1285,7 @@ class intaro_retailcrm extends CModule UnRegisterModuleDependences('main', 'OnBeforeProlog', $this->MODULE_ID, 'RetailCrmUa', 'add'); UnRegisterModuleDependences('sale', 'OnSalePaymentEntitySaved', $this->MODULE_ID, 'RetailCrmEvent', 'paymentSave'); UnRegisterModuleDependences('sale', 'OnSalePaymentEntityDeleted', $this->MODULE_ID, 'RetailCrmEvent', 'paymentDelete'); + UnRegisterModuleDependences('main', 'OnAfterUserRegister', $this->MODULE_ID, 'RetailCrmEvent', 'OnAfterUserRegister'); if ( CModule::IncludeModule('catalog') diff --git a/intaro.retailcrm/lib/component/constants.php b/intaro.retailcrm/lib/component/constants.php index f91d2beb..aa865463 100644 --- a/intaro.retailcrm/lib/component/constants.php +++ b/intaro.retailcrm/lib/component/constants.php @@ -101,11 +101,7 @@ class Constants public const LP_EVENTS = [ ['EVENT_NAME' => 'OnSaleOrderSaved', 'FROM_MODULE' => 'sale'], ['EVENT_NAME' => 'OnSaleComponentOrderResultPrepared', 'FROM_MODULE' => 'sale'], - ['EVENT_NAME' => 'OnAfterUserRegister', 'FROM_MODULE' => 'main'], - ]; - public const SUBSCRIBE_EVENTS = [ - ['EVENT_NAME' => 'OnAfterUserRegister', 'FROM_MODULE' => 'main'], - ['EVENT_NAME' => 'OnAfterUserUpdate', 'FROM_MODULE' => 'main'] + //['EVENT_NAME' => 'OnAfterUserRegister', 'FROM_MODULE' => 'main'], ]; public const SITES_AVAILABLE = 'sites_available'; } diff --git a/intaro.retailcrm/lib/component/installer/installertrait.php b/intaro.retailcrm/lib/component/installer/installertrait.php new file mode 100644 index 00000000..c34248a8 --- /dev/null +++ b/intaro.retailcrm/lib/component/installer/installertrait.php @@ -0,0 +1,257 @@ +registerEventHandler( + $event['FROM_MODULE'], + $event['EVENT_NAME'], + Constants::MODULE_ID, + EventsHandlers::class, + $event['EVENT_NAME'] . 'Handler' + ); + } + } catch (ObjectPropertyException | ArgumentException | SystemException $exception) { + RCrmActions::eventLog( + 'intaro.retailcrm/install/index.php', + 'RetailCrm\ApiClient::addLPEvents', + $exception->getMessage() + ); + } + } + } + + /** + * Перемещение модульных шаблонов в CMS + */ + public function CopyFiles(): void + { + $pathFrom = $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . Constants::MODULE_ID . '/install'; + + CopyDirFiles( + $pathFrom . '/export', + $_SERVER['DOCUMENT_ROOT'], + true, + true, + false + ); + + $templateNames = [ + 'default_loyalty' => [ + 'sale.order.ajax', + 'sale.basket.basket', + 'main.register', + ], + + 'default_subscribe' => [ + 'sale.personal.section' + ] + ]; + + foreach ($templateNames as $directory => $names) { + foreach ($names as $name) { + $this->copy($directory, $name); + } + } + } + + /** + * Add USER fields for LP + */ + public function addUserFields(): void + { + $this->addCustomUserFields( + [ + [ + 'name' => 'UF_CARD_NUM_INTARO', + 'title' => GetMessage('UF_CARD_NUMBER_INTARO_TITLE'), + ], + ], + 'string' + ); + + $this->addCustomUserFields( + [ + [ + 'name' => 'UF_LP_ID_INTARO', + 'title' => GetMessage('UF_LP_ID_INTARO_TITLE'), + ], + ], + 'string', + ['EDIT_IN_LIST' => 'N'] + ); + + $this->addCustomUserFields( + [ + [ + 'name' => 'UF_REG_IN_PL_INTARO', + 'title' => GetMessage('UF_REG_IN_PL_INTARO_TITLE'), + ], + [ + 'name' => 'UF_AGREE_PL_INTARO', + 'title' => GetMessage('UF_AGREE_PL_INTARO_TITLE'), + ], + [ + 'name' => 'UF_PD_PROC_PL_INTARO', + 'title' => GetMessage('UF_PD_PROC_PL_INTARO_TITLE'), + ], + [ + 'name' => 'UF_EXT_REG_PL_INTARO', + 'title' => GetMessage('UF_EXT_REG_PL_INTARO_TITLE'), + ], + [ + 'name' => 'UF_SUBSCRIBE_USER_EMAIL', + 'title' => GetMessage('UF_SUBSCRIBE_USER_EMAIL_TITLE') + ] + ] + ); + } + + /** + * @param $fields + * @param string $filedType + * @param array $customProps + */ + public function addCustomUserFields($fields, string $filedType = 'boolean', array $customProps = []): void + { + foreach ($fields as $filed) { + $arProps = [ + 'ENTITY_ID' => 'USER', + 'FIELD_NAME' => $filed['name'], + 'USER_TYPE_ID' => $filedType, + 'MULTIPLE' => 'N', + 'MANDATORY' => 'N', + 'EDIT_FORM_LABEL' => ['ru' => $filed['title']], + + ]; + $props = array_merge($arProps, $customProps); + $obUserField = new CUserTypeEntity(); + $dbRes = CUserTypeEntity::GetList([], ['FIELD_NAME' => $filed['name']])->fetch(); + + if (!$dbRes['ID']) { + $obUserField->Add($props); + } + } + } + + /** + * Добавление соглашений для формы регистрации + * + * @throws \Bitrix\Main\ArgumentException + * @throws \Bitrix\Main\ObjectPropertyException + * @throws \Bitrix\Main\SystemException + */ + public function addAgreement(): void + { + $isAgreementLoyaltyProgram = AgreementRepository::getFirstByWhere( + ['ID'], + [ + ['CODE', '=', Constants::AGREEMENT_LOYALTY_PROGRAM_CODE] + ] + ); + + if (!isset($isAgreementLoyaltyProgram['ID'])) { + $agreementLoyaltyProgram = new Agreement(); + $agreementLoyaltyProgram->setCode(Constants::AGREEMENT_LOYALTY_PROGRAM_CODE); + $agreementLoyaltyProgram->setDateInsert(new DateTime()); + $agreementLoyaltyProgram->setActive('Y'); + $agreementLoyaltyProgram->setName(GetMessage('AGREEMENT_LOYALTY_PROGRAM_TITLE')); + $agreementLoyaltyProgram->setType('C'); + $agreementLoyaltyProgram->setAgreementText(GetMessage('AGREEMENT_LOYALTY_PROGRAM_TEXT')); + $agreementLoyaltyProgram->save(); + } + + $isAgreementPersonalProgram = AgreementRepository::getFirstByWhere( + ['ID'], + [ + ['CODE', '=', Constants::AGREEMENT_PERSONAL_DATA_CODE] + ] + ); + + if (!isset($isAgreementPersonalProgram['ID'])) { + $agreementPersonalData = new Agreement(); + $agreementPersonalData->setCode(Constants::AGREEMENT_PERSONAL_DATA_CODE); + $agreementPersonalData->setDateInsert(new DateTime()); + $agreementPersonalData->setActive('Y'); + $agreementPersonalData->setName(GetMessage('AGREEMENT_PERSONAL_DATA_TITLE')); + $agreementPersonalData->setType('C'); + $agreementPersonalData->setAgreementText(GetMessage('AGREEMENT_PERSONAL_DATA_TEXT')); + $agreementPersonalData->save(); + } + } + + /** + * delete loyalty program events handlers + */ + private function deleteLPEvents(): void + { + $eventManager = EventManager::getInstance(); + + foreach (Constants::LP_EVENTS as $event){ + $eventManager->unRegisterEventHandler( + $event['FROM_MODULE'], + $event['EVENT_NAME'], + $this->MODULE_ID, + EventsHandlers::class, + $event['EVENT_NAME'].'Handler' + ); + } + } + + private function copy($directory, $name): void + { + $templatePath = $_SERVER['DOCUMENT_ROOT'] + . '/local/templates/.default/components/bitrix/' . $name . '/'. $directory; + + if (!file_exists($templatePath)) { + $pathFrom = $_SERVER['DOCUMENT_ROOT'] + . '/bitrix/modules/intaro.retailcrm/install/export/local/components/intaro/' + . $name + . '/templates/.default'; + + CopyDirFiles( + $pathFrom, + $templatePath, + true, + true, + false + ); + } + } +} \ No newline at end of file diff --git a/intaro.retailcrm/updater.php b/intaro.retailcrm/updater.php index c2783c69..3f46fea7 100644 --- a/intaro.retailcrm/updater.php +++ b/intaro.retailcrm/updater.php @@ -1074,6 +1074,92 @@ class LoyaltyProgramUpdater } } +class UpdateSubscribe +{ + public function CopyFiles(): self + { + $pathFrom = $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/intaro.retailcrm/install'; + + CopyDirFiles( + $pathFrom . '/export', + $_SERVER['DOCUMENT_ROOT'], + true, + true, + false + ); + + $templateNames = [ + 'default_subscribe' => [ + 'sale.personal.section' + ] + ]; + + foreach ($templateNames as $directory => $names) { + foreach ($names as $name) { + $lpTemplatePath = $_SERVER['DOCUMENT_ROOT'] + . '/local/templates/.default/components/bitrix/' . $name . '/' . $directory; + + if (!file_exists($lpTemplatePath)) { + $pathFrom = $_SERVER['DOCUMENT_ROOT'] + . '/bitrix/modules/intaro.retailcrm/install/export/local/components/intaro/' + . $name + . '/templates/.default'; + + CopyDirFiles( + $pathFrom, + $lpTemplatePath, + true, + true, + false + ); + } + } + } + + return $this; + } + + public function addEvent(): self + { + $eventManager = EventManager::getInstance(); + + $eventManager->unRegisterEventHandler( + 'main', + 'OnAfterUserRegister', + 'intaro.retailcrm', + 'Intaro\RetailCrm\Component\Handlers\EventsHandlers', + 'OnAfterUserRegisterHandler' + ); + + RegisterModuleDependences('main', 'OnAfterUserRegister', 'intaro.retailcrm', 'RetailCrmEvent', 'OnAfterUserRegister'); + + return $this; + } + + public function addCustomUserField(): self + { + $arProps = [ + 'ENTITY_ID' => 'USER', + 'FIELD_NAME' => 'UF_SUBSCRIBE_USER_EMAIL', + 'USER_TYPE_ID' => 'boolean', + 'MULTIPLE' => 'N', + 'MANDATORY' => 'N', + 'EDIT_FORM_LABEL' => ['ru' => 'Подписка на события'], + + ]; + + $props = array_merge($arProps, []); + $obUserField = new CUserTypeEntity(); + $dbRes = CUserTypeEntity::GetList([], ['FIELD_NAME' => 'UF_SUBSCRIBE_USER_EMAIL'])->fetch(); + + if (!$dbRes['ID']) { + $obUserField->Add($props); + } + + return $this; + } +} + /** * @throws \Bitrix\Main\ArgumentException * @throws \Bitrix\Main\ObjectPropertyException @@ -1093,6 +1179,11 @@ function update() UnRegisterModuleDependences("main", "OnBeforeProlog", 'intaro.retailcrm', "RetailCrmPricePrchase", "add"); UnRegisterModuleDependences("main", "OnBeforeProlog", 'intaro.retailcrm', "RetailCrmDc", "add"); UnRegisterModuleDependences("main", "OnBeforeProlog", 'intaro.retailcrm', "RetailCrmCc", "add"); + + (new UpdateSubscribe()) + ->CopyFiles() + ->addEvent() + ->addCustomUserField(); } try {