<?php

//ini_set("memory_limit","1024M");
//set_time_limit(60000);

// get data
// connect import database

// old database
$connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('intelectra', $connection) or die("Konnte die Datenbank nicht waehlen.1");

echo "get items<br>";
// get items
$query = mysql_query('SELECT * FROM import_items') or die("Anfrage nicht erfolgreich");
$data = array();
while($obj = mysql_fetch_object($query)) {
	$data[] = $obj;
}

// new database
$connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('intelectra', $connection) or die("Konnte die Datenbank nicht waehlen.2");

/*
echo "get hersteller<br>";
// get manufacturers
$sql = "SELECT * FROM manufacturers ORDER BY name";
$query = mysql_query($sql);
$manufacturers = array();
while($obj = mysql_fetch_object($query)) {
	$manufacturers[$obj->name] = $obj->id;
}
*/
echo "imports<br>";
foreach ($data as $object) {
	//echo "import: ".$object->HauptArtikel."<br>";
	//if($object->HauptArtikel > 0) {
	
	/*$object->{'Verkaufs-Preis'} = substr($object->{'Verkaufs-Preis'},0,-4);
	$object->{'Verkaufs-Preis'} = str_replace(",",".",$object->{'Verkaufs-Preis'});
	$object->{'Verkaufs-Preis'} = (float)$object->{'Verkaufs-Preis'};
	$object->{'Verkaufs-Preis'} = ($object->{'Verkaufs-Preis'} / (100 + $object->{'Steuerklasse'}) * 100);
	*/
		$sql = "
			INSERT INTO items
			SET
                                old_id='".$object->pid."',
				number='".mysql_real_escape_string($object->produktid)."',
				shippable=1,
				schema_id=3,
				price_type=1,
				seo_keywords='".mysql_real_escape_string($object->keywords)."',
				tax_id=1,
				detail_description='".mysql_real_escape_string($object->beschreibung)."',
				name='".mysql_real_escape_string($object->bezeichnung)."'
		";
		$query = mysql_query($sql);
		if (!$query) {
			echo $sql;
			echo "<br />";
		}
	
		$item_id = mysql_insert_id($connection);
		/*
		if ($item_id) {
			$sql = "
			INSERT INTO item_prices
			SET
			item_id=$item_id,
			customergroup_id=1,
			quantity_1=1,
			price_1='".mysql_real_escape_string(price($object->{'Verkaufs-Preis'}))."',
			bargain_price_1='0'
			";
			//		echo $sql."<br>";
			$query = mysql_query($sql);
		}*/
	//}
	
	
}

echo "ende<br>";

function price ($p) {
	$p = str_replace(',', '.', $p);
	$p = substr($p, 0, -2);
	return $p;
}

?>