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

$shop = & singleton('shop');
$DateClass = & singleton('DateClass');
$site = & singleton('site');
$DataBase = & singleton('DataBase');

if (class_exists("SiteUsers"))
{
	$SiteUsers = & singleton('SiteUsers');
}

// Получаем данные о заказе
$order_row = $shop->GetOrder(to_int($_REQUEST['show_order_card']));

// Получили данные о заказе 
if ($order_row)
{
	$shop_order_account_number = " № " . $order_row['shop_order_account_number'];
	
	$shop_order_date_time = $GLOBALS['MSG_shops']['order_card_date_from'] . $DateClass->date_format($order_row['shop_order_date_time'], "%d.%m.%Y") . " г.";

	$site_users_login = '';
	$user_data_address = '';
	$site_users_id = 0;
	
	if (class_exists("SiteUsers"))
	{
		// Получаем данные о плательщике
		$site_user_row = $SiteUsers->GetSiteUser($order_row['site_users_id']);
		
		if ($site_user_row)
		{
			$site_users_login = $site_user_row['site_users_login'];
			$site_users_id = to_int($site_user_row['site_users_id']); 
		}
	}
	
	// Получаем данные о платежной системе
	$shop_system_of_pay_id = to_int($order_row['shop_system_of_pay_id']);
	
	$shop_system_of_pay_row = $shop->GetSystemOfPay($shop_system_of_pay_id);
	
	if ($shop_system_of_pay_row)
	{
		// Получили данные о платежной системе, получаем ее имя
		$shop_system_of_pay_name = $shop_system_of_pay_row['shop_system_of_pay_name'];
	}
	else
	{
		$shop_system_of_pay_name = '';
	}
	
	// Получаем статус заказа
	$shop_order_status_of_pay = to_int($order_row['shop_order_status_of_pay']);
	
	// Получаем статус отмены заказа
	$shop_order_cancel = to_int($order_row['shop_order_cancel']);
	
	// Получаем статус заказа
	$shop_order_status_id = to_int($order_row['shop_order_status_id']);
	
	$shop_order_status_row = $shop->GetOrdersStatus($shop_order_status_id);
	
	// Получаем название заказа
	if ($shop_order_status_row)
	{
		$shop_order_status_name = $shop_order_status_row['shop_order_status_name'];
	}
	else
	{
		$shop_order_status_name = '';
	}
	
	// Получаем дату изменения статуса заказа
	$shop_order_change_status_datetime = " (" . $DateClass->datetime_format($order_row['shop_order_change_status_datetime']) . ")";
	
	// Получаем условие доставки
	$shop_cond_of_delivery_id = to_int($order_row['shop_cond_of_delivery_id']);
	
	// По условию доставки получаем тип доставки
	if ($shop_cond_of_delivery_id)
	{
		$shop_delivery_data = $shop->GetCondOfDelivery($shop_cond_of_delivery_id, true);

		if ($shop_delivery_data)
		{
			$shop_type_of_delivery_name = $shop_delivery_data['shop_type_of_delivery_name'];
			
			$shop_cond_of_delivery_name = $shop_delivery_data['shop_cond_of_delivery_name'];
		}
		else
		{
			$shop_type_of_delivery_name = '';
			
			$shop_cond_of_delivery_name = '';
		}
	}
	
	// Получаем описание заказа
	$shop_order_description = $order_row['shop_order_description'];
	
	// Получаем информацию о заказе
	$shop_order_system_information = $order_row['shop_order_system_information'];
	
	// Получаем данные о пользователе из таблицы заказа
	$shop_order_users_company = $order_row['shop_order_users_company'];

	$shop_order_users_name  = $order_row['shop_order_users_name'];
	
	$shop_order_users_surname = $order_row['shop_order_users_surname'];

	$shop_order_users_patronymic = $order_row['shop_order_users_patronymic'];
	
	$shop_order_phone = $order_row['shop_order_phone'];
	
	$shop_order_users_fax = $order_row['shop_order_users_fax'];
	
	$shop_order_users_email = $order_row['shop_order_users_email'];
	
	$address_array = array();
	
	// Получаем индекс
	$shop_order_index = $order_row['shop_order_index'];
	
	if ($shop_order_index != '')
	{
		$address_array[] = $shop_order_index;
	}
	
	// Получаем страну
	$shop_country_row = $shop->GetCountry($order_row['shop_country_id']);
	
	if ($shop_country_row && $shop_country_row['shop_country_name'] != '')
	{
		$address_array[] = $shop_country_row['shop_country_name'];
	}
	
	// Получаем область
	$shop_location_row = $shop->GetLocation($order_row['shop_location_id']);
	
	if ($shop_location_row && $shop_location_row['shop_location_name'] != '')
	{
		$address_array[] = $shop_location_row['shop_location_name'];
	}
	
	// Получаем город
	$shop_city_row = $shop->GetCity($order_row['shop_city_id']);
	
	if ($shop_city_row && $shop_city_row['shop_city_name'] != '')
	{
		$address_array[] = $shop_city_row['shop_city_name'];
	}
	
	// Получаем район
	$shop_city_area_row = $shop->GetCityArea($order_row['shop_city_area_id']);
	
	if ($shop_city_area_row && $shop_city_area_row['shop_city_area_name'])
	{
		$address_array[] = $shop_city_area_row['shop_city_area_name'];
	}
	
	// Получаем остальной адрес
	$shop_order_address = $order_row['shop_order_address'];
	
	if ($shop_order_address != '')
	{
		$address_array[] = $shop_order_address;
	}
	
	// Собираем адрес
	$full_address = implode(", ", $address_array);

// Получаем данные о магазине
$shop_row = $shop->GetShop($order_row['shop_shops_id']);

// Данные о магазине получены
if ($shop_row)
{
	// Получаем данные о компании
	$company_row = $shop->GetCompany($shop_row['shop_company_id']);
	$shop_company_name = $company_row['shop_company_name'];
	$shop_company_address = $company_row['shop_company_address'];
	$shop_company_phone = $company_row['shop_company_phone'];
	$shop_company_site = $company_row['shop_company_site'];
	$shop_company_email = $company_row['shop_company_email'];
	
	// Получаем валюту магазина
	$shop_currency_id = to_int($shop_row['shop_currency_id']);
	
	// Получаем данные о валюте
	$currency_row = $shop->GetCurrency($shop_currency_id);
	
	if ($currency_row)
	{
		$shop_currency_name = $currency_row['shop_currency_name'];
	}
	else
	{
		$shop_currency_name = '';
	}
	
	$shop_company_inn = $company_row['shop_company_inn'];
	$shop_company_kpp = $company_row['shop_company_kpp'];
	$shop_company_ogrn = $company_row['shop_company_ogrn'];
	$shop_company_fax = $company_row['shop_company_fax'];
}
// нет данных о магазине
else
{
	// Невозможно получить данные о компании
	$shop_company_name = '';
	$shop_company_address = '';
	$shop_company_phone = '';
	$shop_company_site = '';
	$shop_company_email = '';
	$shop_currency_name = '';
	$shop_company_inn = '';
	$shop_company_kpp = '';
	$shop_company_ogrn = '';
	$shop_company_fax = '';
}

if (defined('SHOP_ORDER_CARD_XSL'))
{
	header("Content-type: text/html; charset=UTF-8");
	
	$xmlData = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
	$xmlData .= "<order>\n";

	$xmlData .= $shop->GetXmlForOrder(0, $order_row, false);

	/* Выводим информацию о пользователе сайта*/
	$xmlData .= '<site_user_id>'.$site_users_id.'</site_user_id>'."\n";

	if (class_exists('SiteUsers'))
	{
		/* Получаем xml для пользователя*/
		$SiteUsers = & singleton('SiteUsers');
		$xmlData .= $SiteUsers->GetSiteUserXml($site_users_id);
	}

	$xmlData .= "</order>\n";

	$xsl = & singleton('xsl');
	$result = $xsl->build($xmlData, SHOP_ORDER_CARD_XSL);
	echo $result;
}
else
{
	//Формируем html-код страницы
	?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><?php echo $GLOBALS['MSG_shops']['order_card'] . $shop_order_account_number?></title>
<meta http-equiv="Content-Language" content="ru">
<meta content="text/html; charset=UTF-8" http-equiv=Content-Type>
<link rel="stylesheet" type="text/css" href="shop.php_files/style.css">

<style type="text/css">html, body, td
	{
		font-family: Arial, Verdana, Tahoma, sans-serif;
		font-size: 9pt;
		background-color: #FFFFFF;
		color: #000000;
	}

	.main_div
	{
		margin-left: 0.5em;
		margin-right: 0.5em;
		margin-top: 2em;
		margin-bottom: 1em;
	}
	
	.td_main
	{
		border-top: black 1px solid;
		border-left: black 1px solid;
	}
	
	.td_header
	{
		border-left: black 1px solid;
		border-top: black 1px solid;
		border-bottom: black 1px solid;
		text-align: center;
		font-weight: bold;
	}
	
	.td_main_2
	{
		border-left: black 1px solid;
		border-bottom: black 1px solid;
	}

	.tr_footer td
	{
		font-size: 11pt;
		font-weight: bold;
		white-space: nowrap;
	}
	
	table, td
	{
		empty-cells: show;
	}
</style>
</head>
<body style="margin: 3.5em">

<p style="margin-bottom: 40px"><img src="/admin/images/logo.gif" alt="(^) <?php echo PRODUCT_NAME?>" title="<?php echo PRODUCT_NAME?>"></p>

<table cellpadding="2" cellspacing="2" border="0" width="100%">
<tr>
		<td valign="top" width="17%">
			<?php echo $GLOBALS['MSG_shops']['order_card_supplier'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_company_name?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_inn_kpp'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_company_inn."/".$shop_company_kpp?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_ogrn'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_company_ogrn?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_address'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_company_address?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_phone'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_company_phone?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_fax'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_company_fax?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_email'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_company_email?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_site'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_company_site?>
			</b>
		</td>
	</tr>
</table>

<h2 align="center"><?php echo $GLOBALS['MSG_shops']['order_card'] . $shop_order_account_number . $shop_order_date_time?></h2>

<table cellpadding="2" cellspacing="2" border="0" width="100%">  
	<tr>
		<td valign="top" width="17%">
			<?php echo $GLOBALS['MSG_shops']['payer'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_order_users_company?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_contact_person'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_order_users_surname . " " . $shop_order_users_name . " " . $shop_order_users_patronymic?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_address'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $full_address?>
			</b>
		</td>
	</tr>
	<?php
	if (class_exists("SiteUsers"))
	{
	?>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_site_user'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $site_users_login . " (" . $GLOBALS['MSG_shops']['order_card_site_user_id'] . " " . $site_users_id . ")"?>
			</b>
		</td>
	</tr>
	
	<?php
	}
	?>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_site_user_phone'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_order_phone?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_site_user_fax'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_order_users_fax?>
			</b>
		</td>
	</tr>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_site_user_email'] . ":"?>
		</td>
		<td valign="top">
			<b>
				<?php echo $shop_order_users_email?>
			</b>
		</td>
	</tr>
</table>
<br>
<table cellspacing="0" cellpadding="3" width="100%">
<tr>
	<td class="td_header">
		<?php echo "№"?>
	</td>
	<td class="td_header">
		<?php echo $GLOBALS['MSG_shops']['table_description']?>
	</td>
	<td class="td_header">
		<?php echo $GLOBALS['MSG_shops']['table_mark']?>
	</td>
	<td class="td_header">
		<?php echo $GLOBALS['MSG_shops']['table_mesures']?>
	</td>
	<td class="td_header">
		<?php echo $GLOBALS['MSG_shops']['table_price'] . ", " . $shop_currency_name?>
	</td>
	<td class="td_header">
		<?php echo $GLOBALS['MSG_shops']['table_amount']?>
	</td>
	<td class="td_header">
		<?php echo $GLOBALS['MSG_shops']['table_nds_tax']?>
	</td>
	<td class="td_header">
		<?php echo $GLOBALS['MSG_shops']['table_nds_value'] . ", " . $shop_currency_name?>
	</td>
	<td class="td_header" style="border-right: 1px solid black; white-space: nowrap;">
		<?php echo $GLOBALS['MSG_shops']['table_amount_value'] . ", " . $shop_currency_name?>
	</td>
</tr>
<?php
// Получаем список товаров заказа (если получили данные о заказе)
if ($order_row === false)
{
	exit();
}
else
{
	$order_items_res = $shop->GetOrderItems($order_row['shop_order_id']);
	
	$i = 1;
	
	if ($order_items_res)
	{
		$shop_tax_value_sum = 0.0;
		
		$shop_order_items_price_sum = 0.0;
	
		while($order_items_row = mysql_fetch_assoc($order_items_res))
		{
			// Получаем информацию о товаре
			$item_row = $shop->GetItem($order_items_row['shop_items_catalog_item_id']);
			
			if ($item_row)
			{
				// Получаем информацию о еденице измерения
				$mesure_row = $shop->GetMesure($item_row['shop_mesures_id']);
				
				// Получаем название единицы измерения
				$shop_mesures_name = $mesure_row['shop_mesures_name'];
			}
			else
			{
				// Товар не выбран
				$shop_mesures_name = '';
			}
			
			// Получаем инормацию о налоге из таблицы товаров заказа
			$shop_tax_rate = $order_items_row['shop_tax_rate'];
			
			// Цена
			$shop_order_items_price = $shop->Round(to_float($order_items_row['shop_order_items_price']) * to_float($order_items_row['shop_order_items_quantity']));
			
			if ($shop_tax_rate)
			{
				//$shop_tax_rate = $shop_tax_rate . "%";
				$shop_tax_value = $shop->Round(to_int($order_items_row['shop_order_items_quantity']) * $order_items_row['shop_order_items_price'] / (100 + to_float($order_items_row['shop_tax_rate'])) * to_float($order_items_row['shop_tax_rate']));
			}
			else
			{
				$shop_tax_rate = "&mdash;";
				$shop_tax_value = "&mdash;";
			}
			
			// Суммируем налоги
			$shop_tax_value_sum += to_float($shop_tax_value);
			
			// Суммируем цены
			$shop_order_items_price_sum += to_float($shop_order_items_price);
			?>
				<tr>
					<td style="text-align: center;" class="td_main_2" >
					<?php echo $i++?>
					</td>
					<td class="td_main_2">
					<?php echo $order_items_row['shop_order_items_name']?>
					</td>
					<td class="td_main_2">
					<?php echo $order_items_row['shop_order_items_marking']?>
					</td>
					<td class="td_main_2">
					<?php echo $shop_mesures_name?>
					</td>
					<td class="td_main_2">
					<?php echo ((to_int($shop_tax_rate) != 0 && $order_items_row['shop_order_items_quantity'] != 0)
					? $shop->Round(to_float($order_items_row['shop_order_items_price']) - to_float($shop_tax_value) / $order_items_row['shop_order_items_quantity'])
					: $order_items_row['shop_order_items_price'])?>
					</td>
					<td style="text-align: center;" class="td_main_2">
					<?php echo $order_items_row['shop_order_items_quantity']?>
					</td>
					<td style="text-align: center;" class="td_main_2">
					<?php echo ((to_int($shop_tax_rate) != 0)
					? "{$shop_tax_rate}%"
					: $shop_tax_rate)?>
					</td>
					<td style="text-align: center;" class="td_main_2">
					<?php echo $shop_tax_value?>
					</td>
					<td class="td_main_2" style="border-right: 1px solid black; white-space: nowrap;">
					<?php echo $shop_order_items_price?>
					</td>
				</tr>
			<?php
		}
	}
}
?>
</table>
<table width="100%" cellspacing="0" cellpadding="3">
<tr class="tr_footer">
	<td width="80%" align="right" style="border-bottom: 1px solid black;" colspan="6">
		<?php echo $GLOBALS['MSG_shops']['table_nds']?>
	</td>
	<td width="80%" align="right"  style="border-bottom: 1px solid black;" colspan="2">
		<?php echo $shop->Round($shop_tax_value_sum) . " " . $shop_currency_name?>
	</td>
</tr>
<tr class="tr_footer">
	<td align="right" colspan="6">
		<?php echo $GLOBALS['MSG_shops']['table_all_to_pay']?>
	</td>
	<td align="right" colspan="2">
		<?php echo $shop->Round($shop_order_items_price_sum) . " " . $shop_currency_name?>
	</td>
</tr>
</table>

<table cellpadding="2" cellspacing="2" border="0"  width="100%">
<tr>
	<td valign="top" width="17%">
		<?php echo $GLOBALS['MSG_shops']['order_card_system_of_pay'] . ": "?>
	</td>
	<td valign="top">
		<b><?php echo $shop_system_of_pay_name?></b>
	</td>
</tr>
<tr>
	<td valign="top">
		<?php echo $GLOBALS['MSG_shops']['order_card_status_of_pay'] . ": "?>
	</td>
	<td valign="top">
		<?php 
		if ($shop_order_status_of_pay == 1)
		{
			echo "<b>" . $GLOBALS['MSG_shops']['order_card_status_of_pay_yes'] . "</b>";
		}
		else
		{
			echo $GLOBALS['MSG_shops']['order_card_status_of_pay_no'];
		}
		?>
	</td>
</tr>
<tr>
	<td valign="top">
		<?php echo  $GLOBALS['MSG_shops']['order_card_cancel'] . ": "?>
	</td>
	<td valign="top">
		<?php 
		if ($shop_order_cancel == 1)
		{
			echo "<b>" . $GLOBALS['MSG_shops']['order_card_cancel_yes'] . "</b>";
		}
		else
		{
			echo $GLOBALS['MSG_shops']['order_card_cancel_no'];
		}
		
		//(($shop_order_cancel == 1)?$string = "<b>" . $GLOBALS['MSG_shops']['order_card_cancel_yes'] . "</b>/" . $GLOBALS['MSG_shops']['order_card_cancel_no'] :$string = $GLOBALS['MSG_shops']['order_card_cancel_yes'] . "/<b>" . $GLOBALS['MSG_shops']['order_card_cancel_no'] . "</b>"); echo $string
		?>
	</td>
</tr>
<?php
if ($shop_order_status_id > 0)
{
	?>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_order_status'] . ": "?>
		</td>
		<td valign="top">
			<b><?php echo $shop_order_status_name . $shop_order_change_status_datetime?></b>
		</td>
	</tr>
	<?php
}
if ($shop_cond_of_delivery_id > 0)
{
	?>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_type_of_delivery'] . ": "?>
		</td>
		<td valign="top">
			<b><?php echo $shop_type_of_delivery_name . " (" . $shop_cond_of_delivery_name . ")"?></b>
		</td>
	</tr>
	<?php
}
if ($shop_order_description != '')
{
	?>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_description'] . ": "?>
		</td>
		<td>
			<?php echo nl2br($shop_order_description)?>
		</td>
	</tr>
	<?php
}
if ($shop_order_system_information != '')
{
	?>
	<tr>
		<td valign="top">
			<?php echo $GLOBALS['MSG_shops']['order_card_information'] . ": "?>
		</td>
		<td>
			<?php echo nl2br($shop_order_system_information)?>
		</td>
	</tr>
	<?php
}
?>
</table>
<?php
	}
}
exit();
?>