<?php 
/**
 * Система управления сайтом HostCMS v. 5.xx
 * Copyright © 2005-2010 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 *
 * Обработчик добавления/редактирования товара внутри заказа.
 * 
 * Файл: /admin/shop/action/add_order_items.php
 * 
 * @author Hostmake LLC
 * @version 5.x
 */
$param = array();
$shop = & singleton('shop');
if (isset($_REQUEST['edit_order_items']))
{
	if (to_int($_REQUEST['edit_order_items']) != 0)
	{
		$param['shop_order_items_id'] = to_int($_REQUEST['edit_order_items']);
	}	
}

$param['shop_order_items_type'] = to_int($_REQUEST['shop_order_items_type']);

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

$quantity = to_float($_REQUEST['shop_order_items_quantity']);

if($quantity > 0)
{
	$param['shop_order_items_quantity'] = $quantity;
}
else
{
	$param['shop_order_items_quantity'] = 1;
}

$param['shop_items_catalog_item_id'] = to_int(trim($_REQUEST['shop_items_catalog_item_id']));

$param['shop_warehouse_id'] = to_int($_REQUEST['shop_warehouse_id']);

if($param['shop_items_catalog_item_id'] > 0)
{
	// Если передан идентификатор товара, то все данные берем из данного товара
	$item_row = $shop->GetItem($param['shop_items_catalog_item_id']);
	
	if(isset($_REQUEST['shop_order_items_price']) && $_REQUEST['shop_order_items_price'] != '0')
	{
		$param['shop_order_items_price'] = to_float($_REQUEST['shop_order_items_price']);
	}
	else
	{
		//$param['shop_order_items_price'] = to_float($item_row['shop_items_catalog_price']);
		$price_array = $shop->GetPriceForUser(false, $param['shop_items_catalog_item_id'], array (), array ('item_count' => $param['shop_order_items_quantity']));
		$param['shop_order_items_price'] = $price_array['price_discount'];
		//var_dump($param);
	}
	
	if(isset($_REQUEST['shop_order_items_name']) && trim($_REQUEST['shop_order_items_name']) != '')
	{
		$param['shop_order_items_name'] = to_str($_REQUEST['shop_order_items_name']);
	}
	else
	{
		$param['shop_order_items_name'] = to_str($item_row['shop_items_catalog_name']);
	}
	
	if(isset($_REQUEST['shop_order_items_marking']) && $_REQUEST['shop_order_items_marking'] != '')
	{
		$param['shop_order_items_marking'] = to_str($_REQUEST['shop_order_items_marking']);
	}
	else
	{
		$param['shop_order_items_marking'] = to_str($item_row['shop_items_catalog_marking']);
	}
}
else
{
	$param['shop_order_items_marking'] = to_str($_REQUEST['shop_order_items_marking']);
	$param['shop_order_items_name'] = to_str($_REQUEST['shop_order_items_name']);
	$param['shop_order_items_price'] = to_float($_REQUEST['shop_order_items_price']);
}

if ($orderitems = $shop->InsertOrderItems($param))
{
	// Информация о заказе получаем после обновления
	$shop_order_id = $param['shop_order_id'];
	$order_row = $shop->GetOrder($shop_order_id);
	
	$aExecParam = array('shop_order_id' => $shop_order_id, 'action' => 'edit_item');
	
	// Предыдущие даные о заказе до редактирования
	// Отдельно получать не нужно, т.к. было редактирование товара, а не заказа
	$aExecParam['prev_order_row'] = $order_row;

	// Вызываем обработчик платежной системы для события "редактирование товара"
	$shop->ExecSystemsOfPayChangeStatus($order_row['shop_system_of_pay_id'], $aExecParam);

	$admin_forms = new admin_forms();
	$window_id = $admin_forms->GetWindowId();
	?><script type="text/javascript">
	$.appendInput('<?php echo $window_id?>', 'EditAddOrderItems', 'edit_order_items', '<?php echo $orderitems?>');
	</script><?php

	if (isset($_REQUEST['edit_order_items']))
	{
		show_message($GLOBALS['MSG_shops']['success_edit_order_items']);
	}
	else 
	{
		show_message($GLOBALS['MSG_shops']['success_add_order_items']);
	}
}
else
{
	if (isset($_REQUEST['edit_order_items']))
	{
		show_error_message($GLOBALS['MSG_shops']['error_edit_order_items']);
	}
	else 
	{
		show_error_message($GLOBALS['MSG_shops']['error_add_order_items']);
	}
}
?>