/** @global CMain $APPLICATION */
use Bitrix\Main;
use Bitrix\Main\Loader;
use Bitrix\Main\Localization\Loc;
use Bitrix\Sale\Internals;
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_before.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/sale/prolog.php');
$subWindow = defined('BX_PUBLIC_MODE') && BX_PUBLIC_MODE == 1;
$prefix = ($subWindow ? 'COUPON_' : '');
$saleModulePermissions = $APPLICATION->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)
{
?>
die();
}
else
{
if (!empty($_POST['apply']))
LocalRedirect('sale_discount_coupon_edit.php?lang='.LANGUAGE_ID.'&ID='.$couponID.GetFilterParams('filter_', false));
else
LocalRedirect('sale_discount_coupons.php?lang='.LANGUAGE_ID.'&'.$control->ActiveTabParam().GetFilterParams('filter_', false));
}
}
}
elseif ($subWindow)
{
if (!empty($_REQUEST['dontsave']))
{
?>
die();
}
}
$APPLICATION->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_");?>
if ($subWindow)
{
?>
}
if ($copy)
{
?>
}
if (!empty($returnUrl))
{
?>
}
echo bitrix_sessid_post();
$control->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);
?>