Example #1
0
 public function GetDiscount($intProductID, $intIBlockID, $arCatalogGroups = array(), $arUserGroups = array(), $strRenewal = "N", $siteID = false, $arDiscountCoupons = false, $boolSKU = true, $boolGetIDS = false)
 {
     global $DB;
     global $APPLICATION;
     global $stackCacheManager;
     foreach (GetModuleEvents("catalog", "OnGetDiscount", true) as $arEvent) {
         $mxResult = ExecuteModuleEventEx($arEvent, array($intProductID, $intIBlockID, $arCatalogGroups, $arUserGroups, $strRenewal, $siteID, $arDiscountCoupons, $boolSKU, $boolGetIDS));
         if (true !== $mxResult) {
             return $mxResult;
         }
     }
     $boolSKU = true === $boolSKU ? true : false;
     $boolGetIDS = true === $boolGetIDS ? true : false;
     $intProductID = intval($intProductID);
     if (0 >= $intProductID) {
         $APPLICATION->ThrowException(GetMessage("BT_MOD_CATALOG_DISC_ERR_PRODUCT_ID_ABSENT"), "NO_PRODUCT_ID");
         return false;
     }
     $intIBlockID = intval($intIBlockID);
     if (0 >= $intIBlockID) {
         $APPLICATION->ThrowException(GetMessage("BT_MOD_CATALOG_DISC_ERR_IBLOCK_ID_ABSENT"), "NO_IBLOCK_ID");
         return false;
     }
     if (isset($arCatalogGroups)) {
         if (is_array($arCatalogGroups)) {
             array_walk($arCatalogGroups, create_function("&\$item", "\$item=intval(\$item);"));
             $arCatalogGroups = array_unique($arCatalogGroups);
         } else {
             if (intval($arCatalogGroups) . "|" == $arCatalogGroups . "|") {
                 $arCatalogGroups = array(intval($arCatalogGroups));
             } else {
                 $arCatalogGroups = array();
             }
         }
     } else {
         $arCatalogGroups = array();
     }
     if (!is_array($arUserGroups) && intval($arUserGroups) . "|" == $arUserGroups . "|") {
         $arUserGroups = array(intval($arUserGroups));
     }
     if (!is_array($arUserGroups)) {
         $arUserGroups = array();
     }
     if (!in_array(2, $arUserGroups)) {
         $arUserGroups[] = 2;
     }
     $strRenewal = $strRenewal == "Y" ? "Y" : "N";
     if ($siteID === false) {
         $siteID = SITE_ID;
     }
     if ($arDiscountCoupons === false) {
         $arDiscountCoupons = CCatalogDiscountCoupon::GetCoupons();
     }
     $arSKU = false;
     if ($boolSKU) {
         $arSKU = CCatalogSKU::GetProductInfo($intProductID, $intIBlockID);
         if (!is_array($arSKU)) {
             $boolSKU = false;
         }
     }
     $arResult = array();
     $arResultID = array();
     $arFilter = array('PRICE_TYPE_ID' => $arCatalogGroups, 'USER_GROUP_ID' => $arUserGroups);
     $arDiscountIDs = CCatalogDiscount::__GetDiscountID($arFilter);
     if (!empty($arDiscountIDs)) {
         $boolGenerate = false;
         $arSelect = array("ID", "TYPE", "SITE_ID", "ACTIVE", "ACTIVE_FROM", "ACTIVE_TO", "RENEWAL", "NAME", "SORT", "MAX_DISCOUNT", "VALUE_TYPE", "VALUE", "CURRENCY", "PRIORITY", "LAST_DISCOUNT", "COUPON", "COUPON_ONE_TIME", "COUPON_ACTIVE", 'UNPACK');
         $strDate = date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL")));
         $arFilter = array("ID" => $arDiscountIDs, "SITE_ID" => $siteID, "TYPE" => DISCOUNT_TYPE_STANDART, "ACTIVE" => "Y", "RENEWAL" => $strRenewal, "+<=ACTIVE_FROM" => $strDate, "+>=ACTIVE_TO" => $strDate);
         if (is_array($arDiscountCoupons)) {
             $arFilter["+COUPON"] = $arDiscountCoupons;
         }
         $rsPriceDiscounts = CCatalogDiscount::GetList(array(), $arFilter, false, false, $arSelect);
         while ($arPriceDiscount = $rsPriceDiscounts->Fetch()) {
             if ($arPriceDiscount['COUPON_ACTIVE'] != 'N') {
                 if (!$boolGenerate) {
                     $arProduct = array('ID' => $intProductID, 'IBLOCK_ID' => $intIBlockID);
                     if (!self::__GenerateFields($arProduct)) {
                         return false;
                     }
                     $boolGenerate = true;
                 }
                 if (CCatalogDiscount::__Unpack($arProduct, $arPriceDiscount['UNPACK'])) {
                     unset($arPriceDiscount['UNPACK']);
                     $arResult[] = $arPriceDiscount;
                     $arResultID[] = $arPriceDiscount['ID'];
                 }
             }
         }
     }
     if ($boolSKU) {
         $arDiscountParent = CCatalogDiscount::GetDiscount($arSKU['ID'], $arSKU['IBLOCK_ID'], $arCatalogGroups, $arUserGroups, $strRenewal, $siteID, $arDiscountCoupons, false, false);
         if (!empty($arDiscountParent)) {
             if (empty($arResult)) {
                 $arResult = $arDiscountParent;
             } else {
                 foreach ($arDiscountParent as &$arOneParentDiscount) {
                     if (in_array($arOneParentDiscount['ID'], $arResultID)) {
                         continue;
                     }
                     $arResult[] = $arOneParentDiscount;
                     $arResultID[] = $arOneParentDiscount['ID'];
                 }
                 if (isset($arOneParentDiscount)) {
                     unset($arOneParentDiscount);
                 }
             }
         }
     }
     if (!$boolGetIDS) {
         $arDiscSave = CCatalogDiscountSave::GetDiscount(array('USER_ID' => 0, 'USER_GROUPS' => $arUserGroups, 'SITE_ID' => $siteID));
         if (!empty($arDiscSave)) {
             $arResult = !empty($arResult) ? array_merge($arResult, $arDiscSave) : $arDiscSave;
         }
     } else {
         $arResult = $arResultID;
     }
     foreach (GetModuleEvents("catalog", "OnGetDiscountResult", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array(&$arResult));
     }
     return $arResult;
 }
