Example #1
0
 /**
  * @return \Bitrix\Sale\Result|bool
  * @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 true;
     }
     $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["PROFILES"]) > 0) {
             $delivery["PROFILES"] = unserialize($delivery["PROFILES"]);
         } else {
             $delivery["PROFILES"] = $handlers[$delivery["HID"]]["PROFILES"];
             foreach ($delivery["PROFILES"] as $id => $params) {
                 $delivery["PROFILES"][$id]["ACTIVE"] = $delivery["ACTIVE"];
             }
         }
         // 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;
         }
         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 (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"];
                 }
             }
         }
         $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="' . $sqlHelper->forSql($delivery["HID"]) . '"');
         $ids = array($id);
         foreach ($delivery["PROFILES"] as $profileName => $profileData) {
             $fullSid = $delivery["HID"] . ":" . $profileName;
             $profileId = \Bitrix\Sale\Delivery\Services\Table::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='' 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='' 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\Delivery\Restrictions\Table::add(array("DELIVERY_ID" => $d2p["DELIVERY_ID"], "CLASS_NAME" => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem', "SORT" => 100));
             if (!$res->isSuccess()) {
                 $result->addErrors($res->getErrors());
             }
         }
     }
     if ($renameTable && $result->isSuccess()) {
         $con->queryExecute("ALTER TABLE b_sale_delivery_handler RENAME b_sale_delivery_handler_old");
     }
     return $result;
 }
Example #2
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")) {
            $fields = $con->getTableFields('b_sale_delivery2location');
            $con->createTable('b_sale_delivery2location_tmp', $fields, array('DELIVERY_ID', 'LOCATION_CODE', 'LOCATION_TYPE'));
            $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\Delivery\Restrictions\Table::add(array("DELIVERY_ID" => $newId, "CLASS_NAME" => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByLocation', "SORT" => 100));
            $result->addErrors($res->getErrors());
            if ($result->isSuccess()) {
                $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) . ", LINK_DIRECTION='" . DeliveryPaySystemTable::LINK_DIRECTION_DELIVERY_PAYSYSTEM . "' 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\Delivery\Restrictions\Table::add(array("DELIVERY_ID" => $d2p["DELIVERY_ID"], "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;
    }
Example #3
0
 public static function delete($restrictionId)
 {
     $dbRes = Table::getList(array('filter' => array('ID' => $restrictionId)));
     if ($fields = $dbRes->fetch()) {
         DeliveryLocationTable::resetMultipleForOwner($fields["DELIVERY_ID"]);
     }
     return parent::delete($restrictionId);
 }
Example #4
0
             }
         }
         $fields = array("DELIVERY_ID" => $deliveryId, "SORT" => $sort, "PARAMS" => $params);
         $res = $restriction->save($fields, $restrictionId);
         if (!$res->isSuccess()) {
             $arResult["ERROR"] .= implode(".", $res->getErrorMessages());
         }
         $arResult["HTML"] = getRestrictionHtml($deliveryId);
         break;
     case "delete_restriction":
         $restrictionId = isset($_REQUEST['restrictionId']) ? intval($_REQUEST['restrictionId']) : 0;
         $deliveryId = isset($_REQUEST['deliveryId']) ? intval($_REQUEST['deliveryId']) : 0;
         if (!$restrictionId) {
             throw new \Bitrix\Main\ArgumentNullException('restrictionId');
         }
         $dbRes = \Bitrix\Sale\Delivery\Restrictions\Table::getById($restrictionId);
         if ($fields = $dbRes->fetch()) {
             $restriction = Services\Manager::getRestrictionObject($fields["CLASS_NAME"]);
             $res = $restriction->delete($restrictionId, $deliveryId);
             if (!$res->isSuccess()) {
                 $arResult["ERROR"] .= implode(".", $res->getErrorMessages());
             }
         } else {
             $arResult["ERROR"] .= "Can't find restriction with id: " . $restrictionId;
         }
         $arResult["HTML"] = getRestrictionHtml($deliveryId);
         break;
     default:
         $arResult["ERROR"] = "Error! Wrong action!";
         break;
 }
Example #5
0
 public static function saveRestriction($deliveryId, $className, $params = false)
 {
     $res = Restrictions\Table::getList(array('filter' => array("=DELIVERY_ID" => $deliveryId, "=CLASS_NAME" => $className), 'select' => array("ID")));
     if ($restriction = $res->fetch()) {
         if ($params !== false) {
             $res = Restrictions\Table::update($restriction["ID"], array("DELIVERY_ID" => $deliveryId, "CLASS_NAME" => $className, "PARAMS" => $params));
         }
     } else {
         $res = Restrictions\Table::add(array("DELIVERY_ID" => $deliveryId, "CLASS_NAME" => $className, "PARAMS" => is_array($params) ? $params : array()));
     }
     return $res;
 }
Example #6
0
 public static function delete($restrictionId, $deliveryId = 0)
 {
     return \Bitrix\Sale\Delivery\Restrictions\Table::delete($restrictionId);
 }
    $APPLICATION->AuthForm(Loc::getMessage("SALE_ESDL_ACCESS_DENIED"));
}
/**
 * @var CDatabase $DB
 * @var CMain  $APPLICATION
 */
