<?php

if (class_exists('SiteUsers'))
{
	$shop = & singleton('shop');
	
	// Обработка принятия заказа от платежной системы
	// Получаем информацию о текущем пользователе
	if (class_exists('SiteUsers'))
	{
		$SiteUsers = & singleton('SiteUsers');
		$site_user_id = $SiteUsers->GetCurrentSiteUser();
	}
	else
	{
		$site_user_id = 0;
	}
	
	$shop_shops_id = end($GLOBALS['URL_ARRAY']);
	
	// Необходимо получить валюту магазина
	$shop_row = $shop->GetShop($shop_shops_id);
	$shop_currency_row = $shop->GetCurrency($shop_row['shop_currency_id']);
	$shop_currency_id = $shop_currency_row['shop_currency_id'];
	$shop_currency_name = $shop_currency_row['shop_currency_name'];

	if ($site_user_id > 0)
	{
		if(!isset($_REQUEST['apply']))
		{
			$aInfoSiteUser = $SiteUsers->GetSiteUser($site_user_id);
						
			$external_propertys[] = array('xml_name' => 'site_users_name', 'value' => $_SESSION['site_users_name'] = to_str($aInfoSiteUser['site_users_name']));
			$external_propertys[] = array('xml_name' => 'site_users_surname', 'value' => $_SESSION['site_users_surname'] = to_str($aInfoSiteUser['site_users_surname']));
			$external_propertys[] = array('xml_name' => 'site_users_patronymic', 'value' => $_SESSION['site_users_patronymic'] = to_str($aInfoSiteUser['site_users_patronymic']));
			$external_propertys[] = array('xml_name' => 'site_users_patronymic', 'value' => to_str($aInfoSiteUser['site_users_patronymic']));
			$external_propertys[] = array('xml_name' => 'site_users_email', 'value' => $_SESSION['site_users_email'] = to_str($aInfoSiteUser['site_users_email']));
			$external_propertys[] = array('xml_name' => 'site_users_phone', 'value' => $_SESSION['site_users_phone'] = to_str($aInfoSiteUser['site_users_phone']));
			$external_propertys[] = array('xml_name' => 'site_users_fax', 'value' => $_SESSION['site_users_fax'] = to_str($aInfoSiteUser['site_users_fax']));
			$external_propertys[] = array('xml_name' => 'site_users_address', 'value' => $_SESSION['site_users_address'] = to_str($aInfoSiteUser['site_users_address']));
			$external_propertys[] = array('xml_name' => 'site_users_company', 'value' => $_SESSION['site_users_company'] = to_str($aInfoSiteUser['site_users_company']));
			$external_propertys[] = array('xml_name' => 'site_users_postcode', 'value' => $_SESSION['index'] = to_str($aInfoSiteUser['site_users_postcode']));
			//$external_propertys[] = array('xml_name' => 'options', 'value' => $aOptions);
			
			?><form action="./" method="post"><?php
			$shop->ShowAddress(to_str($GLOBALS['LA']['xsl_delivery_address']), $shop_shops_id, array('show_country' => true, 'show_location' => false, 'show_city' => false, 'show_city_area' => false), $external_propertys);
			$shop->ShowSystemOfPay(to_str($GLOBALS['LA']['xsl_system_of_pay']), array(), array('shop_shops_id' => $shop_shops_id));
			?></form><?php
		}
		else
		{
			// Сбрасываем информацию о последнем заказе
			$_SESSION['last_order_id'] = 0;
		
			$new_order_id = $shop->InsertOrder(array(
			'shop_shops_id' => $shop_shops_id,
			'location_id' => to_int($_REQUEST['location']),
			'country_id' => to_int($_REQUEST['country']),
			'shop_city_id' => to_int($_REQUEST['sel_city']),
			'site_user_id' => $site_user_id,
			'currency_id' => $shop_currency_id,
			'shop_system_of_pay_id' => to_int($_REQUEST['system_of_pay_id']),
			'order_date_time' => date("Y-m-d H:i:s"),
			'status_of_pay' => 0,
			'address' => to_str($_REQUEST['full_address']),
			'index' => to_str($_REQUEST['index']),
			'phone' => to_str($_REQUEST['site_users_phone']),
			'description' => to_str($_REQUEST['description']),
			'shop_order_users_name' => to_str($_REQUEST['site_users_name']),
			'shop_order_users_surname' => to_str($_REQUEST['site_users_surname']),
			'shop_order_users_patronymic' => to_str($_REQUEST['site_users_patronymic']),
			'shop_order_users_email' => to_str($_REQUEST['site_users_email']),
			'shop_order_users_company' => to_str($_REQUEST['site_users_company']),
			'shop_order_users_fax' => to_str($_REQUEST['site_users_fax']),
			'system_information' => "",
			'users_id' => false
			));
			
			$SiteUsers = & singleton('SiteUsers');
			
			// Получаем данные пользователя
			$site_user_row = $SiteUsers->GetSiteUser($site_user_id);
			
			// Получаем данные магазина
			$shop_row = $shop->GetShop($shop_shops_id);
			
			$summ = to_float($_REQUEST['summ']);
			
			if ($summ < 0)
			{
				$summ = 0;
			}
			
			// Необходимо добавить товар к заказу
			$shop->InsertOrderItems(array(
			'shop_items_catalog_item_id' => 0, // У нас уникальный товар, поэтому связи с товарами нет 
			'shop_order_id' => $new_order_id, 
			'shop_order_items_quantity' => 1, 
			'shop_order_items_type' => 2, // тип товара - пополнение счета
			'shop_order_items_price' => $summ, 
			'shop_order_items_name' => sprintf("Пополнение лицевого счета пользователя %s в магазине \"%s\"", $site_user_row['site_users_login'], $shop_row['shop_shops_name'])
			));
			
			/* Пишем информацию об адресе доставки в сессию */
			$_SESSION['country'] = to_int($_REQUEST['country']);
			$_SESSION['location'] = to_int($_REQUEST['location']);
			$_SESSION['city'] = to_int($_REQUEST['sel_city']);
			$_SESSION['index'] = to_str($_REQUEST['index']);
			$_SESSION['full_address'] = to_str($_REQUEST['full_address']);
		
			$_SESSION['site_users_name'] = to_str($_REQUEST['site_users_name']);
			$_SESSION['site_users_surname'] = to_str($_REQUEST['site_users_surname']);
			$_SESSION['site_users_patronymic'] = to_str($_REQUEST['site_users_patronymic']);
			$_SESSION['site_users_email'] = to_str($_REQUEST['site_users_email']);
			$_SESSION['site_users_phone'] = to_str($_REQUEST['site_users_phone']);
			$_SESSION['site_users_fax'] = to_str($_REQUEST['site_users_fax']);
			$_SESSION['site_users_company'] = to_str($_REQUEST['site_users_company']);
			$_SESSION['description'] = to_str($_POST['description']);
			$_SESSION['last_order_id'] = $new_order_id;
			$_SESSION['send_mail'] = true;
			$GLOBALS['shop_id'] = $shop_shops_id;
			
			$shop->ExecSystemsOfPayHandler($_REQUEST['system_of_pay_id']);
		}
	}
}
else
{
	?>
	<h1>Пользователи сайта</h1>
	<p>Функционал недоступен, приобретите более старшую редакцию.</p>
	<p>Модуль &laquo;<a href="http://www.hostcms.ru/hostcms/modules/users/">Пользователи сайта</a>&raquo; доступен в редакциях &laquo;<a href="http://www.hostcms.ru/hostcms/editions/corporation/">Корпорация</a>&raquo; и &laquo;<a href="http://www.hostcms.ru/hostcms/editions/business/">Бизнес</a>&raquo;.</p> 
	<?php
}
?>