32 protected function getBasketItemRefreshTimestamp(
BasketItemBase $basketItem)
35 $refreshDate = $basketItem->
getField(self::REFRESH_FIELD);
38 return $refreshDate->getTimestamp();
46 $changedValues = $basketItem->getFields()->getChangedValues();
49 unset($changedValues[self::REFRESH_FIELD]);
51 return !empty($changedValues);
56 if (!isset($this->refreshGap))
58 $this->refreshGap = (int)Option::get(
'sale',
'basket_refresh_gap', 0);
64 protected function getBasketIndexList(
BasketBase $basket)
66 $basketIndexList = array();
69 foreach ($basket as $basketItem)
71 $providerName = $basketItem->getProviderName();
73 if (trim((
string)$providerName) ===
'')
75 $callbackFunction = $basketItem->getCallbackFunction();
76 if (!empty($callbackFunction))
78 $providerName = $callbackFunction;
86 if (!empty($providerName) && $providerName[0] ==
"\\")
88 $providerName = ltrim($providerName,
'\\');
91 $basketIndexList[$providerName][$basketItem->getProductId()][] = $basketItem;
94 return $basketIndexList;
97 protected function updateBasket(BasketBase $basket, array $productDataList)
99 $result =
new Result();
100 $changedBasketItems = array();
102 $basketIndexList = $this->getBasketIndexList($basket);
104 foreach ($productDataList as $providerName => $productValueList)
106 foreach ($productValueList as $productId => $productData)
108 if (empty($basketIndexList[$providerName][$productId]))
110 $basketIndexList[$providerName][
$productId][] = $basket->createItem($productData[
'MODULE_ID'], $productData[
'PRODUCT_ID']);
114 foreach ($basketIndexList[$providerName][$productId] as $item)
116 if ($item && isset($productData[
'DELETE']) && $productData[
'DELETE'])
122 $r = $this->updateBasketItem($item, $productData);
123 if (!$r->isSuccess())
125 $result->addErrors($r->getErrors());
130 $changedBasketItems[] = $item->getBasketCode();
136 $result->addData(array(
137 'CHANGED_BASKET_ITEMS' => $changedBasketItems
143 protected function updateBasketItem(BasketItemBase $item,
$data)
145 $result =
new Result();
149 $preparedData = $this->prepareData($item,
$data);
156 if (!$item->isCustomPrice() && isset($preparedData[
'DISCOUNT_PRICE']) && isset($preparedData[
'BASE_PRICE']))
158 $preparedData[
'PRICE'] = $preparedData[
'BASE_PRICE'] - $preparedData[
'DISCOUNT_PRICE'];
163 || (isset($preparedData[
'QUANTITY']) && $preparedData[
'QUANTITY'] <= 0)
164 || (isset(
$data[
'ACTIVE']) &&
$data[
'ACTIVE'] ===
'N')
167 $preparedData[
'CAN_BUY'] =
'N';
168 unset($preparedData[
'QUANTITY']);
175 'PREPARED_VALUES' => $preparedData
179 if ($event->getResults())
182 foreach ($event->getResults() as $eventResult)
184 if ($eventResult->getType() == Main\EventResult::ERROR)
186 $errorMsg =
new ResultError(
187 Main\Localization\
Loc::getMessage(
'SALE_EVENT_ON_BASKET_ITEM_REFRESH_DATA'),
188 'SALE_EVENT_ON_BASKET_ITEM_REFRESH_DATA'
190 if ($eventResultData = $eventResult->getParameters())
192 if (isset($eventResultData) && $eventResultData instanceof ResultError)
195 $errorMsg = $eventResultData;
199 $result->addError($errorMsg);
206 $item->setFieldNoDemand(self::REFRESH_FIELD,
new DateTime());
211 $preparedData[
'CAN_BUY'] =
'N';
216 if (!$r->isSuccess())
218 $result->addErrors($r->getErrors());
224 protected function prepareData(BasketItemBase $item,
$data)
231 $preparedData = array();
235 $basketItemClassName = $registry->getBasketItemClassName();
237 if (!empty(
$data[
'PRICE_LIST']))
239 $basketItemCode = $item->getBasketCode();
241 if (!empty(
$data[
'PRICE_LIST'][$basketItemCode]))
243 $priceData =
$data[
'PRICE_LIST'][$basketItemCode];
245 if (!isset($priceData[
'QUANTITY']) && isset($priceData[
'AVAILABLE_QUANTITY']))
247 $priceData[
'QUANTITY'] = $priceData[
'AVAILABLE_QUANTITY'];
251 if (isset(
$data[
'QUANTITY']))
253 $data[
'QUANTITY'] =
$data[
'AVAILABLE_QUANTITY'] = static::getAvailableQuantityFromPool($item,
$data[
'QUANTITY']);
262 $settableFields = $basketItemClassName::getSettableFieldsMap();
263 $roundFields = array_fill_keys($basketItemClassName::getRoundFields(),
true);
265 foreach (
$data as $key => $value)
267 if (isset($settableFields[$key]))
270 $item->isMarkedFieldCustom($key)
272 $key ===
'DISCOUNT_PRICE' && $item->isMarkedFieldCustom(
'PRICE')
275 $value = $item->getField($key);
278 if (isset($roundFields[$key]))
283 $preparedData[$key] = $value;
287 return $preparedData;
290 protected static function getAvailableQuantityFromPool(BasketItemBase $item, $quantity)
292 $availableQuantity = $quantity;
294 $basket = $item->getCollection();
297 throw new Main\ObjectNotFoundException(
'Basket');
300 $order = $basket->getOrder();
301 if ($order && $order->getId() > 0)
311 if (isset($quantityList[$productId]))
316 if (isset($reserveQuantityList[$productId]))
318 $poolQuantity += $reserveQuantityList[
$productId];
321 if ($poolQuantity < 0)
323 $poolQuantity = abs($poolQuantity);
326 if (($quantity + $poolQuantity) >= $item->getQuantity())
328 $availableQuantity = $item->getQuantity();
332 return $availableQuantity;
338 return $basket->getContext();
343 $itemsToRefresh = array();
344 $currentItemsCount = 0;
346 $basketRefreshStart = time();
349 foreach ($basket as $basketItem)
351 if ($quantity > 0 && $currentItemsCount >= $quantity)
356 $basketItemLastRefresh = $this->getBasketItemRefreshTimestamp($basketItem);
357 if ($basketRefreshStart - $basketItemLastRefresh >=
$refreshGap)
359 $itemsToRefresh[] = $basketItem;
360 $currentItemsCount++;
364 return $itemsToRefresh;
375 if (!empty($itemsToRefresh))
383 $result->setData(array(
384 'PRODUCT_DATA_LIST' => array()
419 if ($result->isSuccess())
421 $productData = $result->get(
'PRODUCT_DATA_LIST');
422 if (!empty($productData))
424 $r = $this->updateBasket($basket, $productData);
427 $result->addData($r->getData());
431 $result->addErrors($r->getErrors());
436 $result->addData(array(
437 'CHANGED_BASKET_ITEMS' => array()
445 $result->setData(array(
446 'PRODUCT_DATA_LIST' => array(),
447 'CHANGED_BASKET_ITEMS' => array()
static getMessage($code, $replace=null, $language=null)