<?php

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

// get data
// connect import database

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

echo "get items<br>";
// get items
$query = mysql_query('select import_item_prices.*, items.id as item_id from import_item_prices left join items on items.old_id=import_item_prices.pid  order by pid, menge asc') 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 "imports<br>";
foreach ($data as $object) {
    $customer_group = 1;
    if ($object->uid == 6) {
        $customer_group = 101;
    }
    if ($object->menge == 1) {
			$sql = "
			INSERT INTO item_prices
			SET
			item_id=".$object->item_id.",
			customergroup_id=".$customer_group.",
			quantity_1=1,
			price_1='".mysql_real_escape_string(price($object->preis))."',
			bargain_price_1='0'
			";

    }
    else {
        $sql = "
			UPDATE item_prices
			SET
			quantity_2=".$object->menge.",
			price_2='".mysql_real_escape_string(price($object->preis))."',
			bargain_price_2='0'
                        WHERE
                        item_id=".$object->item_id." AND customergroup_id=".$customer_group."
			";
                        
    }
    echo $sql."<br>";
    $query = mysql_query($sql);
}

	

echo "ende<br>";

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

?>