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

$shop_order_id = to_int($_REQUEST['view_order_id']);

$shop = & singleton('shop');
$order_row = $shop->GetOrder($shop_order_id);
$shop_order_account_number = to_str($order_row['shop_order_account_number']);

$datetime = & singleton('DateClass');

$form_params = array();
$form_params['title'] = $edit_order_items == 0
? sprintf($GLOBALS['MSG_shops']['order_items_add_form_title'], $shop_order_account_number)
: sprintf($GLOBALS['MSG_shops']['order_items_edit_form_title'], $shop_order_account_number);

$form_params['form_attribs']['method'] = 'post';
$form_params['form_attribs']['action'] = '/admin/shop/shop.php';
$form_params['form_attribs']['name'] = 'EditAddOrderItems';

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

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

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

$warehouse = & singleton('warehouse');

$edit_order_items_id = to_int($_REQUEST['edit_order_items_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']['shops_add_form_link_orders'],
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id", 75, '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", 75, 'load_data'));

$new_path_array[] = array("name" => sprintf($GLOBALS['MSG_shops']['show_order_items_title'], $shop_order_account_number),
"link" => $admin_forms_fields->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$shop_groups_parent_id&shop_shops_id=$shop_shops_id&view_order_id=$view_order_id", 76, 'load_data'),
"onclick" => $admin_forms_fields->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_groups_parent_id=$shop_groups_parent_id&shop_shops_id=$shop_shops_id&view_order_id=$view_order_id", 76, 'load_data')
);
$form_params['path_array'] = $new_path_array;

$admin_forms_fields->admin_forms_fields($form_params);

// Редактировние товара оформленного заказа
if ($edit_order_items)
{
	$result = $shop->GetOrderItem($edit_order_items);
	$shop_order_items_quantity = to_float($result['shop_order_items_quantity']);
	$shop_order_items_price = to_float($result['shop_order_items_price']);
	$shop_order_items_name = to_str($result['shop_order_items_name']);
	$shop_order_items_marking = to_str($result['shop_order_items_marking']);
	$shop_tax_rate = to_int($result['shop_tax_rate']);
	// Получаем информацию о товаре, для определения продавца по идентификатору товара
	$shop_items_catalog_item_id = to_int($result['shop_items_catalog_item_id']);
	$items_catalog_data_row = $shop->GetItem($shop_items_catalog_item_id, array('cache_off' => true));
	$shop_order_items_type = to_int($result['shop_order_items_type']);
	$shop_warehouse_id = to_int($result['shop_warehouse_id']);
}
// Добавление товара оформленного заказа
else
{
	$shop_items_catalog_item_id = 0;
	$shop_order_items_quantity = 0;
	$shop_order_items_price = 0;
	$shop_order_items_name = '';
	$shop_order_items_marking = '';
	$shop_order_items_type = 0;
	$shop_warehouse_id = 0;
}

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

$order_data = $shop->GetOrder($shop_order_id);

// Наименование товара
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_items_name';
$params['caption'] = $GLOBALS['MSG_shops']['shop_items_catalog_item_name'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_order_items_name;
$params['attributes']['class'] = 'large';
// Может строится название по ID товара
//$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_items_quantity';
$params['caption'] = $GLOBALS['MSG_shops']['shop_order_items_quantity'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_order_items_quantity;
$params['format']['lib']['value'] = 'decimal';
$params['format']['lib']['message'] = $GLOBALS['MSG_shops']['show_invalid_characters_integer'];
$params['format']['minlen']['value'] = 1;
$params['format']['maxlen']['value'] = 11;
$params['attributes']['class'] = 'large';
$params['attributes']['style'] = 'width: 200px;';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);

// Цена товара
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_items_price';
$params['caption'] = $GLOBALS['MSG_shops']['shop_order_items_price'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_order_items_price;
$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'] = 11;
$params['attributes']['class'] = 'large';
$params['attributes']['style'] = 'width: 200px;';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);

$currency_data = $shop->GetCurrency($order_data['shop_currency_id'], array('cache_off' => true));

// Валюта товара
$param = array();
$param['tab_id'] = $tab_id;
$param['name'] = '';
$param['caption'] = '&nbsp;';
$param['type'] = 8; // вывод текста
$param['value'] = to_str($currency_data['shop_currency_name']);
$param['attributes']['style'] = 'width: 100px;';
$param['div_attributes']['style'] = 'float: left; margin-top: 10px';
$admin_forms_fields->AddField($param);

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

$warehouses_array = array(0 => ' ... ');
// Получаем список складов текущего магазина
$aWarehouses = $warehouse->GetAllWarehousesForShop($shop_shops_id);

if($aWarehouses)
{
	foreach ($aWarehouses as $warehouses_row)
	{
		$warehouses_array[$warehouses_row['shop_warehouse_id']] = $warehouses_row['shop_warehouse_name'];
	}
}

// Склад
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_warehouse_id';
$params['caption'] = $GLOBALS['MSG_shops']['warehouse_from_which_the_product_was_written_off'];
$params['type'] = 2; // Выпадающий список
$params['value'] = $shop_warehouse_id;
$params['items'] = $warehouses_array;
$params['attributes']['style'] = 'width: 200px;';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);

