<?php 
if (class_exists('typograph'))
{
	$typograph = & singleton('typograph');
}

$DateClass = & singleton('DateClass');

$DataBase = & singleton('DataBase');

/* Идентификатор магазина */
$shop_shops_id = to_int($_REQUEST['shop_shops_id']);

// Получаем информацию о магазине
$shop_row = $shop->GetShop($shop_shops_id);

$admin_forms_fields = new admin_forms_fields();

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

$type_of_delivery_row = $shop->GetTypeOfDelivery($shop_type_of_delivery_id);

?>                
<h1><?php echo $GLOBALS['MSG_shops']['import_delivery_link']?></h1>
<p><a
	href="<?php echo $admin_forms->GetHtmlCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_type_of_delivery_id=$shop_type_of_delivery_id", 78, 'load_data')?>"onclick="<?php echo $admin_forms->GetOnClickCallDoLoadAjax('/admin/shop/shop.php', "&shop_shops_id=$shop_shops_id&shop_groups_parent_id=$shop_groups_parent_id&shop_type_of_delivery_id=$shop_type_of_delivery_id", 78, 'load_data')?>"><?php sprintf($GLOBALS['MSG_shops']['show_cond_of_delivery_title'], $type_of_delivery_row['shop_type_of_delivery_name'])?>
&quot;<?php echo sprintf($GLOBALS['MSG_shops']['show_cond_of_delivery_title'], $type_of_delivery_row['shop_type_of_delivery_name'])?>&quot;</a></p>
<?php

if($_FILES['import_price_file']['tmp_name'] != '')
{
	$fp = fopen($_FILES['import_price_file']['tmp_name'], "rb");

	if(!$fp)
	{
		return true;
	}
}
else
{
	return true;
}

$counter = 0;

// Цикл по строкам CSV-файла
while(!feof($fp))
{
	$current_csv_line_array = fgetcsv($fp, 10000, ';', '"');
	
	// Если пустая строка - пропускаем
	if (!is_array($current_csv_line_array) || (count($current_csv_line_array) == 1 && empty ($current_csv_line_array[0])))
	{
		continue;
	}
	
	if($current_csv_line_array)
	{
		$param = array();
	
		foreach($current_csv_line_array as $code => $data)
		{
			switch($code)
			{
				case 0:
				{
					// Название условия доставки
					$param['name'] = $data;
					
					break;
				}
				case 1:
				{
					// Поле "Вес от"
					$param['weight_from'] = $data;
					
					break;
				}
				case 2:
				{
					// Поле "Вес до"
					$param['weight_to'] = $data;
					
					break;
				}
				case 3:
				{
					// Поле "Цена от"
					$param['price_from'] = $data;
					
					break;
				}
				case 4:
				{
					// Поле "Цена до"
					$param['price_to'] = $data;
					
					break;
				}
				case 5:
				{
					// Поле "Цена доставки"
					$param['price'] = $data;
					
					break;
				}
				case 6:
				{
					// Поле "Валюта"
					
					// Ищем валюту среди значений базы данных
					$currency_row = $shop->GetCurrencyByLike($data);
					
					if($currency_row)
					{
						$param['currency'] = $currency_row['shop_currency_id'];
					}
					else
					{
						// Валюты не нашли
						$param['currency'] = 0;
					}
					
					break;
				}
				case 7:
				{
					// Поле "Страна"
					
					// Ищем страну среди значений БД
					
					$country_row = $shop->GetCountryByName($data);
					
					if($country_row)
					{
						$param['country'] = $country_row['shop_country_id'];
					}
					else
					{
						$param['country'] = 0;
					}
					
					break;
				}
				case 8:
				{
					// Поле "Область"
					
					// Ищем область среди значений БД
					
					$location_row = $shop->GetLocationByName($data);
					
					if($location_row)
					{
						$param['location'] = $location_row['shop_location_id'];
					}
					else
					{
						$param['location'] = 0;
					}
					
					break;
				}
				case 9:
				{
					// Поле "Город"
					
					// Ищем город среди значений БД
					 
					//$city_row = $shop->GetCityByName($data);
					
					// Т.к. существует возможность найти несколько городов, и неочевидно какой из них брать, необходимо искать еще и по идентификатору области
					
					if($data != '')
					{
						$location_id = to_int($param['location']);
						
						$data = quote_smart($data);
						
						$sql = "SELECT * FROM `shop_city_table`
								WHERE `shop_city_name` LIKE '%$data%' 
								AND `shop_location_id`='$location_id'
								LIMIT 0,1";
						
						$result = $DataBase->query($sql);
						
						if($result)
						{
							$city_row = mysql_fetch_assoc($result);
						}
						else
						{
							$city_row = false;
						}
						
						if($city_row)
						{
							$param['city'] = $city_row['shop_city_id'];
						}
						else
						{
							$param['city'] = 0;
						}
					}
					else
					{
						$param['city'] = 0;
					}
					
					break;
				}
				case 10:
				{
					// Поле "Район"
					
					// Ищем город среди значений БД
					
					$city_area_row = $shop->GetCityAreaByName($data);
					
					if($city_area_row)
					{
						$param['city_area'] = $city_area_row['shop_city_area_id'];
					}
					else
					{
						$param['city_area'] = 0;
					}
					
					break;
				}
				case 11:
				{
					// Поле "Налог"
					$param['shop_tax_id'] = $data;
					
					break;
				}
				case 12:
				{ 
					// Поле "Описание"
					$param['description'] = $data;
					
					break;
				}
				default:
				{
				}
			}
		}
		
		$param['type_of_delivery'] = $shop_type_of_delivery_id;
		
		$shop->InsertCondOfDelivery($param);
		
		$counter++;
		
		// Очищаем массив
		$param_array = array();
	}
}

// Закрываем файл
@ fclose($fp);
 
?>
<p>
	<?php 
	show_message(sprintf($GLOBALS['MSG_shops']['import_delivery_result'], $counter));
	?>
</p>
<?php

return true;
?>