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

$param['name'] = to_str($_REQUEST['discount_name']);
$date = & singleton('DateClass');
$param['from'] = $date->datetime_format_sql(to_str($_REQUEST['discount_from']));
$param['to'] = $date->datetime_format_sql(to_str($_REQUEST['discount_to']));
$param['percent']= to_float($_REQUEST['discount_percent']);

/* Идентификатор магазина */
$param['shop_shops_id'] = to_int($_REQUEST['shop_shops_id']);

if (isset($_REQUEST['discount_is_active']))
{
	$param['is_active'] = 1;
}
else
{
	$param['is_active'] = 0;
}

if (to_int($param['percent']) > 100 || to_int($param['percent']) < 0) 
{
	$error_was_occured_d = true;	
	
	show_error_message($GLOBALS['MSG_shops']['error_add_discount_w']);
}
elseif ($iDiscount = $shop->InsertDiscount($param))
{
	$admin_forms = new admin_forms();
	$window_id = $admin_forms->GetWindowId();
	?><script type="text/javascript">
	$.appendInput('<?php echo $window_id?>', 'EditAddDiscount', 'edit_discount', '<?php echo $iDiscount?>');
	</script><?php

	if (isset($_REQUEST['edit_discount'])) 
	{
		show_message($GLOBALS['MSG_shops']['success_edit_discount']);
	}
	else 
	{
		show_message($GLOBALS['MSG_shops']['success_add_discount']);
	}
}
else
{
	if (isset($_REQUEST['edit_discount'])) 
	{
		show_error_message($GLOBALS['MSG_shops']['error_edit_discount']);
	}
	else 
	{
		show_error_message($GLOBALS['MSG_shops']['error_add_discount']);
	}
}
?>