<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Форма добаления/редактирования скидок от суммы товара.
 * 
 * Файл: /admin/shop/action/form_edit_add_order_discount.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
if (isset($id))
{
	$shop_order_discount_id = $id;
}
else
{
	$shop_order_discount_id = 0;
}

$form_params = array();
$form_params['title'] = $shop_order_discount_id==0?$GLOBALS['MSG_shops']['add_order_discount_form_title']:$GLOBALS['MSG_shops']['edit_order_discount_form_title'];
$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'EditAddOrderDiscount';

$shop_shops_id = to_int($_REQUEST['shop_shops_id']);
$shop = & singleton('shop');
$shop_groups_parent_id = to_int($_REQUEST['shop_groups_parent_id']);
$DataBase = & singleton('DataBase');

$admin_forms_fields = new admin_forms_fields();

$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']['order_discount_form_shop_link'],
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 73, 'load_data'),
"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 73, 'load_data'),
);
$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

$datetime = & singleton('DateClass');

// Редактировние
if ($shop_order_discount_id)
{
	$type = 1;
	$result = $shop->GetOrderDiscount($shop_order_discount_id);
	$shop_shop_id = to_int($result['shop_shops_id']);
	$shop_currency_id = to_int($result['shop_currency_id']);
	$shop_order_discount_name = to_str($result['shop_order_discount_name']);
	$shop_order_discount_sum_from = to_float($result['shop_order_discount_sum_from']);
	$shop_order_discount_sum_to = to_float($result['shop_order_discount_sum_to']);
	$shop_order_discount_active = to_int($result['shop_order_discount_active']);
	$shop_order_discount_active_from = $datetime->datetime_format(date($result['shop_order_discount_active_from']));
	$shop_order_discount_active_to = $datetime->datetime_format(date($result['shop_order_discount_active_to']));
	$shop_order_discount_type = to_int($result['shop_order_discount_type']);
	$shop_order_discount_value = to_float($result['shop_order_discount_value']);
	$shop_order_discount_is_coupon = to_int($result['shop_order_discount_is_coupon']);
	$users_id = to_int($result['users_id']);
	$shop_order_discount_count_from = to_int($result['shop_order_discount_count_from']);
	$shop_order_discount_count_to = to_int($result['shop_order_discount_count_to']);
	$shop_order_discount_logic_between_elements = to_int($result['shop_order_discount_logic_between_elements']);
}
else // Добавление
{
	$shop_shop_id = $shop_shops_id;
	$shop_currency_id = 0;
	$shop_order_discount_name = '';
	$shop_order_discount_sum_from = '';
	$shop_order_discount_sum_to = '';
	$shop_order_discount_active = 0;
	$shop_order_discount_active_from = date("d.m.Y H:i:s");
	$shop_order_discount_active_to = date("d.m.Y H:i:s", time() + 60 * 60 * 24 * 365 * 5);
	$shop_order_discount_type = 0;
	$shop_order_discount_value = '';
	$shop_order_discount_is_coupon = 0;
	$users_id = false;
	$shop_order_discount_count_from = 0;
	$shop_order_discount_count_to = 0;
	$shop_order_discount_logic_between_elements = 0;
}

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

// Название скидки от общей суммы заказа
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_name';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_form_name'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_order_discount_name;
$params['attributes']['class'] = 'large';
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 255;
$admin_forms_fields->AddField($params);

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

// Активность скидки от общей суммы заказа
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_active';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_form_active'];
$params['type'] = 1; // Флажок
$params['value'] = $shop_order_discount_active;

$admin_forms_fields->AddField($params);

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

// Применять скидку только при наличии купона
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_is_coupon';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_form_is_coupon'];
$params['type'] = 1; // Флажок
$params['value'] = $shop_order_discount_is_coupon;

$admin_forms_fields->AddField($params);

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

// Величина скидки
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_value';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_form_value'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_order_discount_value;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_decimal'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 14;
$params['div_attributes']['style'] = 'width: 170px; float: left;';
$admin_forms_fields->AddField($params);

// Заполняем данными контейнер поля "Тип скидки"
$arr = array();
for ($i = 0; $i <= 1; $i++)
{
	$arr[$i] = $GLOBALS['MSG_shops']["order_discount_form_type_$i"];
}

// Тип скидки - процент или сумма
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_type';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_form_type'];
$params['type'] = 2; // Выпадающий список
$params['items'] = $arr;
$params['value'] = $shop_order_discount_type;
$params['div_attributes']['style'] = 'width: 100px; float: left; margin-left: 10px;';
$admin_forms_fields->AddField($params);

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

// Период активности от
$params = array();
$params['tab_id'] = $tab_id;
$params['html_id'] = 'shop_order_discount_active_from';
$params['name'] = 'shop_order_discount_active_from';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_form_date_from_to_from'];
$params['type'] = 11; // Дата-время.
$params['value'] = $shop_order_discount_active_from;
$params['div_attributes']['style'] = 'width: 170px; float: left;';
$admin_forms_fields->AddField($params);

// Период активности до
$params = array();
$params['tab_id'] = $tab_id;
$params['html_id'] = 'shop_order_discount_active_to';
$params['name'] = 'shop_order_discount_active_to';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_form_date_from_to_to'];
$params['type'] = 11; // Дата-время.
$params['value'] = $shop_order_discount_active_to;
$params['div_attributes']['style'] = 'width: 170px; float: left; margin-left: 10px;';
$admin_forms_fields->AddField($params);

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

