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

$shop = & singleton('shop');

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

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

$param['shop_shops_id'] = to_int($_REQUEST['shop_shops_id']);
$param['shop_currency_id'] = to_int($_REQUEST['shop_currency_id']);
$param['shop_order_discount_name'] = to_str($_REQUEST['shop_order_discount_name']);
$param['shop_order_discount_sum_from'] = to_float($_REQUEST['shop_order_discount_sum_from']);
$param['shop_order_discount_sum_to'] = to_float($_REQUEST['shop_order_discount_sum_to']);
$param['shop_order_discount_type'] = to_int($_REQUEST['shop_order_discount_type']);
$param['shop_order_discount_value'] = to_float($_REQUEST['shop_order_discount_value']);

$param['shop_order_discount_count_from'] = to_int($_REQUEST['shop_order_discount_count_from']);
$param['shop_order_discount_count_to'] = to_int($_REQUEST['shop_order_discount_count_to']);
$param['shop_order_discount_logic_between_elements'] = to_int($_REQUEST['shop_order_discount_logic_between_elements']);

/* Дата */
$date = & singleton('DateClass');
$param['shop_order_discount_active_from'] = $date->datetime_format_sql(to_str($_REQUEST['shop_order_discount_active_from']));
$param['shop_order_discount_active_to'] = $date->datetime_format_sql(to_str($_REQUEST['shop_order_discount_active_to']));
if($param['shop_order_discount_type'] == 0 && ($param['shop_order_discount_value'] > 100 || $param['shop_order_discount_value'] < 0) || $param['shop_order_discount_value'] < 0)
{
	$error_has_occured = true;
	
	show_error_message($GLOBALS['MSG_shops']['error_add_discount_w']);
}
elseif ($OrderDiscount = $shop->InsertOrderDiscount($param))
{
	$admin_forms = new admin_forms();
	$window_id = $admin_forms->GetWindowId();
	?><script type="text/javascript">
	$.appendInput('<?php echo $window_id?>', 'EditAddOrderDiscount', 'shop_order_discount_id', '<?php echo $OrderDiscount?>');
	</script><?php

	if(isset($_REQUEST['shop_order_discount_id']))
	{
		show_message($GLOBALS['MSG_shops']['order_discount_insert_success_edit']);
	} 
	else 
	{
		show_message($GLOBALS['MSG_shops']['order_discount_insert_success_add']);
	}
}
else
{
	if(isset($_REQUEST['shop_order_discount_id']))
	{
		show_error_message($GLOBALS['MSG_shops']['order_discount_insert_error_edit']);
	} 
	else 
	{
		show_error_message($GLOBALS['MSG_shops']['order_discount_insert_error_add']);
	}
}
?>