Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/sproduction.datasync/lib/ |
Upload File : |
<?php /** * Synchronization from order to deal * * @mail support@s-production.online * @link s-production.online */ namespace SProduction\Datasync; use Bitrix\Main, Bitrix\Main\Type, Bitrix\Main\Entity, Bitrix\Main\Localization\Loc, Bitrix\Main\SiteTable, Bitrix\Sale; Loc::loadMessages(__FILE__); class SyncPortal { const PORTAL_MAX_SIZE = 52428800; // 50 Mb public static function getPortalMaxSize() { return self::PORTAL_MAX_SIZE; } /** * Sync (create or update) portal products by store products */ public static function syncProducts(array $store_prod_ids, $only_add=false) { FileLog::put('(SyncPortal::syncProducts) store_prod_ids ' . print_r($store_prod_ids, true)); // // Lock the order for other processes // OrderProcessLock::add($order_data['ID']); // Sort ids sort($store_prod_ids); // // Check for changes in store // $store_prod_ids = self::getChanged($store_prod_ids); // Find portal products $prods_store_crm_ids = PortalProducts::getIDsBySearchValues($store_prod_ids); FileLog::put('(SyncPortal::syncProducts) prods_store_crm_ids '. print_r($prods_store_crm_ids, true)); // Send store products $req_list = []; foreach ($store_prod_ids as $i => $store_prod_id) { $req_item = []; // Store product $store_product = StoreProducts::getById($store_prod_id); if ($store_product) { $store_iblock_id = $store_product['IBLOCK_ID']; if (SettingsIblocksProfiles::isActive($store_iblock_id)) { // Update product if ($prods_store_crm_ids[$store_prod_id] ?? false) { if ( ! $only_add) { $crm_prod_id = $prods_store_crm_ids[$store_prod_id]; $values = PortalProducts::getValuesByProfile($store_prod_id, $crm_prod_id); $crm_iblock_id = SettingsIblocksProfiles::getCrmIblockId($store_iblock_id); // Fix for pictures update if ($values['detailPicture']) { $req_list[] = PortalProducts::updateReq($crm_prod_id, $crm_iblock_id, [ 'detailPicture' => ['remove' => 'Y'], ]); } // Find section of product in CRM $values['iblockSectionId'] = PortalSections::findByStore($store_product['IBLOCK_SECTION_ID']); // Update product request $req_item = PortalProducts::updateReq($crm_prod_id, $crm_iblock_id, $values); } } // Add product else { $values = PortalProducts::getValuesByProfile($store_prod_id); $crm_iblock_id = SettingsIblocksProfiles::getCrmIblockId($store_iblock_id); $crm_search_field = SettingsIblocksProfiles::getCrmSearchField($store_iblock_id); $store_search_field = SettingsIblocksProfiles::getStoreSearchField($store_iblock_id); if ($crm_search_field && $store_search_field) { $search_value = StoreProducts::getSearchValue($store_prod_id); if ($search_value) { // Find section of product in CRM $section_id = PortalSections::findByStore($store_product['IBLOCK_SECTION_ID']); // Add product request $req_item = PortalProducts::addReq($crm_iblock_id, $values, $search_value, $crm_search_field, $section_id); } else { FileLog::put('(SyncPortal::syncProducts) store search value not found'); } } } if (!empty($req_item)) { // Send part of data $size_fine = self::checkSendDataSize(array_merge($req_list, $req_item)); if ($i >= count($store_prod_ids) - 1 || ! $size_fine) { if ($size_fine) { $req_list[] = $req_item; } FileLog::put('(SyncPortal::syncProducts) req_list ' . print_r($req_list, true)); $resp = Rest::batch($req_list); if ( ! empty($resp[Rest::BATCH_RESP_ERRORS])) { FileLog::put('(SyncPortal::syncProducts) errors ' . print_r($resp[Rest::BATCH_RESP_ERRORS], true)); } if ( ! $size_fine) { if (self::checkSendDataSize($req_item)) { FileLog::put('(SyncPortal::syncProducts) req_list ' . print_r([$req_item], true)); $resp = Rest::batch([$req_item]); if ( ! empty($resp[Rest::BATCH_RESP_ERRORS])) { FileLog::put('(SyncPortal::syncProducts) errors ' . print_r($resp[Rest::BATCH_RESP_ERRORS], true)); } } else { FileLog::put('(SyncPortal::syncProducts) error size of product ' . $store_prod_id); } } $req_list = []; } else { $req_list[] = $req_item; } } } } } // // Process the event handlers // foreach (GetModuleEvents(Controller::MODULE_ID, "OnAfterDealProcessed", true) as $event) { // ExecuteModuleEventEx($event, [$deal_id]); // } // // Unlock order // OrderProcessLock::delete($order_data['ID']); } /** * Sync (create or update) portal products by store products */ public static function syncPrices(array $store_prod_ids) { // Find portal products $prods_store_crm_ids = PortalProducts::getIDsBySearchValues($store_prod_ids); foreach ($prods_store_crm_ids as $store_prod_id => $crm_prod_id) { $prices = PortalPrices::getCrmPricesFromStore($store_prod_id); FileLog::put('(SyncPortal::syncPrices) product ' . $crm_prod_id . ' prices ' . print_r($prices, true)); PortalPrices::modify($crm_prod_id, $prices); } } /** * TODO Check products for the changes */ public static function getChanged(array $store_prod_ids) { return $store_prod_ids; } /** * Delete portal products by store products * * @param array $store_prod_ids * * @return void */ public static function deleteProducts(array $store_prod_ids) { FileLog::put('(SyncPortal::deleteProducts) store_prod_ids ' . print_r($store_prod_ids, true)); // // Lock the product for other processes // OrderProcessLock::add($order_data['ID']); // Sort ids sort($store_prod_ids); // Check profiles active for store products $store_prod_ids_checked = []; foreach ($store_prod_ids as $store_prod_id) { $store_product = StoreProducts::getById($store_prod_id); if ($store_product) { if (SettingsIblocksProfiles::isActive($store_product['IBLOCK_ID'])) { $store_prod_ids_checked[] = $store_prod_id; } } } // Find portal products $prods_crm_by_store_ids = PortalProducts::getIDsBySearchValues($store_prod_ids_checked); FileLog::put('(SyncPortal::deleteProducts) deleting products ' . print_r($prods_crm_by_store_ids, true)); // Delete products foreach ($prods_crm_by_store_ids as $crm_prod_id) { PortalProducts::delete($crm_prod_id); } // // Process the event handlers // foreach (GetModuleEvents(Controller::MODULE_ID, "OnAfterDealProcessed", true) as $event) { // ExecuteModuleEventEx($event, [$deal_id]); // } // // Unlock product // OrderProcessLock::delete($order_data['ID']); } /** * Sync (create or update) portal sections by store sections * * @param array $store_sect_ids * * @return void */ public static function syncSections(array $store_sect_ids, $only_add=false) { FileLog::put('(SyncPortal::syncSections) store_sect_ids ' . print_r($store_sect_ids, true)); // Sort ids sort($store_sect_ids); // Send store sections foreach ($store_sect_ids as $store_sect_id) { // Store section $store_section = StoreSections::getById($store_sect_id); if ($store_section) { if (SettingsIblocksProfiles::isActive($store_section['IBLOCK_ID'])) { // Find portal sections $crm_sect_id = PortalSections::findByStore($store_sect_id); // Update section if ($crm_sect_id) { if ( ! $only_add) { $values = PortalSections::getValuesByProfile($store_sect_id, $crm_sect_id); $crm_iblock_id = SettingsIblocksProfiles::getCrmIblockId($store_section['IBLOCK_ID']); // Find parent section in CRM $crm_parent_sect_id = PortalSections::findByStore($store_section['IBLOCK_SECTION_ID']); // Update section request PortalSections::update($crm_sect_id, $crm_iblock_id, $values, $crm_parent_sect_id); } } // Add section else { $values = PortalSections::getValuesByProfile($store_sect_id); $crm_iblock_id = SettingsIblocksProfiles::getCrmIblockId($store_section['IBLOCK_ID']); // Find parent section in CRM $crm_parent_sect_id = PortalSections::findByStore($store_section['IBLOCK_SECTION_ID']); // Add section request PortalSections::add($crm_iblock_id, $values, false, false, $crm_parent_sect_id); } } } } // // Process the event handlers // foreach (GetModuleEvents(Controller::MODULE_ID, "OnAfterDealProcessed", true) as $event) { // ExecuteModuleEventEx($event, [$deal_id]); // } } /** * Delete portal sections by store sections * * @param array $store_sect_ids * * @return void */ public static function deleteSections(array $store_sect_ids) { FileLog::put('(SyncPortal::deleteSections) store_sect_ids ' . print_r($store_sect_ids, true)); // Sort ids sort($store_sect_ids); // Delete products foreach ($store_sect_ids as $store_sect_id) { $store_section = StoreSections::getById($store_sect_id); if (SettingsIblocksProfiles::isActive($store_section['IBLOCK_ID'])) { $crm_sect_id = PortalSections::findByStore($store_sect_id); PortalSections::delete($crm_sect_id); } } } /** * Check send data size */ public static function checkSendDataSize($data) { return (self::getDataSize($data) < self::getPortalMaxSize()); } /** * Get data size */ public static function getDataSize($data) { $serialized = serialize((array)$data); if (function_exists('mb_strlen')) { $size = mb_strlen($serialized, '8bit'); } else { $size = strlen($serialized); } return $size; } }