<?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('topreef', $connection) or die("Konnte die Datenbank old nicht waehlen.");

// get customers
$query = mysql_query('SELECT * FROM kundenimport') or die("Anfrage nicht erfolgreich");
$data = array();
while($obj = mysql_fetch_object($query)) {
	$data[] = $obj;
}

// imported data
// connect target database

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



foreach ($data as $object) {
	$anrede = 0;
	if ($object->Anrede == 'Herr') {
		$anrede = 0;
	}
	if ($object->Anrede == 'Frau') {
		$anrede = 1;
	}
	if ($object->Anrede == 'Firma') {
		$anrede = 2;
	}
	$sql = "
		INSERT INTO customers
		SET
			number='".$object->KundenID."',
			group_id=1,
			honorific='".$anrede."',
			company='".$object->Firmenname."',
			firstname='".$object->Vorname."',
			surname='".$object->Nachname."',
			phone='".$object->telefon."',
			fax='".$object->fax."',
			email='".$object->Email."',
			locked='0',
			tax_id='".$object->UstIdNr."'
	";
	$query = mysql_query($sql);
	$customer_id = mysql_insert_id($connection);
	if ($customer_id) {
		$sql = "
			INSERT INTO customer_addresses
			SET
				customer_id=$customer_id,
				company='".$object->Firmenname."',
				honorific='".$anrede."',
				firstname='".$object->Vorname."',
				surname='".$object->Nachname."',
				street='".$object->Strasse."',
				house_number='".$object->HausNr."',
				zip_code='".$object->plz."',
				city='".$object->ort."',
				country='27'
		";
		$query = mysql_query($sql);
		
		echo $object->Vorname."<br>";
		
		$address_id = mysql_insert_id($connection);
		$sql = "
			UPDATE customers
			SET
				default_address=$address_id
			WHERE
				id=$customer_id
		";
		$query = mysql_query($sql);
	}
}

?>