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

$form_params = array();
$form_params['title'] = $GLOBALS['MSG_shops']['show_sales_order_title'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['ENCTYPE'] = 'multipart/form-data';
$form_params['form_attribs']['target'] = '_blank';

$admin_forms_fields = new admin_forms_fields();

$shop_shops_id = to_int($_REQUEST['shop_shops_id']);

$shop = & singleton('shop');

$shop_groups_parent_id = to_int($_REQUEST['shop_groups_parent_id']);

$begin_date = date('d.m.Y', time() - 2628000 * 2);

$end_date = date('d.m.Y'); 

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

$path_array = $shop->GetShopPathArray($shop_groups_parent_id, false);

// Преобразуем идентификаторы групп в ссылки на группы.
$new_path_array = array();

$new_path_array[0]['name'] = $GLOBALS['MSG_shops']['header_admin__list_forms'];
$new_path_array[0]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', ' ', 54, 'load_data');
$new_path_array[0]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', ' ', 54, 'load_data');

$new_path_array[1]['name'] = $row['shop_shops_name'];
$new_path_array[1]['link'] = $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');
$new_path_array[1]['onclick'] = $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=0&shop_shops_id=$shop_shops_id", 65, 'load_data');

if ($path_array)
{
	foreach ($path_array as $key => $value)
	{
		$new_path_array[] = array (
		'link' => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
		'onclick' => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$key&shop_shops_id=$shop_shops_id", 65, 'load_data'),
		'name' => $value);
	}
}

$new_path_array[] = array("name" => $GLOBALS['MSG_shops']['show_sales_order_link'],
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&action=show_sales_order&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 0, 'load_data'),
"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&action=show_sales_order&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 0, 'load_data')
);

$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Параметры.
$tab_id = $admin_forms_fields->AddTab($GLOBALS['MSG_shops']['tab']);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Группировка периодов
$arr = array(0 => $GLOBALS['MSG_shops']['form_sales_order_grouping_monthly'],
1 => $GLOBALS['MSG_shops']['form_sales_order_grouping_weekly'],
2 => $GLOBALS['MSG_shops']['form_sales_order_grouping_daily']);
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'sales_order_grouping';
$params['caption'] = $GLOBALS['MSG_shops']['form_sales_order_select_grouping'];
$params['type'] = 13; // радиокнопки
$params['value'] = 2;
$params['separator'] = ''; 
$params['options']['count'] = 3;
$params['options']['labels'] = $arr;
//$params['options']['input_text'] = array(1 => 0, 0 => 1);
$params['options']['index'] = array(0=>0, 1=>1, 2=>2);
$params['options']['values'] = array(0=>'monthly', 1=>'weekly', 2=>'daily');
$params['attributes']['style'] = 'width: 75px;';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Отображать список товаров
$param = array();
$param['tab_id'] = $tab_id;
$param['type'] = 1;
$param['name'] = 'sales_order_show_list_items';
$param['html_id'] = 'id_sales_order_show_list_items';
$param['lable'] = $GLOBALS['MSG_shops']['form_sales_order_show_list_items'];
$param['value'] = 1;

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Начальная дата
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'sales_order_begin_date';
$params['caption'] = $GLOBALS['MSG_shops']['form_sales_order_begin_date'];
$params['type'] = 10; // Дата-время.
$params['value'] = $begin_date;
$params['attributes'] = array();
$params['attributes']['size'] = 15;
$params['div_attributes']['style'] = 'float: left; margin-top: 5px';

$admin_forms_fields->AddField($params);

// Конечная дата
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'sales_order_end_date';
$params['caption'] = $GLOBALS['MSG_shops']['form_sales_order_end_date'];
$params['type'] = 10; // Дата-время.
$params['value'] = $end_date;
$params['attributes'] = array();
$params['attributes']['size'] = 15;
$params['div_attributes']['style'] = 'float: left; margin-top: 5px';

$admin_forms_fields->AddField($params);

// Разделитель.
//$admin_forms_fields->AddSeparator($tab_id);

// Период
$arr = array(0=>$GLOBALS['MSG_shops']['form_sales_order_period_2_months'], 1=>$GLOBALS['MSG_shops']['form_sales_order_period_month'], 2=>$GLOBALS['MSG_shops']['form_sales_order_period_week']);
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'sales_order_period';
$params['caption'] = $GLOBALS['MSG_shops']['form_sales_order_period'];
$params['type'] = 13; // радиокнопки
$params['value'] = 0;
$params['separator'] = '';
$params['options']['count'] = 3;
$params['options']['labels'] = $arr;
$params['options']['index'] = array(0=>0, 1=>1, 2=>2);
$params['options']['values'] = array(0=>'2months', 1=>'month', 2=>'week');
$params['attributes']['style'] = 'width: 75px;';
$params['div_attributes']['style'] = 'float: left; margin-top: 5px';
$admin_forms_fields->AddField($params);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Отображать только оплаченные
$param = array();
$param['tab_id'] = $tab_id;
$param['type'] = 1;
$param['name'] = 'sales_order_show_only_paid_items';
$param['html_id'] = 'id_sales_order_show_only_paid_items';
//$param['caption'] = $GLOBALS['MSG_information_systems']['information_systems_add_form_apply_tags_automatic'];
$param['lable'] = $GLOBALS['MSG_shops']['form_sales_order_show_paid_items'];
$param['value'] = 1;

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