// Стоимость заказа от
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_sum_from';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_form_price_from'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_order_discount_sum_from;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_decimal'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 14;
$params['div_attributes']['style'] = 'width: 170px; float: left;';
$admin_forms_fields->AddField($params);

// Стоимость заказа до
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_sum_to';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_form_price_to'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_order_discount_sum_to;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_decimal'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 14;
$params['div_attributes']['style'] = 'width: 170px; float: left; margin-left: 10px;';
$admin_forms_fields->AddField($params);

// Заполняем контейнер поля "Стоимость заказа"
$arr = array();
$currency_resource = $shop->GetAllCurrency();

if (mysql_num_rows($currency_resource) > 0)
{
	while ($row = mysql_fetch_assoc($currency_resource))
	{
		$arr[$row['shop_currency_id']] = $row['shop_currency_name'];
	}
}

// Стоимость заказа (валюта)
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_currency_id';
$params['caption'] = $GLOBALS['MSG_shops']['items_catalog_discount'];
$params['type'] = 2; // Выпадающий список.
$params['value'] = $shop_currency_id;
$params['items'] = $arr;
$params['div_attributes']['style'] = 'width: 100px; float: left; margin-left: 10px;';
$admin_forms_fields->AddField($params);

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

// и/или
$arr = array(0 => $GLOBALS['MSG_shops']['order_discount_case_and'],
1 => $GLOBALS['MSG_shops']['order_discount_case_or']);
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_logic_between_elements';
$params['div_id'] = 'shop_order_discount_logic_between_elements';
$params['caption'] = '';
$params['type'] = 13; // радиокнопки
$params['value'] = $shop_order_discount_logic_between_elements;
$params['options']['count'] = 2;
$params['options']['labels'] = $arr;
$params['options']['input_text'] = -1;
$params['options']['index'] = array(0 => 0, 1 => 1);
$params['div_attributes']['style'] = 'font-weight: bold';
$params['separator'] = '';
$admin_forms_fields->AddField($params);

/*

// Разделитель
$arr = array(0 => $GLOBALS['MSG_shops']['import_price_list_separator1'],
1 => $GLOBALS['MSG_shops']['import_price_list_separator2'],
2 => $GLOBALS['MSG_shops']['import_price_list_separator3'],
3 => $GLOBALS['MSG_shops']['import_price_list_separator4']);
$params = array();
$params['tab_id'] = $tab_id;
$params['div_id'] = 'import_price_separator';
$params['name'] = 'import_price_separator';
$params['caption'] = $GLOBALS['MSG_shops']['import_price_list_separator'];
$params['type'] = 13; // радиокнопки
$params['value'] = 1;
$params['options']['count'] = count($arr);
$params['options']['labels'] = $arr;
$params['options']['input_text'] = 2;
$params['options']['index'] = array(0 => 0, 1 => 1, 2 => 2, 3 => 3);
$params['div_attributes']['style'] = 'float: left;';
$params['separator'] = '';
$admin_forms_fields->AddField($params);

*/

// Количество заказа от
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_count_from';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_count_from'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_order_discount_count_from;
$params['format']['maxlen']['value'] = 11;
$params['div_attributes']['style'] = 'width: 170px; float: left;';
$admin_forms_fields->AddField($params);

// Количество заказа до
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_discount_count_to';
$params['caption'] = $GLOBALS['MSG_shops']['order_discount_count_to'];
$params['type'] = 0; // Поле ввода.
$params['value'] = $shop_order_discount_count_to;
$params['format']['maxlen']['value'] = 11;
$params['div_attributes']['style'] = 'width: 170px; float: left; margin-left: 10px;';
$admin_forms_fields->AddField($params);

// передеам скрытое поле с идентификатором адреса если он есть
if ($shop_order_discount_id)
{
	$params = array();
	$params['tab_id'] = $tab_id;
	$params['name'] = 'shop_order_discount_id';
	$params['caption'] = '';
	$params['type'] = 4; // hidden.
	$params['value'] = $shop_order_discount_id;
	$admin_forms_fields->AddField($params);
}

$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'action';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = 'show_order_discount';
$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'] = 'shop_groups_parent_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $shop_groups_parent_id;
$admin_forms_fields->AddField($params);

// Кнопка "Сохранить".
$param = array();
$param['name'] = 'save_order_discount';
$param['caption'] = $GLOBALS['MSG_shops']['save'];
$param['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).", 'load_data', " . to_int($admin_forms_fields->form_params['current_page']) . ", " . to_int($admin_forms_fields->form_params['on_page']) . " )";
$param['attributes']['type'] = 'button';
$admin_forms_fields->AddButton($param);

// Кнопка "Применить".
$params = array();
$params['name'] = 'add_order_discount';
$params['caption'] = $GLOBALS['MSG_shops']['apply'];
$params['image'] = '/admin/images/add.gif';
$params['attributes']['onclick'] = "return SendForm('{$admin_forms_fields->GetWindowId()}', '{$admin_forms_fields->AAction}', '{$admin_forms_fields->AAdditionalParams}', this, ".to_int($_REQUEST['admin_forms_id']).")";
$params['attributes']['type'] = 'submit';
$admin_forms_fields->AddButton($params);

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