// Тип товара
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_items_type';
$params['caption'] = $GLOBALS['MSG_shops']['order_item_type_option_caption'];
$params['type'] = 2; // Выпадающий список
$params['value'] = $shop_order_items_type;
$params['items'] = array(
0 => $GLOBALS['MSG_shops']['order_item_type_caption0'],
1 => $GLOBALS['MSG_shops']['order_item_type_caption1'],
2 => $GLOBALS['MSG_shops']['order_item_type_caption2']
);
$params['attributes']['style'] = 'width: 200px;';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);

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


if (isset($items_catalog_data_row))
{
	$shop = new shop();

	// Получаем данные о продавце
	$seller_data_row = $shop->GetSeller($items_catalog_data_row['shop_sallers_id'], array('cache_off' => true));

	// Наименование продавца
	$params = array();
	$params['tab_id'] = $tab_id;
	$params['name'] = 'shop_sallers_name';
	$params['caption'] = $GLOBALS['MSG_shops']['shop_items_seller_name'];
	$params['type'] = 0; // Поле ввода
	$params['value'] = $seller_data_row['shop_sallers_name'];
	$params['attributes']['readonly'] = '';
	$params['attributes']['style'] = 'width: 200px;';
	$params['div_attributes']['style'] = 'float: left;';
	$admin_forms_fields->AddField($params);

	// Идентификатор продавца
	$params = array();
	$params['tab_id'] = $tab_id;
	$params['name'] = 'shop_sallers_id';
	$params['caption'] = $GLOBALS['MSG_shops']['shop_items_seller_id'];
	$params['type'] = 0; // Поле ввода
	$params['value'] = $seller_data_row['shop_sallers_id'];
	$params['attributes']['style'] = 'width: 200px;';
	$params['attributes']['readonly'] = '';
	$params['div_attributes']['style'] = 'float: left;';
	$admin_forms_fields->AddField($params);

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

// Артикул заказанного товара
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_order_items_marking';
$params['caption'] = $GLOBALS['MSG_shops']['shop_items_catalog_item_marking'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_order_items_marking;
$params['format']['maxlen']['value'] = 50;
$params['attributes']['style'] = 'width: 200px;';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);

/*if (isset($items_catalog_data_row))
 {*/
// Идентификатор товара
$params = array();
$params['tab_id'] = $tab_id;
$params['name'] = 'shop_items_catalog_item_id';
$params['caption'] = $GLOBALS['MSG_shops']['items_catalog_item_id'];
$params['type'] = 0; // Поле ввода
$params['value'] = $shop_items_catalog_item_id;
$params['attributes']['style'] = 'width: 200px;';
$params['format']['maxlen']['value'] = 50;
//$params['attributes']['readonly'] = '';
$params['div_attributes']['style'] = 'float: left;';
$admin_forms_fields->AddField($params);
//}

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

// передеам скрытое поле с идентификатором адреса если он есть
if ($edit_order_items)
{
	$params = array();
	$params['tab_id'] = $tab_id;
	$params['name'] = 'edit_order_items';
	$params['caption'] = '';
	$params['type'] = 4; // hidden.
	$params['value'] = $edit_order_items;
	$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_items';
$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'] = 'view_order_id';
$params['caption'] = '';
$params['type'] = 4; // hidden.
$params['value'] = $view_order_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_items';
$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_items';
$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();
?>