public function Update($ID, $arFields) { global $DB; $ID = (int) $ID; if ($ID <= 0) { return false; } $boolNewVersion = true; $arFields['ID'] = $ID; if (!array_key_exists('CONDITIONS', $arFields) && !array_key_exists('ACTIONS', $arFields)) { $boolConvert = CSaleDiscount::__ConvertOldFormat('UPDATE', $arFields); if (!$boolConvert) { return false; } $boolNewVersion = false; } if (!CSaleDiscount::CheckFields("UPDATE", $arFields)) { return false; } if ($boolNewVersion) { $boolConvert = CSaleDiscount::__SetOldFields('UPDATE', $arFields); if (!$boolConvert) { return false; } } $strUpdate = $DB->PrepareUpdate("b_sale_discount", $arFields); if (!empty($strUpdate)) { $strSql = "update b_sale_discount set " . $strUpdate . " where ID = " . $ID; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } if (isset($arFields['USER_GROUPS'])) { Internals\DiscountGroupTable::updateByDiscount($ID, $arFields['USER_GROUPS'], isset($arFields['ACTIVE']) ? $arFields['ACTIVE'] : '', true); } elseif (isset($arFields['ACTIVE'])) { Internals\DiscountGroupTable::changeActiveByDiscount($ID, $arFields['ACTIVE']); } if (isset($arFields['HANDLERS'])) { self::updateDiscountHandlers($ID, $arFields['HANDLERS'], true); } if (isset($arFields['ENTITIES'])) { Internals\DiscountEntitiesTable::updateByDiscount($ID, $arFields['ENTITIES'], true); } return $ID; }
protected function updateUserGroups($discountID, $userGroups, $active = '', $updateData) { Internals\DiscountGroupTable::updateByDiscount($discountID, $userGroups, $active, $updateData); }
/** * Default onAfterDelete handler. Absolutely necessary. * * @param Main\Entity\Event $event Event object. * @return void */ public static function onAfterDelete(Main\Entity\Event $event) { $id = $event->getParameter('id'); $id = end($id); DiscountEntitiesTable::deleteByDiscount($id); DiscountModuleTable::deleteByDiscount($id); DiscountGroupTable::deleteByDiscount($id); if (self::$deleteCoupons !== false) { DiscountCouponTable::deleteByDiscount(self::$deleteCoupons); self::$deleteCoupons = false; } unset($id); }
/** * Load from database discount id for user groups. * * @return void * @throws Main\ArgumentException */ protected function loadDiscountByUserGroups() { if (!array_key_exists('USER_ID', $this->orderData)) { return; } $userGroups = \CUser::getUserGroup($this->orderData['USER_ID']); Main\Type\Collection::normalizeArrayValuesByInt($userGroups); $cacheKey = md5('U' . implode('_', $userGroups)); if (!isset($this->discountByUserCache[$cacheKey])) { $discountCache = array(); $groupDiscountIterator = Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => $userGroups, '=ACTIVE' => 'Y'), 'order' => array('DISCOUNT_ID' => 'ASC'))); while ($groupDiscount = $groupDiscountIterator->fetch()) { $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID']; if ($groupDiscount['DISCOUNT_ID'] > 0) { $discountCache[$groupDiscount['DISCOUNT_ID']] = $groupDiscount['DISCOUNT_ID']; } } unset($groupDiscount, $groupDiscountIterator); if (!empty($discountCache)) { $this->discountByUserCache[$cacheKey] = $discountCache; } unset($discountCache); } $this->discountIds = $this->discountByUserCache[$cacheKey]; }
/** * Calculate discount by new order. * * @return Result */ protected function calculateFull() { $result = new Result(); $this->discountIds = array(); if (!$this->isMixedBasket()) { $this->fillEmptyDiscountResult(); } else { $this->discountResult['ORDER'] = array(); } $order = $this->getOrder(); $basket = $order->getBasket(); /** @var BasketItem $basketItem */ foreach ($basket as $basketItem) { $code = $basketItem->getBasketCode(); if ($this->isCustomPriceByCode($code)) { if (array_key_exists($code, $this->basketDiscountList)) { unset($this->basketDiscountList[$code]); } } else { if (!isset($this->basketDiscountList[$code])) { $this->basketDiscountList[$code] = $basketItem->getField('DISCOUNT_LIST'); if ($this->basketDiscountList[$code] === null) { unset($this->basketDiscountList[$code]); } } } } unset($code, $basketItem, $basket); $this->resetBasketPrices(); if ($this->isMixedBasket()) { DiscountCouponsManager::clearApply(false); $basketDiscountResult = $this->calculateMixedBasketDiscount(); } else { DiscountCouponsManager::clearApply(); $basketDiscountResult = $this->calculateFullBasketDiscount(); } if (!$basketDiscountResult->isSuccess()) { $result->addErrors($basketDiscountResult->getErrors()); unset($basketDiscountResult); return $result; } unset($basketDiscountResult); $this->roundBasketPrices(); $codeList = array_keys($this->orderData['BASKET_ITEMS']); switch (self::getApplyMode()) { case self::APPLY_MODE_DISABLE: foreach ($codeList as &$code) { if (isset($this->basketDiscountList[$code]) && !empty($this->basketDiscountList[$code])) { $this->orderData['BASKET_ITEMS'][$code]['LAST_DISCOUNT'] = 'Y'; } } unset($code); break; case self::APPLY_MODE_LAST: foreach ($codeList as &$code) { if (!isset($this->basketDiscountList[$code]) || empty($this->basketDiscountList[$code])) { continue; } $lastDiscount = end($this->basketDiscountList[$code]); if (!empty($lastDiscount['LAST_DISCOUNT']) && $lastDiscount['LAST_DISCOUNT'] == 'Y') { $this->orderData['BASKET_ITEMS'][$code]['LAST_DISCOUNT'] = 'Y'; } } unset($code); break; case self::APPLY_MODE_ADD: break; } unset($codeList); $userGroups = \CUser::getUserGroup($this->orderData['USER_ID']); Main\Type\Collection::normalizeArrayValuesByInt($userGroups); $cacheKey = md5('U' . implode('_', $userGroups)); if (!isset($this->discountByUserCache[$cacheKey])) { $discountCache = array(); $groupDiscountIterator = Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => $userGroups, '=ACTIVE' => 'Y'))); while ($groupDiscount = $groupDiscountIterator->fetch()) { $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID']; if ($groupDiscount['DISCOUNT_ID'] > 0) { $discountCache[$groupDiscount['DISCOUNT_ID']] = $groupDiscount['DISCOUNT_ID']; } } unset($groupDiscount, $groupDiscountIterator); if (!empty($discountCache)) { Main\Type\Collection::normalizeArrayValuesByInt($discountCache); $this->discountByUserCache[$cacheKey] = $discountCache; } unset($discountCache); } if (!empty($this->discountByUserCache[$cacheKey])) { $this->discountIds = $this->discountByUserCache[$cacheKey]; } if (empty($this->discountIds)) { $this->discountIds = null; } else { $this->getDiscountModules(); $couponList = DiscountCouponsManager::getForApply(array('MODULE' => 'sale', 'DISCOUNT_ID' => $this->discountIds), array(), true); $cacheKey = md5('D' . implode('_', $this->discountIds)); if (!empty($couponList)) { $cacheKey .= '-C' . implode('_', array_keys($couponList)); } $currentList = array(); if (!isset($this->saleDiscountCache[$cacheKey])) { $discountApply = array(); $currentDatetime = new Main\Type\DateTime(); $discountSelect = array('ID', 'PRIORITY', 'SORT', 'LAST_DISCOUNT', 'UNPACK', 'APPLICATION', 'USE_COUPONS', 'EXECUTE_MODULE', 'NAME', 'CONDITIONS_LIST', 'ACTIONS_LIST'); $discountOrder = array('PRIORITY' => 'DESC', 'SORT' => 'ASC', 'ID' => 'ASC'); $discountFilter = array('@ID' => $this->discountIds, '=LID' => $this->orderData['SITE_ID'], '@EXECUTE_MODULE' => array('sale', 'all'), array('LOGIC' => 'OR', 'ACTIVE_FROM' => '', '<=ACTIVE_FROM' => $currentDatetime), array('LOGIC' => 'OR', 'ACTIVE_TO' => '', '>=ACTIVE_TO' => $currentDatetime)); unset($currentDatetime); if (empty($couponList)) { $discountFilter['=USE_COUPONS'] = 'N'; } else { $discountFilter[] = array('LOGIC' => 'OR', '=USE_COUPONS' => 'N', array('=USE_COUPONS' => 'Y', '@COUPON.COUPON' => array_keys($couponList))); $discountSelect['DISCOUNT_COUPON'] = 'COUPON.COUPON'; } $discountIterator = Internals\DiscountTable::getList(array('select' => $discountSelect, 'filter' => $discountFilter, 'order' => $discountOrder)); while ($discount = $discountIterator->fetch()) { $discount['ID'] = (int) $discount['ID']; if (isset($discountApply[$discount['ID']])) { continue; } $discountApply[$discount['ID']] = true; if (!$this->loadDiscountModules('sale' . $discount['ID'])) { continue; } if ($discount['USE_COUPONS'] == 'Y') { $discount['COUPON'] = $couponList[$discount['DISCOUNT_COUPON']]; } $discount['CONDITIONS'] = $discount['CONDITIONS_LIST']; $discount['ACTIONS'] = $discount['ACTIONS_LIST']; $discount['MODULE_ID'] = 'sale'; if (isset($this->cacheDiscountModules['sale' . $discount['ID']])) { $discount['MODULES'] = $this->cacheDiscountModules['sale' . $discount['ID']]; } unset($discount['ACTIONS_LIST'], $discount['CONDITIONS_LIST']); $currentList[] = $discount; } unset($discount, $discountIterator, $discountApply); $this->saleDiscountCache[$cacheKey] = $currentList; } else { $currentList = $this->saleDiscountCache[$cacheKey]; } unset($couponList); $this->discountIds = array(); if (!empty($currentList)) { foreach ($currentList as &$discount) { $this->discountIds[] = $discount['ID']; } unset($discount); $this->extendOrderData(); foreach ($currentList as $indexDiscount => $discount) { $this->fillCurrentStep(array('discount' => $discount)); if (!self::checkDiscountConditions()) { continue; } $actionsResult = $this->applySaleDiscount(); if (!$actionsResult->isSuccess()) { $result->addErrors($actionsResult->getErrors()); unset($actionsResult); return $result; } if ($this->currentStep['stop']) { break; } } unset($discount, $indexDiscount, $currentList); } $this->fillEmptyCurrentStep(); } return $result; }
public function Update($ID, $arFields) { global $DB; $ID = (int) $ID; if ($ID <= 0) { return false; } $boolNewVersion = true; $arFields['ID'] = $ID; if (!array_key_exists('CONDITIONS', $arFields) && !array_key_exists('ACTIONS', $arFields)) { $boolConvert = CSaleDiscount::__ConvertOldFormat('UPDATE', $arFields); if (!$boolConvert) { return false; } $boolNewVersion = false; } if (!CSaleDiscount::CheckFields("UPDATE", $arFields)) { return false; } if ($boolNewVersion) { $boolConvert = CSaleDiscount::__SetOldFields('UPDATE', $arFields); if (!$boolConvert) { return false; } } $strUpdate = $DB->PrepareUpdate("b_sale_discount", $arFields); if (!empty($strUpdate)) { $strSql = "update b_sale_discount set " . $strUpdate . " where ID = " . $ID; $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } if (isset($arFields['USER_GROUPS'])) { Internals\DiscountGroupTable::updateByDiscount($ID, $arFields['USER_GROUPS'], isset($arFields['ACTIVE']) ? $arFields['ACTIVE'] : '', true); } elseif (isset($arFields['ACTIVE'])) { Internals\DiscountGroupTable::changeActiveByDiscount($ID, $arFields['ACTIVE']); } if (isset($arFields['HANDLERS'])) { self::updateDiscountHandlers($ID, $arFields['HANDLERS'], true); } if (isset($arFields['ENTITIES'])) { Internals\DiscountEntitiesTable::updateByDiscount($ID, $arFields['ENTITIES'], true); } if (isset($arFields['ACTIONS']) && is_string($arFields['ACTIONS'])) { \Bitrix\Sale\Discount\Gift\RelatedDataTable::deleteByDiscount($ID); $giftManager = \Bitrix\Sale\Discount\Gift\Manager::getInstance(); if ($giftManager->isContainGiftAction($arFields)) { if (!$giftManager->existsDiscountsWithGift()) { $giftManager->enableExistenceDiscountsWithGift(); } \Bitrix\Sale\Discount\Gift\RelatedDataTable::fillByDiscount($arFields + array('ID' => $ID)); } } return $ID; }