Example #2
0
 /**
  * @param int $intProductID
  * @param int $intIBlockID
  * @param array $arCatalogGroups
  * @param array $arUserGroups
  * @param string $strRenewal
  * @param bool|string $siteID
  * @param bool|array $arDiscountCoupons
  * @param bool $boolSKU
  * @param bool $boolGetIDS
  * @return array|false
  */
 public function GetDiscount($intProductID, $intIBlockID, $arCatalogGroups = array(), $arUserGroups = array(), $strRenewal = "N", $siteID = false, $arDiscountCoupons = false, $boolSKU = true, $boolGetIDS = false)
 {
     static $eventOnGetExists = null;
     static $eventOnResultExists = null;
     global $DB, $APPLICATION;
     self::initDiscountSettings();
     if ($eventOnGetExists === true || $eventOnGetExists === null) {
         foreach (GetModuleEvents("catalog", "OnGetDiscount", true) as $arEvent) {
             $eventOnGetExists = true;
             $mxResult = ExecuteModuleEventEx($arEvent, array($intProductID, $intIBlockID, $arCatalogGroups, $arUserGroups, $strRenewal, $siteID, $arDiscountCoupons, $boolSKU, $boolGetIDS));
             if ($mxResult !== true) {
                 return $mxResult;
             }
         }
         if ($eventOnGetExists === null) {
             $eventOnGetExists = false;
         }
     }
     $boolSKU = $boolSKU === true;
     $boolGetIDS = $boolGetIDS === true;
     $intProductID = (int) $intProductID;
     if ($intProductID <= 0) {
         $APPLICATION->ThrowException(Loc::getMessage("BT_MOD_CATALOG_DISC_ERR_PRODUCT_ID_ABSENT"), "NO_PRODUCT_ID");
         return false;
     }
     $intIBlockID = (int) $intIBlockID;
     if ($intIBlockID <= 0) {
         $APPLICATION->ThrowException(Loc::getMessage("BT_MOD_CATALOG_DISC_ERR_IBLOCK_ID_ABSENT"), "NO_IBLOCK_ID");
         return false;
     }
     if (!is_array($arUserGroups)) {
         $arUserGroups = array($arUserGroups);
     }
     $arUserGroups[] = 2;
     if (!empty($arUserGroups)) {
         Main\Type\Collection::normalizeArrayValuesByInt($arUserGroups, true);
     }
     if (!is_array($arCatalogGroups)) {
         $arCatalogGroups = array($arCatalogGroups);
     }
     if (empty($arCatalogGroups)) {
         $catalogGroupIterator = CCatalogGroup::GetGroupsList(array('GROUP_ID' => $arUserGroups, 'BUY' => array('Y', 'N')));
         while ($catalogGroup = $catalogGroupIterator->Fetch()) {
             $arCatalogGroups[$catalogGroup['CATALOG_GROUP_ID']] = $catalogGroup['CATALOG_GROUP_ID'];
         }
         unset($catalogGroup, $catalogGroupIterator);
     }
     if (!empty($arCatalogGroups)) {
         Main\Type\Collection::normalizeArrayValuesByInt($arCatalogGroups, true);
     }
     if (empty($arCatalogGroups)) {
         return false;
     }
     $strRenewal = (string) $strRenewal == 'Y' ? 'Y' : 'N';
     if ($siteID === false) {
         $siteID = SITE_ID;
     }
     $arSKUExt = false;
     if ($boolSKU) {
         $arSKUExt = CCatalogSKU::GetInfoByOfferIBlock($intIBlockID);
         $boolSKU = !empty($arSKUExt);
     }
     $arResult = array();
     $arResultID = array();
     if (self::$useSaleDiscount) {
     } else {
         $strCacheKey = md5('C' . implode('_', $arCatalogGroups) . '-' . 'U' . implode('_', $arUserGroups));
         if (!isset(self::$arCacheDiscountFilter[$strCacheKey])) {
             $arFilter = array('PRICE_TYPE_ID' => $arCatalogGroups, 'USER_GROUP_ID' => $arUserGroups);
             $arDiscountIDs = CCatalogDiscount::__GetDiscountID($arFilter);
             if (!empty($arDiscountIDs)) {
                 sort($arDiscountIDs);
             }
             self::$arCacheDiscountFilter[$strCacheKey] = $arDiscountIDs;
         } else {
             $arDiscountIDs = self::$arCacheDiscountFilter[$strCacheKey];
         }
         $arProduct = array();
         if (!empty($arDiscountIDs)) {
             if ($arDiscountCoupons === false) {
                 if (self::$existCouponsManager && Loader::includeModule('sale')) {
                     $arDiscountCoupons = DiscountCouponsManager::getForApply(array('MODULE' => 'catalog', 'DISCOUNT_ID' => $arDiscountIDs), array('MODULE' => 'catalog', 'PRODUCT_ID' => $intProductID, 'BASKET_ID' => '0'), true);
                     if (!empty($arDiscountCoupons)) {
                         $arDiscountCoupons = array_keys($arDiscountCoupons);
                     }
                 } else {
                     if (!isset($_SESSION['CATALOG_USER_COUPONS']) || !is_array($_SESSION['CATALOG_USER_COUPONS'])) {
                         $_SESSION['CATALOG_USER_COUPONS'] = array();
                     }
                     $arDiscountCoupons = $_SESSION["CATALOG_USER_COUPONS"];
                 }
             }
             if ($arDiscountCoupons === false) {
                 $arDiscountCoupons = array();
             }
             $boolGenerate = false;
             if (empty(self::$cacheDiscountHandlers)) {
                 self::$cacheDiscountHandlers = CCatalogDiscount::getDiscountHandlers($arDiscountIDs);
             } else {
                 $needDiscountHandlers = array();
                 foreach ($arDiscountIDs as &$discountID) {
                     if (!isset(self::$cacheDiscountHandlers[$discountID])) {
                         $needDiscountHandlers[] = $discountID;
                     }
                 }
                 unset($discountID);
                 if (!empty($needDiscountHandlers)) {
                     $discountHandlersList = CCatalogDiscount::getDiscountHandlers($needDiscountHandlers);
                     if (!empty($discountHandlersList)) {
                         foreach ($discountHandlersList as $discountID => $discountHandlers) {
                             self::$cacheDiscountHandlers[$discountID] = $discountHandlers;
                         }
                         unset($discountHandlers, $discountID);
                     }
                     unset($discountHandlersList);
                 }
                 unset($needDiscountHandlers);
             }
             $strCacheKey = 'D' . implode('_', $arDiscountIDs) . '-' . 'S' . $siteID . '-R' . $strRenewal;
             if (!empty($arDiscountCoupons)) {
                 $strCacheKey .= '-C' . implode('|', $arDiscountCoupons);
             }
             $strCacheKey = md5($strCacheKey);
             if (!isset(self::$arCacheDiscountResult[$strCacheKey])) {
                 $arDiscountList = array();
                 $arSelect = array('ID', 'TYPE', 'SITE_ID', 'ACTIVE', 'ACTIVE_FROM', 'ACTIVE_TO', 'RENEWAL', 'NAME', 'SORT', 'MAX_DISCOUNT', 'VALUE_TYPE', 'VALUE', 'CURRENCY', 'PRIORITY', 'LAST_DISCOUNT', 'COUPON', 'COUPON_ONE_TIME', 'COUPON_ACTIVE', 'UNPACK', 'CONDITIONS');
                 $strDate = date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL")));
                 $discountRows = array_chunk($arDiscountIDs, 500);
                 foreach ($discountRows as &$row) {
                     $arFilter = array('@ID' => $row, 'SITE_ID' => $siteID, 'TYPE' => self::ENTITY_ID, 'RENEWAL' => $strRenewal, '+<=ACTIVE_FROM' => $strDate, '+>=ACTIVE_TO' => $strDate);
                     if (is_array($arDiscountCoupons)) {
                         $arFilter['+COUPON'] = $arDiscountCoupons;
                     }
                     CTimeZone::Disable();
                     $rsPriceDiscounts = CCatalogDiscount::GetList(array(), $arFilter, false, false, $arSelect);
                     CTimeZone::Enable();
                     while ($arPriceDiscount = $rsPriceDiscounts->Fetch()) {
                         $arPriceDiscount['HANDLERS'] = array();
                         $arPriceDiscount['MODULE_ID'] = 'catalog';
                         $arDiscountList[] = $arPriceDiscount;
                     }
                 }
                 unset($row, $discountRows);
                 self::$arCacheDiscountResult[$strCacheKey] = $arDiscountList;
             } else {
                 $arDiscountList = self::$arCacheDiscountResult[$strCacheKey];
             }
             if (!empty($arDiscountList)) {
                 $discountApply = array();
                 foreach ($arDiscountList as &$arPriceDiscount) {
                     if (!isset($discountApply[$arPriceDiscount['ID']]) && $arPriceDiscount['COUPON_ACTIVE'] != 'N') {
                         if (!$boolGenerate) {
                             if (!isset(self::$arCacheProduct[$intProductID])) {
                                 $arProduct = array('ID' => $intProductID, 'IBLOCK_ID' => $intIBlockID);
                                 if (!self::__GenerateFields($arProduct)) {
                                     return false;
                                 }
                                 if ($boolSKU) {
                                     if (!self::__GenerateParent($arProduct, $arSKUExt)) {
                                         $boolSKU = false;
                                     }
                                 }
                                 $boolGenerate = true;
                                 self::$arCacheProduct[$intProductID] = $arProduct;
                             } else {
                                 $boolGenerate = true;
                                 $arProduct = self::$arCacheProduct[$intProductID];
                             }
                         }
                         $discountApply[$arPriceDiscount['ID']] = true;
                         $applyFlag = true;
                         if (isset(self::$cacheDiscountHandlers[$arPriceDiscount['ID']])) {
                             $arPriceDiscount['HANDLERS'] = self::$cacheDiscountHandlers[$arPriceDiscount['ID']];
                             $moduleList = self::$cacheDiscountHandlers[$arPriceDiscount['ID']]['MODULES'];
                             if (!empty($moduleList)) {
                                 foreach ($moduleList as &$moduleID) {
                                     if (!isset(self::$usedModules[$moduleID])) {
                                         self::$usedModules[$moduleID] = Loader::includeModule($moduleID);
                                     }
                                     if (!self::$usedModules[$moduleID]) {
                                         $applyFlag = false;
                                         break;
                                     }
                                 }
                                 unset($moduleID);
                             }
                             unset($moduleList);
                         }
                         if ($applyFlag && CCatalogDiscount::__Unpack($arProduct, $arPriceDiscount['UNPACK'])) {
                             $arResult[] = $arPriceDiscount;
                             $arResultID[] = $arPriceDiscount['ID'];
                         }
                     }
                 }
                 if (isset($arPriceDiscount)) {
                     unset($arPriceDiscount);
                 }
                 unset($discountApply);
             }
         }
         if (!$boolGetIDS) {
             $arDiscSave = CCatalogDiscountSave::GetDiscount(array('USER_ID' => 0, 'USER_GROUPS' => $arUserGroups, 'SITE_ID' => $siteID));
             if (!empty($arDiscSave)) {
                 $arResult = !empty($arResult) ? array_merge($arResult, $arDiscSave) : $arDiscSave;
             }
         } else {
             $arResult = $arResultID;
         }
     }
     if ($eventOnResultExists === true || $eventOnResultExists === null) {
         foreach (GetModuleEvents("catalog", "OnGetDiscountResult", true) as $arEvent) {
             $eventOnResultExists = true;
             ExecuteModuleEventEx($arEvent, array(&$arResult));
         }
         if ($eventOnResultExists === null) {
             $eventOnResultExists = false;
         }
     }
     return $arResult;
 }
