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/cvetdv.ru/bitrix/modules/sproduction.datasync/lib/ |
Upload File : |
<?php /** * Additional synchronization * * @mail support@s-production.online * @link s-production.online */ namespace SProduction\Datasync; use Bitrix\Main, Bitrix\Main\DB\Exception, Bitrix\Main\Config\Option; class IblockProfilesAddSync { const MODULE_ID = 'sproduction.datasync'; public static function set($profile_id) { $result = true; $sync_schedule = SettingsIblocksProfiles::getParam($profile_id, 'sync', 'add_sync_schedule'); self::remove($profile_id); // Create agent $agent_period = false; if ($sync_schedule == '1h') { $agent_period = 3600; } elseif ($sync_schedule == '1d') { $agent_period = 86400; } if ($agent_period) { \CAgent::AddAgent('\SProduction\Datasync\IblockProfilesAddSync::run(' . $profile_id . ');', self::MODULE_ID, 'N', $agent_period); } return $result; } public static function remove($profile_id) { $result = true; // Remove agent \CAgent::RemoveAgent('\SProduction\Datasync\IblockProfilesAddSync::run(' . $profile_id . ');', self::MODULE_ID); return $result; } // Run sync public static function run($profile_id) { if (CheckState::isSyncActive() && SettingsIblocksProfiles::isActive($profile_id) && Rest::checkConnection()) { $sync_period = 0; $sync_schedule = SettingsIblocksProfiles::getParam($profile_id, 'sync', 'add_sync_schedule'); if ($sync_schedule == '1h') { $sync_period = 3600 * 2; } elseif ($sync_schedule != '') { $sync_period = 3600 * 24 * 2; } FileLog::put('(IblockProfilesAddSync::run) start'); self::runSectSync($profile_id, $sync_period); self::runSectDel($profile_id); self::runProdSync($profile_id, $sync_period); self::runPriceSync($profile_id, $sync_period); self::runProdDel($profile_id); FileLog::put('(IblockProfilesAddSync::run) finish'); } return '\SProduction\Datasync\IblockProfilesAddSync::run(' . $profile_id . ');'; } public static function check($profile_id) { $result = false; $db = \CAgent::GetList(['NAME' => 'ASC'], [ 'MODULE_ID' => self::MODULE_ID, 'NAME' => '\SProduction\Datasync\IblockProfilesAddSync::run(' . $profile_id . ');' ]); if ($db->Fetch()) { $result = true; } return $result; } /** * Run sections synchronization */ public static function runSectSync($profile_id, $sync_period) { $last_id = 0; do { $id_list = []; $list = StoreSections::getList($profile_id, $last_id, ['ID'], 50); foreach ($list as $item) { $id_list[] = $item['ID']; } if (!empty($id_list)) { $last_id = $id_list[count($id_list)-1]; } // Send section to CRM FileLog::put('(IblockProfilesAddSync::runSectSync) ids ' . print_r($id_list, true)); try { SyncPortal::syncSections($id_list); } catch (\Exception $e) { FileLog::put('(IblockProfilesAddSync::runSectSync) error ' . $e->getMessage()); // TODO: Save error to report } } while (!empty($id_list)); } /** * Run sections clear */ public static function runSectDel($profile_id) { $crm_iblock_id = SettingsIblocksProfiles::getCrmIblockId($profile_id); $last_id = 0; do { $id_list = []; $list = PortalSections::getList($crm_iblock_id, $last_id, [PortalSections::SEARCH_FIELD_DEFAULT]); foreach ($list as $item) { $search_value = $item[PortalSections::SEARCH_FIELD_DEFAULT]; if ($search_value && !StoreSections::find($profile_id, $search_value)) { $id_list[] = $item['id']; } } if (!empty($id_list)) { $last_id = $id_list[count($id_list)-1]; } // Delete sections from CRM FileLog::put('(IblockProfilesAddSync::runSectDel) ids ' . print_r($id_list, true)); foreach ($id_list as $id) { try { PortalSections::delete($id); } catch (\Exception $e) { FileLog::put('(IblockProfilesAddSync::runSectDel) error ' . $e->getMessage()); // TODO: Save error to report } } } while (!empty($id_list)); } /** * Run products synchronization */ public static function runProdSync($profile_id, $sync_period) { $last_id = 0; do { $id_list = []; $list = StoreProducts::getList($profile_id, $last_id, ['ID'], 50); foreach ($list as $item) { $id_list[] = $item['ID']; } if (!empty($id_list)) { $last_id = $id_list[count($id_list)-1]; } // Send products to CRM FileLog::put('(IblockProfilesAddSync::runProdSync) ids ' . print_r($id_list, true)); try { SyncPortal::syncProducts($id_list); } catch (\Exception $e) { FileLog::put('(IblockProfilesAddSync::runProdSync) error ' . $e->getMessage()); // TODO: Save error to report } } while (!empty($id_list)); } /** * Run prices synchronization */ public static function runPriceSync($profile_id, $sync_period) { $last_id = 0; do { $id_list = []; $list = StoreProducts::getList($profile_id, $last_id, ['ID'], 50); foreach ($list as $item) { $id_list[] = $item['ID']; } if (!empty($id_list)) { $last_id = $id_list[count($id_list)-1]; } // Send prices to CRM FileLog::put('(IblockProfilesAddSync::runPriceSync) ids ' . print_r($id_list, true)); try { SyncPortal::syncPrices($id_list); } catch (\Exception $e) { FileLog::put('(IblockProfilesAddSync::runPriceSync) error ' . $e->getMessage()); // TODO: Save error to report } } while (!empty($id_list)); } /** * Run products clear */ public static function runProdDel($profile_id) { $crm_iblock_id = SettingsIblocksProfiles::getCrmIblockId($profile_id); $crm_search_field = SettingsIblocksProfiles::getCrmSearchField($profile_id); $store_search_field = SettingsIblocksProfiles::getStoreSearchField($profile_id); $last_id = 0; do { $id_list = []; $list = PortalProducts::getList($crm_iblock_id, $last_id, [$crm_search_field], 20); foreach ($list as $item) { $search_value = $item[$crm_search_field]; if ($search_value && !StoreProducts::find($profile_id, $search_value, $store_search_field)) { $id_list[] = $item['id']; } } if (!empty($id_list)) { $last_id = $id_list[count($id_list)-1]; } // Delete products from CRM FileLog::put('(IblockProfilesAddSync::runProdDel) ids ' . print_r($id_list, true)); foreach ($id_list as $id) { try { PortalProducts::delete($id); } catch (\Exception $e) { FileLog::put('(IblockProfilesAddSync::runProdDel) error ' . $e->getMessage()); // TODO: Save error to report } } } while (!empty($id_list)); } }