<?php

/*
CREATE TABLE `categories` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(255) NOT NULL,
  `PARENTID` int(11) DEFAULT NULL,
  `IMAGE` mediumblob,
  `COLOR` int(11) DEFAULT NULL,

CREATE TABLE `products` (  
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `REFERENCE` varchar(255) NOT NULL,
  `CODE` varchar(255) NOT NULL,
  `CODETYPE` varchar(255) DEFAULT NULL,
  `NAME` varchar(255) NOT NULL,
  `PRICEBUY` decimal(10,4) NOT NULL,
  `PRICESELL` decimal(10,4) NOT NULL,
  `CATEGORY` int(11) NOT NULL,
  `TAXCAT` int(11) NOT NULL,
  `ATTRIBUTESET_ID` int(11) DEFAULT NULL,
  `STOCKCOST` decimal(10,4) DEFAULT NULL,
  `STOCKVOLUME` double DEFAULT NULL,
  `IMAGE` mediumblob,
  `ISCOM` bit(1) NOT NULL DEFAULT b'0',
  `ISSCALE` bit(1) NOT NULL DEFAULT b'0',
  `ISKITCHEN` bit(1) NOT NULL DEFAULT b'0',
  `PRINTKB` bit(1) NOT NULL DEFAULT b'0',
  `SENDSTATUS` bit(1) NOT NULL DEFAULT b'0',
  `ISSERVICE` bit(1) NOT NULL DEFAULT b'0',
  `ATTRIBUTES` mediumblob,
  `DISPLAY` varchar(255) DEFAULT NULL,
  `VARPRICE` bit(1) NOT NULL DEFAULT b'0',
  `PRINTNAME` varchar(50) DEFAULT NULL,
  `NEGATIVEPRICE` bit(1) NOT NULL DEFAULT b'0',
  
CREATE TABLE `taxes` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(255) NOT NULL,
  `CATEGORY` int(11) NOT NULL,
  `CUSTCATEGORY` int(11) DEFAULT NULL,
  `PARENTID` int(11) DEFAULT NULL,
  `RATE` decimal(10,4) NOT NULL,
  `RATECASCADE` bit(1) NOT NULL DEFAULT b'0',
  `RATEORDER` int(11) DEFAULT NULL,
*/
  
function ist_0_oder_1($string)
{
	if ($string=='0x01') return 1;
	else if ($string=='0x00') return 0;
}

echo "<pre>";
$steuer_array=array();
$kategorie_array=array();
$myfile = fopen("C:\csb\Weltladen_Kassa_1001_09012016.sql", "r") or die("Unable to open file!");
while ($line=fgets($myfile)) {
	if (substr( $line, 0, 24 ) === "INSERT INTO `categories`") {
		$line=substr($line, 33);
		
		$cat_arr=explode("),(", $line);
		//echo ("ID, NAME, PARENTID\n");
		foreach ($cat_arr as $category)
		{
			//echo $category."\n";
			$csv_arr=str_getcsv($category, ",", "'");
			//echo $csv_arr[0].",'".$csv_arr[1]."',".$csv_arr[2]."\n";
			$kategorie_array[$csv_arr[0]]=$csv_arr;
		}
		//print_r($cat_arr);
	}
	
	// Taxes

	if (substr( $line, 0, 19 ) === "INSERT INTO `taxes`") {
		$line=substr($line, 28);
		$cat_arr=explode("),(", $line);
		//echo ("ID, NAME, RATE\n");
		foreach ($cat_arr as $category)
		{
			//echo $category."\n";
			$csv_arr=str_getcsv($category, ",", "'");
			//echo $csv_arr[0].",'".$csv_arr[1]."',".$csv_arr[5]."\n";
			$steuer_array[$csv_arr[0]]=$csv_arr;
		}
	}
}
fclose($myfile);

//print_r($kategorie_array);
//print_r($steuer_array);
// jetzt die Produkte durchsuchen
$myfile = fopen("C:\csb\Weltladen_Kassa_1001_09012016.sql", "r") or die("Unable to open file!");
while ($line=fgets($myfile)) {

	if (substr( $line, 0, 22 ) === "INSERT INTO `products`") {
		$line=substr($line, 31);
		$cat_arr=explode("),(", $line);
		//echo "ID, REFERENCE, CODE, CODETYPE, NAME, PRICEBUY, PRICESELL, CATEGORY, TAXCAT, ATTRIBUTESET_ID, STOCKCOST, STOCKVOLUME, IMAGE, ISCOM, ISSCALE, ISKITCHEN, PRINTKB, SENDSTATUS, ISSERVICE, ATTRIBUTES, DISPLAY, VARPRICE, PRINTNAME, NEGATIVEPRICE\n";
		echo "ID, REFERENCE, CODE, NAME, PRICEBUY, PRICESELL, CATEGORY, TAXCAT, DISPLAY, VARPRICE, PRINTNAME, NEGATIVEPRICE\n";
		foreach ($cat_arr as $product)
		{
			//echo $product."\n";
			//print_r($steuer_array[$csv_arr[8]]);
			$csv_arr=str_getcsv($product, ",", "'");
			echo "'".$csv_arr[0]."','".$csv_arr[1]."','".$csv_arr[2]."','".$csv_arr[4]."','".$csv_arr[5]."','".$csv_arr[6]."','"/*.$csv_arr[7]*/.$kategorie_array[$csv_arr[7]][1]."','"/*.$csv_arr[8]*/.$steuer_array[$csv_arr[8]][5]."','".$csv_arr[20]."','".ist_0_oder_1($csv_arr[21])."','".$csv_arr[22]."','".ist_0_oder_1($csv_arr[23])."'\n"; 
		}
		//print_r($cat_arr);
	}

}
fclose($myfile);


echo "</pre>";
?> 