<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Обработчик добавления/редактирования купона.
 * 
 * Файл: /admin/shop/action/add_coupon.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
$param = array();
$shop = & singleton('shop');

/* Проверяем существует ли идентификатор */
if (isset($_REQUEST['shop_coupon_id']))
{
	$param['shop_coupon_id'] = to_int($_REQUEST['shop_coupon_id']);
}

/* Устанавливаем параметры */
if (isset($_REQUEST['shop_coupon_active']))
{
	$param['shop_coupon_active'] = 1;
}
else
{
	$param['shop_coupon_active'] = 0;
}

$param['shop_order_discount_id'] = to_int($_REQUEST['shop_order_discount_id']);
$param['shop_coupon_name'] = to_str($_REQUEST['shop_coupon_name']);
$param['shop_coupon_count'] = to_int($_REQUEST['shop_coupon_count']);
$param['shop_coupon_text'] = to_str($_REQUEST['shop_coupon_text']);

if ($param['shop_order_discount_id'] == 0)
{
	$error_was_occured_wr = true;

	$returned = 'error';

	show_error_message($GLOBALS['MSG_shops']['error_add_coupon_w']);
}
elseif ($param['shop_order_discount_id'] != 0)
{
	$returned = $shop->InsertCoupon($param);
	
	$admin_forms = new admin_forms();
	$window_id = $admin_forms->GetWindowId();
	?><script type="text/javascript">
	$.appendInput('<?php echo $window_id?>', 'EditAddCoupon', 'shop_coupon_id', '<?php echo $returned?>');
	</script><?php

}
else
{
	$returned = 0;
}
if (($returned != 'error') && to_int($returned) > 0)
{
	if (isset($_REQUEST['shop_coupon_id']))
	{
		show_message($GLOBALS['MSG_shops']['coupon_insert_success_edit']);
	}
	else
	{
		show_message($GLOBALS['MSG_shops']['coupon_insert_success_add']);
	}
}
else
{
	if ($returned == -1)
	{
		show_error_message($GLOBALS['MSG_shops']['coupon_insert_error_already_exists']);
	}
	else
	{
		if ($returned != 'error')
		{
			if (isset($_REQUEST['shop_coupon_id']))
			{
				show_error_message($GLOBALS['MSG_shops']['coupon_insert_error_edit']);
			}
			else
			{
				show_error_message($GLOBALS['MSG_shops']['coupon_insert_error_add']);
			}

		}

	}
}
?>