use Bitrix\Main\Localization\Loc;
use Bitrix\Sale\Delivery\Services;
use Bitrix\Sale\Internals\Input;
Loc::loadMessages(__FILE__);
$ID = intval($_GET['ID']);
$tableId = 'table_delivery_restrictions';
$oSort = new \CAdminSorting($tableId);
$lAdmin = new \CAdminList($tableId, $oSort);
$restrictionClassNames = Services\Manager::getRestrictionClassNames();
$res = \Bitrix\Sale\Delivery\Restrictions\Table::getList(array('filter' => array('DELIVERY_ID' => $ID), 'select' => array('ID', 'CLASS_NAME', 'SORT', 'PARAMS'), 'order' => array('SORT' => 'ASC', 'ID' => 'DESC')));
$data = $res->fetchAll();
$dbRes = new \CDBResult();
$dbRes->InitFromArray($data);
$dbRecords = new \CAdminResult($dbRes, $tableId);
$dbRecords->NavStart();
$lAdmin->NavText($dbRecords->GetNavPrint(Loc::getMessage('SALE_RDL_LIST')));
$header = array(array('id' => 'ID', 'content' => Loc::getMessage('SALE_RDL_COL_ID'), "sort" => "", 'default' => true), array('id' => 'SORT', 'content' => Loc::getMessage('SALE_RDL_COL_SORT'), "sort" => "", 'default' => true), array('id' => 'CLASS_NAME', 'content' => Loc::getMessage('SALE_RDL_COL_CLASS_NAME'), "sort" => "", 'default' => true), array('id' => 'PARAMS', 'content' => Loc::getMessage('SALE_RDL_COL_PARAMS'), "sort" => "", 'default' => true));
$lAdmin->AddHeaders($header);
$restrictionClassNamesUsed = array();
while ($record = $dbRecords->Fetch()) {
    if (strlen($record['CLASS_NAME']) > 0) {
        $restrictionClassNamesUsed[] = $record['CLASS_NAME'];
        if (is_callable($record['CLASS_NAME'] . '::getClassTitle')) {
            $className = $record['CLASS_NAME']::getClassTitle();
        } else {
 protected static function actualizeDeliveriesRestrictionByPS()
 {
     $con = \Bitrix\Main\Application::getConnection();
     $sqlHelper = $con->getSqlHelper();
     $restrictions = array();
     $dbR = Restrictions\Table::getList(array('filter' => array('=CLASS_NAME' => '\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem'), 'select' => array('DELIVERY_ID')));
     while ($restr = $dbR->fetch()) {
         $restrictions[] = $restr['DELIVERY_ID'];
     }
     $deliveryList = self::getEntityItemsFullList(self::ENTITY_TYPE_DELIVERY);
     $dLinkedToP = array();
     $deliveriesToPs = array();
     $linkedPS = array();
     $dbP2S = DeliveryPaySystemTable::getList();
     while ($d2p = $dbP2S->fetch()) {
         if ($d2p["LINK_DIRECTION"] == self::LINK_DIRECTION_DELIVERY_PAYSYSTEM && !in_array($d2p["DELIVERY_ID"], $dLinkedToP)) {
             $dLinkedToP[] = $d2p["DELIVERY_ID"];
         }
         if ($d2p["LINK_DIRECTION"] == self::LINK_DIRECTION_PAYSYSTEM_DELIVERY) {
             if (!isset($deliveriesToPs[$d2p["DELIVERY_ID"]])) {
                 $deliveriesToPs[$d2p["DELIVERY_ID"]] = array();
             }
             $linkedPS[] = $d2p["PAYSYSTEM_ID"];
             $deliveriesToPs[$d2p["DELIVERY_ID"]][] = $d2p["PAYSYSTEM_ID"];
         }
     }
     $notLinkedToPS = array_diff($deliveryList, $dLinkedToP);
     $existLinkedPs = !empty($linkedPS);
     $notNeedRestriction = array();
     $needRestriction = array();
     foreach ($deliveryList as $id) {
         $need = true;
         //DS not linked to PS and (All PS having links linked to current DS
         if (in_array($id, $notLinkedToPS)) {
             if (isset($deliveriesToPs[$id])) {
                 $diff = array_diff($linkedPS, $deliveriesToPs[$id]);
             } else {
                 $diff = $linkedPS;
             }
             if (!$existLinkedPs || empty($diff)) {
                 $notNeedRestriction[] = $id;
                 $need = false;
             }
         }
         // DS linked to PS or exist linked PS but not linked to current DS
         if ($need) {
             $needRestriction[] = $id;
         }
     }
     $notNeedRestriction = array_intersect($notNeedRestriction, $restrictions);
     if (!empty($notNeedRestriction)) {
         $sql = "";
         foreach ($notNeedRestriction as $deliveryId) {
             $sql .= " " . ($sql == "" ? "WHERE CLASS_NAME='" . $sqlHelper->forSql('\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem') . "' AND (" : "OR ") . "DELIVERY_ID=" . $sqlHelper->forSql($deliveryId);
         }
         $sql = "DELETE FROM " . Restrictions\Table::getTableName() . $sql . ")";
         $con->queryExecute($sql);
     }
     $needRestriction = array_diff($needRestriction, $restrictions);
     //let's... add missing
     if (!empty($needRestriction)) {
         $sql = "";
         foreach ($needRestriction as $deliveryId) {
             $sql .= ($sql == "" ? " " : ", ") . "(" . $sqlHelper->forSql($deliveryId) . ", '" . $sqlHelper->forSql('\\Bitrix\\Sale\\Delivery\\Restrictions\\ByPaySystem') . "')";
         }
         $sql = "INSERT INTO " . Restrictions\Table::getTableName() . "(DELIVERY_ID, CLASS_NAME) VALUES" . $sql;
         $con->queryExecute($sql);
     }
 }