/** * Create one and more coupons for discount. * * @param array $data Coupon data. * @param int $count Coupos count. * @param int $limit Maximum number of attempts. * @return Main\Entity\Result */ public static function addPacket(array $data, $count, $limit = 0) { $result = new Main\Entity\Result(); $result->setData(array('result' => 0, 'count' => $count, 'limit' => $limit, 'all' => 0)); $count = (int) $count; if ($count <= 0) { $result->addError(new Main\Entity\EntityError(Loc::getMessage('DISCOUNT_COUPON_PACKET_COUNT_ERR'), 'COUPON_PACKET')); } foreach (static::getEntity()->getFields() as $field) { if ($field instanceof Main\Entity\ScalarField && !array_key_exists($field->getName(), $data)) { $defaultValue = $field->getDefaultValue(); if ($defaultValue !== null) { $data[$field->getName()] = $field->getDefaultValue(); } } } $checkResult = static::checkPacket($data, false); if (!$checkResult->isSuccess()) { foreach ($checkResult->getErrors() as $checkError) { $result->addError($checkError); } unset($checkError); } unset($checkResult); $useCoupons = false; $discountIterator = DiscountTable::getList(array('select' => array('ID', 'USE_COUPONS'), 'filter' => array('=ID' => $data['DISCOUNT_ID']))); if ($discount = $discountIterator->fetch()) { $useCoupons = $discount['USE_COUPONS'] == 'Y'; } else { $result->addError(new Main\Entity\EntityError(Loc::getMessage('DISCOUNT_COUPON_PACKET_DISCOUNT_ERR'), 'COUPON_PACKET')); } if (!$result->isSuccess(true)) { return $result; } self::setDiscountCheckList($data['DISCOUNT_ID']); self::disableCheckCouponsUse(); $limit = (int) $limit; if ($limit < $count) { $limit = $count * 2; } $resultCount = 0; $all = 0; do { $data['COUPON'] = self::generateCoupon(true); $couponResult = self::add($data); if ($couponResult->isSuccess()) { $resultCount++; } $all++; } while ($resultCount < $count && $all < $limit); $result->setData(array('result' => $resultCount, 'count' => $count, 'limit' => $limit, 'all' => $all)); if ($resultCount == 0) { $result->addError(new Main\Entity\EntityError($useCoupons ? Loc::getMessage('DISCOUNT_COUPON_PACKET_GENERATE_COUPON_ZERO_ERR') : Loc::getMessage('DISCOUNT_COUPON_PACKET_NEW_GENERATE_COUPON_ZERO_ERR'), 'COUPON_PACKET')); self::clearDiscountCheckList(); } elseif ($resultCount < $count) { $result->addError(new Main\Entity\EntityError(Loc::getMessage('DISCOUNT_COUPON_PACKET_GENERATE_COUPON_COUNT_ERR', array('#RESULT#' => $resultCount, '#COUNT#' => $count, '#ALL#' => $all)), 'COUPON_PACKET')); } self::enableCheckCouponsUse(); self::updateUseCoupons(); return $result; }
public static function DoProcessOrder(&$arOrder, $arOptions, &$arErrors) { if (empty($arOrder['BASKET_ITEMS']) || !is_array($arOrder['BASKET_ITEMS'])) { return; } $arIDS = array(); $groupDiscountIterator = Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => CUser::GetUserGroup($arOrder['USER_ID']), '=ACTIVE' => 'Y'))); while ($groupDiscount = $groupDiscountIterator->fetch()) { $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID']; if ($groupDiscount['DISCOUNT_ID'] > 0) { $arIDS[$groupDiscount['DISCOUNT_ID']] = true; } } if (!empty($arIDS)) { $arIDS = array_keys($arIDS); $couponList = DiscountCouponsManager::getForApply(array('MODULE' => 'sale', 'DISCOUNT_ID' => $arIDS), array(), true); $arExtend = array('catalog' => array('fields' => true, 'props' => true)); foreach (GetModuleEvents('sale', 'OnExtendBasketItems', true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(&$arOrder['BASKET_ITEMS'], $arExtend)); } foreach ($arOrder['BASKET_ITEMS'] as &$arOneItem) { if (array_key_exists('PRODUCT_PROVIDER_CLASS', $arOneItem) && empty($arOneItem['PRODUCT_PROVIDER_CLASS']) && array_key_exists('CALLBACK_FUNC', $arOneItem) && empty($arOneItem['CALLBACK_FUNC']) && (!isset($arOneItem['CUSTOM_PRICE']) || $arOneItem['CUSTOM_PRICE'] != 'Y')) { if (isset($arOneItem['DISCOUNT_PRICE'])) { $arOneItem['PRICE'] += $arOneItem['DISCOUNT_PRICE']; $arOneItem['DISCOUNT_PRICE'] = 0; } } } if (isset($arOneItem)) { unset($arOneItem); } if (empty(self::$cacheDiscountHandlers)) { self::$cacheDiscountHandlers = CSaleDiscount::getDiscountHandlers($arIDS); } else { $needDiscountHandlers = array(); foreach ($arIDS as &$discountID) { if (!isset(self::$cacheDiscountHandlers[$discountID])) { $needDiscountHandlers[] = $discountID; } } unset($discountID); if (!empty($needDiscountHandlers)) { $discountHandlersList = CSaleDiscount::getDiscountHandlers($needDiscountHandlers); if (!empty($discountHandlersList)) { foreach ($discountHandlersList as $discountID => $discountHandlers) { self::$cacheDiscountHandlers[$discountID] = $discountHandlers; } unset($discountHandlers, $discountID); } unset($discountHandlersList); } unset($needDiscountHandlers); } CTimeZone::Disable(); $currentDatetime = new Type\DateTime(); $discountSelect = array('ID', 'PRIORITY', 'SORT', 'LAST_DISCOUNT', 'UNPACK', 'APPLICATION', 'USE_COUPONS'); $discountOrder = array('PRIORITY' => 'DESC', 'SORT' => 'ASC', 'ID' => 'ASC'); $discountFilter = array('@ID' => $arIDS, '=LID' => $arOrder['SITE_ID'], array('LOGIC' => 'OR', 'ACTIVE_FROM' => '', '<=ACTIVE_FROM' => $currentDatetime), array('LOGIC' => 'OR', 'ACTIVE_TO' => '', '>=ACTIVE_TO' => $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)); $discountApply = array(); $resultDiscountList = array(); $resultDiscountKeys = array(); $resultDiscountIndex = 0; while ($discount = $discountIterator->fetch()) { $discount['ID'] = (int) $discount['ID']; if (isset($discountApply[$discount['ID']])) { continue; } $discount['MODULE'] = 'sale'; $discountApply[$discount['ID']] = true; $applyFlag = true; if (isset(self::$cacheDiscountHandlers[$discount['ID']])) { $moduleList = self::$cacheDiscountHandlers[$discount['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 && self::__Unpack($arOrder, $discount['UNPACK'])) { $oldOrder = $arOrder; self::__ApplyActions($arOrder, $discount['APPLICATION']); $discountResult = self::getDiscountResult($oldOrder, $arOrder, false); if (!empty($discountResult['DELIVERY']) || !empty($discountResult['BASKET'])) { if ($discount['USE_COUPONS'] == 'Y' && !empty($discount['DISCOUNT_COUPON'])) { if ($couponList[$discount['DISCOUNT_COUPON']]['TYPE'] == Internals\DiscountCouponTable::TYPE_BASKET_ROW) { self::changeDiscountResult($oldOrder, $arOrder, $discountResult); } $couponApply = DiscountCouponsManager::setApply($discount['DISCOUNT_COUPON'], $discountResult); } $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'UNPACK' => $discount['UNPACK'], 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => $discountResult, 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false); $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex; $resultDiscountIndex++; if ($discount['LAST_DISCOUNT'] == 'Y') { break; } } unset($discountResult); } } unset($discount, $discountIterator); CTimeZone::Enable(); if ($resultDiscountIndex > 0) { $discountIterator = Internals\DiscountTable::getList(array('select' => array('ID', 'NAME', 'CONDITIONS', 'ACTIONS'), 'filter' => array('@ID' => array_keys($resultDiscountKeys)))); while ($discount = $discountIterator->fetch()) { $discount['ID'] = (int) $discount['ID']; if (isset($resultDiscountKeys[$discount['ID']])) { $key = $resultDiscountKeys[$discount['ID']]; $resultDiscountList[$key]['NAME'] = $discount['NAME']; $resultDiscountList[$key]['CONDITIONS'] = $discount['CONDITIONS']; $resultDiscountList[$key]['ACTIONS'] = $discount['ACTIONS']; } } unset($discount, $discountIterator); } $arOrder["ORDER_PRICE"] = 0; $arOrder["ORDER_WEIGHT"] = 0; $arOrder["USE_VAT"] = false; $arOrder["VAT_RATE"] = 0; $arOrder["VAT_SUM"] = 0; $arOrder["DISCOUNT_PRICE"] = 0.0; $arOrder["DISCOUNT_VALUE"] = $arOrder["DISCOUNT_PRICE"]; $arOrder["DELIVERY_PRICE"] = $arOrder["PRICE_DELIVERY"]; foreach ($arOrder['BASKET_ITEMS'] as &$arShoppingCartItem) { if (!CSaleBasketHelper::isSetItem($arShoppingCartItem)) { $arOrder["ORDER_PRICE"] += doubleval($arShoppingCartItem["PRICE"]) * doubleval($arShoppingCartItem["QUANTITY"]); $arOrder["ORDER_WEIGHT"] += $arShoppingCartItem["WEIGHT"] * $arShoppingCartItem["QUANTITY"]; $arShoppingCartItem["PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arShoppingCartItem["PRICE"], $arShoppingCartItem["CURRENCY"], true); $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = 0; if ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"] > 0) { $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = $arShoppingCartItem["DISCOUNT_PRICE"] * 100 / ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"]); } $arShoppingCartItem["DISCOUNT_PRICE_PERCENT_FORMATED"] = roundEx($arShoppingCartItem["DISCOUNT_PRICE_PERCENT"], SALE_VALUE_PRECISION) . "%"; if ($arShoppingCartItem["VAT_RATE"] > 0) { $arOrder["USE_VAT"] = true; if ($arShoppingCartItem["VAT_RATE"] > $arOrder["VAT_RATE"]) { $arOrder["VAT_RATE"] = $arShoppingCartItem["VAT_RATE"]; } $arOrder["VAT_SUM"] += $arShoppingCartItem["VAT_VALUE"] * $arShoppingCartItem["QUANTITY"]; } } } if (isset($arShoppingCartItem)) { unset($arShoppingCartItem); } $arOrder['DISCOUNT_LIST'] = $resultDiscountList; } }
$usePageNavigation = false; } else { $navyParams = CDBResult::GetNavParams(CAdminResult::GetNavSize($adminListTableID)); if ($navyParams['SHOW_ALL']) { $usePageNavigation = false; } else { $navyParams['PAGEN'] = (int) $navyParams['PAGEN']; $navyParams['SIZEN'] = (int) $navyParams['SIZEN']; } } $getListParams = array('select' => array_keys($selectFields), 'filter' => $filter, 'order' => array($by => $order)); if ($usePageNavigation) { $getListParams['limit'] = $navyParams['SIZEN']; $getListParams['offset'] = $navyParams['SIZEN'] * ($navyParams['PAGEN'] - 1); } $discountIterator = new CAdminResult(Internals\DiscountTable::getList($getListParams), $adminListTableID); if ($usePageNavigation) { $countQuery = new Main\Entity\Query(Internals\DiscountTable::getEntity()); $countQuery->addSelect(new Main\Entity\ExpressionField('CNT', 'COUNT(1)')); $countQuery->setFilter($getListParams['filter']); $totalCount = $countQuery->setLimit(null)->setOffset(null)->exec()->fetch(); $totalCount = (int) $totalCount['CNT']; $totalPages = ceil($totalCount / $getListParams['limit']); unset($countQuery); $discountIterator->NavStart($getListParams['limit'], $navyParams['SHOW_ALL'], $navyParams['PAGEN']); $discountIterator->NavRecordCount = $totalCount; $discountIterator->NavPageCount = $totalPages; $discountIterator->NavPageNomer = $navyParams['PAGEN']; } else { $discountIterator->NavStart(); }
<?php } else { if ($couponID > 0 && !$copy) { $control->AddViewField($prefix . 'ID', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_ID'), $couponID, false); } $control->AddCheckBoxField($prefix . 'ACTIVE', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_ACTIVE'), true, array('Y', 'N'), $coupon['ACTIVE'] == 'Y'); if ($couponID > 0) { $discountName = '<a href="sale_discount_edit.php?lang=' . LANGUAGE_ID . '&ID=' . $coupon['DISCOUNT_ID'] . '">[' . $coupon['DISCOUNT_ID'] . ']</a>'; if ($coupon['DISCOUNT_NAME'] !== '') { $discountName .= ' ' . $coupon['DISCOUNT_NAME']; } $discountName .= '<input type="hidden" name="DISCOUNT_ID" value="' . $coupon['DISCOUNT_ID'] . '">'; $control->AddViewField('DISCOUNT_ID', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_DISCOUNT'), $discountName, true); } elseif (!$subWindow) { $discountList = array(); $discountIterator = Internals\DiscountTable::getList(array('select' => array('ID', 'NAME'), 'filter' => array('=ACTIVE' => 'Y'), 'order' => array('SORT' => 'ASC', 'NAME' => 'ASC'))); while ($discount = $discountIterator->fetch()) { $discount['ID'] = (int) $discount['ID']; $discount['NAME'] = (string) $discount['NAME']; $discountList[$discount['ID']] = '[' . $discount['ID'] . ']' . ($discount['NAME'] !== '' ? ' ' . $discount['NAME'] : ''); } unset($discount, $discountIterator); if (!empty($discountList)) { $control->AddDropDownField('DISCOUNT_ID', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_DISCOUNT'), true, $discountList, $coupon['DISCOUNT_ID']); } else { $control->BeginCustomField('DISCOUNT_ID', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_DISCOUNT'), true); $dicountEditPath = 'sale_discount_edit.php?lang=' . LANGUAGE_ID . '&return_url=' . urlencode($APPLICATION->GetCurPageParam()); ?> <tr id="tr_DISCOUNT_ID"> <td width="40%"><?php echo $control->GetCustomLabelHTML();
public static function DoProcessOrder(&$arOrder, $arOptions, &$arErrors) { if (empty($arOrder['BASKET_ITEMS']) || !is_array($arOrder['BASKET_ITEMS'])) { return; } $isOrderConverted = \Bitrix\Main\Config\Option::get("main", "~sale_converted_15", 'N'); $oldDelivery = ''; $checkIds = true; $arIDS = array(); if ($isOrderConverted == 'Y') { if (isset($arOrder['DELIVERY_ID']) && $arOrder['DELIVERY_ID'] != '') { $oldDelivery = $arOrder['DELIVERY_ID']; $arOrder['DELIVERY_ID'] = \CSaleDelivery::getIdByCode($arOrder['DELIVERY_ID']); } $adminSection = defined('ADMIN_SECTION') && ADMIN_SECTION === true; if (!$adminSection) { $mode = Sale\Compatible\DiscountCompatibility::MODE_MANAGER; $modeParams = array(); if (isset($arOrder['CURRENCY'])) { $modeParams['CURRENCY'] = $arOrder['CURRENCY']; } if (isset($arOrder['SITE_ID'])) { $modeParams['SITE_ID'] = $arOrder['SITE_ID']; if (!isset($modeParams['CURRENCY'])) { $modeParams['CURRENCY'] = Sale\Internals\SiteCurrencyTable::getSiteCurrency($modeParams['SITE_ID']); } } } else { $mode = Sale\Compatible\DiscountCompatibility::MODE_CLIENT; $modeParams = array('SITE_ID' => SITE_ID, 'CURRENCY' => Sale\Internals\SiteCurrencyTable::getSiteCurrency(SITE_ID)); } unset($adminSection); if (!empty($modeParams)) { Sale\Discount\Actions::setUseMode(Sale\Discount\Actions::MODE_CALCULATE, array('USE_BASE_PRICE' => \Bitrix\Main\Config\Option::get('sale', 'get_discount_percent_from_base_price'), 'SITE_ID' => $modeParams['SITE_ID'], 'CURRENCY' => $modeParams['CURRENCY'])); } if (!Sale\Compatible\DiscountCompatibility::isInited()) { if (!empty($modeParams)) { Sale\Compatible\DiscountCompatibility::init($mode, $modeParams); } } unset($modeParams, $mode); Sale\Compatible\DiscountCompatibility::clearDiscountResult(); Sale\Compatible\DiscountCompatibility::fillBasketData($arOrder['BASKET_ITEMS']); Sale\Compatible\DiscountCompatibility::calculateBasketDiscounts($arOrder['BASKET_ITEMS']); Sale\Compatible\DiscountCompatibility::setApplyMode($arOrder['BASKET_ITEMS']); $applyMode = Sale\Discount::getApplyMode(); if ($applyMode == Sale\Discount::APPLY_MODE_FULL_LAST || $applyMode == Sale\Discount::APPLY_MODE_FULL_DISABLE) { foreach ($arOrder['BASKET_ITEMS'] as &$basketItem) { if (isset($basketItem['LAST_DISCOUNT']) && $basketItem['LAST_DISCOUNT'] == 'Y') { $checkIds = false; break; } } unset($basketItem); } } if ($checkIds) { $groupDiscountIterator = Sale\Internals\DiscountGroupTable::getList(array('select' => array('DISCOUNT_ID'), 'filter' => array('@GROUP_ID' => CUser::GetUserGroup($arOrder['USER_ID']), '=ACTIVE' => 'Y'))); while ($groupDiscount = $groupDiscountIterator->fetch()) { $groupDiscount['DISCOUNT_ID'] = (int) $groupDiscount['DISCOUNT_ID']; if ($groupDiscount['DISCOUNT_ID'] > 0) { $arIDS[$groupDiscount['DISCOUNT_ID']] = true; } } } if (!empty($arIDS)) { $arIDS = array_keys($arIDS); $couponList = Sale\DiscountCouponsManager::getForApply(array('MODULE_ID' => 'sale', 'DISCOUNT_ID' => $arIDS), array(), true); //TODO: fix this condition $useProps = true; $iblockPropList = array(); $entityList = Sale\Internals\DiscountEntitiesTable::getByDiscount($arIDS, array('=MODULE_ID' => 'catalog', '=ENTITY' => 'ELEMENT_PROPERTY')); if (empty($entityList)) { $useProps = false; } else { if (empty($entityList['catalog']['ELEMENT_PROPERTY'])) { $useProps = false; } else { foreach ($entityList['catalog']['ELEMENT_PROPERTY'] as $entity) { $entityField = explode(':', $entity['FIELD_TABLE']); if (isset($entityField[1])) { $propId = (int) $entityField[1]; if ($propId > 0) { $iblockPropList[$propId] = $propId; } unset($propId); } unset($entityField); } unset($entity); if (empty($iblockPropList)) { $useProps = false; } } } $arExtend = array('catalog' => array('fields' => true, 'props' => $useProps)); if ($useProps) { $arExtend['iblock']['props'] = $iblockPropList; } unset($iblockPropList, $useProps); foreach (GetModuleEvents('sale', 'OnExtendBasketItems', true) as $arEvent) { ExecuteModuleEventEx($arEvent, array(&$arOrder['BASKET_ITEMS'], $arExtend)); } foreach ($arOrder['BASKET_ITEMS'] as &$arOneItem) { if (array_key_exists('PRODUCT_PROVIDER_CLASS', $arOneItem) && empty($arOneItem['PRODUCT_PROVIDER_CLASS']) && array_key_exists('CALLBACK_FUNC', $arOneItem) && empty($arOneItem['CALLBACK_FUNC']) && (!isset($arOneItem['CUSTOM_PRICE']) || $arOneItem['CUSTOM_PRICE'] != 'Y')) { if (isset($arOneItem['DISCOUNT_PRICE'])) { $arOneItem['PRICE'] += $arOneItem['DISCOUNT_PRICE']; $arOneItem['DISCOUNT_PRICE'] = 0; $arOneItem['BASE_PRICE'] = $arOneItem['PRICE']; } } } if (isset($arOneItem)) { unset($arOneItem); } if (empty(self::$cacheDiscountHandlers)) { self::$cacheDiscountHandlers = CSaleDiscount::getDiscountHandlers($arIDS); } else { $needDiscountHandlers = array(); foreach ($arIDS as &$discountID) { if (!isset(self::$cacheDiscountHandlers[$discountID])) { $needDiscountHandlers[] = $discountID; } } unset($discountID); if (!empty($needDiscountHandlers)) { $discountHandlersList = CSaleDiscount::getDiscountHandlers($needDiscountHandlers); if (!empty($discountHandlersList)) { foreach ($discountHandlersList as $discountID => $discountHandlers) { self::$cacheDiscountHandlers[$discountID] = $discountHandlers; } unset($discountHandlers, $discountID); } unset($discountHandlersList); } unset($needDiscountHandlers); } $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' => $arIDS, '=LID' => $arOrder['SITE_ID'], array('LOGIC' => 'OR', 'ACTIVE_FROM' => '', '<=ACTIVE_FROM' => $currentDatetime), array('LOGIC' => 'OR', 'ACTIVE_TO' => '', '>=ACTIVE_TO' => $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 = Sale\Internals\DiscountTable::getList(array('select' => $discountSelect, 'filter' => $discountFilter, 'order' => $discountOrder)); $discountApply = array(); $resultDiscountFullList = array(); $resultDiscountList = array(); $resultDiscountKeys = array(); $resultDiscountIndex = 0; while ($discount = $discountIterator->fetch()) { $discount['ID'] = (int) $discount['ID']; if (isset($discountApply[$discount['ID']])) { continue; } $discount['MODULE'] = 'sale'; $discount['MODULE_ID'] = 'sale'; if ($discount['USE_COUPONS'] == 'Y') { $discount['COUPON'] = $couponList[$discount['DISCOUNT_COUPON']]; } $discountApply[$discount['ID']] = true; $applyFlag = true; if (isset(self::$cacheDiscountHandlers[$discount['ID']])) { $moduleList = self::$cacheDiscountHandlers[$discount['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); if ($applyFlag) { $discount['MODULES'] = $moduleList; } } unset($moduleList); } if ($isOrderConverted == 'Y') { Sale\Compatible\DiscountCompatibility::setOrderData($arOrder); } if ($applyFlag && self::__Unpack($arOrder, $discount['UNPACK'])) { $oldOrder = $arOrder; if ($isOrderConverted == 'Y') { Sale\Discount\Actions::clearAction(); } self::__ApplyActions($arOrder, $discount['APPLICATION']); if ($isOrderConverted == 'Y') { $resultDiscountFullList[] = $discount; if (Sale\Compatible\DiscountCompatibility::calculateSaleDiscount($arOrder, $discount)) { $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'NAME' => $discount['NAME'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'CONDITIONS' => serialize($discount['CONDITIONS_LIST']), 'UNPACK' => $discount['UNPACK'], 'ACTIONS' => serialize($discount['ACTIONS_LIST']), 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => self::getDiscountResult($oldOrder, $arOrder, false), 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false); $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex; $resultDiscountIndex++; if ($discount['LAST_DISCOUNT'] == 'Y') { break; } } Sale\Discount\Actions::clearAction(); } else { $discountResult = self::getDiscountResult($oldOrder, $arOrder, false); if (!empty($discountResult['DELIVERY']) || !empty($discountResult['BASKET'])) { if ($discount['USE_COUPONS'] == 'Y' && !empty($discount['DISCOUNT_COUPON'])) { if ($couponList[$discount['DISCOUNT_COUPON']]['TYPE'] == Sale\Internals\DiscountCouponTable::TYPE_BASKET_ROW) { self::changeDiscountResult($oldOrder, $arOrder, $discountResult); } $couponApply = Sale\DiscountCouponsManager::setApply($discount['DISCOUNT_COUPON'], $discountResult); unset($couponApply); } $resultDiscountList[$resultDiscountIndex] = array('MODULE_ID' => $discount['MODULE_ID'], 'ID' => $discount['ID'], 'NAME' => $discount['NAME'], 'PRIORITY' => $discount['PRIORITY'], 'SORT' => $discount['SORT'], 'LAST_DISCOUNT' => $discount['LAST_DISCOUNT'], 'CONDITIONS' => serialize($discount['CONDITIONS_LIST']), 'UNPACK' => $discount['UNPACK'], 'ACTIONS' => serialize($discount['ACTIONS_LIST']), 'APPLICATION' => $discount['APPLICATION'], 'RESULT' => $discountResult, 'HANDLERS' => self::$cacheDiscountHandlers[$discount['ID']], 'USE_COUPONS' => $discount['USE_COUPONS'], 'COUPON' => $discount['USE_COUPONS'] == 'Y' ? $couponList[$discount['DISCOUNT_COUPON']] : false); $resultDiscountKeys[$discount['ID']] = $resultDiscountIndex; $resultDiscountIndex++; if ($discount['LAST_DISCOUNT'] == 'Y') { break; } } unset($discountResult); } } } unset($discount, $discountIterator); $arOrder["ORDER_PRICE"] = 0; $arOrder["ORDER_WEIGHT"] = 0; $arOrder["USE_VAT"] = false; $arOrder["VAT_RATE"] = 0; $arOrder["VAT_SUM"] = 0; $arOrder["DISCOUNT_PRICE"] = 0.0; $arOrder["DISCOUNT_VALUE"] = $arOrder["DISCOUNT_PRICE"]; $arOrder["PRICE_DELIVERY"] = roundEx($arOrder["PRICE_DELIVERY"], SALE_VALUE_PRECISION); $arOrder["DELIVERY_PRICE"] = $arOrder["PRICE_DELIVERY"]; foreach ($arOrder['BASKET_ITEMS'] as &$arShoppingCartItem) { if (isset($arShoppingCartItem['CATALOG'])) { unset($arShoppingCartItem['CATALOG']); } if (!CSaleBasketHelper::isSetItem($arShoppingCartItem)) { $customPrice = isset($arShoppingCartItem['CUSTOM_PRICE']) && ($arShoppingCartItem['CUSTOM_PRICE'] = 'Y'); if (!$customPrice) { $arShoppingCartItem['DISCOUNT_PRICE'] = roundEx($arShoppingCartItem['DISCOUNT_PRICE'], SALE_VALUE_PRECISION); if ($arShoppingCartItem['DISCOUNT_PRICE'] > 0) { $arShoppingCartItem['PRICE'] = $arShoppingCartItem['BASE_PRICE'] - $arShoppingCartItem['DISCOUNT_PRICE']; } else { $arShoppingCartItem['PRICE'] = roundEx($arShoppingCartItem['PRICE'], SALE_VALUE_PRECISION); } } else { $arShoppingCartItem['DISCOUNT_PRICE'] = 0; } if (isset($arShoppingCartItem['VAT_RATE'])) { $vatRate = (double) $arShoppingCartItem['VAT_RATE']; if ($vatRate > 0) { $arShoppingCartItem['VAT_VALUE'] = $arShoppingCartItem['PRICE'] / ($vatRate + 1) * $vatRate; } unset($vatRate); } $arOrder["ORDER_PRICE"] += $arShoppingCartItem["PRICE"] * $arShoppingCartItem["QUANTITY"]; $arOrder["ORDER_WEIGHT"] += $arShoppingCartItem["WEIGHT"] * $arShoppingCartItem["QUANTITY"]; $arShoppingCartItem["PRICE_FORMATED"] = CCurrencyLang::CurrencyFormat($arShoppingCartItem["PRICE"], $arShoppingCartItem["CURRENCY"], true); $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = 0; if ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"] > 0) { $arShoppingCartItem["DISCOUNT_PRICE_PERCENT"] = $arShoppingCartItem["DISCOUNT_PRICE"] * 100 / ($arShoppingCartItem["DISCOUNT_PRICE"] + $arShoppingCartItem["PRICE"]); } $arShoppingCartItem["DISCOUNT_PRICE_PERCENT_FORMATED"] = roundEx($arShoppingCartItem["DISCOUNT_PRICE_PERCENT"], SALE_VALUE_PRECISION) . "%"; if ($arShoppingCartItem["VAT_RATE"] > 0) { $arOrder["USE_VAT"] = true; if ($arShoppingCartItem["VAT_RATE"] > $arOrder["VAT_RATE"]) { $arOrder["VAT_RATE"] = $arShoppingCartItem["VAT_RATE"]; } $arOrder["VAT_SUM"] += $arShoppingCartItem["VAT_VALUE"] * $arShoppingCartItem["QUANTITY"]; } } } unset($arShoppingCartItem); $arOrder['DISCOUNT_LIST'] = $resultDiscountList; $arOrder['FULL_DISCOUNT_LIST'] = $resultDiscountFullList; if ($isOrderConverted == 'Y') { Sale\Compatible\DiscountCompatibility::setOldDiscountResult($resultDiscountList); } } if ($isOrderConverted == 'Y' && $oldDelivery != '') { $arOrder['DELIVERY_ID'] = $oldDelivery; } $arOrder["ORDER_PRICE"] = roundEx($arOrder["ORDER_PRICE"], SALE_VALUE_PRECISION); }
public function runOperation() { global $APPLICATION; $this->getDeliveryCodes(); $found = false; $filter = array('>ID' => $this->lastID, '=VERSION' => Sale\Internals\DiscountTable::VERSION_NEW); $discountsIterator = Sale\Internals\DiscountTable::getList(array('select' => array('ID', 'MODIFIED_BY', 'TIMESTAMP_X', 'CONDITIONS_LIST', 'ACTIONS_LIST', 'NAME'), 'filter' => $filter, 'order' => array('ID' => 'ASC'), 'limit' => $this->maxOperationCounter)); while ($discount = $discountsIterator->fetch()) { $found = true; $error = array(); $this->convertDelivery($discount['CONDITIONS_LIST']); $rawFields = array('ID' => $discount['ID'], 'CONDITIONS' => $discount['CONDITIONS_LIST'], 'ACTIONS' => $discount['ACTIONS_LIST']); if (\CSaleDiscount::checkFields('UPDATE', $rawFields)) { $fields = array('MODIFIED_BY' => $discount['MODIFIED_BY'], 'TIMESTAMP_X' => $discount['TIMESTAMP_X'], 'UNPACK' => $rawFields['UNPACK'], 'CONDITIONS' => $discount['CONDITIONS_LIST'], 'VERSION' => Sale\Internals\DiscountTable::VERSION_15); if (isset($rawFields['EXECUTE_MODULE'])) { $fields['EXECUTE_MODULE'] = $rawFields['EXECUTE_MODULE']; } $updateResult = Sale\Internals\DiscountTable::update($discount['ID'], $fields); if ($updateResult->isSuccess()) { if (isset($rawFields['ENTITIES'])) { Sale\Internals\DiscountEntitiesTable::updateByDiscount($discount['ID'], $rawFields['ENTITIES'], true); } if (isset($rawFields['HANDLERS']['MODULES'])) { Sale\Internals\DiscountModuleTable::updateByDiscount($discount['ID'], $rawFields['HANDLERS']['MODULES'], true); } } else { $error = $updateResult->getErrorMessages(); } } else { if ($ex = $APPLICATION->GetException()) { $error[] = $ex->GetString(); } else { $error[] = Loc::getMessage('SALE_DISCOUNT_REINDEX_UPDATE_UNKNOWN_ERROR'); } } $this->lastID = $discount['ID']; $this->allOperationCounter++; if (!empty($error)) { $this->errorCounter++; $this->errors[] = Loc::getMessage('SALE_DISCOUNT_REINDEX_ORDER_ERROR_REPORT', array('#URL#' => str_replace('#ID#', $discount['ID'], $this->discountEditUrl), '#TITLE#' => trim((string) $discount['NAME']) != '' ? $discount['NAME'] : $discount['ID'], '#ERRORS#' => implode('; ', $error))); } if ($this->maxExecutionTime > 0 && time() - $this->startOperationTime > $this->maxExecutionTime) { break; } } unset($discount, $discountsIterator); if (!$found) { $this->finishOperation = true; } }
<div class="grayDownLine"></div> <p class="finalCost"><span id="allSum_FORMATED"><?php echo str_replace(" ", " ", $arResult["allSum_FORMATED"]); ?> </span></p> <p class="finalQuant">Кол-во: <span id="totalQuantity"><?php echo $totalQuantity; ?> </span></p> <p class="finalText">Итого</p> <?php //TODO сделать автоматический расчет суммы, оставшейся до скидки, основываясь на правилах работы с корзиной //получаем правила работы с корзиной $filterCoup = array(); $discountIteratorCoup = Internals\DiscountTable::getList(array('filter' => $filterCoup)); $arDiscount = $discountIteratorCoup->fetch(); // arshow($arDiscount); ?> <?php /* <p class="finalDiscount">Вам не хватает 770 руб. до получения скидки в 10%</p> */ ?> <p class="promoWrap"><span class="promocode" onclick="$('#coupon').toggle()">Есть промокод?<span></p> <div class="bx_ordercart_order_pay_left" id="coupons_block"> <div class="bx_ordercart_coupon"> <input type="text" id="coupon" name="COUPON" value="" onchange="enterCoupon();"> </div><?php
/** * Load sale discount from database * * @return void * @throws Main\ArgumentException */ protected function loadDiscountList() { if (empty($this->discountIds)) { $this->discountIds = null; return; } $this->getDiscountModules(); $couponList = DiscountCouponsManager::getForApply(array('MODULE' => 'sale', 'DISCOUNT_ID' => $this->discountIds), array(), true); $this->saleDiscountCacheKey = md5('D' . implode('_', $this->discountIds)); if (!empty($couponList)) { $this->saleDiscountCacheKey .= '-C' . implode('_', array_keys($couponList)); } if (!isset($this->saleDiscountCache[$this->saleDiscountCacheKey])) { $currentList = array(); $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'); $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' => array('PRIORITY' => 'DESC', 'SORT' => 'ASC', 'ID' => 'ASC'))); unset($discountSelect, $discountFilter); 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['ID']] = $discount; } unset($discount, $discountIterator, $discountApply); $this->saleDiscountCache[$this->saleDiscountCacheKey] = $currentList; unset($currentList); } unset($couponList); }
/** * 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; }