/** @global CMain $APPLICATION */
use Bitrix\Main;
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');
Main\Loader::includeModule('sale');
$saleModulePermissions = $APPLICATION->GetGroupRight('sale');
$readOnly = ($saleModulePermissions < 'W');
if ($readOnly)
$APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));
if ($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();
}
IncludeModuleLangFile(__FILE__);
$tabList = array(
array('DIV' => 'edit1', 'ICON' => 'sale', 'TAB' => GetMessage("BT_SALE_DISCOUNT_EDIT_TAB_NAME_COMMON"), 'TITLE' => GetMessage("BT_SALE_DISCOUNT_EDIT_TAB_TITLE_COMMON")),
array('DIV' => 'edit3', 'ICON' => 'sale', 'TAB' => GetMessage("BT_SALE_DISCOUNT_EDIT_TAB_NAME_ACTIONS"), 'TITLE' => GetMessage("BT_CAT_DISCOUNT_EDIT_TAB_TITLE_ACTIONS")),
array('DIV' => 'edit2', 'ICON' => 'sale', 'TAB' => GetMessage("BT_SALE_DISCOUNT_EDIT_TAB_NAME_GROUPS"), 'TITLE' => GetMessage("BT_SALE_DISCOUNT_EDIT_TAB_TITLE_GROUPS")),
array('DIV' => 'edit5', 'ICON' => 'sale', 'TAB' => GetMessage('BX_SALE_DISCOUNT_EDIT_TAB_NAME_COUPONS'), 'TITLE' => GetMessage('BX_SALE_DISCOUNT_EDIT_TAB_TITLE_COUPONS')),
array('DIV' => 'edit4', 'ICON' => 'sale', 'TAB' => GetMessage("BT_SALE_DISCOUNT_EDIT_TAB_NAME_MISC"), 'TITLE' => GetMessage("BT_SALE_DISCOUNT_EDIT_TAB_TITLE_MISC")),
);
$control = new CAdminForm("sale_discount", $tabList);
$couponTypes = Internals\DiscountCouponTable::getCouponTypes(true);
$errors = array();
$boolCondParseError = false;
$boolActParseError = false;
$couponsAdd = false;
$discountID = 0;
$copy = false;
if (isset($_REQUEST['ID']))
{
$discountID = (int)$_REQUEST['ID'];
if ($discountID < 0)
$discountID = 0;
}
if ($discountID > 0)
{
$copy = (isset($_REQUEST['action']) && (string)$_REQUEST['action'] == 'copy');
}
$arFields = array();
if (
check_bitrix_sessid()
&& !$readOnly
&& $_SERVER['REQUEST_METHOD'] == 'POST'
&& isset($_POST['Update']) && (string)$_POST['Update'] == 'Y'
)
{
$obCond3 = new CSaleCondTree();
$boolCond = $obCond3->Init(BT_COND_MODE_PARSE, BT_COND_BUILD_SALE, array('INIT_CONTROLS' => array(
'SITE_ID' => $_POST['LID'],
'CURRENCY' => CSaleLang::GetLangCurrency($_POST['LID']),
)));
if (!$boolCond)
{
if ($ex = $APPLICATION->GetException())
$errors[] = $ex->GetString();
else
$errors[] = (0 < $discountID ? str_replace('#ID#', $discountID, GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_UPDATE')) : GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_ADD'));
}
else
{
$boolCond = false;
if (array_key_exists('CONDITIONS', $_POST) && array_key_exists('CONDITIONS_CHECK', $_POST))
{
if (is_string($_POST['CONDITIONS']) && is_string($_POST['CONDITIONS_CHECK']) && md5($_POST['CONDITIONS']) == $_POST['CONDITIONS_CHECK'])
{
$CONDITIONS = base64_decode($_POST['CONDITIONS']);
if (CheckSerializedData($CONDITIONS))
{
$CONDITIONS = unserialize($CONDITIONS);
$boolCond = true;
}
else
{
$boolCondParseError = true;
}
}
}
if (!$boolCond)
$CONDITIONS = $obCond3->Parse();
if (empty($CONDITIONS))
{
if ($ex = $APPLICATION->GetException())
$errors[] = $ex->GetString();
else
$errors[] = (0 < $discountID ? str_replace('#ID#', $discountID, GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_UPDATE')) : GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_ADD'));
$boolCondParseError = true;
}
}
$obAct3 = new CSaleActionTree();
$boolAct = $obAct3->Init(BT_COND_MODE_PARSE, BT_COND_BUILD_SALE_ACTIONS, array('PREFIX' => 'actrl', 'INIT_CONTROLS' => array(
'SITE_ID' => $_POST['LID'],
'CURRENCY' => CSaleLang::GetLangCurrency($_POST['LID']),
)));
if (!$boolAct)
{
if ($ex = $APPLICATION->GetException())
$errors[] = $ex->GetString();
else
$errors[] = (0 < $discountID ? str_replace('#ID#', $discountID, GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_UPDATE')) : GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_ADD'));
}
else
{
$boolAct = false;
if (array_key_exists('ACTIONS', $_POST) && array_key_exists('ACTIONS_CHECK', $_POST))
{
if (is_string($_POST['ACTIONS']) && is_string($_POST['ACTIONS_CHECK']) && md5($_POST['ACTIONS']) == $_POST['ACTIONS_CHECK'])
{
$ACTIONS = base64_decode($_POST['ACTIONS']);
if (CheckSerializedData($ACTIONS))
{
$ACTIONS = unserialize($ACTIONS);
$boolAct = true;
}
else
{
$boolActParseError = true;
}
}
}
if (!$boolAct)
$ACTIONS = $obAct3->Parse();
if (empty($ACTIONS))
{
if ($ex = $APPLICATION->GetException())
$errors[] = $ex->GetString();
else
$errors[] = (0 < $discountID ? str_replace('#ID#', $discountID, GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_UPDATE')) : GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_ADD'));
$boolActParseError = true;
}
}
$arGroupID = array();
if (array_key_exists('USER_GROUPS', $_POST) && is_array($_POST['USER_GROUPS']))
{
foreach ($_POST['USER_GROUPS'] as &$intValue)
{
$intValue = intval($intValue);
if ($intValue > 0)
{
$arGroupID[] = $intValue;
}
}
if (isset($intValue))
unset($intValue);
}
$arFields = array(
"LID" => (array_key_exists('LID', $_POST) ? $_POST['LID'] : ''),
"NAME" => (array_key_exists('NAME', $_POST) ? $_POST['NAME'] : ''),
"ACTIVE_FROM" => (array_key_exists('ACTIVE_FROM', $_POST) ? $_POST['ACTIVE_FROM'] : ''),
"ACTIVE_TO" => (array_key_exists('ACTIVE_TO', $_POST) ? $_POST['ACTIVE_TO'] : ''),
"ACTIVE" => (array_key_exists('ACTIVE', $_POST) && 'Y' == $_POST['ACTIVE'] ? 'Y' : 'N'),
"SORT" => (array_key_exists('SORT', $_POST) ? $_POST['SORT'] : 500),
"PRIORITY" => (array_key_exists('PRIORITY', $_POST) ? $_POST['PRIORITY'] : ''),
"LAST_DISCOUNT" => (array_key_exists('LAST_DISCOUNT', $_POST) && 'N' == $_POST['LAST_DISCOUNT'] ? 'N' : 'Y'),
"XML_ID" => (array_key_exists('XML_ID', $_POST) ? $_POST['XML_ID'] : ''),
'CONDITIONS' => $CONDITIONS,
'ACTIONS' => $ACTIONS,
'USER_GROUPS' => $arGroupID,
);
if ($discountID == 0 || $copy)
{
$additionalFields = array(
'COUPON_ADD' => (isset($_POST['COUPON_ADD']) && $_POST['COUPON_ADD'] == 'Y' ? 'Y' : 'N'),
'COUPON_COUNT' => (isset($_POST['COUPON_COUNT']) ? (int)$_POST['COUPON_COUNT'] : 0)
);
if ($additionalFields['COUPON_ADD'] == 'Y')
{
if ($additionalFields['COUPON_COUNT'] <= 0)
{
$errors[] = GetMessage('BX_SALE_DISCOUNT_EDIT_ERR_COUPONS_COUNT');
}
$couponsFields = array(
'DISCOUNT_ID' => $discountID,
'ACTIVE' => 'Y'
);
if (isset($_POST['COUPON']))
{
$couponsFields['TYPE'] = (isset($_POST['COUPON']['TYPE']) ? (int)$_POST['COUPON']['TYPE'] : 0);
$couponsFields['ACTIVE_FROM'] = (!empty($_POST['COUPON']['ACTIVE_FROM']) ? new Main\Type\DateTime($_POST['COUPON']['ACTIVE_FROM']) : null);
$couponsFields['ACTIVE_TO'] = (!empty($_POST['COUPON']['ACTIVE_TO']) ? new Main\Type\DateTime($_POST['COUPON']['ACTIVE_TO']) : null);
$couponsFields['MAX_USE'] = (isset($_POST['COUPON']['MAX_USE']) ? (int)$_POST['COUPON']['MAX_USE'] : 0);
}
$couponsResult = Internals\DiscountCouponTable::checkPacket($couponsFields, ($discountID <= 0));
if (!$couponsResult->isSuccess(true))
{
$errors = (empty($errors) ? $couponsResult->getErrorMessages() : array_merge($errors, $couponsResult->getErrorMessages()));
}
unset($couponsResult);
$additionalFields['COUPON'] = $couponsFields;
$couponsAdd = true;
}
}
if (empty($errors))
{
if ($discountID > 0 && !$copy)
{
if (!CSaleDiscount::Update($discountID, $arFields))
{
if ($ex = $APPLICATION->GetException())
$errors[] = $ex->GetString();
else
$errors[] = str_replace('#ID#', $discountID, GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_UPDATE'));
}
}
else
{
$discountID = (int)CSaleDiscount::Add($arFields);
if ($discountID <= 0)
{
if ($ex = $APPLICATION->GetException())
$errors[] = $ex->GetString();
else
$errors[] = GetMessage('BT_SALE_DISCOUNT_EDIT_ERR_ADD');
}
else
{
if ($couponsAdd)
{
$couponsFields['DISCOUNT_ID'] = $discountID;
$couponsResult = Internals\DiscountCouponTable::addPacket(
$couponsFields,
$additionalFields['COUPON_COUNT']
);
if (!$couponsResult->isSuccess())
{
$errors = $couponsResult->getErrorMessages();
}
}
}
}
}
if (empty($errors))
{
if (empty($_POST['apply']))
LocalRedirect("/bitrix/admin/sale_discount.php?lang=".LANGUAGE_ID.GetFilterParams("filter_", false));
else
LocalRedirect("/bitrix/admin/sale_discount_edit.php?lang=".LANGUAGE_ID."&ID=".$discountID.'&'.$control->ActiveTabParam());
}
}
if ($discountID > 0 && !$copy)
$APPLICATION->SetTitle(GetMessage('BT_SALE_DISCOUNT_EDIT_MESS_UPDATE_DISCOUNT', array('#ID#' => $discountID)));
else
$APPLICATION->SetTitle(GetMessage('BT_SALE_DISCOUNT_EDIT_MESS_ADD_DISCOUNT'));
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_after.php");
$defaultValues = array(
'LID' => '',
'NAME' => '',
'CURRENCY' => '',
'DISCOUNT_VALUE' => '',
'DISCOUNT_TYPE' => 'P',
'ACTIVE' => 'Y',
'SORT' => '100',
'ACTIVE_FROM' => '',
'ACTIVE_TO' => '',
'PRIORITY' => 1,
'LAST_DISCOUNT' => 'Y',
'CONDITIONS' => '',
'XML_ID' => '',
'ACTIONS' => '',
);
if (isset($_REQUEST['LID']))
$defaultValues['LID'] = trim($_REQUEST['LID']);
if ('' == $defaultValues['LID'])
$defaultValues['LID'] = 's1';
$defaultCoupons = array(
'COUPON_ADD' => 'N',
'COUPON_COUNT' => '',
'COUPON' => array(
'ACTIVE_FROM' => null,
'ACTIVE_TO' => null,
'COUPON_TYPE' => Internals\DiscountCouponTable::TYPE_ONE_ORDER,
'MAX_USE' => 0
)
);
$arSelect = array_merge(array('ID'), array_keys($defaultValues));
$arDiscount = array();
$arDiscountGroupList = array();
$coupons = $defaultCoupons;
$rsDiscounts = CSaleDiscount::GetList(array(), array("ID" => $discountID), false, false, $arSelect);
if (!($arDiscount = $rsDiscounts->Fetch()))
{
$discountID = 0;
$arDiscount = $defaultValues;
}
else
{
$rsDiscountGroups = CSaleDiscount::GetDiscountGroupList(array(),array('DISCOUNT_ID' => $discountID),false,false,array('GROUP_ID'));
while ($arDiscountGroup = $rsDiscountGroups->Fetch())
{
$arDiscountGroupList[] = intval($arDiscountGroup['GROUP_ID']);
}
}
if (!empty($errors))
{
if ($boolCondParseError || $boolActParseError)
{
$mxTempo = $arDiscount['CONDITIONS'];
$mxTempo2 = $arDiscount['ACTIONS'];
$arDiscount = $arFields;
if ($boolCondParseError)
$arDiscount['CONDITIONS'] = $mxTempo;
if ($boolActParseError)
$arDiscount['ACTIONS'] = $mxTempo2;
unset($mxTempo);
unset($mxTempo2);
}
else
{
$arDiscount = $arFields;
}
$arDiscountGroupList = $arFields['USER_GROUPS'];
if (isset($additionalFields))
$coupons = array_merge($coupons, $additionalFields);
}
$contextMenuItems = array(
array(
"TEXT" => GetMessage("BT_SALE_DISCOUNT_EDIT_MESS_DISCOUNT_LIST"),
"LINK" => "/bitrix/admin/sale_discount.php?lang=".LANGUAGE_ID.GetFilterParams("filter_"),
"ICON" => "btn_list"
)
);
if ($discountID > 0 && $saleModulePermissions == 'W')
{
if (!$copy)
{
$contextMenuItems[] = array("SEPARATOR" => "Y");
$contextMenuItems[] = array(
"TEXT" => GetMessage("BT_SALE_DISCOUNT_EDIT_MESS_NEW_DISCOUNT"),
"LINK" => "/bitrix/admin/sale_discount_edit.php?lang=".LANGUAGE_ID.GetFilterParams("filter_"),
"ICON" => "btn_new"
);
$contextMenuItems[] = array(
"TEXT"=>GetMessage("BT_SALE_DISCOUNT_EDIT_MESS_COPY_DISCOUNT"),
"LINK"=>'/bitrix/admin/sale_discount_edit.php?lang='.LANGUAGE_ID.'&ID='.$discountID.'&action=copy&'.GetFilterParams('filter_'),
"ICON"=>"btn_copy",
);
$contextMenuItems[] = array(
"TEXT" => GetMessage("BT_SALE_DISCOUNT_EDIT_MESS_DELETE_DISCOUNT"),
"LINK" => "javascript:if(confirm('".GetMessageJS("BT_SALE_DISCOUNT_EDIT_MESS_DELETE_DISCOUNT_CONFIRM")."')) window.location='/bitrix/admin/sale_discount.php?lang=".LANGUAGE_ID."&ID=".$discountID."&action=delete&".bitrix_sessid_get()."';",
"WARNING" => "Y",
"ICON" => "btn_delete"
);
}
}
$contextMenu = new CAdminContextMenu($contextMenuItems);
$contextMenu->Show();
unset($contextMenu, $contextMenuItems);
if (!empty($errors))
{
$errorMessage = new CAdminMessage(
array(
"DETAILS" => implode('
', $errors),
"TYPE" => "ERROR",
"MESSAGE" => GetMessage("BT_SALE_DISCOUNT_EDIT_MESS_SAVE_ERROR"),
"HTML" => true
)
);
echo $errorMessage->Show();
unset($errorMessage);
}
$arSiteList = array();
$siteIterator = Main\SiteTable::getList(array(
'select' => array('LID', 'NAME'),
'order' => array('SORT' => 'ASC')
));
while ($site = $siteIterator->fetch())
{
$arSiteList[$site['LID']] = '('.$site['LID'].') '.$site['NAME'];
}
unset($site, $siteIterator);
$control->BeginPrologContent();
CJSCore::Init(array('date'));
$control->EndPrologContent();
$control->BeginEpilogContent();
echo GetFilterHiddens("filter_");?>
if ($copy)
{
?>
}
echo bitrix_sessid_post();
$control->EndEpilogContent();
$control->Begin(array(
"FORM_ACTION" => '/bitrix/admin/sale_discount_edit.php?lang='.LANGUAGE_ID,
));
$control->BeginNextFormTab();
if ($discountID > 0 && !$copy)
$control->AddViewField('ID','ID:',$discountID,false);
$control->AddCheckBoxField("ACTIVE", GetMessage("SDEN_ACTIVE").":", false, "Y", $arDiscount['ACTIVE'] == "Y");
$control->AddDropDownField("LID", GetMessage('SDEN_SITE').':', true, $arSiteList, $arDiscount['LID']);
$control->AddEditField("NAME", GetMessage("BT_SALE_DISCOUNT_EDIT_FIELDS_NAME").":", false, array("size" => 50, "maxlength" => 255), htmlspecialcharsbx($arDiscount['NAME']));
$control->BeginCustomField("PERIOD", GetMessage('SDEN_PERIOD').":",false);
?>