GetGroupRight('sale'); $readOnly = ($saleModulePermissions < 'W'); if ($saleModulePermissions < 'R') $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED")); Loader::includeModule('sale'); Loc::loadMessages(__FILE__); if ($subWindow) { require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/iblock/classes/general/subelement.php'); } if (!$subWindow && $ex = $APPLICATION->GetException()) { require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_after.php'); ShowError($ex->GetString()); require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/epilog_admin.php'); die(); } $couponTypes = Internals\DiscountCouponTable::getCouponTypes(true); $multiCoupons = false; $discountID = 0; if ($subWindow) { $multiCoupons = (isset($_REQUEST['MULTI']) && $_REQUEST['MULTI'] == 'Y'); if (isset($_REQUEST['DISCOUNT_ID'])) $discountID = (int)$_REQUEST['DISCOUNT_ID']; $discountIterator = Internals\DiscountTable::getList(array( 'select' => array('ID'), 'filter' => array('=ID' => $discountID) )); if (!($discount = $discountIterator->fetch())) { require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_after.php'); ShowError(Loc::getMessage('BX_SALE_DISCOUNT_COUPON_ERR_DISCOUNT_ID_ABSENT')); require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/epilog_admin.php'); die(); } } $returnUrl = ''; if (!$subWindow && !empty($_REQUEST['return_url'])) { $currentUrl = $APPLICATION->GetCurPage(); if (strtolower(substr($_REQUEST['return_url'], strlen($currentUrl))) != strtolower($currentUrl)) $returnUrl = $_REQUEST['return_url']; } $tabList = array( array( 'ICON' => 'sale', 'DIV' => 'couponEdit01', 'TAB' => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TAB_NAME_COMMON'), 'TITLE' => ( $multiCoupons ? Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TAB_TITLE_MULTI_COMMON') : Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TAB_TITLE_COMMON') ) ) ); $couponFormID = ''; if ($subWindow) { $arPostParams = array( 'bxpublic' => 'Y', 'DISCOUNT_ID' => $discountID, 'sessid' => bitrix_sessid() ); $listUrl = array( 'LINK' => $APPLICATION->GetCurPageParam(), 'POST_PARAMS' => $arPostParams, ); $couponFormID = 'saleSubCouponControl'; $control = new CAdminSubForm($couponFormID, $tabList, false, true, $listUrl, false); } else { $couponFormID = ($multiCoupons ? 'saleMultiCouponControl' : 'saleCouponControl'); $control = new CAdminForm($couponFormID, $tabList); $control->SetShowSettings(false); } unset($tabList); $couponFormID .= '_form'; $errors = array(); $fields = array(); $couponID = 0; $copy = false; if (isset($_REQUEST['ID'])) { $couponID = (int)$_REQUEST['ID']; if ($couponID < 0) $couponID = 0; } if ($couponID > 0) $copy = (isset($_REQUEST['action']) && (string)$_REQUEST['action'] == 'copy'); if ( check_bitrix_sessid() && !$readOnly && $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['Update']) && (string)$_POST['Update'] == 'Y' ) { if ($multiCoupons) { $fields['COUNT'] = 0; $fields['COUPON'] = array( 'DISCOUNT_ID' => $discountID ); if (!empty($_POST[$prefix.'ACTIVE_FROM'])) $fields['COUPON']['ACTIVE_FROM'] = new Main\Type\DateTime($_POST[$prefix.'ACTIVE_FROM']); if (!empty($_POST[$prefix.'ACTIVE_TO'])) $fields['COUPON']['ACTIVE_TO'] = new Main\Type\DateTime($_POST[$prefix.'ACTIVE_TO']); if (isset($_POST[$prefix.'TYPE'])) $fields['COUPON']['TYPE'] = $_POST[$prefix.'TYPE']; if (isset($_POST[$prefix.'MAX_USE'])) $fields['COUPON']['MAX_USE'] = $_POST[$prefix.'MAX_USE']; if (isset($_POST[$prefix.'COUNT'])) $fields['COUNT'] = (int)$_POST[$prefix.'COUNT']; if ($fields['COUNT'] <= 0) $errors[] = Loc::getMessage('BX_SALE_DISCOUNT_COUPON_ERR_COUPON_COUNT'); $checkResult = Internals\DiscountCouponTable::checkPacket($fields['COUPON'], false); if (!$checkResult->isSuccess(true)) { $errors = $checkResult->getErrorMessages(); } else { $couponsResult = Internals\DiscountCouponTable::addPacket( $fields['COUPON'], $fields['COUNT'] ); if (!$couponsResult->isSuccess()) { $errors = $couponsResult->getErrorMessages(); } } unset($checkResult); } else { if ($subWindow) $fields['DISCOUNT_ID'] = $discountID; elseif (!empty($_POST['DISCOUNT_ID'])) $fields['DISCOUNT_ID'] = $_POST['DISCOUNT_ID']; if (isset($_POST['COUPON'])) $fields['COUPON'] = $_POST['COUPON']; if (!empty($_POST[$prefix.'ACTIVE'])) $fields['ACTIVE'] = $_POST[$prefix.'ACTIVE']; if (!empty($_POST[$prefix.'ACTIVE_FROM'])) $fields['ACTIVE_FROM'] = new Main\Type\DateTime($_POST[$prefix.'ACTIVE_FROM']); if (!empty($_POST[$prefix.'ACTIVE_TO'])) $fields['ACTIVE_TO'] = new Main\Type\DateTime($_POST[$prefix.'ACTIVE_TO']); if (isset($_POST[$prefix.'TYPE'])) $fields['TYPE'] = $_POST[$prefix.'TYPE']; if (isset($_POST[$prefix.'MAX_USE'])) $fields['MAX_USE'] = $_POST[$prefix.'MAX_USE']; if (isset($_POST[$prefix.'USER_ID'])) $fields['USER_ID'] = $_POST[$prefix.'USER_ID']; if (isset($_POST[$prefix.'DESCRIPTION'])) $fields['DESCRIPTION'] = $_POST[$prefix.'DESCRIPTION']; if ($couponID == 0) $result = Internals\DiscountCouponTable::add($fields); else $result = Internals\DiscountCouponTable::update($couponID, $fields); if (!$result->isSuccess()) { $errors = $result->getErrorMessages(); } else { if ($couponID == 0) $couponID = $result->getId(); } unset($result); } if (empty($errors)) { if ($subWindow) { ?>ActiveTabParam().GetFilterParams('filter_', false)); } } } elseif ($subWindow) { if (!empty($_REQUEST['dontsave'])) { ?>SetTitle( $couponID == 0 ? ( !$multiCoupons ? Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TITLE_ADD') : Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TITLE_MULTI_ADD') ) : ( !$copy ? Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TITLE_UPDATE', array('#ID#' => $couponID)) : Loc::getMessage('BX_SALE_DISCOUNT_COUPON_EDIT_TITLE_COPY', array('#ID#' => $couponID)) ) ); require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_after.php'); $contextMenuItems = array( array( 'ICON' => 'btn_list', 'TEXT' => Loc::getMessage('BX_SALE_DISCOUNT_COUPONT_CONTEXT_COUPON_LIST'), 'LINK' => 'sale_discount_coupons.php?lang='.LANGUAGE_ID.GetFilterParams('filter_') ) ); if (!$subWindow && !$readOnly && $couponID > 0) { if (!$copy) { $contextMenuItems[] = array('SEPARATOR' => 'Y'); $contextMenuItems[] = array( 'ICON' => 'btn_new', 'TEXT' => Loc::getMessage('BX_SALE_DISCOUNT_COUPONT_CONTEXT_NEW'), 'LINK' => 'sale_discount_coupon_edit.php?lang='.LANGUAGE_ID.GetFilterParams('filter_') ); $contextMenuItems[] = array( 'ICON' => 'btn_copy', 'TEXT' => Loc::getMessage('BX_SALE_DISCOUNT_COUPONT_CONTEXT_COPY'), 'LINK' => 'sale_discount_coupon_edit.php?lang='.LANGUAGE_ID.'&ID='.$couponID.'&action=copy'.GetFilterParams('filter_') ); $contextMenuItems[] = array( 'ICON' => 'btn_delete', 'TEXT' => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_CONTEXT_DELETE'), 'LINK' => "javascript:if(confirm('".CUtil::JSEscape(Loc::getMessage('BX_SALE_DISCOUNT_COUPON_CONTEXT_DELETE_CONFIRM'))."')) window.location='/bitrix/admin/sale_discount_coupons.php?lang=".LANGUAGE_ID."&ID=".$couponID."&action=delete&".bitrix_sessid_get()."';", 'WARNING' => 'Y', ); } } $contextMenu = new CAdminContextMenu($contextMenuItems); $contextMenu->Show(); unset($contextMenu, $contextMenuItems); if (!empty($errors)) { $errorMessage = new CAdminMessage( array( 'DETAILS' => implode('
', $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']; $coupon['DESCRIPTION'] = (string)$coupon['DESCRIPTION']; } else { $coupon['COUNT'] = (int)$coupon['COUNT']; $coupon['COUPON']['DISCOUNT_ID'] = (int)$coupon['COUPON']['DISCOUNT_ID']; $coupon['COUPON']['TYPE'] = (int)$coupon['COUPON']['TYPE']; $coupon['COUPON']['MAX_USE'] = (int)$coupon['COUPON']['MAX_USE']; } if (!empty($errors)) $coupon = array_merge($coupon, $fields); $control->BeginPrologContent(); CJSCore::Init(array('date')); $control->EndPrologContent(); $control->BeginEpilogContent(); echo GetFilterHiddens("filter_");?> EndEpilogContent(); $control->Begin(array( 'FORM_ACTION' => 'sale_discount_coupon_edit.php?lang='.LANGUAGE_ID )); $control->BeginNextFormTab(); if ($multiCoupons) { $control->AddEditField($prefix.'COUNT', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_COUNT'), true, array(), ($coupon['COUNT'] > 0 ? $coupon['COUNT'] : '')); $control->BeginCustomField($prefix.'PERIOD', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_PERIOD'), false); ?> GetCustomLabelHTML(); ?> toString() : ''); $activeTo = ($coupon['COUPON']['ACTIVE_TO'] instanceof Main\Type\DateTime ? $coupon['COUPON']['ACTIVE_TO']->toString() : ''); if ($activeFrom != '' || $activeTo != '') $periodValue = CAdminCalendar::PERIOD_INTERVAL; $calendar = new CAdminCalendar; echo $calendar->CalendarPeriodCustom( $prefix.'ACTIVE_FROM', $prefix.'ACTIVE_TO', $activeFrom, $activeTo, true, 19, true, array( CAdminCalendar::PERIOD_EMPTY => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_PERIOD_EMPTY'), CAdminCalendar::PERIOD_INTERVAL => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_PERIOD_INTERVAL') ), $periodValue ); unset($calendar, $activeTo, $activeFrom, $periodValue); ?> EndCustomField($prefix.'PERIOD'); $control->AddDropDownField( $prefix.'TYPE', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_TYPE'), true, $couponTypes, $coupon['COUPON']['TYPE'] ); $control->AddEditField($prefix.'MAX_USE', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_MAX_USE'), false, array(), ($coupon['COUPON']['MAX_USE'] > 0 ? $coupon['COUPON']['MAX_USE'] : '')); $control->Buttons(false, ''); $control->Show(); ?> 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 = '['.$coupon['DISCOUNT_ID'].']'; if ($coupon['DISCOUNT_NAME'] !== '') $discountName .= ' '.$coupon['DISCOUNT_NAME']; $discountName .= ''; $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); $discountEditPath = 'sale_discount_edit.php?lang='.LANGUAGE_ID.'&return_url='.urlencode($APPLICATION->GetCurPageParam()); ?> GetCustomLabelHTML(); ?> EndCustomField('DISCOUNT_ID'); } unset($discountList); } $control->BeginCustomField('COUPON', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_COUPON'), true); ?> GetCustomLabelHTML(); ?>   EndCustomField('COUPON', '' ); if ($couponID == 0 || $coupon['USE_COUNT'] == 0 || !isset($couponTypes[$coupon['TYPE']])) { $control->AddDropDownField( $prefix.'TYPE', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_TYPE'), true, $couponTypes, $coupon['TYPE'] ); } else { $control->AddViewField( $prefix.'TYPE', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_TYPE'), $couponTypes[$coupon['TYPE']], true ); } $control->BeginCustomField($prefix.'PERIOD', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_PERIOD'), false); ?> GetCustomLabelHTML(); ?> toString() : ''); $activeTo = ($coupon['ACTIVE_TO'] instanceof Main\Type\DateTime ? $coupon['ACTIVE_TO']->toString() : ''); if ($activeFrom != '' || $activeTo != '') $periodValue = CAdminCalendar::PERIOD_INTERVAL; $calendar = new CAdminCalendar; echo $calendar->CalendarPeriodCustom( $prefix.'ACTIVE_FROM', $prefix.'ACTIVE_TO', $activeFrom, $activeTo, true, 19, true, array( CAdminCalendar::PERIOD_EMPTY => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_PERIOD_EMPTY'), CAdminCalendar::PERIOD_INTERVAL => Loc::getMessage('BX_SALE_DISCOUNT_COUPON_PERIOD_INTERVAL') ), $periodValue ); unset($activeTo, $activeFrom, $periodValue); ?> EndCustomField($prefix.'PERIOD'); $control->BeginCustomField($prefix.'USER_ID', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_USER_ID'), false); ?> GetCustomLabelHTML(); ?> 0 ? $coupon['USER_ID'] : ''), '', $couponFormID ); ?> EndCustomField($prefix.'USER_ID'); $control->AddEditField($prefix.'MAX_USE', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_MAX_USE'), false, array(), ($coupon['MAX_USE'] > 0 ? $coupon['MAX_USE'] : '')); $control->AddTextField($prefix.'DESCRIPTION', Loc::getMessage('BX_SALE_DISCOUNT_COUPON_FIELD_DESCRIPTION'), $coupon['DESCRIPTION'], array(), false); if ($subWindow) { $control->Buttons(false, ''); } else { $control->Buttons( array( 'disabled' => $readOnly, 'back_url' => "sale_discount_coupons.php?lang=".LANGUAGE_ID.GetFilterParams('filter_') ) ); } $control->Show(); ?>