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 '';
    }
 /**
  * @return \Bitrix\Sale\Result
  * @throws Exception
  * @throws \Bitrix\Main\SystemException
  */
 public static function convertToNew($renameTable = false)
 {
     $result = new \Bitrix\Sale\Result();
     $con = \Bitrix\Main\Application::getConnection();
     if (!$con->isTableExists("b_sale_delivery_handler")) {
         return $result;
     }
     $sqlHelper = $con->getSqlHelper();
     $deliveryRes = $con->query('SELECT * FROM b_sale_delivery_handler WHERE CONVERTED != \'Y\'');
     $tablesToUpdate = array('b_sale_order', 'b_sale_order_history');
     \CSaleDeliveryHandler::Initialize();
     $handlers = \CSaleDeliveryHandler::__getRegisteredHandlers();
     while ($delivery = $deliveryRes->fetch()) {
         if (strlen($delivery["HID"]) <= 0) {
             $result->addError(new \Bitrix\Main\Entity\EntityError("Can't find delivery HID. ID: \"" . $delivery["ID"] . "\""));
             continue;
         }
         if (!isset($handlers[$delivery["HID"]])) {
             \CEventLog::Add(array("SEVERITY" => "ERROR", "AUDIT_TYPE_ID" => "SALE_CONVERTER_ERROR", "MODULE_ID" => "sale", "ITEM_ID" => "CAllSaleDeliveryHandler::convertToNew()", "DESCRIPTION" => "Can't find delivery handler for registered HID: \"" . $delivery["HID"] . "\""));
             //$result->addError( new \Bitrix\Main\Entity\EntityError("Can't find delivery handler for registered HID: \"".$delivery["HID"]."\""));
             continue;
         }
         if (strlen($delivery["PROFILES"]) > 0) {
             //get from base
             $delivery["PROFILES"] = unserialize($delivery["PROFILES"]);
         } else {
             //or default.
             $delivery["PROFILES"] = $handlers[$delivery["HID"]]["PROFILES"];
         }
         // Something strange it probably not used
         if ($delivery["PROFILES"] == false || !is_array($delivery["PROFILES"]) || empty($delivery["PROFILES"])) {
             $result->addError(new \Bitrix\Main\Entity\EntityError("Can't receive info about profiles. Delivery HID: \"" . $delivery["HID"] . "\""));
             continue;
         }
         //Set profiles activity
         foreach ($delivery["PROFILES"] as $id => $params) {
             if (!isset($delivery["PROFILES"][$id]["ACTIVE"]) || $delivery["ACTIVE"] == "N") {
                 $delivery["PROFILES"][$id]["ACTIVE"] = $delivery["ACTIVE"];
             }
         }
         unset($delivery["ID"]);
         $delivery["CONFIG"] = array();
         if (strlen($delivery["SETTINGS"]) > 0) {
             if (isset($handlers[$delivery["HID"]]["DBGETSETTINGS"]) && is_callable($handlers[$delivery["HID"]]["DBGETSETTINGS"])) {
                 $delivery["CONFIG"] = call_user_func($handlers[$delivery["HID"]]["DBGETSETTINGS"], $delivery["SETTINGS"]);
             } else {
                 $delivery["CONFIG"] = $delivery["SETTINGS"];
             }
         } elseif (isset($handlers[$delivery["HID"]]["GETCONFIG"]) && is_callable($handlers[$delivery["HID"]]["GETCONFIG"])) {
             $config = call_user_func($handlers[$delivery["HID"]]["GETCONFIG"], strlen($delivery["LID"]) > 0 ? $delivery["LID"] : false);
             foreach ($config["CONFIG"] as $key => $arConfig) {
                 if (!empty($arConfig["DEFAULT"])) {
                     $delivery["CONFIG"][$key] = $arConfig["DEFAULT"];
                 }
             }
         }
         if (empty($delivery["NAME"])) {
             if (!empty($handlers[$delivery["HID"]]["NAME"])) {
                 $delivery["NAME"] = $handlers[$delivery["HID"]]["NAME"];
             } else {
                 $delivery["NAME"] = "-";
             }
         }
         $delivery["SID"] = $handlers[$delivery["HID"]]["SID"];
         $id = \CSaleDeliveryHandler::Set($delivery["HID"], $delivery, strlen($delivery["LID"]) > 0 ? $delivery["LID"] : false);
         if (intval($id) <= 0) {
             $result->addError(new \Bitrix\Main\Entity\EntityError("Can't convert delivery handler with hid: " . $delivery["HID"] . (strlen($delivery["LID"]) > 0 ? " for site: " . $delivery["LID"] : "")));
             continue;
         }
         $con->queryExecute("UPDATE b_sale_delivery_handler SET CONVERTED='Y' WHERE HID LIKE '" . $sqlHelper->forSql($delivery["HID"]) . "'");
         $ids = array($id);
         foreach ($delivery["PROFILES"] as $profileName => $profileData) {
             $fullSid = $delivery["HID"] . ":" . $profileName;
             $profileId = \CSaleDelivery::getIdByCode($fullSid);
             $ids[] = $profileId;
             if (intval($profileId) > 0) {
                 foreach ($tablesToUpdate as $table) {
                     $con->queryExecute("UPDATE " . $table . " SET DELIVERY_ID='" . $sqlHelper->forSql($profileId) . "' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($fullSid) . "'");
                 }
                 $con->queryExecute("UPDATE b_sale_delivery2paysystem SET DELIVERY_ID='" . $sqlHelper->forSql($profileId) . "', DELIVERY_PROFILE_ID='##CONVERTED##' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["HID"]) . "' AND DELIVERY_PROFILE_ID='" . $profileName . "'");
             } else {
                 $result->addError(new \Bitrix\Main\Entity\EntityError("Cant determine id for profile code: " . $fullSid));
             }
         }
         $con->queryExecute("UPDATE b_sale_delivery2paysystem SET DELIVERY_ID='" . $sqlHelper->forSql($id) . "', DELIVERY_PROFILE_ID='##CONVERTED##' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["HID"]) . "' AND (DELIVERY_PROFILE_ID='' OR DELIVERY_PROFILE_ID IS NULL)");
         $d2pRes = \Bitrix\Sale\Internals\DeliveryPaySystemTable::getList(array('filter' => array('DELIVERY_ID' => $ids), 'select' => array("DELIVERY_ID"), 'group' => array("DELIVERY_ID")));
         while ($d2p = $d2pRes->fetch()) {
             $res = \Bitrix\Sale\Internals\ServiceRestrictionTable::add(array("SERVICE_ID" => $d2p["DELIVERY_ID"], "SERVICE_TYPE" => \Bitrix\Sale\Services\Base\RestrictionManager::SERVICE_TYPE_SHIPMENT, "CLASS_NAME" => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem', "SORT" => 100));
             if (!$res->isSuccess()) {
                 $result->addErrors($res->getErrors());
             }
         }
     }
     if ($renameTable && $result->isSuccess()) {
         $con->renameTable('b_sale_delivery_handler', 'b_sale_delivery_handler_old');
     }
     return $result;
 }
示例#3
0
     $logoFileArray = CFile::GetFileArray($f_LOGOTIP);
     $f_LOGOTIP = CFile::ShowImage($logoFileArray, 100, 100, "border=0", "", false);
 }
 $row->AddField("LOGOTIP", $f_LOGOTIP);
 $row->AddField("DESCRIPTION", $f_DESCRIPTION);
 $pTypes = '';
 $aFiles = '';
 $dbRestriction = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array('select' => array('PARAMS'), 'filter' => array('SERVICE_ID' => $f_ID, 'SERVICE_TYPE' => \Bitrix\Sale\Services\PaySystem\Restrictions\Manager::SERVICE_TYPE_PAYMENT, '=CLASS_NAME' => '\\Bitrix\\Sale\\Services\\PaySystem\\Restrictions\\PersonType')));
 if ($restriction = $dbRestriction->fetch()) {
     $ptRes = \Bitrix\Sale\PersonTypeTable::getList(array('select' => array('NAME'), 'filter' => array('ID' => $restriction['PARAMS']['PERSON_TYPE_ID'])));
     while ($personType = $ptRes->fetch()) {
         $pTypes .= "<div>" . $personType['NAME'] . "</div>";
     }
 }
 $row->AddField("PERSON_TYPES", $pTypes);
 $dbRestriction = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array('select' => array('PARAMS'), 'filter' => array('SERVICE_ID' => $f_ID, 'SERVICE_TYPE' => \Bitrix\Sale\Services\PaySystem\Restrictions\Manager::SERVICE_TYPE_PAYMENT, '=CLASS_NAME' => '\\Bitrix\\Sale\\Services\\PaySystem\\Restrictions\\Site')));
 $pSite = '';
 if ($restriction = $dbRestriction->fetch()) {
     $siteRes = \Bitrix\Main\SiteTable::getList(array('select' => array('NAME', 'LID'), 'filter' => array('LID' => $restriction['PARAMS']['SITE_ID'], 'LANGUAGE_ID' => $context->getLanguage())));
     while ($site = $siteRes->fetch()) {
         $pSite .= "<div>" . $site['NAME'] . " (" . $site['LID'] . ")</div>";
     }
 }
 $row->AddField("LID", $pSite);
 $description = \Bitrix\Sale\PaySystem\Manager::getHandlerDescription($f_ACTION_FILE);
 $row->AddField("ACTION_FILES", $description['NAME']);
 $arActions = array(array("ICON" => "edit", "TEXT" => GetMessage("SALE_EDIT"), "TITLE" => GetMessage("SALE_EDIT_DESCR"), "ACTION" => $lAdmin->ActionRedirect("sale_pay_system_edit.php?ID=" . $f_ID . "&lang=" . $context->getLanguage()), "DEFAULT" => true));
 if ($saleModulePermissions >= "W") {
     $arActions[] = array("SEPARATOR" => true);
     $arActions[] = array("ICON" => "delete", "TEXT" => GetMessage("SALE_DELETE"), "TITLE" => GetMessage("SALE_DELETE_DESCR"), "ACTION" => "if(confirm('" . GetMessage('SALE_CONFIRM_DEL_MESSAGE') . "')) " . $lAdmin->ActionDoGroup($f_ID, "delete"));
 }
示例#4
0
 public static function GetList($arOrder = array("SORT" => "ASC", "NAME" => "ASC"), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
 {
     if (array_key_exists("PSA_PERSON_TYPE_ID", $arFilter)) {
         $arFilter['PERSON_TYPE_ID'] = $arFilter['PSA_PERSON_TYPE_ID'];
         unset($arFilter["PSA_PERSON_TYPE_ID"]);
     }
     $salePaySystemFields = array('ID', 'NAME', 'ACTIVE', 'SORT', 'DESCRIPTION');
     $ignoredFields = array('LID', 'CURRENCY', 'PERSON_TYPE_ID');
     if (!$arSelectFields) {
         $select = array('ID', 'NAME', 'ACTIVE', 'SORT', 'DESCRIPTION');
     } else {
         $select = array();
         foreach ($arSelectFields as $key => $field) {
             if (in_array($field, $ignoredFields)) {
                 continue;
             }
             $select[$key] = self::getAlias($field);
         }
     }
     $filter = array();
     foreach ($arFilter as $key => $value) {
         if (in_array($key, $ignoredFields)) {
             continue;
         }
         $filter[self::getAlias($key)] = $value;
     }
     if (isset($arFilter['PERSON_TYPE_ID'])) {
         $select = array_merge($select, array('PSA_ID' => 'ID', 'PSA_NAME', 'ACTION_FILE', 'RESULT_FILE', 'NEW_WINDOW', 'PERSON_TYPE_ID', 'PARAMS', 'TARIF', 'HAVE_PAYMENT', 'HAVE_ACTION', 'HAVE_RESULT', 'HAVE_PREPAY', 'HAVE_RESULT_RECEIVE', 'ENCODING', 'LOGOTIP'));
     }
     if (in_array('PARAMS', $select) && !array_key_exists('PSA_ID', $select)) {
         $select['PSA_ID'] = 'ID';
     }
     if (in_array('PARAMS', $select) && !in_array('PERSON_TYPE_ID', $select)) {
         $select[] = 'PERSON_TYPE_ID';
     }
     $order = array();
     foreach ($arOrder as $key => $value) {
         $order[self::getAlias($key)] = $value;
     }
     $groupBy = array();
     if ($arGroupBy !== false) {
         $arGroupBy = !is_array($arGroupBy) ? array($arGroupBy) : $arGroupBy;
         foreach ($arGroupBy as $key => $value) {
             $groupBy[$key] = self::getAlias($value);
         }
     }
     $dbRes = PaySystemActionTable::getList(array('select' => $select, 'filter' => $filter, 'order' => $order, 'group' => $groupBy));
     $limit = null;
     if (is_array($arNavStartParams) && isset($arNavStartParams['nTopCount'])) {
         if ($arNavStartParams['nTopCount'] > 0) {
             $limit = $arNavStartParams['nTopCount'];
         }
     }
     $result = array();
     while ($data = $dbRes->fetch()) {
         if ($limit !== null && !$limit) {
             break;
         }
         $dbRestriction = \Bitrix\Sale\Internals\ServiceRestrictionTable::getList(array('filter' => array('SERVICE_ID' => $data['ID'], 'SERVICE_TYPE' => \Bitrix\Sale\Services\PaySystem\Restrictions\Manager::SERVICE_TYPE_PAYMENT)));
         while ($restriction = $dbRestriction->fetch()) {
             if (!CSalePaySystemAction::checkRestriction($restriction, $arFilter)) {
                 continue 2;
             }
         }
         if (isset($data['ACTION_FILE'])) {
             $oldHandler = array_search($data['ACTION_FILE'], CSalePaySystemAction::getOldToNewHandlersMap());
             if ($oldHandler !== false) {
                 $data['ACTION_FILE'] = $oldHandler;
             }
         }
         if (array_key_exists('PARAMS', $data)) {
             $params = CSalePaySystemAction::getParamsByConsumer('PAYSYSTEM_' . $data['PSA_ID'], $data['PERSON_TYPE_ID']);
             $params['BX_PAY_SYSTEM_ID'] = array('TYPE' => '', 'VALUE' => $data['PSA_ID']);
             $data['PARAMS'] = serialize($params);
         }
         foreach ($data as $key => $value) {
             if (!in_array($key, $salePaySystemFields)) {
                 $newKey = self::getAliasBack($key);
                 if ($newKey != $key) {
                     $data[$newKey] = $value;
                     unset($data[$key]);
                 }
             }
         }
         $result[] = $data;
         $limit--;
     }
     $dbRes = new \CDBResult();
     $dbRes->InitFromArray($result);
     return $dbRes;
 }
示例#5
0
    /**
     * @param bool|false $renameTable
     * @return \Bitrix\Sale\Result
     * @throws Exception
     * @throws \Bitrix\Main\ArgumentException
     * @internal
     */
    public static function convertToNew($renameTable = false)
    {
        $result = new \Bitrix\Sale\Result();
        $con = \Bitrix\Main\Application::getConnection();
        if (!$con->isTableExists("b_sale_delivery")) {
            return $result;
        }
        if (!$con->isTableExists("b_sale_delivery2location_tmp")) {
            $res = self::createD2LTable();
            if (!$res->isSuccess()) {
                $result->addErrors($res->getErrors());
                return $result;
            }
            $con->queryExecute('
				INSERT INTO
					b_sale_delivery2location_tmp(DELIVERY_ID, LOCATION_CODE, LOCATION_TYPE)
				SELECT
					DELIVERY_ID, LOCATION_CODE, LOCATION_TYPE FROM b_sale_delivery2location
			');
            $con->queryExecute('DELETE FROM b_sale_delivery2location');
        }
        $sqlHelper = $con->getSqlHelper();
        $deliveryRes = $con->query('SELECT * FROM b_sale_delivery WHERE CONVERTED != \'Y\'');
        while ($delivery = $deliveryRes->fetch()) {
            $delivery["CODE"] = $delivery["ID"];
            unset($delivery["ID"]);
            $newId = \CSaleDelivery::Add($delivery);
            if (intval($newId) <= 0) {
                $result->addError(new \Bitrix\Main\Entity\EntityError("Can't convert old delivery id: " . $delivery["CODE"]));
                continue;
            }
            $res = \Bitrix\Sale\Internals\ServiceRestrictionTable::add(array("SERVICE_ID" => $newId, "SERVICE_TYPE" => \Bitrix\Sale\Services\Base\RestrictionManager::SERVICE_TYPE_SHIPMENT, "CLASS_NAME" => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByLocation', "SORT" => 100));
            if (!$res->isSuccess()) {
                $result->addErrors($res->getErrors());
            }
            $con->queryExecute('UPDATE b_sale_delivery SET CONVERTED=\'Y\' WHERE ID=' . $sqlHelper->forSql($delivery["CODE"]));
            $con->queryExecute("UPDATE b_sale_order SET DELIVERY_ID='" . $sqlHelper->forSql($newId) . "' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["CODE"]) . "'");
            $con->queryExecute("UPDATE b_sale_order_history SET DELIVERY_ID='" . $sqlHelper->forSql($newId) . "' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["CODE"]) . "'");
            $con->queryExecute("UPDATE b_sale_delivery2paysystem SET DELIVERY_ID='" . $sqlHelper->forSql($newId) . "', DELIVERY_PROFILE_ID='##CONVERTED##' WHERE DELIVERY_ID = '" . $sqlHelper->forSql($delivery["CODE"]) . "'");
            $con->queryExecute('
				INSERT INTO
					b_sale_delivery2location(DELIVERY_ID, LOCATION_CODE, LOCATION_TYPE)
				SELECT
					' . $sqlHelper->forSql($newId) . ', LOCATION_CODE, LOCATION_TYPE FROM b_sale_delivery2location_tmp
				WHERE
					DELIVERY_ID = ' . $sqlHelper->forSql($delivery["CODE"]) . '
			');
            $con->queryExecute('DELETE FROM b_sale_delivery2location_tmp WHERE DELIVERY_ID = ' . $sqlHelper->forSql($delivery["CODE"]));
            $d2pRes = \Bitrix\Sale\Internals\DeliveryPaySystemTable::getList(array('filter' => array('DELIVERY_ID' => $newId), 'select' => array("DELIVERY_ID"), 'group' => array("DELIVERY_ID")));
            if ($d2p = $d2pRes->fetch()) {
                $res = \Bitrix\Sale\Internals\ServiceRestrictionTable::add(array("SERVICE_ID" => $d2p["DELIVERY_ID"], "SERVICE_TYPE" => \Bitrix\Sale\Services\Base\RestrictionManager::SERVICE_TYPE_SHIPMENT, "CLASS_NAME" => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem', "SORT" => 100));
                if (!$res->isSuccess()) {
                    $result->addErrors($res->getErrors());
                }
            }
        }
        if ($result->isSuccess()) {
            $con->dropTable('b_sale_delivery2location_tmp');
            if ($renameTable) {
                $con->renameTable("b_sale_delivery", "b_sale_delivery_old");
            }
        }
        return $result;
    }