45 Collection::normalizeArrayValuesByInt($userGroups);
46 $cacheKey = md5(
'G' . implode(
'|', $userGroups));
48 if(!isset($this->discountIds[$cacheKey]))
50 $this->discountIds[$cacheKey] = array();
52 $groupDiscountIterator = DiscountGroupTable::getList(array(
53 'select' => array(
'DISCOUNT_ID'),
55 '@GROUP_ID' => $userGroups,
58 'order' => array(
'DISCOUNT_ID' =>
'ASC')
60 while ($groupDiscount = $groupDiscountIterator->fetch())
62 $groupDiscount[
'DISCOUNT_ID'] = (int)$groupDiscount[
'DISCOUNT_ID'];
63 if ($groupDiscount[
'DISCOUNT_ID'] > 0)
65 $this->discountIds[$cacheKey][$groupDiscount[
'DISCOUNT_ID']] = $groupDiscount[
'DISCOUNT_ID'];
71 return $this->discountIds[$cacheKey];
76 if(empty($discountIds))
81 Collection::normalizeArrayValuesByInt($discountIds);
83 $discountIds = array_combine($discountIds, $discountIds);
84 $needToLoad = array_diff($discountIds, array_keys($this->discountModules));
88 $this->discountModules[$discountId] = $modules;
91 return array_intersect_key($this->discountModules, $discountIds);
96 if(empty($discountIds))
100 Collection::normalizeArrayValuesByInt($discountIds);
101 if(empty($discountIds))
106 $cacheKey =
'D' . implode(
'_', $discountIds);
107 if (!isset($this->discountEntities[$cacheKey]))
109 $needToLoad = $discountIds;
110 $resultEntities = array();
112 foreach ($this->discountEntities as $data)
114 list($loadedDiscountIds, $entities) = $data;
116 $needToLoadPortion = array_diff($needToLoad, $loadedDiscountIds);
118 if (count($needToLoad) > count($needToLoadPortion))
120 $resultEntities = array_merge($resultEntities, $entities);
123 $needToLoad = $needToLoadPortion;
124 if (empty($needToLoad))
130 if (!empty($needToLoad))
133 $this->discountEntities[$cacheKey] = array($discountIds, $resultEntities);
135 return $this->discountEntities[$cacheKey][1];
138 public function getDiscounts(array $discountIds, array $executeModuleFilter, $siteId, array $couponList = array())
140 $cacheKey =
'D'.implode(
'_', $discountIds).
'-S'.$siteId;
141 if(!empty($couponList))
143 $cacheKey .=
'-C'. implode(
'_', array_keys($couponList));
146 $cacheKey .=
'-MF'.implode(
'_', $executeModuleFilter);
147 $cacheKey = md5($cacheKey);
149 if(!isset($this->discounts[$cacheKey]))
151 $currentList = array();
153 \CTimeZone::Disable();
155 $discountSelect = array(
156 'ID',
'PRIORITY',
'SORT',
'LAST_DISCOUNT',
'LAST_LEVEL_DISCOUNT',
'UNPACK',
'APPLICATION',
'USE_COUPONS',
'EXECUTE_MODULE',
157 'NAME',
'CONDITIONS_LIST',
'ACTIONS_LIST',
'ACTIVE_FROM',
'ACTIVE_TO',
'PREDICTIONS_LIST',
'PREDICTIONS_APP',
158 'PREDICTION_TEXT',
'PRESET_ID',
'CURRENCY',
'LID',
'SHORT_DESCRIPTION',
'SHORT_DESCRIPTION_STRUCTURE',
160 $discountFilter = array(
161 '@ID' => $discountIds,
163 '@EXECUTE_MODULE' => $executeModuleFilter,
166 '=ACTIVE_FROM' =>
null,
167 '<=ACTIVE_FROM' => $currentDatetime
171 '=ACTIVE_TO' =>
null,
172 '>=ACTIVE_TO' => $currentDatetime
176 $couponsDiscount = array();
177 if (!empty($couponList))
179 $iterator = DiscountCouponTable::getList(array(
180 'select' => array(
'DISCOUNT_ID',
'COUPON'),
181 'filter' => array(
'@DISCOUNT_ID' => $discountIds,
'@COUPON' => array_keys($couponList)),
182 'order' => array(
'DISCOUNT_ID' =>
'ASC')
184 while ($row = $iterator->fetch())
186 $id = (int)$row[
'DISCOUNT_ID'];
187 if (isset($couponsDiscount[$id]))
189 $couponsDiscount[$id] = $row[
'COUPON'];
191 unset($id, $row, $iterator);
194 if (empty($couponsDiscount))
196 $discountFilter[
'=USE_COUPONS'] =
'N';
200 $discountFilter[] = array(
202 '=USE_COUPONS' =>
'N',
204 '=USE_COUPONS' =>
'Y',
205 '@ID' => array_keys($couponsDiscount)
211 $discountIterator = DiscountTable::getList(array(
212 'select' => $discountSelect,
213 'filter' => $discountFilter,
214 'order' => array(
'PRIORITY' =>
'DESC',
'SORT' =>
'ASC',
'ID' =>
'ASC')
217 while($discount = $discountIterator->fetch())
219 $discount[
'ID'] = (int)$discount[
'ID'];
220 if ($discount[
'USE_COUPONS'] ==
'Y')
221 $discount[
'COUPON'] = $couponList[$couponsDiscount[$discount[
'ID']]];
222 $discount[
'CONDITIONS'] = $discount[
'CONDITIONS_LIST'];
223 $discount[
'ACTIONS'] = $discount[
'ACTIONS_LIST'];
224 $discount[
'PREDICTIONS'] = $discount[
'PREDICTIONS_LIST'];
225 $discount[
'MODULE_ID'] =
'sale';
226 $discount[
'MODULES'] = array();
227 unset($discount[
'ACTIONS_LIST'], $discount[
'CONDITIONS_LIST'], $discount[
'PREDICTIONS_LIST']);
228 $currentList[$discount[
'ID']] = $discount;
230 unset($discount, $discountIterator);
232 \CTimeZone::Enable();
234 if (!empty($currentList))
236 $discountModules = static::getDiscountModules(array_keys($currentList));
237 foreach ($discountModules as $id => $modules)
238 $currentList[$id][
'MODULES'] = $modules;
239 unset($id, $modules, $discountModules);
242 $this->discounts[$cacheKey] = $currentList;
245 return $this->discounts[$cacheKey];