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

if (isset($_REQUEST['edit_order']))
{
	$param['id'] = to_str($_REQUEST['edit_order']);
}

$date = & singleton('DateClass');
$param['order_date_time'] = $date->datetime_format_sql(to_str($_REQUEST['order_date_time']));
$param['sum'] = to_float($_REQUEST['order_sum']);

$param['status_of_pay'] = isset($_REQUEST['order_status_of_pay']);

if ($param['status_of_pay'])
{
	$param['date_of_pay'] = $date->datetime_format_sql(to_str($_REQUEST['order_date_of_pay']));
}
else 
{
	$param['date_of_pay'] = "0000-00-00 00:00:00";
}

$param['address'] = to_str($_REQUEST['order_address']);
$param['index'] = to_str($_REQUEST['order_index']);
$param['currency_id'] = to_str($_REQUEST['currency_id']);
$param['country_id'] = to_str($_REQUEST['country_id']);
$param['shop_city_id'] = to_str($_REQUEST['city_id']);
$param['city_area_id'] = to_str($_REQUEST['city_area_id']);
$param['phone'] = to_str($_REQUEST['order_phone']);
$param['shop_shops_id'] = to_int($_REQUEST['shop_shops_id']);
$param['location_id'] = to_int($_REQUEST['location_id']);
$param['site_user_id'] = to_int($_REQUEST['site_users_id']);
$param['shop_currency_id'] = to_int($_REQUEST['order_currency']);
$param['shop_system_of_pay_id'] = to_int($_REQUEST['system_of_pay']);
$param['shop_cond_of_delivery_id'] = to_int($_REQUEST['shop_cond_of_delivery_id']);
$param['shop_type_of_delivery_id'] = to_int($_REQUEST['shop_type_of_delivery_id']);
$param['shop_order_status_id'] = to_int($_REQUEST['shop_order_status_id']);
if (isset($_REQUEST['order_cancel_status']))
{
	$shop_order_cancel = 1;
}
else
{
	$shop_order_cancel = 0;
}
$param['shop_order_cancel'] = $shop_order_cancel;
$param['description'] = to_str($_REQUEST['shop_order_description']);
$param['system_information'] = to_str($_REQUEST['shop_order_system_information']);

// Данные о заказчике.
$param['shop_order_users_name'] = to_str($_REQUEST['shop_order_users_name']);
$param['shop_order_users_surname'] = to_str($_REQUEST['shop_order_users_surname']);
$param['shop_order_users_patronymic'] = to_str($_REQUEST['shop_order_users_patronymic']);
$param['shop_order_users_email'] = to_str($_REQUEST['shop_order_users_email']);
$param['shop_order_users_company'] = to_str($_REQUEST['shop_order_users_company']);
$param['shop_order_users_fax'] = to_str($_REQUEST['shop_order_users_fax']);

$param['shop_order_account_number'] = to_str($_REQUEST['shop_order_account_number']);

if ($shop_order_id = $shop->InsertOrder($param))
{
	$order_row = $shop->GetOrder($shop_order_id);
	
	if (isset($_REQUEST['order_status_of_pay']))
	{
		// Получаем информацию о магазине
		$shop_shops_id = to_int($order_row['shop_shops_id']);

		$shop_row = $shop->GetShop($shop_shops_id);

		$structure = & singleton('Structure');
		$structure_row = $structure->GetStructureItem(to_int($shop_row['structure_id']));

		$lib = new lib();
		$LA = $lib->LoadLibPropertiesValue(to_int($structure_row['lib_id']), to_int($structure_row['structure_id']));

		if ($order_row)
		{
			$DateClass = new DateClass();
			$date_str = $DateClass->datetime_format($order_row['shop_order_date_time']);
		}
		else
		{
			$date_str = '';
		}

		$shop->SendMailAboutOrder($order_row['shop_shops_id'],
		$shop_order_id,
		$order_row['site_users_id'],
		to_str($LA['xsl_letter_to_admin']),
		to_str($LA['xsl_letter_to_user']),
		$order_row['shop_order_users_email'],
		array(
		'admin-content-type' => 'html',
		'user-content-type' => 'html',
		'admin-subject' => sprintf($GLOBALS['MSG_shops']['shop_order_confirm_admin_subject'], $shop_order_id, $shop_row['shop_shops_name'], $date_str),
		'user-subject' => sprintf($GLOBALS['MSG_shops']['shop_order_confirm_user_subject'], $shop_order_id, $shop_row['shop_shops_name'], $date_str)));
	}
	
	// Вызываем обработчик платежной системы для события "редактирование"
	$shop->ExecSystemsOfPayChangeStatus($order_row['shop_system_of_pay_id'], array('shop_order_id' => $shop_order_id, 'action' => 'edit'));
	?>
	<script>
	var ElementInput = document.createElement("input");
	ElementInput.setAttribute("type", "hidden");
	ElementInput.setAttribute("name", "edit_order");
	ElementInput.setAttribute("value", "<?php echo $shop_order_id?>");

	document.getElementById('EditAddOrder').appendChild(ElementInput);
	</script>
	<?php 
	if (isset($_REQUEST['edit_order']))
	{
		show_message($GLOBALS['MSG_shops']['success_edit_order']);
	}
	else
	{
		show_message($GLOBALS['MSG_shops']['success_add_order']);
	}
}
else
{
	show_error_message($GLOBALS['MSG_shops']['error_add_order']);
}

?>