/** * Update use coupon flag for discount list. * * @return void */ public static function updateUseCoupons() { if (!self::isCheckedCouponsUse() || empty(self::$discountCheckList)) { return; } Main\Type\Collection::normalizeArrayValuesByInt(self::$discountCheckList); if (empty(self::$discountCheckList)) { return; } $withoutCoupons = array_fill_keys(self::$discountCheckList, true); $withCoupons = array(); $couponIterator = DiscountCouponTable::getList(array('select' => array('DISCOUNT_ID', new Main\Entity\ExpressionField('CNT', 'COUNT(*)')), 'filter' => array('@DISCOUNT_ID' => self::$discountCheckList), 'group' => array('DISCOUNT_ID'))); while ($coupon = $couponIterator->fetch()) { $coupon['CNT'] = (int) $coupon['CNT']; if ($coupon['CNT'] > 0) { $coupon['DISCOUNT_ID'] = (int) $coupon['DISCOUNT_ID']; unset($withoutCoupons[$coupon['DISCOUNT_ID']]); $withCoupons[$coupon['DISCOUNT_ID']] = true; } } unset($coupon, $couponIterator); if (!empty($withoutCoupons)) { $withoutCoupons = array_keys($withoutCoupons); DiscountTable::setUseCoupons($withoutCoupons, 'N'); } if (!empty($withCoupons)) { $withCoupons = array_keys($withCoupons); DiscountTable::setUseCoupons($withCoupons, 'Y'); } unset($withCoupons, $withoutCoupons); static::clearDiscountCheckList(); }
if (!empty($errors)) { $errorMessage = new CAdminMessage(array('DETAILS' => implode('<br>', $errors), 'TYPE' => 'ERROR', 'MESSAGE' => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_ERR_SAVE'), 'HTML' => true)); echo $errorMessage->Show(); unset($errorMessage); } $selectFields = array(); if (!$multiCoupons) { $defaultValues = array('DISCOUNT_ID' => '', 'COUPON' => '', 'ACTIVE' => 'Y', 'ACTIVE_FROM' => null, 'ACTIVE_TO' => null, 'TYPE' => Internals\DiscountCouponTable::TYPE_ONE_ORDER, 'MAX_USE' => 0, 'USE_COUNT' => 0, 'USER_ID' => 0, 'DESCRIPTION' => ''); $selectFields = array('ID', 'DISCOUNT_NAME' => 'DISCOUNT.NAME'); $selectFields = array_merge($selectFields, array_keys($defaultValues)); } else { $defaultValues = array('COUNT' => '', array('DISCOUNT_ID' => '', 'ACTIVE_FROM' => null, 'ACTIVE_TO' => null, 'TYPE' => Internals\DiscountCouponTable::TYPE_ONE_ORDER, 'MAX_USE' => 0)); } $coupon = array(); if (!$multiCoupons && $couponID > 0) { $couponIterator = Internals\DiscountCouponTable::getList(array('select' => $selectFields, 'filter' => array('=ID' => $couponID))); if (!($coupon = $couponIterator->fetch())) { $couponID = 0; } unset($couponIterator); } if ($couponID == 0) { $coupon = $defaultValues; } if (!$multiCoupons) { $coupon['DISCOUNT_NAME'] = (string) $coupon['DISCOUNT_NAME']; $coupon['DISCOUNT_ID'] = (int) $coupon['DISCOUNT_ID']; $coupon['TYPE'] = (int) $coupon['TYPE']; $coupon['USE_COUNT'] = (int) $coupon['USE_COUNT']; $coupon['MAX_USE'] = (int) $coupon['MAX_USE']; $coupon['USER_ID'] = (int) $coupon['USER_ID'];
{ $totalPages = ceil($totalCount/$navyParams['SIZEN']); if ($navyParams['PAGEN'] > $totalPages) $navyParams['PAGEN'] = $totalPages; $getListParams['limit'] = $navyParams['SIZEN']; $getListParams['offset'] = $navyParams['SIZEN']*($navyParams['PAGEN']-1); } else { $navyParams['PAGEN'] = 1; $getListParams['limit'] = $navyParams['SIZEN']; $getListParams['offset'] = 0; } } $couponIterator = new CAdminResult(Internals\DiscountCouponTable::getList($getListParams), $adminListTableID); if ($usePageNavigation) { $couponIterator->NavStart($getListParams['limit'], $navyParams['SHOW_ALL'], $navyParams['PAGEN']); $couponIterator->NavRecordCount = $totalCount; $couponIterator->NavPageCount = $totalPages; $couponIterator->NavPageNomer = $navyParams['PAGEN']; } else { $couponIterator->NavStart(); } $adminList->NavText($couponIterator->GetNavPrint(Loc::getMessage('BT_SALE_DISCOUNT_COUPON_LIST_MESS_NAV'))); while ($coupon = $couponIterator->Fetch()) {
unset($countQuery); $totalCount = (int) $totalCount['CNT']; if ($totalCount > 0) { $totalPages = ceil($totalCount / $navyParams['SIZEN']); if ($navyParams['PAGEN'] > $totalPages) { $navyParams['PAGEN'] = $totalPages; } $getListParams['limit'] = $navyParams['SIZEN']; $getListParams['offset'] = $navyParams['SIZEN'] * ($navyParams['PAGEN'] - 1); } else { $navyParams['PAGEN'] = 1; $getListParams['limit'] = $navyParams['SIZEN']; $getListParams['offset'] = 0; } } $couponIterator = new CAdminSubResult(Internals\DiscountCouponTable::getList($getListParams), $adminListTableID, $adminList->GetListUrl(true)); if ($usePageNavigation) { $couponIterator->NavStart($getListParams['limit'], $navyParams['SHOW_ALL'], $navyParams['PAGEN']); $couponIterator->NavRecordCount = $totalCount; $couponIterator->NavPageCount = $totalPages; $couponIterator->NavPageNomer = $navyParams['PAGEN']; } else { $couponIterator->NavStart(); } $adminList->NavText($couponIterator->GetNavPrint(Loc::getMessage('BT_SALE_DISCOUNT_COUPON_LIST_MESS_NAV'))); while ($coupon = $couponIterator->Fetch()) { $coupon['ID'] = (int) $coupon['ID']; $coupon['MAX_USE'] = (int) $coupon['MAX_USE']; $coupon['USE_COUNT'] = (int) $coupon['USE_COUNT']; if ($selectFieldsMap['CREATED_BY']) { $coupon['CREATED_BY'] = (int) $coupon['CREATED_BY'];
/** * Check existing coupon. * * @param string $coupon Coupon for check. * @return array|bool */ public static function isExist($coupon) { $coupon = trim((string) $coupon); if ($coupon === '') { return false; } if (self::$onlySaleDiscount === null) { self::initUseDiscount(); } $couponIterator = Internals\DiscountCouponTable::getList(array('select' => array('ID', 'COUPON'), 'filter' => array('=COUPON' => $coupon))); if ($existCoupon = $couponIterator->fetch()) { return array('ID' => $existCoupon['ID'], 'COUPON' => $existCoupon['COUPON'], 'MODULE' => 'sale'); } else { if (!self::$onlySaleDiscount && !empty(self::$couponProviders)) { foreach (self::$couponProviders as &$provider) { $existCoupon = call_user_func_array($provider['isExist'], array($coupon)); if (!empty($existCoupon) && is_array($existCoupon)) { if (!isset($existCoupon['ID']) || !isset($existCoupon['COUPON'])) { continue; } return array('ID' => $existCoupon['ID'], 'COUPON' => $existCoupon['COUPON'], 'MODULE' => $provider['module']); } } unset($provider); } } return false; }
public function runOperation() { global $APPLICATION; $found = false; $filter = array('>ID' => $this->lastID, '=VERSION' => Sale\Internals\DiscountTable::VERSION_15); $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(); $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'], 'APPLICATION' => $rawFields['APPLICATION']); if (isset($rawFields['EXECUTE_MODULE'])) { $fields['EXECUTE_MODULE'] = $rawFields['EXECUTE_MODULE']; } $useCoupons = 'N'; $coupon = Sale\Internals\DiscountCouponTable::getList(array('select' => array('ID', 'DISCOUNT_ID'), 'filter' => array('=DISCOUNT_ID' => $discount['ID']), 'limit' => 1))->fetch(); if (!empty($coupon)) { $useCoupons = 'Y'; } $fields['USE_COUPONS'] = $useCoupons; unset($coupon, $useCoupons); $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; } }