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 of products * * @mail support@s-production.online * @link s-production.online */ namespace SProduction\Datasync; \Bitrix\Main\Loader::includeModule("catalog"); use Bitrix\Main, Bitrix\Main\Entity, Bitrix\Main\Type, Bitrix\Main\Localization\Loc; Loc::loadMessages(__FILE__); class PortalProducts { const FIELD_TYPE_STRING = 'string'; const FIELD_TYPE_CHAR = 'char'; const FIELD_TYPE_INTEGER = 'integer'; const FIELD_TYPE_FILE = 'file'; const FIELD_TYPE_DATETIME = 'datetime'; const FIELD_TYPE_PROPERTY = 'productproperty'; /** * Create new product */ public static function add($iblock_id, $values, $search_value, $search_field, $parent_sect_id = 0) { $req_params = self::addReq($iblock_id, $values, $search_value, $search_field, $parent_sect_id); $resp = Rest::execute($req_params['method'], $req_params['params']); if (isset($resp['element'])) { //sku $product_id = $resp['element']['id']; } return $product_id; } /** * Create new product (request) */ public static function addReq($iblock_id, $values, $search_value, $search_field, $parent_sect_id = 0) { if ($iblock_id) { $values['iblockId'] = $iblock_id; } if ($parent_sect_id) { $values['iblockSectionId'] = $parent_sect_id; } $values[$search_field] = $search_value; $req_params = [ 'method' => 'catalog.product.add', 'params' => [ 'fields' => $values, ] ]; return $req_params; } /** * Update product */ public static function update($product_id, $iblock_id, $values) { $result = false; $req_params = self::updateReq($product_id, $iblock_id, $values); $resp = Rest::execute($req_params['method'], $req_params['params']); if (isset($resp['element'])) { $result = true; } return $result; } /** * Update product (request) */ public static function updateReq($product_id, $iblock_id, $values) { if ($iblock_id) { $values['iblockId'] = $iblock_id; } $method = 'catalog.product.update'; if (isset($values['type']) && $values['type'] == 4) { $method = 'catalog.product.offer.update'; } $req_params = [ 'method' => $method, 'params' => [ 'id' => $product_id, 'fields' => $values, ] ]; return $req_params; } /** * Get product */ public static function getById($product_id) { $result = false; $resp = Rest::execute('catalog.product.get', [ 'id' => $product_id, ]); if (isset($resp['product'])) { $result = $resp['product']; } return $result; } /** * Delete product */ public static function delete($product_id) { Rest::execute('catalog.product.delete', [ 'id' => $product_id, ]); } /** * Get count */ public static function getCount($iblock_id) { $count = 0; $filter = [ 'iblockId' => $iblock_id, ]; $resp = Rest::execute('catalog.product.list', [ 'order' => ['id' => 'asc'], 'filter' => $filter, 'select' => ['id', 'iblockId'], ], false, true, false); if (isset($resp['total'])) { $count = $resp['total']; } return $count; } /** * Get list */ public static function getList($iblock_id, int $after_id = 0, array $select = [], $limit = 0) { $products = []; $filter = [ 'iblockId' => $iblock_id, ]; if ($after_id) { $filter['>id'] = $after_id; } $select = array_merge(['id', 'iblockId'], $select); $resp = Rest::execute('catalog.product.list', [ 'order' => ['id' => 'asc'], 'filter' => $filter, 'select' => $select, ]); if (isset($resp['products']) && ! empty($resp['products'])) { $products = $resp['products']; } if ($limit) { $products = array_slice($products, 0, $limit); } return $products; } /** * CRM products fields */ public static function getFields($iblock_id, $get_enum_values = false) { $fields = []; if ($iblock_id) { $req_list = []; $req_list[] = [ 'method' => 'catalog.product.getFieldsByFilter', 'params' => [ 'filter' => [ 'iblockId' => $iblock_id, 'productType' => 1, ], ] ]; $req_list[] = [ 'method' => 'catalog.product.offer.getFieldsByFilter', 'params' => [ 'filter' => [ 'iblockId' => $iblock_id, ], ] ]; $resp = Rest::batch($req_list, false, true); // Get fields all types of products foreach ($resp[Rest::BATCH_RESP_RESULT] as $ptype_fields) { $fields = array_merge($fields, $ptype_fields['product'] ?? $ptype_fields['offer']); } } // Get enum values if ($get_enum_values && ! empty($fields)) { $enum_props = []; foreach ($fields as $field_code => $field) { if ($field['type'] == self::FIELD_TYPE_PROPERTY && $field['propertyType'] == 'L') { $enum_props[] = (int) str_replace('property', '', $field_code); } } if ( ! empty($enum_props)) { $req_list = []; foreach ($enum_props as $enum_prop_id) { $req_list['property' . $enum_prop_id] = [ 'method' => 'catalog.productPropertyEnum.list', 'params' => [ 'filter' => [ 'propertyId' => $enum_prop_id, ], ] ]; } $resp = Rest::batch($req_list, false, true); foreach ($resp[Rest::BATCH_RESP_RESULT] as $prop_id => $prop) { $values = []; if (isset($prop['productPropertyEnums'])) { $values = $prop['productPropertyEnums']; } // Get full list of enum values if (count($values) == 50) { $enum_prop_id = (int) str_replace('property', '', $prop_id);; $fields[$prop_id]['values'] = Rest::getList('catalog.productPropertyEnum.list', 'productPropertyEnums', [ 'filter' => ['propertyId' => $enum_prop_id], 'order' => ['id' => 'asc'], ]); } else { $fields[$prop_id]['values'] = $values; } } } } return $fields; } /** * Get product values by compare table */ public static function getValuesByProfile($store_prod_id, $crm_prod_id = false) { $values = []; // Store product data $store_prod = StoreProducts::getById($store_prod_id); // CRM product data $product = false; if ($crm_prod_id) { $product = self::getById($crm_prod_id); } if ($store_prod) { // Get compare tables for product $profile = SettingsIblocksProfiles::get($store_prod['IBLOCK_ID']); // Get product fields info $fields_info = self::getFields($profile[SettingsIblocksProfiles::BLOCK_MAIN]['crm_iblock_id'], true); // Get product values foreach ($profile as $p_block) { if (is_array($p_block) && isset($p_block['comp_table'])) { $comp_table = $p_block['comp_table']; foreach ($comp_table as $crm_prod_f_id => $sync_params) { $store_prod_f_id = $sync_params['value']; if ($store_prod_f_id && ($sync_params['direction'] == SettingsIblocksProfilesInfo::SYNC_DIR_ALL || $sync_params['direction'] == SettingsIblocksProfilesInfo::SYNC_DIR_STOC)) { // Get order value $order_value = false; // Parent field if (strpos($store_prod_f_id, 'PARENT_') === 0) { $store_prod_f_id = str_replace('PARENT_', '', $store_prod_f_id); $store_fields = $store_prod['parent']; } else { $store_fields = $store_prod; } // Get value of store field if ($store_prod_f_id) { // Properties if (is_numeric($store_prod_f_id)) { $prop_id = (int) $store_prod_f_id; if ($store_fields['properties'][$prop_id]) { if ($store_fields['properties'][$prop_id]['USER_TYPE'] == 'directory') { $value_code = $store_fields['properties'][$prop_id]['VALUE']; $table_name = $store_fields['properties'][$prop_id]['USER_TYPE_SETTINGS']['TABLE_NAME']; if (is_array($value_code)) { $order_value = []; foreach ($value_code as $value_code_item) { $order_value[] = StoreProducts::getHLValue($table_name, $value_code_item); } } else { $order_value = StoreProducts::getHLValue($table_name, $value_code); } } else { $order_value = $store_fields['properties'][$prop_id]['VALUE']; } if ($store_fields['properties'][$prop_id]['MULTIPLE'] == 'Y' && !is_array($order_value) && !$order_value) { $order_value = []; } } } // Other fields else { $order_value = $store_fields['catalog'][$store_prod_f_id] ?? $store_fields[$store_prod_f_id] ?? ''; } } // Adapt value for crm product fields // Temporary value $crm_value = []; $value = $order_value; // Detail/preview picture if (isset($fields_info[$crm_prod_f_id]) && $fields_info[$crm_prod_f_id]['type'] == 'file') { $value = ! is_array($value) ? [$value] : $value; foreach ($value as $path) { if ($path) { $path = $_SERVER['DOCUMENT_ROOT'] . $path; $name = pathinfo($path, PATHINFO_BASENAME); $data = file_get_contents($path); $crm_value_item = []; // $crm_value_item['valueId'] = 0; $crm_value_item['fileData'] = [ $name, base64_encode($data) ]; // $crm_value_item['fileId'] = $path; $crm_value = $crm_value_item; } } } // Properties elseif (isset($fields_info[$crm_prod_f_id]) && $fields_info[$crm_prod_f_id]['type'] == self::FIELD_TYPE_PROPERTY) { // Basic modifications if (! is_array($value)) { if ( ! $fields_info[$crm_prod_f_id]['isMultiple']) { $crm_value = [ 'value' => $value, ]; if (isset($product[$crm_prod_f_id]['valueId'])) { $crm_value['valueId'] = $product[$crm_prod_f_id]['valueId']; } } else { $crm_value_item = [ 'value' => $value, ]; if (isset($product[$crm_prod_f_id]['valueId'])) { $crm_value_item['valueId'] = $product[$crm_prod_f_id]['valueId']; } $crm_value[] = $crm_value_item; } } elseif (! empty($value)) { if ( ! $fields_info[$crm_prod_f_id]['isMultiple']) { $crm_value = [ 'value' => $value[0], ]; if (isset($product[$crm_prod_f_id]['valueId'])) { $crm_value['valueId'] = $product[$crm_prod_f_id]['valueId']; } } else { foreach ($value as $i => $value_item) { $crm_value_item = [ 'value' => $value[$i], ]; if (isset($product[$crm_prod_f_id][$i]['valueId'])) { $crm_value_item['valueId'] = $product[$crm_prod_f_id][$i]['valueId']; } $crm_value[] = $crm_value_item; } } } // // Remove other values // if ($fields_info[$crm_prod_f_id]['isMultiple'] && is_array($product[$crm_prod_f_id])) { // foreach ($product[$crm_prod_f_id] as $i => $prod_value) { // if ($i >= count($crm_value)) { // $crm_value[] = [ // 'valueId' => $prod_value['valueId'] ?? 0, // 'value' => ['remove' => 'Y'], // ]; // } // } // } // Files properties if ($fields_info[$crm_prod_f_id]['propertyType'] == 'F') { if (isset($crm_value['value'])) { $path = \CFile::GetPath($crm_value['value']); if ($path) { $crm_value['value'] = Utilities::getImageFieldForCrm($_SERVER['DOCUMENT_ROOT'] . $path); } } else { // Add/update values foreach ($crm_value as $i => $value_item) { $f_id = $value_item['value']; if (is_numeric($f_id)) { $path = \CFile::GetPath($f_id); } else { $path = $f_id; } if ($path) { $crm_value_item = Utilities::getImageFieldForCrm($_SERVER['DOCUMENT_ROOT'] . $path); $crm_value[$i]['value'] = $crm_value_item; } } // Remove other values if (isset($product[$crm_prod_f_id]) && is_array($product[$crm_prod_f_id])) { foreach ($product[$crm_prod_f_id] as $i => $prod_value) { if ($i >= count($crm_value)) { $crm_value[] = [ 'valueId' => $prod_value['valueId'] ?? 0, 'value' => ['remove' => 'Y'], ]; } } } } } // List elseif ($fields_info[$crm_prod_f_id]['propertyType'] == 'L') { if (isset($crm_value['value'])) { $crm_value['value'] = self::getListValueIDByValue($fields_info[$crm_prod_f_id], $crm_value['value']); } else { foreach ($crm_value as $i => $value_item) { $crm_value[$i]['value'] = self::getListValueIDByValue($fields_info[$crm_prod_f_id], $value_item['value']); } } } // Get parent product id elseif ($crm_prod_f_id == 'parentId') { if (isset($crm_value['value']) && $crm_value['value'] != '') { $parent_store_prod_id = $crm_value['value']; $prods_store_crm_ids = PortalProducts::getIDsBySearchValues([$parent_store_prod_id]); $parent_crm_prod_id = $prods_store_crm_ids[$parent_store_prod_id] ?? false; if ($parent_crm_prod_id) { $crm_value['value'] = $parent_crm_prod_id; } } } } // Other types else { $crm_value = $value; } // Returned value $values[$crm_prod_f_id] = Utilities::convEncForDeal($crm_value); } } } } // // Product type // if ($values['parentId'] ?? false) { // $values['productType'] = 4; // } // Correcting fields if (isset($values['purchasingPrice']) && $values['purchasingPrice'] && $values['purchasingCurrency'] == '') { $values['purchasingCurrency'] = $store_prod['catalog']['PURCHASING_CURRENCY']; } if (isset($values['purchasingPrice']) && ! $values['purchasingPrice']) { unset($values['purchasingPrice']); } if (isset($values['vatId'])) { if ($values['vatId']) { $values['vatId'] = SettingsIblocksMain::getPortalVat($values['vatId']); } else { unset($values['vatId']); } } // Not send empty currency if (isset($values['purchasingCurrency']) && ! $values['purchasingCurrency']) { unset($values['purchasingCurrency']); } // Check quantityReserved value if (isset($values['quantityReserved']) && $values['quantityReserved'] < 0) { $values['quantityReserved'] = 0; } // Modify measure if (isset($values['measure']) && $values['measure']) { $values['measure'] = SettingsIblocksMain::getPortalMeasure($values['measure']) ?: $values['measure']; } // Set possibility to buy by zero quantity $values['canBuyZero'] = 'Y'; } return $values; } /** * Get list ID by store value */ public static function getListValueIDByValue($field_info, $value) { $result = false; if (isset($field_info['values'])) { foreach ($field_info['values'] as $f_info_value) { if ($f_info_value['value'] == $value) { $result = $f_info_value['id']; } } } return $result; } /** * CRM products IDs by store search values */ public static function getIDsBySearchValues($store_prod_ids) { $crm_ids_by_store_ids = []; // Request $req_list = []; foreach ($store_prod_ids as $store_prod_id) { // Get search value $search_value = StoreProducts::getSearchValue($store_prod_id); if ($search_value) { $store_product = StoreProducts::getById($store_prod_id); $store_iblock_id = $store_product['IBLOCK_ID']; $crm_iblock_id = SettingsIblocksProfiles::getCrmIblockId($store_iblock_id); $crm_search_field = SettingsIblocksProfiles::getCrmSearchField($store_iblock_id); $req_list[$store_prod_id] = [ 'method' => 'catalog.product.list', 'params' => [ 'select' => [ 'id', 'iblockId', $crm_search_field ], 'filter' => [ 'iblockId' => $crm_iblock_id, $crm_search_field => $search_value, ], ] ]; } } // Get data if ( ! empty($req_list)) { $resp = Rest::batch($req_list); if (isset($resp[Rest::BATCH_RESP_RESULT])) { foreach ($resp[Rest::BATCH_RESP_RESULT] as $store_prod_id => $resp_item) { $products = $resp_item['products'] ?? []; if ( ! empty($products) && isset($products[0]['id'])) { $crm_ids_by_store_ids[$store_prod_id] = $products[0]['id']; } } } } return $crm_ids_by_store_ids; } }