Ejemplo n.º 1
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;
 }
Ejemplo n.º 2
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;
 }