public static function convertPsBusVal()
    {
        if (\Bitrix\Main\Config\Option::get('main', '~sale_paysystem_converted') == 'Y') {
            return '';
        }
        \Bitrix\Main\Config\Option::set('main', '~sale_paysystem_converted', 'Y');
        if (!\Bitrix\Main\Loader::includeModule('sale')) {
            return '';
        }
        global $DB;
        if ($DB->TableExists('b_sale_pay_system_map') || $DB->TableExists('B_SALE_PAY_SYSTEM_MAP')) {
            return '';
        }
        $dbRes = \Bitrix\Sale\Internals\PaySystemActionTable::getList();
        $oldActionFiles = self::getOldToNewHandlersMap();
        $paySystems = array();
        while ($paySystem = $dbRes->fetch()) {
            $codesAliases = array();
            $params = unserialize($paySystem['PARAMS']);
            if (is_array($params)) {
                if (isset($oldActionFiles[$paySystem['ACTION_FILE']])) {
                    $codesAliases = self::getCodesAliases($oldActionFiles[$paySystem['ACTION_FILE']]);
                }
                foreach ($params as $key => $value) {
                    if (isset($oldActionFiles[$paySystem['ACTION_FILE']])) {
                        if ($key == 'IS_TEST' || $key == 'CHANGE_STATUS_PAY' || $key == 'TEST' || $key == 'DEMO' || $key == 'AUTOPAY') {
                            $keyValue = $value['VALUE'] != 'Y' && $value['VALUE'] != 'N' ? 'N' : $value['VALUE'];
                            $value = array('TYPE' => 'INPUT', 'VALUE' => $keyValue);
                        }
                        if ($key == 'TEST_MODE') {
                            $value = array('TYPE' => 'INPUT', 'VALUE' => $value['VALUE'] == 'TEST' ? 'Y' : 'N');
                        }
                    }
                    if ($value['TYPE'] == 'SELECT' || $value['TYPE'] == 'FILE') {
                        $value['TYPE'] = 'INPUT';
                    }
                    if (isset($codesAliases[$key])) {
                        $params[$codesAliases[$key]] = $value;
                        unset($params[$key]);
                    } else {
                        $params[$key] = $value;
                    }
                }
            }
            if (isset($oldActionFiles[$paySystem['ACTION_FILE']]) && !IsModuleInstalled('intranet')) {
                if (isset($params['PAYMENT_ID'])) {
                    $value = $params['PAYMENT_ID']['VALUE'] == 'ACCOUNT_NUMBER' ? 'ACCOUNT_NUMBER' : 'ID';
                    $params['PAYMENT_ID'] = array('TYPE' => 'PAYMENT', 'VALUE' => $value);
                }
                if (isset($params['PAYMENT_CURRENCY'])) {
                    $params['PAYMENT_CURRENCY'] = array('TYPE' => 'PAYMENT', 'VALUE' => 'CURRENCY');
                }
                if (isset($params['PAYMENT_DATE_INSERT'])) {
                    if ($params['PAYMENT_DATE_INSERT']['VALUE'] == 'DATE_INSERT_DATE' || $params['PAYMENT_DATE_INSERT']['VALUE'] == 'DATE_BILL_DATE') {
                        $date = 'DATE_BILL_DATE';
                    } else {
                        $date = 'DATE_BILL';
                    }
                    $params['PAYMENT_DATE_INSERT'] = array('TYPE' => 'PAYMENT', 'VALUE' => $date);
                }
                if (isset($params['PAYMENT_SHOULD_PAY'])) {
                    $params['PAYMENT_SHOULD_PAY'] = array('TYPE' => 'PAYMENT', 'VALUE' => 'SUM');
                }
                if (isset($params['PAYMENT_VALUE'])) {
                    $paySystem['PS_MODE'] = $params['PAYMENT_VALUE']['VALUE'];
                }
            }
            if (isset($oldActionFiles[$paySystem['ACTION_FILE']])) {
                $paySystem['ACTION_FILE'] = $oldActionFiles[$paySystem['ACTION_FILE']];
            }
            $paySystem['PARAMS'] = $params;
            if (!isset($paySystems[$paySystem['PAY_SYSTEM_ID']])) {
                $paySystems[$paySystem['PAY_SYSTEM_ID']] = array();
            }
            if (!isset($paySystems[$paySystem['PAY_SYSTEM_ID']][$paySystem['ACTION_FILE']])) {
                $paySystems[$paySystem['PAY_SYSTEM_ID']][$paySystem['ACTION_FILE']] = array();
            }
            $paySystems[$paySystem['PAY_SYSTEM_ID']][$paySystem['ACTION_FILE']][] = $paySystem;
        }
        $codes = array();
        foreach ($paySystems as $items) {
            foreach ($items as $psItem) {
                foreach ($psItem as $item) {
                    $params = $item['PARAMS'];
                    if ($params) {
                        foreach ($params as $code => $value) {
                            if ($value['VALUE'] == '') {
                                continue;
                            }
                            if ($value['TYPE'] == '') {
                                $key = 'VALUE|' . $value['VALUE'];
                            } else {
                                $key = $value['TYPE'] . '|' . $value['VALUE'];
                            }
                            if (!isset($codes[$code][$key])) {
                                $codes[$code][$key] = 0;
                            }
                            $codes[$code][$key]++;
                        }
                    }
                }
            }
        }
        $generalBusVal = array();
        foreach ($codes as $code => $values) {
            $generalBusVal[$code] = null;
            foreach ($values as $i => $cnt) {
                if ($generalBusVal[$code] === null || $values[$generalBusVal[$code]] < $cnt) {
                    $generalBusVal[$code] = $i;
                }
            }
        }
        //set general
        foreach ($generalBusVal as $code => $param) {
            list($type, $value) = explode('|', $param);
            \Bitrix\Sale\BusinessValue::setMapping($code, null, null, array('PROVIDER_KEY' => $type, 'PROVIDER_VALUE' => $value));
        }
        $mustDeleted = array();
        $duplicateRecords = array();
        foreach ($paySystems as $actions) {
            foreach ($actions as $items) {
                $firstItem = current($items);
                if (!array_key_exists($firstItem['ID'], $duplicateRecords)) {
                    $duplicateRecords[$firstItem['ID']] = array();
                }
                if (!array_key_exists('PERSON_TYPE_ID', $duplicateRecords[$firstItem['ID']])) {
                    $duplicateRecords[$firstItem['ID']]['PERSON_TYPE_ID'] = array();
                }
                if ($firstItem['PERSON_TYPE_ID'] > 0) {
                    $duplicateRecords[$firstItem['ID']]['PERSON_TYPE_ID'][] = $firstItem['PERSON_TYPE_ID'];
                }
                $duplicateRecords[$firstItem['ID']]['EXTERNAL_ID'] = $firstItem['PAY_SYSTEM_ID'];
                foreach ($items as $ps) {
                    if (in_array($ps['ACTION_FILE'], array('yandex', 'roboxchange')) && $firstItem['PS_MODE'] && $firstItem['PS_MODE'] != $ps['PS_MODE']) {
                        if (!array_key_exists($ps['ID'], $duplicateRecords)) {
                            $duplicateRecords[$ps['ID']] = array();
                        }
                        if (!array_key_exists('PERSON_TYPE_ID', $duplicateRecords[$firstItem['ID']])) {
                            $duplicateRecords[$ps['ID']]['PERSON_TYPE_ID'] = array();
                        }
                        if ($ps['PERSON_TYPE_ID'] > 0) {
                            $duplicateRecords[$ps['ID']]['PERSON_TYPE_ID'][] = $ps['PERSON_TYPE_ID'];
                        }
                        $duplicateRecords[$ps['ID']]['EXTERNAL_ID'] = $ps['PAY_SYSTEM_ID'];
                        $duplicateRecords[$ps['ID']]['NEW_PS'] = 'Y';
                    } else {
                        if ($ps['ID'] == $firstItem['ID']) {
                            continue;
                        }
                        if ($ps['PERSON_TYPE_ID'] > 0) {
                            $duplicateRecords[$firstItem['ID']]['PERSON_TYPE_ID'][] = $ps['PERSON_TYPE_ID'];
                        }
                        if (!isset($mustDeleted[$firstItem['ID']])) {
                            $mustDeleted[$firstItem['ID']] = array();
                        }
                        $mustDeleted[$firstItem['ID']][] = $ps['ID'];
                    }
                }
                foreach ($items as $item) {
                    $itemParams = array();
                    if ($item['PARAMS']) {
                        $itemParams = $item['PARAMS'];
                        if ($itemParams) {
                            foreach ($itemParams as $code => $param) {
                                $type = $param['TYPE'] ?: 'VALUE';
                                $pT = null;
                                $pS = null;
                                if (in_array($item['ACTION_FILE'], array('yandex', 'roboxchange')) && $firstItem['PS_MODE'] && $firstItem['PS_MODE'] != $item['PS_MODE']) {
                                    $consumer = 'PAYSYSTEM_' . $item['ID'];
                                } else {
                                    $consumer = 'PAYSYSTEM_' . $firstItem['ID'];
                                }
                                $cases = array(1 => array('PS' => null, 'PT' => null), 2 => array('PS' => $consumer, 'PT' => $item['PERSON_TYPE_ID']));
                                foreach ($cases as $case) {
                                    if (\Bitrix\Sale\BusinessValue::isSetMapping($code, $case['PS'], $case['PT'])) {
                                        $map = \Bitrix\Sale\BusinessValue::getMapping($code);
                                        if ($map && $map['PROVIDER_KEY'] == $type && $map['PROVIDER_VALUE'] == $param['VALUE']) {
                                            continue 2;
                                        }
                                    } else {
                                        $pT = $case['PT'];
                                        $pS = $case['PS'];
                                        break;
                                    }
                                }
                                $value = is_array($param['VALUE']) ? key($param['VALUE']) : $param['VALUE'];
                                \Bitrix\Sale\BusinessValue::setMapping($code, $pS, $pT, array('PROVIDER_KEY' => $type, 'PROVIDER_VALUE' => $value), true);
                            }
                        }
                    }
                    if (!isset($mustDeleted[$firstItem['ID']][$item['ID']])) {
                        $itemParams['BX_PAY_SYSTEM_ID'] = array('TYPE' => '', 'VALUE' => $item['ID']);
                        $item['PARAMS'] = serialize($itemParams);
                        $itemId = $item['ID'];
                        unset($item['ID']);
                        \Bitrix\Sale\Internals\PaySystemActionTable::update($itemId, $item);
                    }
                }
            }
        }
        global $DB;
        if ($DB->TableExists('b_sale_pay_system_map')) {
            $DB->Query('DROP TABLE b_sale_pay_system_map');
        }
        if ($DB->type == 'MYSQL') {
            $DB->Query('
				create table if not exists b_sale_pay_system_map
				(
					PS_ID_OLD int null,
					PS_ID int null,
					PT_ID int null,
					NEW_PS char(1) not null default \'N\'
				)');
        }
        if ($DB->type == 'MSSQL') {
            $DB->Query('
				CREATE TABLE B_SALE_PAY_SYSTEM_MAP
				(
					PS_ID int NULL,
					PS_ID_OLD int NULL,
					PT_ID int NULL,
					NEW_PS char(1) NOT NULL DEFAULT \'N\'
				)');
        }
        if ($DB->type == 'ORACLE') {
            $DB->Query('
				CREATE TABLE B_SALE_PAY_SYSTEM_MAP
				(
					PS_ID NUMBER(18) NULL,
					PS_ID_OLD NUMBER(18) NULL,
					PT_ID NUMBER(18) NULL,
					NEW_PS CHAR(1 CHAR) DEFAULT \'N\' NOT NULL
				)');
        }
        foreach ($duplicateRecords as $id => $data) {
            if ($data['PERSON_TYPE_ID']) {
                $params = array('filter' => array("SERVICE_ID" => $id, "SERVICE_TYPE" => \Bitrix\Sale\Services\PaySystem\Restrictions\Manager::SERVICE_TYPE_PAYMENT, "=CLASS_NAME" => '\\Bitrix\\Sale\\Services\\PaySystem\\Restrictions\\PersonType'));
                $dbRes = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList($params);
                if (!$dbRes->fetch()) {
                    $fields = array("SERVICE_ID" => $id, "SERVICE_TYPE" => \Bitrix\Sale\Services\PaySystem\Restrictions\Manager::SERVICE_TYPE_PAYMENT, "SORT" => 100, "PARAMS" => array('PERSON_TYPE_ID' => $data['PERSON_TYPE_ID']));
                    \Bitrix\Sale\Services\PaySystem\Restrictions\PersonType::save($fields);
                }
            }
        }
        foreach ($duplicateRecords as $id => $data) {
            if ($data['EXTERNAL_ID'] <= 0) {
                continue;
            }
            $newPs = $data['NEW_PS'] ?: 'N';
            foreach ($data['PERSON_TYPE_ID'] as $personTypeId) {
                $DB->Query('INSERT INTO b_sale_pay_system_map(PS_ID, PS_ID_OLD, PT_ID, NEW_PS) VALUES(' . $id . ', ' . $data['EXTERNAL_ID'] . ', ' . $personTypeId . ', \'' . $newPs . '\' )');
            }
        }
        $DB->Query('
			UPDATE b_sale_order SET
			PAY_SYSTEM_ID = (
				SELECT bspm.PS_ID
				FROM b_sale_pay_system_map bspm
				WHERE bspm.PT_ID=PERSON_TYPE_ID AND bspm.PS_ID_OLD=PAY_SYSTEM_ID
			)');
        if ($DB->type == 'MYSQL' || $DB->type == 'ORACLE') {
            $DB->Query('
				UPDATE b_sale_order_payment bsop SET
				PAY_SYSTEM_ID = (
					SELECT bspm.PS_ID
					FROM b_sale_pay_system_map bspm, b_sale_order bso
					WHERE bspm.PS_ID_OLD = bsop.PAY_SYSTEM_ID
						AND bso.ID = bsop.ORDER_ID
						AND bspm.PT_ID = bso.PERSON_TYPE_ID
				)');
        } elseif ($DB->type == 'MSSQL') {
            $DB->Query('
				UPDATE bsop SET
				PAY_SYSTEM_ID = (
					SELECT bspm.PS_ID
					FROM b_sale_pay_system_map bspm, b_sale_order bso
					WHERE bspm.PS_ID_OLD = bsop.PAY_SYSTEM_ID
						AND bso.ID = bsop.ORDER_ID
						AND bspm.PT_ID = bso.PERSON_TYPE_ID
				)
				FROM b_sale_order_payment bsop');
        }
        //		\Bitrix\Main\Config\Option::set('main', '~sale_paysystem_converted', 'Y');
        foreach ($mustDeleted as $items) {
            foreach ($items as $id) {
                PaySystemActionTable::delete($id);
            }
        }
        /** DELIVERY2PAYSYSTEM */
        if ($DB->type == 'MYSQL') {
            $DB->Query('
				UPDATE b_sale_delivery2paysystem bsd2p
				SET bsd2p.PAYSYSTEM_ID=(SELECT bspm.PS_ID FROM b_sale_pay_system_map bspm WHERE bspm.PS_ID_OLD=bsd2p.PAYSYSTEM_ID AND bspm.NEW_PS=\'N\' LIMIT 1)');
        }
        if ($DB->type == 'ORACLE') {
            $DB->Query('
				UPDATE b_sale_delivery2paysystem bsd2p
				SET bsd2p.PAYSYSTEM_ID=(SELECT bspm.PS_ID FROM b_sale_pay_system_map bspm WHERE bspm.PS_ID_OLD=bsd2p.PAYSYSTEM_ID AND bspm.NEW_PS=\'N\' AND ROWNUM=1)');
        }
        if ($DB->type == 'MSSQL') {
            $DB->Query('
				UPDATE bsd2p
				SET bsd2p.PAYSYSTEM_ID=(SELECT TOP(1) bspm.PS_ID FROM b_sale_pay_system_map bspm WHERE bspm.PS_ID_OLD=bsd2p.PAYSYSTEM_ID AND bspm.NEW_PS=\'N\')
				FROM b_sale_delivery2paysystem bsd2p');
        }
        $DB->Query('
			INSERT INTO b_sale_delivery2paysystem(DELIVERY_ID, PAYSYSTEM_ID, LINK_DIRECTION)
			SELECT d2p.DELIVERY_ID, pm1.PS_ID, d2p.LINK_DIRECTION
			FROM b_sale_delivery2paysystem d2p
				INNER JOIN b_sale_pay_system_map pm ON d2p.PAYSYSTEM_ID = pm.PS_ID AND pm.NEW_PS = \'N\'
				INNER JOIN b_sale_pay_system_map pm1 ON pm1.PS_ID_OLD = pm.PS_ID_OLD AND pm1.NEW_PS = \'Y\'');
        /** ORDER_PROPS_REL */
        if ($DB->type == 'MYSQL') {
            $DB->Query('
				UPDATE b_sale_order_props_relation bsopr
				SET bsopr.ENTITY_ID=(SELECT bspm.PS_ID FROM b_sale_pay_system_map bspm WHERE bspm.PS_ID_OLD=bsopr.ENTITY_ID AND bspm.NEW_PS=\'N\' LIMIT 1)
				WHERE bsopr.ENTITY_TYPE=\'P\'');
        }
        if ($DB->type == 'ORACLE') {
            $DB->Query('
				UPDATE b_sale_order_props_relation bsopr
				SET bsopr.ENTITY_ID=(SELECT bspm.PS_ID FROM b_sale_pay_system_map bspm WHERE bspm.PS_ID_OLD=bsopr.ENTITY_ID AND bspm.NEW_PS=\'N\' AND ROWNUM=1)
				WHERE bsopr.ENTITY_TYPE=\'P\'');
        }
        if ($DB->type == 'MSSQL') {
            $DB->Query('
				UPDATE bsopr
				SET bsopr.ENTITY_ID=(SELECT TOP(1) bspm.PS_ID FROM b_sale_pay_system_map bspm WHERE bspm.PS_ID_OLD=bsopr.ENTITY_ID AND bspm.NEW_PS=\'N\')
				FROM b_sale_order_props_relation bsopr
				WHERE bsopr.ENTITY_TYPE=\'P\'');
        }
        $DB->Query('
			INSERT INTO b_sale_order_props_relation(ENTITY_ID, ENTITY_TYPE, PROPERTY_ID)
			SELECT pm1.PS_ID, opr.ENTITY_TYPE, opr.PROPERTY_ID
			FROM b_sale_order_props_relation opr
				INNER JOIN b_sale_pay_system_map pm ON pm.PS_ID = opr.ENTITY_ID AND pm.NEW_PS = \'N\'
				INNER JOIN b_sale_pay_system_map pm1 ON pm1.PS_ID_OLD = pm.PS_ID_OLD AND pm1.NEW_PS = \'Y\'
			WHERE opr.ENTITY_TYPE = \'P\'');
        $DB->Query('
			INSERT INTO b_sale_service_rstr(SERVICE_ID, SORT, CLASS_NAME, PARAMS, SERVICE_TYPE)
			SELECT bsd2p.PAYSYSTEM_ID, 100, \'Bitrix\\\\Sale\\\\Services\\\\PaySystem\\\\Restrictions\\\\Delivery\', \'a:0:{}\', 1 FROM b_sale_delivery2paysystem bsd2p GROUP BY bsd2p.PAYSYSTEM_ID');
        $DB->Query('UPDATE b_sale_pay_system_action SET psa_name=name');
        if ($DB->type == 'MYSQL' || $DB->type == 'ORACLE') {
            $DB->Query('UPDATE b_sale_pay_system_action psa
				SET psa.name=(
					SELECT name
					FROM b_sale_pay_system ps
					WHERE ps.ID=psa.PAY_SYSTEM_ID
				)');
        } else {
            if ($DB->type == 'MSSQL') {
                $DB->Query('UPDATE psa
				SET psa.name=(
					SELECT name
					FROM b_sale_pay_system ps
					WHERE ps.ID=psa.PAY_SYSTEM_ID
				)
				FROM b_sale_pay_system_action psa');
            }
        }
        return '';
    }