$arr = array();
$arr[0] = '..';
$result = $shop->GetAllSellers($shop_shops_id);
$count_sellers = 0;
if ($result)
{
	$count_sellers = mysql_num_rows($result);
}

for ($i = 0; $i < $count_sellers; $i++)
{
	$row = mysql_fetch_assoc($result);
	$arr[$row['shop_sallers_id']] = htmlspecialchars($row['shop_sallers_name']);
}

// Продавец
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_sallers_id';
$param['caption'] = $GLOBALS['MSG_shops']['form_sales_order_sallers'];
$param['type'] = 2; // выпадающий список
$param['items'] = $arr;
$param['value'] = 0;
//$param['attributes']['style'] = 'width: 75px;';
$param['div_attributes']['style'] = 'float: left;';

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

// Получаем список платежных систем
$sys_of_pay_res = $shop->GetAllSystemOfPay($shop_shops_id);

$sys_of_pay_arr = array(" ... ");

if(mysql_num_rows($sys_of_pay_res) > 0)
{
	while($sys_of_pay_row = mysql_fetch_assoc($sys_of_pay_res))
	{
		$sys_of_pay_arr[$sys_of_pay_row['shop_system_of_pay_id']] = $sys_of_pay_row['shop_system_of_pay_name'];
	}
}

// Платежная система
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_system_of_pay_id';
$param['caption'] = $GLOBALS['MSG_shops']['form_sales_order_sop'];
$param['type'] = 2; // выпадающий список
$param['items'] = $sys_of_pay_arr;
$param['value'] = 0;
//$param['attributes']['style'] = 'width: 75px;';
$param['div_attributes']['style'] = 'float: left;';

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);

$order_status_res = $shop->GetAllOrderStatus();

$order_status_arr = array(" ... ");

while($order_status_row = mysql_fetch_assoc($order_status_res))
{
	$order_status_arr[$order_status_row['shop_order_status_id']] = $order_status_row['shop_order_status_name'];
}

// Статус заказа
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = 'shop_order_status_id';
$param['caption'] = $GLOBALS['MSG_shops']['form_sales_order_status'];
$param['type'] = 2; // выпадающий список
$param['items'] = $order_status_arr;
$param['value'] = 0;
//$param['attributes']['style'] = 'width: 75px;';
$param['div_attributes']['style'] = 'float: left;';

$admin_forms_fields->AddField($param);

// Разделитель.
$admin_forms_fields->AddSeparator($tab_id);


// Передаем идентификатор группы, из которой пришли
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_group_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $shop_groups_parent_id;
$admin_forms_fields->AddField($params);

// Передаем идентификатор магазина
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_shops_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $shop_shops_id;
$admin_forms_fields->AddField($params);

// Передаем идентификатор магазина
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'hostcmsAAdditionalParams';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = "&shop_shops_id=$shop_shops_id";
$admin_forms_fields->AddField($params);

$params = array();
$params['name'] = 'create_form_sales_report';
$params['caption'] = $GLOBALS['MSG_shops']['comment_form_add'];
$params['attributes']['type'] = 'submit';
$admin_forms_fields->AddButton($params);

// Отображение формы.
$admin_forms_fields->ShowForm();
?>

<script language="JavaScript">

// Функция установки значения события onClick  
function SetOnClick4Period(param_name)
{
	radio_group = document.getElementsByName(param_name);

	radio_count = radio_group.length;
		 
	for (i = 0; i < radio_count; i++)
	{				
		radio_group[i].setAttribute('onclick', 'SetPeriod(this)');						
	}
}

function SetPeriod(param)
{
		
	var today_date = new Date();
	
	switch (param.value)
	{		
	    case "0": // 2 месяца
		    delta_time = 2*2628000000;	        
	        break;
	        
	    case "1": // месяц
	    	delta_time = 2628000000;	    		    	
	        break;
	        
	    case "2": // неделя
	    	delta_time = 7*86400000;	    	
	        break;	    
	}
	 	 
	begin_date = new Date((today_date - delta_time));

	current_day = today_date.getDate();
	if (current_day < 10)
	{
		current_day = '0' + current_day;
	}	
	
	current_month = today_date.getMonth();
	current_month++;
	
	if (current_month < 10)
	{
		current_month = '0' + current_month;
	}	
	current_year = today_date.getFullYear();

	begin_day = begin_date.getDate();

	if (begin_day < 10)
	{
		begin_day = '0' + begin_day;
	}
	begin_month = begin_date.getMonth();
	begin_month++;
	
	if (begin_month < 10)
	{
		begin_month = '0' + begin_month;		
	}
	begin_year = begin_date.getFullYear();  
	
	// Начальная дата отчетного периода
	document.getElementById("field_id_4").value = begin_day + '.' + begin_month + '.' + begin_year;

	// Конечная дата отчетного периода
	document.getElementById("field_id_5").value = current_day + '.' + current_month + '.' + current_year;
}
SetOnClick4Period('sales_order_period');
</script>
<?php 

?>