Example #3
0
    $arParams['USER_ID'] = intval($USER->GetID());
}
if (0 >= $arParams['USER_ID']) {
    return;
}
$arParams['SHOW_NEXT_LEVEL'] = isset($arParams['SHOW_NEXT_LEVEL']) && 'Y' == $arParams['SHOW_NEXT_LEVEL'] ? 'Y' : 'N';
if (!CModule::IncludeModule('catalog')) {
    return;
}
if (!CBXFeatures::IsFeatureEnabled('CatDiscountSave')) {
    CCatalogDiscountSave::Disable();
    ShowError(GetMessage("CAT_FEATURE_NOT_ALLOW"));
    return;
}
$arFields = array('USER_ID' => $arParams['USER_ID'], 'SITE_ID' => $arParams['SITE_ID']);
$arResult = CCatalogDiscountSave::GetDiscount($arFields);
if (!empty($arResult)) {
    foreach ($arResult as $key => $arDiscountSave) {
        if ('Y' == $arParams['SHOW_NEXT_LEVEL']) {
            $rsRanges = CCatalogDiscountSave::GetRangeByDiscount(array('RANGE_FROM' => 'ASC'), array('DISCOUNT_ID' => $arDiscountSave['ID'], '>RANGE_FROM' => $arDiscountSave['RANGE_FROM'], false, array('nTopCount' => 1)));
            if ($arRange = $rsRanges->Fetch()) {
                $arTempo = array('RANGE_FROM' => $arRange['RANGE_FROM'], 'VALUE' => $arRange['VALUE'], 'VALUE_TYPE' => $arRange['TYPE']);
                $arDiscountSave['NEXT_LEVEL'] = $arTempo;
            }
        }
        $arDiscountSave['~NAME'] = $arDiscountSave['NAME'];
        $arDiscountSave['NAME'] = htmlspecialcharsex($arDiscountSave['NAME']);
        $arResult[$key] = $arDiscountSave;
    }
}
$this->IncludeComponentTemplate();
Example #4
0
 public static function GetDiscount($intProductID, $intIBlockID, $arCatalogGroups = array(), $arUserGroups = array(), $strRenewal = "N", $siteID = false, $arDiscountCoupons = false, $boolSKU = true, $boolGetIDS = false)
 {
     global $DB;
     global $APPLICATION;
     foreach (GetModuleEvents("catalog", "OnGetDiscount", true) as $arEvent) {
         $mxResult = ExecuteModuleEventEx($arEvent, array($intProductID, $intIBlockID, $arCatalogGroups, $arUserGroups, $strRenewal, $siteID, $arDiscountCoupons, $boolSKU, $boolGetIDS));
         if ($mxResult !== true) {
             return $mxResult;
         }
     }
     $boolSKU = $boolSKU === true;
     $boolGetIDS = $boolGetIDS === true;
     $intProductID = (int) $intProductID;
     if ($intProductID <= 0) {
         $APPLICATION->ThrowException(GetMessage("BT_MOD_CATALOG_DISC_ERR_PRODUCT_ID_ABSENT"), "NO_PRODUCT_ID");
         return false;
     }
     $intIBlockID = (int) $intIBlockID;
     if ($intIBlockID <= 0) {
         $APPLICATION->ThrowException(GetMessage("BT_MOD_CATALOG_DISC_ERR_IBLOCK_ID_ABSENT"), "NO_IBLOCK_ID");
         return false;
     }
     if (!is_array($arCatalogGroups)) {
         $arCatalogGroups = array($arCatalogGroups);
     }
     if (!empty($arCatalogGroups)) {
         CatalogClearArray($arCatalogGroups);
     }
     if (!is_array($arUserGroups)) {
         $arUserGroups = array($arUserGroups);
     }
     $arUserGroups[] = 2;
     if (!empty($arUserGroups)) {
         CatalogClearArray($arUserGroups);
     }
     $strRenewal = $strRenewal == "Y" ? "Y" : "N";
     if ($siteID === false) {
         $siteID = SITE_ID;
     }
     if ($arDiscountCoupons === false) {
         $arDiscountCoupons = CCatalogDiscountCoupon::GetCoupons();
     }
     $arSKU = false;
     $arSKUExt = false;
     if ($boolSKU) {
         $arSKUExt = CCatalogSKU::GetInfoByOfferIBlock($intIBlockID);
         $boolSKU = !empty($arSKUExt);
     }
     $arResult = array();
     $arResultID = array();
     $strCacheKey = md5('C' . implode('_', $arCatalogGroups) . '-' . 'U' . implode('_', $arUserGroups));
     if (!isset(self::$arCacheDiscountFilter[$strCacheKey])) {
         $arFilter = array('PRICE_TYPE_ID' => $arCatalogGroups, 'USER_GROUP_ID' => $arUserGroups);
         $arDiscountIDs = CCatalogDiscount::__GetDiscountID($arFilter);
         if (!empty($arDiscountIDs)) {
             sort($arDiscountIDs);
         }
         self::$arCacheDiscountFilter[$strCacheKey] = $arDiscountIDs;
     } else {
         $arDiscountIDs = self::$arCacheDiscountFilter[$strCacheKey];
     }
     $arProduct = array();
     if (!empty($arDiscountIDs)) {
         $boolGenerate = false;
         if (empty(self::$cacheDiscountHandlers)) {
             self::$cacheDiscountHandlers = CCatalogDiscount::getDiscountHandlers($arDiscountIDs);
         } else {
             $needDiscountHandlers = array();
             foreach ($arDiscountIDs as &$discountID) {
                 if (!isset(self::$cacheDiscountHandlers[$discountID])) {
                     $needDiscountHandlers[] = $discountID;
                 }
             }
             unset($discountID);
             if (!empty($needDiscountHandlers)) {
                 $discountHandlersList = CCatalogDiscount::getDiscountHandlers($needDiscountHandlers);
                 if (!empty($discountHandlersList)) {
                     foreach ($discountHandlersList as $discountID => $discountHandlers) {
                         self::$cacheDiscountHandlers[$discountID] = $discountHandlers;
                     }
                     unset($discountHandlers, $discountID);
                 }
                 unset($discountHandlersList);
             }
             unset($needDiscountHandlers);
         }
         $strCacheKey = 'D' . implode('_', $arDiscountIDs) . '-' . 'S' . $siteID . '-R' . $strRenewal;
         if (!empty($arDiscountCoupons)) {
             $strCacheKey .= '-C' . implode('|', $arDiscountCoupons);
         }
         $strCacheKey = md5($strCacheKey);
         if (!isset(self::$arCacheDiscountResult[$strCacheKey])) {
             $arSelect = array('ID', 'TYPE', 'SITE_ID', 'ACTIVE', 'ACTIVE_FROM', 'ACTIVE_TO', 'RENEWAL', 'NAME', 'SORT', 'MAX_DISCOUNT', 'VALUE_TYPE', 'VALUE', 'CURRENCY', 'PRIORITY', 'LAST_DISCOUNT', 'COUPON', 'COUPON_ONE_TIME', 'COUPON_ACTIVE', 'UNPACK');
             $strDate = date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL")));
             $arFilter = array('ID' => $arDiscountIDs, 'SITE_ID' => $siteID, 'TYPE' => self::ENTITY_ID, 'RENEWAL' => $strRenewal, '+<=ACTIVE_FROM' => $strDate, '+>=ACTIVE_TO' => $strDate);
             if (is_array($arDiscountCoupons)) {
                 $arFilter["+COUPON"] = $arDiscountCoupons;
             }
             $arDiscountList = array();
             CTimeZone::Disable();
             $rsPriceDiscounts = CCatalogDiscount::GetList(array(), $arFilter, false, false, $arSelect);
             CTimeZone::Enable();
             while ($arPriceDiscount = $rsPriceDiscounts->Fetch()) {
                 $arPriceDiscount['HANDLERS'] = array();
                 $arDiscountList[] = $arPriceDiscount;
             }
             self::$arCacheDiscountResult[$strCacheKey] = $arDiscountList;
         } else {
             $arDiscountList = self::$arCacheDiscountResult[$strCacheKey];
         }
         if (!empty($arDiscountList)) {
             $discountApply = array();
             foreach ($arDiscountList as &$arPriceDiscount) {
                 if (!isset($discountApply[$arPriceDiscount['ID']]) && $arPriceDiscount['COUPON_ACTIVE'] != 'N') {
                     if (!$boolGenerate) {
                         if (!isset(self::$arCacheProduct[$intProductID])) {
                             $arProduct = array('ID' => $intProductID, 'IBLOCK_ID' => $intIBlockID);
                             if (!self::__GenerateFields($arProduct)) {
                                 return false;
                             }
                             if ($boolSKU) {
                                 if (!self::__GenerateParent($arProduct, $arSKUExt)) {
                                     $boolSKU = false;
                                 }
                             }
                             $boolGenerate = true;
                             self::$arCacheProduct[$intProductID] = $arProduct;
                         } else {
                             $boolGenerate = true;
                             $arProduct = self::$arCacheProduct[$intProductID];
                         }
                     }
                     $discountApply[$arPriceDiscount['ID']] = true;
                     $applyFlag = true;
                     if (isset(self::$cacheDiscountHandlers[$arPriceDiscount['ID']])) {
                         $arPriceDiscount['HANDLERS'] = self::$cacheDiscountHandlers[$arPriceDiscount['ID']];
                         $moduleList = self::$cacheDiscountHandlers[$arPriceDiscount['ID']]['MODULES'];
                         if (!empty($moduleList)) {
                             foreach ($moduleList as &$moduleID) {
                                 if (!isset(self::$usedModules[$moduleID])) {
                                     self::$usedModules[$moduleID] = Loader::includeModule($moduleID);
                                 }
                                 if (!self::$usedModules[$moduleID]) {
                                     $applyFlag = false;
                                     break;
                                 }
                             }
                             unset($moduleID);
                         }
                         unset($moduleList);
                     }
                     if ($applyFlag && CCatalogDiscount::__Unpack($arProduct, $arPriceDiscount['UNPACK'])) {
                         unset($arPriceDiscount['UNPACK']);
                         $arResult[] = $arPriceDiscount;
                         $arResultID[] = $arPriceDiscount['ID'];
                     }
                 }
             }
             if (isset($arPriceDiscount)) {
                 unset($arPriceDiscount);
             }
             unset($discountApply);
         }
     }
     if (!$boolGetIDS) {
         $arDiscSave = CCatalogDiscountSave::GetDiscount(array('USER_ID' => 0, 'USER_GROUPS' => $arUserGroups, 'SITE_ID' => $siteID));
         if (!empty($arDiscSave)) {
             $arResult = !empty($arResult) ? array_merge($arResult, $arDiscSave) : $arDiscSave;
         }
     } else {
         $arResult = $arResultID;
     }
     foreach (GetModuleEvents("catalog", "OnGetDiscountResult", true) as $arEvent) {
         ExecuteModuleEventEx($arEvent, array(&$arResult));
     }
     return $arResult;
 }
