<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);	

//old database
$connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('intelectra_shop__', $connection) or die("Konnte die Datenbank old nicht waehlen.");
echo "start<br>";
// get customers
$query = mysql_query('SELECT * FROM import_customers') or die("Anfrage nicht erfolgreich");
$data = array();
while($obj = mysql_fetch_object($query)) {
	$data[] = $obj;
}

echo "import<br>";
foreach ($data as $object) {
	
	$sql = "
		INSERT INTO customers
		SET
			number='".$object->Kundennummer."',
			group_id=101,
			honorific=0,
			company='".mysql_real_escape_string($object->Name1)."',
			firstname='',
			surname='',";
                    if ($object->Rufnummer) {
			$sql .= "phone='".$object->Vorwahl.$object->Rufnummer."', ";
                    }
                    if ($object->Fax) {
			$sql .= "fax='".$object->Vorwahl.$object->Fax."', ";
                    }
			
			$sql .= "email='',
			locked='0',
                        freetextfield1='".$object->Name2."',
                        freetextfield2='".$object->Name3."',
			tax_id=''
	";
	$query = mysql_query($sql);
         if (!$query) {
                echo $sql;
                echo "<br />";
        }
	$customer_id = mysql_insert_id($connection);
        $country = 27;
        if ($object->Land == 'A') {
            $country = 29;
        }
        elseif ($object->Land == 'CH') {
            $country = 28;
        }
        elseif ($object->Land == 'B') {
            $country = 30;
        }
	if ($customer_id) {
		$sql = "
			INSERT INTO customer_addresses
			SET
				customer_id=$customer_id,
				company='".mysql_real_escape_string($object->Name1)."',
				honorific=0,
                                firstname='',
				surname='',
				freetextfield1='".mysql_real_escape_string($object->Name2)."',
                                freetextfield2='".mysql_real_escape_string($object->Name3)."',
				street='".mysql_real_escape_string($object->Strasse)."',
				house_number='',
				zip_code='".mysql_real_escape_string($object->PLZ)."',
				city='".mysql_real_escape_string($object->Ort)."',
				country='".$country."'
		";
		$query = mysql_query($sql);
                $address_id1 = mysql_insert_id($connection);
		
                // abweichende Lieferadresse
                if ($object->PLZ_Lieferadresse) {
                    $sql = "
			INSERT INTO customer_addresses
			SET
				customer_id=$customer_id,
				company='".mysql_real_escape_string($object->Name_Lieferadresse)."',
				honorific=0,
				firstname='',
				surname='',
                                freetextfield1='".mysql_real_escape_string($object->Name2_Lieferadresse)."',
                                freetextfield2='".mysql_real_escape_string($object->Name3_Lieferadresse)."',
				street='".mysql_real_escape_string($object->Strasse_Lieferadresse)."',
				house_number='',
				zip_code='".mysql_real_escape_string($object->PLZ_Lieferadresse)."',
				city='".mysql_real_escape_string($object->Ort_Lieferadresse)."',
				country='".$country."'
                    ";
                    $query = mysql_query($sql);
                    $address_id2 = mysql_insert_id($connection);
                     if (!$query) {
                            echo $sql;
                            echo "<br />";
                    }
                }
                $default_address_id = $address_id1;
                if ($object->Liefer_Rechnung == 1) {
                    $default_address_id = $address_id2;
                }
                
                $sql = "
			UPDATE customers
			SET
				default_address=$default_address_id
			WHERE
				id=$customer_id
		";
		$query = mysql_query($sql);
                 if (!$query) {
                        echo $sql;
                        echo "<br />";
                }
                
		
		//echo $object->Name1."<br>";
		
		
	}
}
echo "ende<br>";

?>