Example #5
0
	static public function GetDiscount($intProductID, $intIBlockID, $arCatalogGroups = array(), $arUserGroups = array(), $strRenewal = "N", $siteID = false, $arDiscountCoupons = false, $boolSKU = true, $boolGetIDS = false)
	{
		global $DB;
		global $APPLICATION;
		global $stackCacheManager;

		foreach (GetModuleEvents("catalog", "OnGetDiscount", true) as $arEvent)
		{
			$mxResult = ExecuteModuleEventEx($arEvent, array($intProductID, $intIBlockID, $arCatalogGroups, $arUserGroups, $strRenewal, $siteID, $arDiscountCoupons, $boolSKU, $boolGetIDS));
			if (true !== $mxResult)
				return $mxResult;
		}

		$boolSKU = (true === $boolSKU ? true : false);
		$boolGetIDS = (true === $boolGetIDS ? true : false);

		$intProductID = intval($intProductID);
		if (0 >= $intProductID)
		{
			$APPLICATION->ThrowException(GetMessage("BT_MOD_CATALOG_DISC_ERR_PRODUCT_ID_ABSENT"), "NO_PRODUCT_ID");
			return false;
		}

		$intIBlockID = intval($intIBlockID);
		if (0 >= $intIBlockID)
		{
			$APPLICATION->ThrowException(GetMessage("BT_MOD_CATALOG_DISC_ERR_IBLOCK_ID_ABSENT"), "NO_IBLOCK_ID");
			return false;
		}

		if (!empty($arCatalogGroups))
		{
			if (!is_array($arCatalogGroups))
			{
				$arCatalogGroups = (intval($arCatalogGroups)."|" == $arCatalogGroups."|" ? array($arCatalogGroups) : array());
			}
		}
		else
		{
			$arCatalogGroups = array();
		}
		if (!empty($arCatalogGroups))
		{
			CatalogClearArray($arCatalogGroups);
		}

		if (!is_array($arUserGroups))
		{
			$arUserGroups = (intval($arUserGroups)."|" == $arUserGroups."|" ? array($arUserGroups) : array());
		}
		$arUserGroups[] = 2;
		if (!empty($arUserGroups))
		{
			CatalogClearArray($arUserGroups);
		}

		$strRenewal = (($strRenewal == "Y") ? "Y" : "N");

		if ($siteID === false)
			$siteID = SITE_ID;

		if ($arDiscountCoupons === false)
			$arDiscountCoupons = CCatalogDiscountCoupon::GetCoupons();

		$arSKU = false;

		$arResult = array();
		$arResultID = array();

		$strCacheKey = md5('C'.implode('_', $arCatalogGroups).'-'.'U'.implode('_', $arUserGroups));
		if (!array_key_exists($strCacheKey, self::$arCacheDiscountFilter))
		{
			$arFilter = array(
				'PRICE_TYPE_ID' => $arCatalogGroups,
				'USER_GROUP_ID' => $arUserGroups,
			);
			$arDiscountIDs = CCatalogDiscount::__GetDiscountID($arFilter);
			if (!empty($arDiscountIDs))
			{
				sort($arDiscountIDs);
			}
			self::$arCacheDiscountFilter[$strCacheKey] = $arDiscountIDs;
		}
		else
		{
			$arDiscountIDs = self::$arCacheDiscountFilter[$strCacheKey];
		}

		$arProduct = array();

		if (!empty($arDiscountIDs))
		{
			$boolGenerate = false;

			$strCacheKey = 'D'.implode('_', $arDiscountIDs).'-'.'S'.$siteID.'-R'.$strRenewal;
			if (!empty($arDiscountCoupons))
			{
				$strCacheKey .= '-C'.implode('|', $arDiscountCoupons);
			}
			$strCacheKey = md5($strCacheKey);

			if (!array_key_exists($strCacheKey, self::$arCacheDiscountResult))
			{
				$arSelect = array(
					"ID", "TYPE", "SITE_ID", "ACTIVE", "ACTIVE_FROM", "ACTIVE_TO",
					"RENEWAL", "NAME", "SORT", "MAX_DISCOUNT", "VALUE_TYPE", "VALUE", "CURRENCY",
					"PRIORITY", "LAST_DISCOUNT",
					"COUPON", "COUPON_ONE_TIME", "COUPON_ACTIVE", 'UNPACK'
				);
				$strDate = date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL")));
				$arFilter = array(
					"ID" => $arDiscountIDs,
					"SITE_ID" => $siteID,
					"TYPE" => DISCOUNT_TYPE_STANDART,
					"ACTIVE" => "Y",
					"RENEWAL" => $strRenewal,
					"+<=ACTIVE_FROM" => $strDate,
					"+>=ACTIVE_TO" => $strDate,
				);

				if (is_array($arDiscountCoupons))
				{
					$arFilter["+COUPON"] = $arDiscountCoupons;
				}

				$arDiscountList = array();

				$rsPriceDiscounts = CCatalogDiscount::GetList(
					array(),
					$arFilter,
					false,
					false,
					$arSelect
				);

				while ($arPriceDiscount = $rsPriceDiscounts->Fetch())
				{
					$arDiscountList[] = $arPriceDiscount;
				}
				self::$arCacheDiscountResult[$strCacheKey] = $arDiscountList;
			}
			else
			{
				$arDiscountList = self::$arCacheDiscountResult[$strCacheKey];
			}

			if (!empty($arDiscountList))
			{
				foreach ($arDiscountList as &$arPriceDiscount)
				{
					if ($arPriceDiscount['COUPON_ACTIVE'] != 'N')
					{
						if (!$boolGenerate)
						{
							if (!array_key_exists($intProductID, self::$arCacheProduct))
							{
								$arProduct = array('ID' => $intProductID, 'IBLOCK_ID' => $intIBlockID);
								if (!self::__GenerateFields($arProduct))
									return false;
								$boolGenerate = true;
								self::$arCacheProduct[$intProductID] = $arProduct;
							}
							else
							{
								$arProduct = self::$arCacheProduct[$intProductID];
							}
						}
						if (CCatalogDiscount::__Unpack($arProduct, $arPriceDiscount['UNPACK']))
						{
							unset($arPriceDiscount['UNPACK']);
							$arResult[] = $arPriceDiscount;
							$arResultID[] = $arPriceDiscount['ID'];
						}
					}
				}
				if (isset($arPriceDiscount))
					unset($arPriceDiscount);
			}
		}

		if ($boolSKU)
		{
			$boolSKU = false;
			$arSKU = false;
			$arSKUExt = false;
			$arSKUExt = CCatalogSKU::GetInfoByOfferIBlock($intIBlockID);
			if (!empty($arSKUExt) && is_array($arSKUExt) && 0 < intval($arSKUExt['SKU_PROPERTY_ID']) && 0 < intval($arSKUExt['PRODUCT_IBLOCK_ID']))
			{
				if (array_key_exists('PROPERTY_'.$arSKUExt['SKU_PROPERTY_ID'].'_VALUE', $arProduct))
				{
					$arVal = $arProduct['PROPERTY_'.$arSKUExt['SKU_PROPERTY_ID'].'_VALUE'];
					$arSKU = array(
						'ID' => $arVal[0],
						'IBLOCK_ID' => $arSKUExt['PRODUCT_IBLOCK_ID'],
					);
					$boolSKU = true;
				}
			}
		}
		if ($boolSKU)
		{
			$arDiscountParent = CCatalogDiscount::GetDiscount($arSKU['ID'], $arSKU['IBLOCK_ID'], $arCatalogGroups, $arUserGroups, $strRenewal, $siteID, $arDiscountCoupons, false, false);
			if (!empty($arDiscountParent))
			{
				if (empty($arResult))
				{
					$arResult = $arDiscountParent;
				}
				else
				{
					foreach ($arDiscountParent as &$arOneParentDiscount)
					{
						if (in_array($arOneParentDiscount['ID'], $arResultID))
							continue;
						$arResult[] = $arOneParentDiscount;
						$arResultID[] = $arOneParentDiscount['ID'];
					}
					if (isset($arOneParentDiscount))
						unset($arOneParentDiscount);
				}
			}
		}

		if (!$boolGetIDS)
		{
			$arDiscSave = CCatalogDiscountSave::GetDiscount(array(
				'USER_ID' => 0,
				'USER_GROUPS' => $arUserGroups,
				'SITE_ID' => $siteID
			));
			if (!empty($arDiscSave))
			{
				$arResult = (!empty($arResult) ? array_merge($arResult, $arDiscSave) : $arDiscSave);
			}
		}
		else
		{
			$arResult = $arResultID;
		}

		foreach (GetModuleEvents("catalog", "OnGetDiscountResult", true) as $arEvent)
		{
			ExecuteModuleEventEx($arEvent, array(&$arResult));
		}

		return $arResult;
	}